Bug 1105011 - Update testDistribution to check for tiles locale. r=rnewman, a=sledru
authorBrian Nicholson <thebnich@gmail.com>
Tue, 02 Dec 2014 15:33:21 -0800
changeset 235485 350a7111af8ec860c5f3e290874a4db33cbabee7
parent 235484 c363abb621a30f671b17cc849bc435b77796990b
child 235486 e4c46cff7af8e12d435886eb771c3d526b9c55f9
push id611
push userraliiev@mozilla.com
push dateMon, 05 Jan 2015 23:23:16 +0000
treeherdermozilla-release@345cd3b9c445 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersrnewman, sledru
bugs1105011
milestone35.0
Bug 1105011 - Update testDistribution to check for tiles locale. r=rnewman, a=sledru
mobile/android/base/tests/BaseTest.java
mobile/android/base/tests/StringHelper.java
mobile/android/base/tests/testDistribution.java
--- a/mobile/android/base/tests/BaseTest.java
+++ b/mobile/android/base/tests/BaseTest.java
@@ -560,16 +560,38 @@ abstract class BaseTest extends BaseRobo
                 // We set the left compound drawable (index 0) to the pin icon.
                 final TextView gridItemTextView = mSolo.getText(gridItemTitle);
                 return isPinned == (gridItemTextView.getCompoundDrawables()[0] != null);
             }
         }, MAX_WAIT_MS);
         mAsserter.ok(success, "Top site item was pinned: " + isPinned, null);
     }
 
