Bug 922694 - Part 6: follow-up to fix test. r=nalexander
authorRichard Newman <rnewman@mozilla.com>
Wed, 16 Oct 2013 18:56:27 -0700
changeset 164872 05ebffcae22e45e3b683d8e463af60f4b9ee1325
parent 164871 6d403b11bd6270c4fde317cc88d3919850568a4e
child 164873 e9e0dfbe7a898ae1aff31ea65a8bb326232c9912
push id3066
push userakeybl@mozilla.com
push dateMon, 09 Dec 2013 19:58:46 +0000
treeherdermozilla-beta@a31a0dce83aa [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersnalexander
bugs922694
milestone27.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 922694 - Part 6: follow-up to fix test. r=nalexander
mobile/android/base/tests/testDistribution.java.in
--- a/mobile/android/base/tests/testDistribution.java.in
+++ b/mobile/android/base/tests/testDistribution.java.in
@@ -33,21 +33,60 @@ public class testDistribution extends Co
 
     private Activity mActivity;
 
     @Override
     protected int getTestType() {
         return TEST_MOCHITEST;
     }
 
+    /**
+     * This is a hack.
+     *
+     * Startup results in us writing prefs -- we fetch the Distribution, which
+     * caches its state. Our tests try to wipe those prefs, but apparently
+     * sometimes race with startup, which leads to us not getting one of our
+     * expected messages. The test fails.
+     *
+     * This hack waits for any existing background tasks -- such as the one that
+     * writes prefs -- to finish before we begin the test.
+     */
+    private void waitForBackgroundHappiness() {
+        try {
+            ClassLoader classLoader = mActivity.getClassLoader();
+            Class threadUtilsClass = classLoader.loadClass("org.mozilla.gecko.util.ThreadUtils");
+            Method postToBackgroundThread = threadUtilsClass.getMethod("postToBackgroundThread", Runnable.class);
+            final Object signal = new Object();
+            final Runnable done = new Runnable() {
+                @Override
+                public void run() {
+                    synchronized (signal) {
+                        signal.notify();
+                    }
+                }
+            };
+            synchronized (signal) {
+                postToBackgroundThread.invoke(null, done);
+                signal.wait();
+            }
+        } catch (Exception e) {
+            mAsserter.ok(false, "Exception waiting on background thread.", e.toString());
+        }
+        mAsserter.dumpLog("Background task completed. Proceeding.");
+    }
+
     public void testDistribution() {
         mActivity = getActivity();
 
         String mockPackagePath = getMockPackagePath();
 
+        // Wait for any startup-related background distribution shenanigans to
+        // finish. This reduces the chance of us racing with startup pref writes.
+        waitForBackgroundHappiness();
+
         // Pre-clear distribution pref, run basic preferences and en-US localized preferences Tests
         clearDistributionPref();
         setTestLocale("en-US");
         initDistribution(mockPackagePath);
         checkPreferences();
         checkLocalizedPreferences("en-US");
         checkSearchPlugin();
 
@@ -263,16 +302,17 @@ public class testDistribution extends Co
             mAsserter.ok(false, "exception copying mock distribution package to data directory", e.toString());
         }
 
         return mockPackagePath;
     }
 
     // Clears the distribution pref to return distribution state to STATE_UNKNOWN
     private void clearDistributionPref() {
+        mAsserter.dumpLog("Clearing distribution pref.");
         SharedPreferences settings = mActivity.getSharedPreferences("GeckoApp", Activity.MODE_PRIVATE);
         String keyName = mActivity.getPackageName() + ".distribution_state";
         settings.edit().remove(keyName).commit();
     }
 
     @Override
     public void setUp() throws Exception {
         // TODO: Set up the content provider after setting the distribution.