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

View File

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

View File

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

View File

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

View File

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

View File

@ -7,6 +7,8 @@ import com.android.launcher3.tapl.LauncherInstrumentation;
import org.junit.Assert;
import java.util.function.Supplier;
/**
* 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,
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);
}
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) {
final long startTime = SystemClock.uptimeMillis();
long endTime = startTime + timeout;
@ -45,6 +53,6 @@ public class Wait {
}
Log.d("Wait", "atMost: timed out: " + SystemClock.uptimeMillis());
launcher.checkForAnomaly();
Assert.fail(message);
Assert.fail(message.get());
}
}