Merge "Adding common code for instrumentation and robolectric tests" into ub-launcher3-rvc-dev
This commit is contained in:
commit
cee56f039e
|
@ -17,10 +17,11 @@ package com.android.quickstep;
|
|||
|
||||
import static androidx.test.InstrumentationRegistry.getContext;
|
||||
import static androidx.test.InstrumentationRegistry.getInstrumentation;
|
||||
import static androidx.test.InstrumentationRegistry.getTargetContext;
|
||||
|
||||
import static com.android.launcher3.common.WidgetUtils.createWidgetInfo;
|
||||
import static com.android.launcher3.testcomponent.TestCommandReceiver.EXTRA_VALUE;
|
||||
import static com.android.launcher3.testcomponent.TestCommandReceiver.SET_LIST_VIEW_SERVICE_BINDER;
|
||||
import static com.android.launcher3.ui.widget.BindWidgetTest.createWidgetInfo;
|
||||
import static com.android.quickstep.NavigationModeSwitchRule.Mode.ZERO_BUTTON;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
@ -187,7 +188,7 @@ public class ViewInflationDuringSwipeUp extends AbstractQuickStepTest {
|
|||
LauncherSettings.Settings.call(mResolver,
|
||||
LauncherSettings.Settings.METHOD_CLEAR_EMPTY_DB_FLAG);
|
||||
LauncherAppWidgetProviderInfo info = TestViewHelpers.findWidgetProvider(this, false);
|
||||
LauncherAppWidgetInfo item = createWidgetInfo(info, true);
|
||||
LauncherAppWidgetInfo item = createWidgetInfo(info, getTargetContext(), true);
|
||||
|
||||
addItemToScreen(item);
|
||||
assertTrue("Widget is not present",
|
||||
|
|
|
@ -22,7 +22,10 @@ LOCAL_MODULE := LauncherRoboTests
|
|||
LOCAL_MODULE_CLASS := JAVA_LIBRARIES
|
||||
|
||||
LOCAL_SDK_VERSION := system_current
|
||||
LOCAL_SRC_FILES := $(call all-java-files-under, src)
|
||||
LOCAL_SRC_FILES := \
|
||||
$(call all-java-files-under, src) \
|
||||
$(call all-java-files-under, ../tests/src_common)
|
||||
|
||||
LOCAL_STATIC_JAVA_LIBRARIES := \
|
||||
androidx.test.runner \
|
||||
androidx.test.rules \
|
||||
|
|
|
@ -62,7 +62,11 @@ else
|
|||
LOCAL_STATIC_JAVA_LIBRARIES += ub-launcher-aosp-tapl
|
||||
endif
|
||||
|
||||
LOCAL_SRC_FILES := $(call all-java-files-under, src)
|
||||
LOCAL_SRC_FILES := \
|
||||
$(call all-java-files-under, src) \
|
||||
$(call all-java-files-under, src_common)
|
||||
|
||||
|
||||
LOCAL_FULL_LIBS_MANIFEST_FILES := $(LOCAL_PATH)/AndroidManifest-common.xml
|
||||
|
||||
LOCAL_PACKAGE_NAME := Launcher3Tests
|
||||
|
|
|
@ -17,7 +17,6 @@ package com.android.launcher3.ui;
|
|||
|
||||
import static androidx.test.InstrumentationRegistry.getInstrumentation;
|
||||
|
||||
import static com.android.launcher3.WorkspaceLayoutManager.FIRST_SCREEN_ID;
|
||||
import static com.android.launcher3.ui.TaplTestsLauncher3.getAppPackageName;
|
||||
import static com.android.launcher3.util.Executors.MAIN_EXECUTOR;
|
||||
|
||||
|
@ -26,7 +25,6 @@ import static org.junit.Assert.assertTrue;
|
|||
|
||||
import android.content.BroadcastReceiver;
|
||||
import android.content.ComponentName;
|
||||
import android.content.ContentResolver;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.IntentFilter;
|
||||
|
@ -53,6 +51,7 @@ import com.android.launcher3.LauncherSettings;
|
|||
import com.android.launcher3.LauncherState;
|
||||
import com.android.launcher3.LauncherStateManager;
|
||||
import com.android.launcher3.Utilities;
|
||||
import com.android.launcher3.common.WidgetUtils;
|
||||
import com.android.launcher3.model.AppLaunchTracker;
|
||||
import com.android.launcher3.model.data.ItemInfo;
|
||||
import com.android.launcher3.tapl.LauncherInstrumentation;
|
||||
|
@ -60,7 +59,6 @@ import com.android.launcher3.tapl.LauncherInstrumentation.ContainerType;
|
|||
import com.android.launcher3.tapl.TestHelpers;
|
||||
import com.android.launcher3.testcomponent.TestCommandReceiver;
|
||||
import com.android.launcher3.testing.TestProtocol;
|
||||
import com.android.launcher3.util.ContentWriter;
|
||||
import com.android.launcher3.util.LooperExecutor;
|
||||
import com.android.launcher3.util.PackageManagerHelper;
|
||||
import com.android.launcher3.util.Wait;
|
||||
|
@ -306,26 +304,7 @@ public abstract class AbstractLauncherUiTest {
|
|||
* Adds {@param item} on the homescreen on the 0th screen
|
||||
*/
|
||||
protected void addItemToScreen(ItemInfo item) {
|
||||
ContentResolver resolver = mTargetContext.getContentResolver();
|
||||
int screenId = FIRST_SCREEN_ID;
|
||||
// Update the screen id counter for the provider.
|
||||
LauncherSettings.Settings.call(resolver,
|
||||
LauncherSettings.Settings.METHOD_NEW_SCREEN_ID);
|
||||
|
||||
if (screenId > FIRST_SCREEN_ID) {
|
||||
screenId = FIRST_SCREEN_ID;
|
||||
}
|
||||
|
||||
// Insert the item
|
||||
ContentWriter writer = new ContentWriter(mTargetContext);
|
||||
item.id = LauncherSettings.Settings.call(
|
||||
resolver, LauncherSettings.Settings.METHOD_NEW_ITEM_ID)
|
||||
.getInt(LauncherSettings.Settings.EXTRA_VALUE);
|
||||
item.screenId = screenId;
|
||||
item.onAddToDatabase(writer);
|
||||
writer.put(LauncherSettings.Favorites._ID, item.id);
|
||||
resolver.insert(LauncherSettings.Favorites.CONTENT_URI,
|
||||
writer.getValues(mTargetContext));
|
||||
WidgetUtils.addItemToScreen(item, mTargetContext);
|
||||
resetLoaderState();
|
||||
|
||||
// Launch the home activity
|
||||
|
|
|
@ -17,18 +17,16 @@ package com.android.launcher3.ui.widget;
|
|||
|
||||
import static androidx.test.InstrumentationRegistry.getTargetContext;
|
||||
|
||||
import static com.android.launcher3.widget.WidgetHostViewLoader.getDefaultOptionsForWidget;
|
||||
import static com.android.launcher3.common.WidgetUtils.createWidgetInfo;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import android.appwidget.AppWidgetHost;
|
||||
import android.appwidget.AppWidgetManager;
|
||||
import android.content.ComponentName;
|
||||
import android.content.ContentResolver;
|
||||
import android.content.Context;
|
||||
import android.content.pm.PackageInstaller;
|
||||
import android.content.pm.PackageInstaller.SessionParams;
|
||||
import android.content.pm.PackageManager;
|
||||
|
@ -39,7 +37,6 @@ import android.widget.RemoteViews;
|
|||
import androidx.test.filters.LargeTest;
|
||||
import androidx.test.runner.AndroidJUnit4;
|
||||
|
||||
import com.android.launcher3.LauncherAppWidgetHost;
|
||||
import com.android.launcher3.LauncherAppWidgetProviderInfo;
|
||||
import com.android.launcher3.LauncherSettings;
|
||||
import com.android.launcher3.R;
|
||||
|
@ -50,7 +47,6 @@ import com.android.launcher3.tapl.Workspace;
|
|||
import com.android.launcher3.ui.AbstractLauncherUiTest;
|
||||
import com.android.launcher3.ui.TestViewHelpers;
|
||||
import com.android.launcher3.util.rule.ShellCommandRule;
|
||||
import com.android.launcher3.widget.PendingAddWidgetInfo;
|
||||
import com.android.launcher3.widget.WidgetManagerHelper;
|
||||
|
||||
import org.junit.After;
|
||||
|
@ -108,7 +104,7 @@ public class BindWidgetTest extends AbstractLauncherUiTest {
|
|||
@Test
|
||||
public void testBindNormalWidget_withConfig() {
|
||||
LauncherAppWidgetProviderInfo info = TestViewHelpers.findWidgetProvider(this, true);
|
||||
LauncherAppWidgetInfo item = createWidgetInfo(info, true);
|
||||
LauncherAppWidgetInfo item = createWidgetInfo(info, getTargetContext(), true);
|
||||
|
||||
addItemToScreen(item);
|
||||
verifyWidgetPresent(info);
|
||||
|
@ -117,7 +113,7 @@ public class BindWidgetTest extends AbstractLauncherUiTest {
|
|||
@Test
|
||||
public void testBindNormalWidget_withoutConfig() {
|
||||
LauncherAppWidgetProviderInfo info = TestViewHelpers.findWidgetProvider(this, false);
|
||||
LauncherAppWidgetInfo item = createWidgetInfo(info, true);
|
||||
LauncherAppWidgetInfo item = createWidgetInfo(info, getTargetContext(), true);
|
||||
|
||||
addItemToScreen(item);
|
||||
verifyWidgetPresent(info);
|
||||
|
@ -126,7 +122,7 @@ public class BindWidgetTest extends AbstractLauncherUiTest {
|
|||
@Test
|
||||
public void testUnboundWidget_removed() {
|
||||
LauncherAppWidgetProviderInfo info = TestViewHelpers.findWidgetProvider(this, false);
|
||||
LauncherAppWidgetInfo item = createWidgetInfo(info, false);
|
||||
LauncherAppWidgetInfo item = createWidgetInfo(info, getTargetContext(), false);
|
||||
item.appWidgetId = -33;
|
||||
|
||||
addItemToScreen(item);
|
||||
|
@ -147,7 +143,7 @@ public class BindWidgetTest extends AbstractLauncherUiTest {
|
|||
LauncherAppWidgetProviderInfo info = TestViewHelpers.findWidgetProvider(this, false);
|
||||
|
||||
// Do not bind the widget
|
||||
LauncherAppWidgetInfo item = createWidgetInfo(info, false);
|
||||
LauncherAppWidgetInfo item = createWidgetInfo(info, getTargetContext(), false);
|
||||
item.restoreStatus = LauncherAppWidgetInfo.FLAG_ID_NOT_VALID;
|
||||
|
||||
addItemToScreen(item);
|
||||
|
@ -160,7 +156,7 @@ public class BindWidgetTest extends AbstractLauncherUiTest {
|
|||
LauncherAppWidgetProviderInfo info = TestViewHelpers.findWidgetProvider(this, true);
|
||||
|
||||
// Do not bind the widget
|
||||
LauncherAppWidgetInfo item = createWidgetInfo(info, false);
|
||||
LauncherAppWidgetInfo item = createWidgetInfo(info, getTargetContext(), false);
|
||||
item.restoreStatus = LauncherAppWidgetInfo.FLAG_ID_NOT_VALID;
|
||||
|
||||
addItemToScreen(item);
|
||||
|
@ -281,47 +277,6 @@ public class BindWidgetTest extends AbstractLauncherUiTest {
|
|||
widget != null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a LauncherAppWidgetInfo corresponding to {@param info}
|
||||
*
|
||||
* @param bindWidget if true the info is bound and a valid widgetId is assigned to
|
||||
* the LauncherAppWidgetInfo
|
||||
*/
|
||||
public static LauncherAppWidgetInfo createWidgetInfo(
|
||||
LauncherAppWidgetProviderInfo info, boolean bindWidget) {
|
||||
Context targetContext = getTargetContext();
|
||||
|
||||
LauncherAppWidgetInfo item = new LauncherAppWidgetInfo(
|
||||
LauncherAppWidgetInfo.NO_ID, info.provider);
|
||||
item.spanX = info.minSpanX;
|
||||
item.spanY = info.minSpanY;
|
||||
item.minSpanX = info.minSpanX;
|
||||
item.minSpanY = info.minSpanY;
|
||||
item.user = info.getProfile();
|
||||
item.cellX = 0;
|
||||
item.cellY = 1;
|
||||
item.container = LauncherSettings.Favorites.CONTAINER_DESKTOP;
|
||||
|
||||
if (bindWidget) {
|
||||
PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(info);
|
||||
pendingInfo.spanX = item.spanX;
|
||||
pendingInfo.spanY = item.spanY;
|
||||
pendingInfo.minSpanX = item.minSpanX;
|
||||
pendingInfo.minSpanY = item.minSpanY;
|
||||
Bundle options = getDefaultOptionsForWidget(targetContext, pendingInfo);
|
||||
|
||||
AppWidgetHost host = new LauncherAppWidgetHost(targetContext);
|
||||
int widgetId = host.allocateAppWidgetId();
|
||||
if (!new WidgetManagerHelper(targetContext)
|
||||
.bindAppWidgetIdIfAllowed(widgetId, info, options)) {
|
||||
host.deleteAppWidgetId(widgetId);
|
||||
throw new IllegalArgumentException("Unable to bind widget id");
|
||||
}
|
||||
item.appWidgetId = widgetId;
|
||||
}
|
||||
return item;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a LauncherAppWidgetInfo with package name which is not present on the device
|
||||
*/
|
||||
|
|
|
@ -0,0 +1 @@
|
|||
Common source code used by both android tests and robolectric tests
|
|
@ -0,0 +1,104 @@
|
|||
/*
|
||||
* Copyright (C) 2020 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.android.launcher3.common;
|
||||
|
||||
import static com.android.launcher3.WorkspaceLayoutManager.FIRST_SCREEN_ID;
|
||||
import static com.android.launcher3.widget.WidgetHostViewLoader.getDefaultOptionsForWidget;
|
||||
|
||||
import android.appwidget.AppWidgetHost;
|
||||
import android.content.ContentResolver;
|
||||
import android.content.Context;
|
||||
import android.os.Bundle;
|
||||
|
||||
import com.android.launcher3.LauncherAppWidgetHost;
|
||||
import com.android.launcher3.LauncherAppWidgetProviderInfo;
|
||||
import com.android.launcher3.LauncherSettings;
|
||||
import com.android.launcher3.model.data.ItemInfo;
|
||||
import com.android.launcher3.model.data.LauncherAppWidgetInfo;
|
||||
import com.android.launcher3.util.ContentWriter;
|
||||
import com.android.launcher3.widget.PendingAddWidgetInfo;
|
||||
import com.android.launcher3.widget.WidgetManagerHelper;
|
||||
|
||||
/**
|
||||
* Common method for widget binding
|
||||
*/
|
||||
public class WidgetUtils {
|
||||
|
||||
/**
|
||||
* Creates a LauncherAppWidgetInfo corresponding to {@param info}
|
||||
*
|
||||
* @param bindWidget if true the info is bound and a valid widgetId is assigned to
|
||||
* the LauncherAppWidgetInfo
|
||||
*/
|
||||
public static LauncherAppWidgetInfo createWidgetInfo(
|
||||
LauncherAppWidgetProviderInfo info, Context targetContext, boolean bindWidget) {
|
||||
LauncherAppWidgetInfo item = new LauncherAppWidgetInfo(
|
||||
LauncherAppWidgetInfo.NO_ID, info.provider);
|
||||
item.spanX = info.minSpanX;
|
||||
item.spanY = info.minSpanY;
|
||||
item.minSpanX = info.minSpanX;
|
||||
item.minSpanY = info.minSpanY;
|
||||
item.user = info.getProfile();
|
||||
item.cellX = 0;
|
||||
item.cellY = 1;
|
||||
item.container = LauncherSettings.Favorites.CONTAINER_DESKTOP;
|
||||
|
||||
if (bindWidget) {
|
||||
PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(info);
|
||||
pendingInfo.spanX = item.spanX;
|
||||
pendingInfo.spanY = item.spanY;
|
||||
pendingInfo.minSpanX = item.minSpanX;
|
||||
pendingInfo.minSpanY = item.minSpanY;
|
||||
Bundle options = getDefaultOptionsForWidget(targetContext, pendingInfo);
|
||||
|
||||
AppWidgetHost host = new LauncherAppWidgetHost(targetContext);
|
||||
int widgetId = host.allocateAppWidgetId();
|
||||
if (!new WidgetManagerHelper(targetContext)
|
||||
.bindAppWidgetIdIfAllowed(widgetId, info, options)) {
|
||||
host.deleteAppWidgetId(widgetId);
|
||||
throw new IllegalArgumentException("Unable to bind widget id");
|
||||
}
|
||||
item.appWidgetId = widgetId;
|
||||
}
|
||||
return item;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds {@param item} on the homescreen on the 0th screen
|
||||
*/
|
||||
public static void addItemToScreen(ItemInfo item, Context targetContext) {
|
||||
ContentResolver resolver = targetContext.getContentResolver();
|
||||
int screenId = FIRST_SCREEN_ID;
|
||||
// Update the screen id counter for the provider.
|
||||
LauncherSettings.Settings.call(resolver,
|
||||
LauncherSettings.Settings.METHOD_NEW_SCREEN_ID);
|
||||
|
||||
if (screenId > FIRST_SCREEN_ID) {
|
||||
screenId = FIRST_SCREEN_ID;
|
||||
}
|
||||
|
||||
// Insert the item
|
||||
ContentWriter writer = new ContentWriter(targetContext);
|
||||
item.id = LauncherSettings.Settings.call(
|
||||
resolver, LauncherSettings.Settings.METHOD_NEW_ITEM_ID)
|
||||
.getInt(LauncherSettings.Settings.EXTRA_VALUE);
|
||||
item.screenId = screenId;
|
||||
item.onAddToDatabase(writer);
|
||||
writer.put(LauncherSettings.Favorites._ID, item.id);
|
||||
resolver.insert(LauncherSettings.Favorites.CONTENT_URI,
|
||||
writer.getValues(targetContext));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue