From 13f572fac571fe812f2ceba0a3140ceb62090ba5 Mon Sep 17 00:00:00 2001 From: Chenxia Liu Date: Tue, 10 Sep 2013 18:10:43 -0700 Subject: [PATCH] Bug 912510 - Part 2: Update base tests to use Conditions. r=gbrown --- .../android/base/tests/AboutHomeTest.java.in | 16 +++---- mobile/android/base/tests/BaseTest.java.in | 42 +++++++++++++------ 2 files changed, 38 insertions(+), 20 deletions(-) diff --git a/mobile/android/base/tests/AboutHomeTest.java.in b/mobile/android/base/tests/AboutHomeTest.java.in index f860d4fc100d..7666c4a344cd 100644 --- a/mobile/android/base/tests/AboutHomeTest.java.in +++ b/mobile/android/base/tests/AboutHomeTest.java.in @@ -1,6 +1,7 @@ #filter substitution package @ANDROID_PACKAGE_NAME@.tests; +import com.jayway.android.robotium.solo.Condition; import @ANDROID_PACKAGE_NAME@.*; import android.content.ContentResolver; @@ -79,12 +80,12 @@ abstract class AboutHomeTest extends BaseTest { /** * Waits for the given ListView to have a non-empty adapter. * - * This method will fail if the given ListView or its adapter are null. + * This method will return false if the given ListView or its adapter are null. */ protected boolean waitForListToLoad(final ListView listView) { - boolean result = waitForTest(new BooleanTest() { + Condition listWaitCondition = new Condition() { @Override - public boolean test() { + public boolean isSatisfied() { if (listView == null) { return false; } @@ -96,9 +97,8 @@ abstract class AboutHomeTest extends BaseTest { return (adapter.getCount() > 0); } - }, MAX_WAIT_MS); - - return result; + }; + return waitForCondition(listWaitCondition, MAX_WAIT_MS); } /** @@ -331,9 +331,9 @@ abstract class AboutHomeTest extends BaseTest { // A wait in order for the about:home tab to be rendered after drag/tab selection private void waitForAboutHomeTab(final int tabIndex) { - boolean correctTab = waitForTest(new BooleanTest() { + boolean correctTab = waitForCondition(new Condition() { @Override - public boolean test() { + public boolean isSatisfied() { ViewPager pager = (ViewPager)mSolo.getView(ViewPager.class, 0); return (pager.getCurrentItem() == tabIndex); } diff --git a/mobile/android/base/tests/BaseTest.java.in b/mobile/android/base/tests/BaseTest.java.in index 089a756cef81..0b26bf803553 100644 --- a/mobile/android/base/tests/BaseTest.java.in +++ b/mobile/android/base/tests/BaseTest.java.in @@ -1,6 +1,7 @@ #filter substitution package @ANDROID_PACKAGE_NAME@.tests; +import com.jayway.android.robotium.solo.Condition; import com.jayway.android.robotium.solo.Solo; import @ANDROID_PACKAGE_NAME@.*; @@ -200,9 +201,9 @@ abstract class BaseTest extends ActivityInstrumentationTestCase2 { mSolo.clickOnView(toolbarView); // Wait for highlighed text to gain focus - boolean success = waitForTest(new BooleanTest() { + boolean success = waitForCondition(new Condition() { @Override - public boolean test() { + public boolean isSatisfied() { EditText urlEditText = mSolo.getEditText(0); if (urlEditText.isInputMethodTarget()) { return true; @@ -278,14 +279,14 @@ abstract class BaseTest extends ActivityInstrumentationTestCase2 { if (urlEditText != null) { // wait for a short time for the expected text, in case there is a delay // in updating the view - waitForTest(new VerifyTextViewText(urlEditText, url), VERIFY_URL_TIMEOUT); + waitForCondition(new VerifyTextViewText(urlEditText, url), VERIFY_URL_TIMEOUT); urlBarText = urlEditText.getText().toString(); } mAsserter.is(urlBarText, url, "Browser toolbar URL stayed the same"); } - class VerifyTextViewText implements BooleanTest { + class VerifyTextViewText implements Condition { private TextView mTextView; private String mExpected; public VerifyTextViewText(TextView textView, String expected) { @@ -294,7 +295,7 @@ abstract class BaseTest extends ActivityInstrumentationTestCase2 { } @Override - public boolean test() { + public boolean isSatisfied() { String textValue = mTextView.getText().toString(); return mExpected.equals(textValue); } @@ -308,6 +309,21 @@ abstract class BaseTest extends ActivityInstrumentationTestCase2 { return mRawBaseUrl + "/" + url.replaceAll("(^/)", ""); } + /* + * Wrapper method for mSolo.waitForCondition with additional logging. + */ + protected final boolean waitForCondition(Condition condition, int timeout) { + boolean result = mSolo.waitForCondition(condition, timeout); + if (!result) { + // Log timeout failure for diagnostic purposes only; a failed wait may + // be normal and does not necessarily warrant a test asssertion/failure. + mAsserter.dumpLog("waitForCondition timeout after " + timeout + " ms."); + } + return result; + } + + // TODO: With Robotium 4.2, we should use Condition and waitForCondition instead. + // Future boolean tests should not use this method. protected final boolean waitForTest(BooleanTest t, int timeout) { long end = SystemClock.uptimeMillis() + timeout; while (SystemClock.uptimeMillis() < end) { @@ -323,6 +339,8 @@ abstract class BaseTest extends ActivityInstrumentationTestCase2 { return false; } + // TODO: With Robotium 4.2, we should use Condition and waitForCondition instead. + // Future boolean tests should not implement this interface. protected interface BooleanTest { public boolean test(); } @@ -412,9 +430,9 @@ abstract class BaseTest extends ActivityInstrumentationTestCase2 { */ public boolean waitForEnabledText(String text) { final String testText = text; - boolean rc = waitForTest(new BooleanTest() { + boolean rc = waitForCondition(new Condition() { @Override - public boolean test() { + public boolean isSatisfied() { // Solo.getText() could be used here, except that it sometimes // hits an assertion when the requested text is not found. ArrayList views = mSolo.getCurrentViews(); @@ -486,9 +504,9 @@ abstract class BaseTest extends ActivityInstrumentationTestCase2 { public final void verifyHomePagerHidden() { final View homePagerView = mSolo.getView("home_pager"); - boolean rc = waitForTest(new BooleanTest() { + boolean rc = waitForCondition(new Condition() { @Override - public boolean test() { + public boolean isSatisfied() { return homePagerView.getVisibility() != View.VISIBLE; } }, MAX_WAIT_HOME_PAGER_HIDDEN_MS); @@ -504,7 +522,7 @@ abstract class BaseTest extends ActivityInstrumentationTestCase2 { if (urlBarTitle != null) { // Wait for the title to make sure it has been displayed in case the view // does not update fast enough - waitForTest(new VerifyTextViewText(urlBarTitle, title), MAX_WAIT_MS); + waitForCondition(new VerifyTextViewText(urlBarTitle, title), MAX_WAIT_MS); pageTitle = urlBarTitle.getText().toString(); } mAsserter.is(pageTitle, title, "Page title is correct"); @@ -542,9 +560,9 @@ abstract class BaseTest extends ActivityInstrumentationTestCase2 { public void addTab(String url) { mSolo.clickOnView(mSolo.getView("tabs")); // wait for addTab to appear (this is usually immediate) - boolean success = waitForTest(new BooleanTest() { + boolean success = waitForCondition(new Condition() { @Override - public boolean test() { + public boolean isSatisfied() { View addTabView = mSolo.getView("add_tab"); if (addTabView == null) { return false;