Merge "Fixing BindWidgetTest failing on Launcher3" into ub-launcher3-master

This commit is contained in:
TreeHugger Robot 2018-10-11 23:15:18 +00:00 committed by Android (Google) Code Review
commit 0e62132923
5 changed files with 52 additions and 42 deletions

View File

@ -36,7 +36,6 @@ import com.android.launcher3.LauncherAppWidgetProviderInfo;
import com.android.launcher3.Workspace; import com.android.launcher3.Workspace;
import com.android.launcher3.testcomponent.WidgetConfigActivity; import com.android.launcher3.testcomponent.WidgetConfigActivity;
import com.android.launcher3.ui.AbstractLauncherUiTest; import com.android.launcher3.ui.AbstractLauncherUiTest;
import com.android.launcher3.tapl.TestHelpers;
import com.android.launcher3.util.Condition; import com.android.launcher3.util.Condition;
import com.android.launcher3.util.Wait; import com.android.launcher3.util.Wait;
import com.android.launcher3.util.rule.ShellCommandRule; import com.android.launcher3.util.rule.ShellCommandRule;
@ -55,7 +54,7 @@ import org.junit.runner.RunWith;
@RunWith(AndroidJUnit4.class) @RunWith(AndroidJUnit4.class)
public class AddConfigWidgetTest extends AbstractLauncherUiTest { public class AddConfigWidgetTest extends AbstractLauncherUiTest {
@Rule public ShellCommandRule mGrantWidgetRule = ShellCommandRule.grandWidgetBind(); @Rule public ShellCommandRule mGrantWidgetRule = ShellCommandRule.grantWidgetBind();
private LauncherAppWidgetProviderInfo mWidgetInfo; private LauncherAppWidgetProviderInfo mWidgetInfo;
private AppWidgetManager mAppWidgetManager; private AppWidgetManager mAppWidgetManager;

View File

@ -45,7 +45,7 @@ import org.junit.runner.RunWith;
@RunWith(AndroidJUnit4.class) @RunWith(AndroidJUnit4.class)
public class AddWidgetTest extends AbstractLauncherUiTest { public class AddWidgetTest extends AbstractLauncherUiTest {
@Rule public ShellCommandRule mGrantWidgetRule = ShellCommandRule.grandWidgetBind(); @Rule public ShellCommandRule mGrantWidgetRule = ShellCommandRule.grantWidgetBind();
@Test @Test
@Ignore @Ignore

View File

@ -30,21 +30,16 @@ import android.content.pm.PackageInstaller.SessionParams;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
import android.database.Cursor; import android.database.Cursor;
import android.os.Bundle; import android.os.Bundle;
import androidx.test.filters.LargeTest;
import androidx.test.runner.AndroidJUnit4;
import androidx.test.uiautomator.UiSelector;
import com.android.launcher3.LauncherAppWidgetHost; import com.android.launcher3.LauncherAppWidgetHost;
import com.android.launcher3.LauncherAppWidgetInfo; import com.android.launcher3.LauncherAppWidgetInfo;
import com.android.launcher3.LauncherAppWidgetProviderInfo; import com.android.launcher3.LauncherAppWidgetProviderInfo;
import com.android.launcher3.LauncherModel;
import com.android.launcher3.LauncherSettings; import com.android.launcher3.LauncherSettings;
import com.android.launcher3.Workspace; import com.android.launcher3.Workspace;
import com.android.launcher3.compat.AppWidgetManagerCompat; import com.android.launcher3.compat.AppWidgetManagerCompat;
import com.android.launcher3.compat.PackageInstallerCompat; import com.android.launcher3.compat.PackageInstallerCompat;
import com.android.launcher3.ui.AbstractLauncherUiTest; import com.android.launcher3.ui.AbstractLauncherUiTest;
import com.android.launcher3.util.ContentWriter; import com.android.launcher3.util.ContentWriter;
import com.android.launcher3.util.LooperExecutor;
import com.android.launcher3.util.rule.ShellCommandRule; import com.android.launcher3.util.rule.ShellCommandRule;
import com.android.launcher3.widget.LauncherAppWidgetHostView; import com.android.launcher3.widget.LauncherAppWidgetHostView;
import com.android.launcher3.widget.PendingAddWidgetInfo; import com.android.launcher3.widget.PendingAddWidgetInfo;
@ -56,12 +51,13 @@ import org.junit.Before;
import org.junit.Ignore; import org.junit.Ignore;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
import org.junit.rules.TestRule;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import java.util.Set; import java.util.Set;
import java.util.concurrent.Callable;
import java.util.concurrent.TimeUnit; import androidx.test.filters.LargeTest;
import androidx.test.runner.AndroidJUnit4;
import androidx.test.uiautomator.UiSelector;
/** /**
* Tests for bind widget flow. * Tests for bind widget flow.
@ -72,6 +68,8 @@ import java.util.concurrent.TimeUnit;
@RunWith(AndroidJUnit4.class) @RunWith(AndroidJUnit4.class)
public class BindWidgetTest extends AbstractLauncherUiTest { public class BindWidgetTest extends AbstractLauncherUiTest {
@Rule public ShellCommandRule mGrantWidgetRule = ShellCommandRule.grantWidgetBind();
private ContentResolver mResolver; private ContentResolver mResolver;
private AppWidgetManagerCompat mWidgetManager; private AppWidgetManagerCompat mWidgetManager;
@ -121,7 +119,8 @@ public class BindWidgetTest extends AbstractLauncherUiTest {
LauncherAppWidgetProviderInfo info = findWidgetProvider(true); LauncherAppWidgetProviderInfo info = findWidgetProvider(true);
LauncherAppWidgetInfo item = createWidgetInfo(info, true); LauncherAppWidgetInfo item = createWidgetInfo(info, true);
setupAndVerifyContents(item, LauncherAppWidgetHostView.class, info.label); setupContents(item);
verifyWidgetPresent(info);
} }
@Test @Test
@ -129,18 +128,21 @@ public class BindWidgetTest extends AbstractLauncherUiTest {
LauncherAppWidgetProviderInfo info = findWidgetProvider(false); LauncherAppWidgetProviderInfo info = findWidgetProvider(false);
LauncherAppWidgetInfo item = createWidgetInfo(info, true); LauncherAppWidgetInfo item = createWidgetInfo(info, true);
setupAndVerifyContents(item, LauncherAppWidgetHostView.class, info.label); setupContents(item);
verifyWidgetPresent(info);
} }
@Test @Ignore @Test @Ignore
public void testUnboundWidget_removed() throws Exception { public void testUnboundWidget_removed() {
LauncherAppWidgetProviderInfo info = findWidgetProvider(false); LauncherAppWidgetProviderInfo info = findWidgetProvider(false);
LauncherAppWidgetInfo item = createWidgetInfo(info, false); LauncherAppWidgetInfo item = createWidgetInfo(info, false);
item.appWidgetId = -33; item.appWidgetId = -33;
// Since there is no widget to verify, just wait until the workspace is ready. setupContents(item);
setupAndVerifyContents(item, Workspace.class, null);
// Since there is no widget to verify, just wait until the workspace is ready.
// TODO: fix LauncherInstrumentation#LAUNCHER_PKG
mLauncher.getWorkspace();
// Item deleted from db // Item deleted from db
mCursor = mResolver.query(LauncherSettings.Favorites.getContentUri(item.id), mCursor = mResolver.query(LauncherSettings.Favorites.getContentUri(item.id),
null, null, null, null, null); null, null, null, null, null);
@ -163,7 +165,9 @@ public class BindWidgetTest extends AbstractLauncherUiTest {
LauncherAppWidgetInfo item = createWidgetInfo(info, false); LauncherAppWidgetInfo item = createWidgetInfo(info, false);
item.restoreStatus = LauncherAppWidgetInfo.FLAG_ID_NOT_VALID; item.restoreStatus = LauncherAppWidgetInfo.FLAG_ID_NOT_VALID;
setupAndVerifyContents(item, LauncherAppWidgetHostView.class, info.label); setupContents(item);
verifyWidgetPresent(info);
if (com.android.launcher3.Utilities.IS_RUNNING_IN_TEST_HARNESS if (com.android.launcher3.Utilities.IS_RUNNING_IN_TEST_HARNESS
&& com.android.launcher3.Utilities.IS_DEBUG_DEVICE) { && com.android.launcher3.Utilities.IS_DEBUG_DEVICE) {
android.util.Log.d("b/117332845", android.util.Log.d("b/117332845",
@ -172,7 +176,7 @@ public class BindWidgetTest extends AbstractLauncherUiTest {
} }
@Test @Test
public void testPendingWidget_withConfigScreen() throws Exception { public void testPendingWidget_withConfigScreen() {
if (com.android.launcher3.Utilities.IS_RUNNING_IN_TEST_HARNESS if (com.android.launcher3.Utilities.IS_RUNNING_IN_TEST_HARNESS
&& com.android.launcher3.Utilities.IS_DEBUG_DEVICE) { && com.android.launcher3.Utilities.IS_DEBUG_DEVICE) {
android.util.Log.d("b/117332845", android.util.Log.d("b/117332845",
@ -185,7 +189,9 @@ public class BindWidgetTest extends AbstractLauncherUiTest {
LauncherAppWidgetInfo item = createWidgetInfo(info, false); LauncherAppWidgetInfo item = createWidgetInfo(info, false);
item.restoreStatus = LauncherAppWidgetInfo.FLAG_ID_NOT_VALID; item.restoreStatus = LauncherAppWidgetInfo.FLAG_ID_NOT_VALID;
setupAndVerifyContents(item, PendingAppWidgetHostView.class, null); setupContents(item);
verifyPendingWidgetPresent();
// Item deleted from db // Item deleted from db
mCursor = mResolver.query(LauncherSettings.Favorites.getContentUri(item.id), mCursor = mResolver.query(LauncherSettings.Favorites.getContentUri(item.id),
null, null, null, null, null); null, null, null, null, null);
@ -205,12 +211,16 @@ public class BindWidgetTest extends AbstractLauncherUiTest {
} }
@Test @Ignore @Test @Ignore
public void testPendingWidget_notRestored_removed() throws Exception { public void testPendingWidget_notRestored_removed() {
LauncherAppWidgetInfo item = getInvalidWidgetInfo(); LauncherAppWidgetInfo item = getInvalidWidgetInfo();
item.restoreStatus = LauncherAppWidgetInfo.FLAG_ID_NOT_VALID item.restoreStatus = LauncherAppWidgetInfo.FLAG_ID_NOT_VALID
| LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY; | LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY;
setupAndVerifyContents(item, Workspace.class, null); setupContents(item);
// Since there is no widget to verify, just wait until the workspace is ready.
// TODO: fix LauncherInstrumentation#LAUNCHER_PKG
mLauncher.getWorkspace();
// The view does not exist // The view does not exist
assertFalse(mDevice.findObject( assertFalse(mDevice.findObject(
new UiSelector().className(PendingAppWidgetHostView.class)).exists()); new UiSelector().className(PendingAppWidgetHostView.class)).exists());
@ -221,7 +231,7 @@ public class BindWidgetTest extends AbstractLauncherUiTest {
} }
@Test @Test
public void testPendingWidget_notRestored_brokenInstall() throws Exception { public void testPendingWidget_notRestored_brokenInstall() {
// A widget which is was being installed once, even if its not being // A widget which is was being installed once, even if its not being
// installed at the moment is not removed. // installed at the moment is not removed.
LauncherAppWidgetInfo item = getInvalidWidgetInfo(); LauncherAppWidgetInfo item = getInvalidWidgetInfo();
@ -229,7 +239,9 @@ public class BindWidgetTest extends AbstractLauncherUiTest {
| LauncherAppWidgetInfo.FLAG_RESTORE_STARTED | LauncherAppWidgetInfo.FLAG_RESTORE_STARTED
| LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY; | LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY;
setupAndVerifyContents(item, PendingAppWidgetHostView.class, null); setupContents(item);
verifyPendingWidgetPresent();
// Verify item still exists in db // Verify item still exists in db
mCursor = mResolver.query(LauncherSettings.Favorites.getContentUri(item.id), mCursor = mResolver.query(LauncherSettings.Favorites.getContentUri(item.id),
null, null, null, null, null); null, null, null, null, null);
@ -255,7 +267,9 @@ public class BindWidgetTest extends AbstractLauncherUiTest {
PackageInstaller installer = mTargetContext.getPackageManager().getPackageInstaller(); PackageInstaller installer = mTargetContext.getPackageManager().getPackageInstaller();
mSessionId = installer.createSession(params); mSessionId = installer.createSession(params);
setupAndVerifyContents(item, PendingAppWidgetHostView.class, null); setupContents(item);
verifyPendingWidgetPresent();
// Verify item still exists in db // Verify item still exists in db
mCursor = mResolver.query(LauncherSettings.Favorites.getContentUri(item.id), mCursor = mResolver.query(LauncherSettings.Favorites.getContentUri(item.id),
null, null, null, null, null); null, null, null, null, null);
@ -271,11 +285,8 @@ public class BindWidgetTest extends AbstractLauncherUiTest {
/** /**
* Adds {@param item} on the homescreen on the 0th screen at 0,0, and verifies that the * Adds {@param item} on the homescreen on the 0th screen at 0,0, and verifies that the
* widget class is displayed on the homescreen. * widget class is displayed on the homescreen.
* @param widgetClass the View class which is displayed on the homescreen
* @param desc the content description of the view or null.
*/ */
private void setupAndVerifyContents( private void setupContents(LauncherAppWidgetInfo item) {
LauncherAppWidgetInfo item, Class<?> widgetClass, String desc) {
int screenId = Workspace.FIRST_SCREEN_ID; int screenId = Workspace.FIRST_SCREEN_ID;
// Update the screen id counter for the provider. // Update the screen id counter for the provider.
LauncherSettings.Settings.call(mResolver, LauncherSettings.Settings.METHOD_NEW_SCREEN_ID); LauncherSettings.Settings.call(mResolver, LauncherSettings.Settings.METHOD_NEW_SCREEN_ID);
@ -302,13 +313,17 @@ public class BindWidgetTest extends AbstractLauncherUiTest {
// Launch the home activity // Launch the home activity
mActivityMonitor.startLauncher(); mActivityMonitor.startLauncher();
waitForModelLoaded(); waitForModelLoaded();
}
// Verify UI private void verifyWidgetPresent(LauncherAppWidgetProviderInfo info) {
UiSelector selector = new UiSelector().packageName(mTargetContext.getPackageName()) UiSelector selector = new UiSelector().packageName(mTargetContext.getPackageName())
.className(widgetClass); .className(LauncherAppWidgetHostView.class).description(info.label);
if (desc != null) { assertTrue(mDevice.findObject(selector).waitForExists(DEFAULT_UI_TIMEOUT));
selector = selector.description(desc); }
}
private void verifyPendingWidgetPresent() {
UiSelector selector = new UiSelector().packageName(mTargetContext.getPackageName())
.className(PendingAppWidgetHostView.class);
assertTrue(mDevice.findObject(selector).waitForExists(DEFAULT_UI_TIMEOUT)); assertTrue(mDevice.findObject(selector).waitForExists(DEFAULT_UI_TIMEOUT));
} }
@ -357,13 +372,9 @@ public class BindWidgetTest extends AbstractLauncherUiTest {
int count = 0; int count = 0;
String pkg = invalidPackage; String pkg = invalidPackage;
Set<String> activePackage = getOnUiThread(new Callable<Set<String>>() { Set<String> activePackage = getOnUiThread(() ->
@Override PackageInstallerCompat.getInstance(mTargetContext)
public Set<String> call() throws Exception { .updateAndGetActiveSessionCache().keySet());
return PackageInstallerCompat.getInstance(mTargetContext)
.updateAndGetActiveSessionCache().keySet();
}
});
while(true) { while(true) {
try { try {
mTargetContext.getPackageManager().getPackageInfo( mTargetContext.getPackageManager().getPackageInfo(

View File

@ -62,7 +62,7 @@ import java.util.UUID;
@RunWith(AndroidJUnit4.class) @RunWith(AndroidJUnit4.class)
public class RequestPinItemTest extends AbstractLauncherUiTest { public class RequestPinItemTest extends AbstractLauncherUiTest {
@Rule public ShellCommandRule mGrantWidgetRule = ShellCommandRule.grandWidgetBind(); @Rule public ShellCommandRule mGrantWidgetRule = ShellCommandRule.grantWidgetBind();
private String mCallbackAction; private String mCallbackAction;
private String mShortcutId; private String mShortcutId;

View File

@ -63,7 +63,7 @@ public class ShellCommandRule implements TestRule {
/** /**
* Grants the launcher permission to bind widgets. * Grants the launcher permission to bind widgets.
*/ */
public static ShellCommandRule grandWidgetBind() { public static ShellCommandRule grantWidgetBind() {
return new ShellCommandRule("appwidget grantbind --package " return new ShellCommandRule("appwidget grantbind --package "
+ InstrumentationRegistry.getTargetContext().getPackageName(), null); + InstrumentationRegistry.getTargetContext().getPackageName(), null);
} }