Improving navigation mode switch rule and around

Logging assertion failures.
Modifying waits for condition to avoid timing out the whole test if the
iteration takes too long in favor of failing with an actionable diag.

Bug: 145985438
Change-Id: Ie32d93e1548ce6ec64c38449eb1be1287ff9cf56
This commit is contained in:
vadimt 2019-12-12 14:18:19 -08:00
parent a11ff18d6b
commit 2eb48498b4
6 changed files with 28 additions and 22 deletions

View File

@ -35,6 +35,7 @@ import androidx.test.uiautomator.UiDevice;
import com.android.launcher3.tapl.LauncherInstrumentation; import com.android.launcher3.tapl.LauncherInstrumentation;
import com.android.launcher3.tapl.TestHelpers; import com.android.launcher3.tapl.TestHelpers;
import com.android.launcher3.util.Wait;
import com.android.launcher3.util.rule.FailureWatcher; import com.android.launcher3.util.rule.FailureWatcher;
import com.android.systemui.shared.system.QuickStepContract; import com.android.systemui.shared.system.QuickStepContract;
@ -57,6 +58,8 @@ public class NavigationModeSwitchRule implements TestRule {
static final String TAG = "QuickStepOnOffRule"; static final String TAG = "QuickStepOnOffRule";
public static final int WAIT_TIME_MS = 10000;
public enum Mode { public enum Mode {
THREE_BUTTON, TWO_BUTTON, ZERO_BUTTON, ALL THREE_BUTTON, TWO_BUTTON, ZERO_BUTTON, ALL
} }
@ -118,8 +121,8 @@ public class NavigationModeSwitchRule implements TestRule {
if (mode == THREE_BUTTON || mode == ALL) { if (mode == THREE_BUTTON || mode == ALL) {
evaluateWithThreeButtons(); evaluateWithThreeButtons();
} }
} catch (Exception e) { } catch (Throwable e) {
Log.e(TAG, "Exception", e); Log.e(TAG, "Error", e);
throw e; throw e;
} finally { } finally {
assertTrue("Couldn't set overlay", assertTrue("Couldn't set overlay",
@ -195,19 +198,14 @@ public class NavigationModeSwitchRule implements TestRule {
currentSysUiNavigationMode() == expectedMode); currentSysUiNavigationMode() == expectedMode);
} }
for (int i = 0; i != 100; ++i) { Wait.atMost("Couldn't switch to " + overlayPackage,
if (mLauncher.getNavigationModel() == expectedMode) break; () -> mLauncher.getNavigationModel() == expectedMode, WAIT_TIME_MS,
Thread.sleep(100); mLauncher);
}
assertTrue("Couldn't switch to " + overlayPackage,
mLauncher.getNavigationModel() == expectedMode);
for (int i = 0; i != 100; ++i) { Wait.atMost(() -> "Switching nav mode: "
if (mLauncher.getNavigationModeMismatchError() == null) break; + mLauncher.getNavigationModeMismatchError(),
Thread.sleep(100); () -> mLauncher.getNavigationModeMismatchError() == null, WAIT_TIME_MS,
} mLauncher);
final String error = mLauncher.getNavigationModeMismatchError();
assertTrue("Switching nav mode: " + error, error == null);
return true; return true;
} }

View File

@ -259,7 +259,7 @@ public abstract class AbstractLauncherUiTest {
protected <T> T getOnUiThread(final Callable<T> callback) { protected <T> T getOnUiThread(final Callable<T> callback) {
try { try {
return mMainThreadExecutor.submit(callback).get(); return mMainThreadExecutor.submit(callback).get();
} catch (Exception e) { } catch (Throwable e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
} }

View File

@ -38,8 +38,8 @@ class PortraitLandscapeRunner implements TestRule {
evaluateInPortrait(); evaluateInPortrait();
evaluateInLandscape(); evaluateInLandscape();
} catch (Exception e) { } catch (Throwable e) {
Log.e(TAG, "Exception", e); Log.e(TAG, "Error", e);
throw e; throw e;
} finally { } finally {
mTest.mDevice.setOrientationNatural(); mTest.mDevice.setOrientationNatural();

View File

@ -103,11 +103,11 @@ public class AddConfigWidgetTest extends AbstractLauncherUiTest {
setResult(acceptConfig); setResult(acceptConfig);
if (acceptConfig) { if (acceptConfig) {
Wait.atMost(null, new WidgetSearchCondition(), DEFAULT_ACTIVITY_TIMEOUT, mLauncher); Wait.atMost("", new WidgetSearchCondition(), DEFAULT_ACTIVITY_TIMEOUT, mLauncher);
assertNotNull(mAppWidgetManager.getAppWidgetInfo(mWidgetId)); assertNotNull(mAppWidgetManager.getAppWidgetInfo(mWidgetId));
} else { } else {
// Verify that the widget id is deleted. // Verify that the widget id is deleted.
Wait.atMost(null, () -> mAppWidgetManager.getAppWidgetInfo(mWidgetId) == null, Wait.atMost("", () -> mAppWidgetManager.getAppWidgetInfo(mWidgetId) == null,
DEFAULT_ACTIVITY_TIMEOUT, mLauncher); DEFAULT_ACTIVITY_TIMEOUT, mLauncher);
} }
} }

View File

@ -170,7 +170,7 @@ public class RequestPinItemTest extends AbstractLauncherUiTest {
// Go back to home // Go back to home
mLauncher.pressHome(); mLauncher.pressHome();
Wait.atMost(null, new ItemSearchCondition(itemMatcher), DEFAULT_ACTIVITY_TIMEOUT, Wait.atMost("", new ItemSearchCondition(itemMatcher), DEFAULT_ACTIVITY_TIMEOUT,
mLauncher); mLauncher);
} }

View File

@ -7,6 +7,8 @@ import com.android.launcher3.tapl.LauncherInstrumentation;
import org.junit.Assert; import org.junit.Assert;
import java.util.function.Supplier;
/** /**
* A utility class for waiting for a condition to be true. * A utility class for waiting for a condition to be true.
*/ */
@ -16,10 +18,16 @@ public class Wait {
public static void atMost(String message, Condition condition, long timeout, public static void atMost(String message, Condition condition, long timeout,
LauncherInstrumentation launcher) { LauncherInstrumentation launcher) {
atMost(() -> message, condition, timeout, DEFAULT_SLEEP_MS, launcher);
}
public static void atMost(Supplier<String> message, Condition condition, long timeout,
LauncherInstrumentation launcher) {
atMost(message, condition, timeout, DEFAULT_SLEEP_MS, launcher); atMost(message, condition, timeout, DEFAULT_SLEEP_MS, launcher);
} }
public static void atMost(String message, Condition condition, long timeout, long sleepMillis, public static void atMost(Supplier<String> message, Condition condition, long timeout,
long sleepMillis,
LauncherInstrumentation launcher) { LauncherInstrumentation launcher) {
final long startTime = SystemClock.uptimeMillis(); final long startTime = SystemClock.uptimeMillis();
long endTime = startTime + timeout; long endTime = startTime + timeout;
@ -45,6 +53,6 @@ public class Wait {
} }
Log.d("Wait", "atMost: timed out: " + SystemClock.uptimeMillis()); Log.d("Wait", "atMost: timed out: " + SystemClock.uptimeMillis());
launcher.checkForAnomaly(); launcher.checkForAnomaly();
Assert.fail(message); Assert.fail(message.get());
} }
} }