+    public void pinTopSite(String gridItemTitle) {
+        verifyPinned(false, gridItemTitle);
+        mSolo.clickLongOnText(gridItemTitle);
+        boolean dialogOpened = mSolo.waitForDialogToOpen();
+        mAsserter.ok(dialogOpened, "Pin site dialog opened: " + gridItemTitle, null);
+        boolean pinSiteFound = waitForText(StringHelper.CONTEXT_MENU_PIN_SITE);
+        mAsserter.ok(pinSiteFound, "Found pin site menu item", null);
+        mSolo.clickOnText(StringHelper.CONTEXT_MENU_PIN_SITE);
+        verifyPinned(true, gridItemTitle);
+    }
+
+    public void unpinTopSite(String gridItemTitle) {
+        verifyPinned(true, gridItemTitle);
+        mSolo.clickLongOnText(gridItemTitle);
+        boolean dialogOpened = mSolo.waitForDialogToOpen();
+        mAsserter.ok(dialogOpened, "Pin site dialog opened: " + gridItemTitle, null);
+        boolean unpinSiteFound = waitForText(StringHelper.CONTEXT_MENU_UNPIN_SITE);
+        mAsserter.ok(unpinSiteFound, "Found unpin site menu item", null);
+        mSolo.clickOnText(StringHelper.CONTEXT_MENU_UNPIN_SITE);
+        verifyPinned(false, gridItemTitle);
+    }
+
     // Used to perform clicks on pop-up buttons without having to close the virtual keyboard
     public void clickOnButton(String label) {
         final Button button = mSolo.getButton(label);
         try {
             runTestOnUiThread(new Runnable() {
                 @Override
                 public void run() {
                     button.performClick();
--- a/mobile/android/base/tests/StringHelper.java
+++ b/mobile/android/base/tests/StringHelper.java
@@ -46,16 +46,17 @@ public class StringHelper {
     public static final String CONTEXT_MENU_SHARE_LINK = "Share Link";
     public static final String CONTEXT_MENU_EDIT = "Edit";
     public static final String CONTEXT_MENU_SHARE = "Share";
     public static final String CONTEXT_MENU_REMOVE = "Remove";
     public static final String CONTEXT_MENU_COPY_ADDRESS = "Copy Address";
     public static final String CONTEXT_MENU_EDIT_SITE_SETTINGS = "Edit Site Settings";
     public static final String CONTEXT_MENU_ADD_TO_HOME_SCREEN = "Add to Home Screen";
     public static final String CONTEXT_MENU_PIN_SITE = "Pin Site";
+    public static final String CONTEXT_MENU_UNPIN_SITE = "Unpin Site";
 
     // Context Menu menu items
     public static final String[] CONTEXT_MENU_ITEMS_IN_PRIVATE_TAB = new String[] {
         CONTEXT_MENU_OPEN_LINK_IN_PRIVATE_TAB,
         CONTEXT_MENU_COPY_LINK,
         CONTEXT_MENU_SHARE_LINK,
         CONTEXT_MENU_BOOKMARK_LINK
     };
--- a/mobile/android/base/tests/testDistribution.java
+++ b/mobile/android/base/tests/testDistribution.java
@@ -2,24 +2,27 @@ package org.mozilla.gecko.tests;
 
 import java.io.File;
 import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
 import java.net.HttpURLConnection;
 import java.net.URI;
+import java.util.Locale;
 import java.util.jar.JarInputStream;
 
 import org.json.JSONArray;
 import org.json.JSONException;
 import org.json.JSONObject;
 
 import org.mozilla.gecko.Actions;
 import org.mozilla.gecko.AppConstants;
+import org.mozilla.gecko.BrowserLocaleManager;
+import org.mozilla.gecko.GeckoSharedPrefs;
 import org.mozilla.gecko.db.BrowserContract;
 import org.mozilla.gecko.db.BrowserDB;
 import org.mozilla.gecko.db.SuggestedSites;
 import org.mozilla.gecko.distribution.Distribution;
 import org.mozilla.gecko.distribution.ReferrerDescriptor;
 import org.mozilla.gecko.distribution.ReferrerReceiver;
 import org.mozilla.gecko.util.ThreadUtils;
 
@@ -124,17 +127,24 @@ public class testDistribution extends Co
         // finish. This reduces the chance of us racing with startup pref writes.
         waitForBackgroundHappiness();
 
         // Pre-clear distribution pref, override suggested sites and run tiles tests.
         clearDistributionPref();
         Distribution dist = initDistribution(mockPackagePath);
         SuggestedSites suggestedSites = new SuggestedSites(mActivity, dist);
         BrowserDB.setSuggestedSites(suggestedSites);
-        checkTilesReporting();
+
+        // Test tiles uploading for an en-US OS locale with no app locale.
+        setOSLocale(Locale.US);
+        checkTilesReporting("en-US");
+
+        // Test tiles uploading for an es-MX OS locale with no app locale.
+        setOSLocale(new Locale("es", "MX"));
+        checkTilesReporting("es-MX");
 
         // Pre-clear distribution pref, run basic preferences and en-US localized preferences Tests
         clearDistributionPref();
         setTestLocale("en-US");
         initDistribution(mockPackagePath);
         checkPreferences();
         checkLocalizedPreferences("en-US");
         checkSearchPlugin();
@@ -149,16 +159,21 @@ public class testDistribution extends Co
         setTestLocale("en-US");
         clearDistributionPref();
         doTestValidReferrerIntent();
 
         clearDistributionPref();
         doTestInvalidReferrerIntent();
     }
 
+    private void setOSLocale(Locale locale) {
+        Locale.setDefault(locale);
+        BrowserLocaleManager.storeAndNotifyOSLocale(GeckoSharedPrefs.forProfile(mActivity), locale);
+    }
+
     private void doReferrerTest(String ref, final TestableDistribution distribution, final Runnable distributionReady) throws InterruptedException {
         final Intent intent = new Intent(ACTION_INSTALL_REFERRER);
         intent.setClassName(AppConstants.ANDROID_PACKAGE_NAME, CLASS_REFERRER_RECEIVER);
         intent.putExtra("referrer", ref);
 
         final BroadcastReceiver receiver = new BroadcastReceiver() {
             @Override
             public void onReceive(Context context, Intent intent) {
@@ -325,47 +340,19 @@ public class testDistribution extends Co
                 }
             }
             mAsserter.ok(foundEngine, "check search plugin", "found test search plugin");
         } catch (JSONException e) {
             mAsserter.ok(false, "exception getting search plugins", e.toString());
         }
     }
 
-    // Sets the distribution locale preference for the test
-    private void setTestLocale(String aLocale) {
-        String prefUseragentLocale = "general.useragent.locale";
-
-        JSONObject jsonPref = new JSONObject();
-        try {
-            // Request the pref change to the locale.
-            jsonPref.put("name", prefUseragentLocale);
-            jsonPref.put("type", "string");
-            jsonPref.put("value", aLocale);
-            mActions.sendGeckoEvent("Preferences:Set", jsonPref.toString());
-
-            // Wait for confirmation of the pref change.
-            final String[] prefNames = { prefUseragentLocale };
-
-            Actions.RepeatedEventExpecter eventExpecter = mActions.expectGeckoEvent("Preferences:Data");
-            mActions.sendPreferencesGetEvent(PREF_REQUEST_ID, prefNames);
-
-            JSONObject data = null;
-            int requestId = -1;
-
-            // Wait until we get the correct "Preferences:Data" event
-            while (requestId != PREF_REQUEST_ID) {
-                data = new JSONObject(eventExpecter.blockForEventData());
-                requestId = data.getInt("requestId");
-            }
-            eventExpecter.unregisterListener();
-
-        } catch (Exception e) {
-            mAsserter.ok(false, "exception setting test locale", e.toString());
-        }
+    // Sets the distribution locale preference for the test.
+    private void setTestLocale(String locale) {
+        BrowserLocaleManager.getInstance().setSelectedLocale(mActivity, locale);
     }
 
     // Test localized distribution and preferences values stored in preferences.json
     private void checkLocalizedPreferences(String aLocale) {
         String prefAbout = "distribution.about";
         String prefLocalizeable = "distribution.test.localizeable";
         String prefLocalizeableOverride = "distribution.test.localizeable-override";
 
@@ -449,42 +436,41 @@ public class testDistribution extends Co
     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();
         TestableDistribution.clearReferrerDescriptorForTesting();
     }
 
-    public void checkTilesReporting() throws JSONException {
+    public void checkTilesReporting(String localeCode) throws JSONException {
         // Slight hack: Force top sites grid to reload.
         inputAndLoadUrl(StringHelper.ABOUT_BLANK_URL);
         inputAndLoadUrl(StringHelper.ABOUT_HOME_URL);
 
         // Click the first tracking tile and verify the posted data.
         JSONObject response = clickTrackingTile(StringHelper.DISTRIBUTION1_LABEL);
-        mAsserter.is(0, response.getInt("click"), "JSON click index matched");
-        mAsserter.is("[{\"id\":123},{\"id\":456},{},{},{},{}]", response.getString("tiles"), "JSON tiles data matched");
+        mAsserter.is(response.getInt("click"), 0, "JSON click index matched");
+        mAsserter.is(response.getString("locale"), localeCode, "JSON locale code matched");
+        mAsserter.is(response.getString("tiles"), "[{\"id\":123},{\"id\":456},{},{},{},{}]", "JSON tiles data matched");
 
         inputAndLoadUrl(StringHelper.ABOUT_HOME_URL);
 
         // Pin the second tracking tile.
-        verifyPinned(false, StringHelper.DISTRIBUTION2_LABEL);
-        mSolo.clickLongOnText(StringHelper.DISTRIBUTION2_LABEL);
-        boolean dialogOpened = mSolo.waitForDialogToOpen();
-        mAsserter.ok(dialogOpened, "Pin site dialog opened", null);
-        boolean pinSiteFound = waitForText(StringHelper.CONTEXT_MENU_PIN_SITE);
-        mAsserter.ok(pinSiteFound, "Found pin site menu item", null);
-        mSolo.clickOnText(StringHelper.CONTEXT_MENU_PIN_SITE);
-        verifyPinned(true, StringHelper.DISTRIBUTION2_LABEL);
+        pinTopSite(StringHelper.DISTRIBUTION2_LABEL);
 
         // Click the second tracking tile and verify the posted data.
         response = clickTrackingTile(StringHelper.DISTRIBUTION2_LABEL);
-        mAsserter.is(1, response.getInt("click"), "JSON click index matched");
-        mAsserter.is("[{\"id\":123},{\"id\":456,\"pin\":true},{},{},{},{}]", response.getString("tiles"), "JSON tiles data matched");
+        mAsserter.is(response.getInt("click"), 1, "JSON click index matched");
+        mAsserter.is(response.getString("tiles"), "[{\"id\":123},{\"id\":456,\"pin\":true},{},{},{},{}]", "JSON tiles data matched");
+
+        inputAndLoadUrl(StringHelper.ABOUT_HOME_URL);
+
+        // Unpin the second tracking tile.
+        unpinTopSite(StringHelper.DISTRIBUTION2_LABEL);
     }
 
     private JSONObject clickTrackingTile(String text) throws JSONException {
         boolean tileFound = waitForText(text);
         mAsserter.ok(tileFound, "Found tile: " + text, null);
 
         Actions.EventExpecter loadExpecter = mActions.expectGeckoEvent("Robocop:TilesResponse");
         mSolo.clickOnText(text);