Bug 1307534 - Test that homepanel migration keep hidden panels hidden r?sebastian draft
authorAndrzej Hunt <ahunt@mozilla.com>
Wed, 05 Oct 2016 21:41:38 +0200
changeset 421319 2364da29855dfb79a017ca8fcb24de4fde767c6d
parent 421318 6134abf8cabc3f6c5340b3935269f0dfba85c004
child 533037 b3c030e47f430610464882b929078cf4fd185f27
push id31456
push userahunt@mozilla.com
push dateWed, 05 Oct 2016 19:44:05 +0000
reviewerssebastian
bugs1307534
milestone52.0a1
Bug 1307534 - Test that homepanel migration keep hidden panels hidden r?sebastian MozReview-Commit-ID: 3s4aM2g4XTi
mobile/android/tests/background/junit4/src/org/mozilla/gecko/home/TestHomeConfigPrefsBackendMigration.java
--- a/mobile/android/tests/background/junit4/src/org/mozilla/gecko/home/TestHomeConfigPrefsBackendMigration.java
+++ b/mobile/android/tests/background/junit4/src/org/mozilla/gecko/home/TestHomeConfigPrefsBackendMigration.java
@@ -76,17 +76,34 @@ public class TestHomeConfigPrefsBackendM
                 },
                 new PanelType[] {
                         // Last version: no migration exists yet, we only need to define a list
                         // of expected panels.
                 }
         ));
     }
 
-    private JSONArray createConfigsForList(Context context, PanelType[] panels, int defaultIndex) throws JSONException {
+    private JSONArray createDisabledConfigsForList(Context context,
+                                                   PanelType[] panels) throws JSONException {
+        final JSONArray jsonPanels = new JSONArray();
+
+        for (int i = 0; i < panels.length; i++) {
+            final PanelType panel = panels[i];
+
+            jsonPanels.put(HomeConfig.createBuiltinPanelConfig(context, panel,
+                    EnumSet.of(PanelConfig.Flags.DISABLED_PANEL)).toJSON());
+        }
+
+        return jsonPanels;
+
+    }
+
+
+    private JSONArray createConfigsForList(Context context, PanelType[] panels,
+                                           int defaultIndex) throws JSONException {
         if (defaultIndex < 0 || defaultIndex >= panels.length) {
             throw new IllegalArgumentException("defaultIndex must point to panel in the array");
         }
 
         final JSONArray jsonPanels = new JSONArray();
 
         for (int i = 0; i < panels.length; i++) {
             final PanelType panel = panels[i];
@@ -115,17 +132,27 @@ public class TestHomeConfigPrefsBackendM
             if (panelConfig.isDefault()) {
                 return panelConfig.getType();
             }
         }
 
         return null;
     }
 
-    private void checkListContainsExpectedPanels(JSONArray jsonPanels, PanelType[] expected) throws JSONException {
+    private void checkAllPanelsAreDisabled(JSONArray jsonPanels) throws JSONException {
+        for (int i = 0; i < jsonPanels.length(); i++) {
+            final JSONObject jsonPanelConfig = jsonPanels.getJSONObject(i);
+            final PanelConfig config = new PanelConfig(jsonPanelConfig);
+
+            assertTrue("Non disabled panel \"" + config.getType().name() + "\" found in list, excpected all panels to be disabled", config.isDisabled());
+        }
+    }
+
+    private void checkListContainsExpectedPanels(JSONArray jsonPanels,
+                                                 PanelType[] expected) throws JSONException {
         // Given the short lists we have here an ArraySet might be more appropriate, but it requires API >= 23.
         final Set<PanelType> expectedSet = new HashSet<>();
         for (PanelType panelType : expected) {
             expectedSet.add(panelType);
         }
 
         for (int i = 0; i < jsonPanels.length(); i++) {
             final JSONObject jsonPanelConfig = jsonPanels.getJSONObject(i);
@@ -147,17 +174,17 @@ public class TestHomeConfigPrefsBackendM
 
         final Pair<PanelType[], PanelType[]> finalConstellation = migrationConstellations.get(HomeConfigPrefsBackend.VERSION);
         assertNotNull("It looks like you added a HomeConfig migration, please add an appropriate entry to migrationConstellations",
                 finalConstellation);
 
         // We want to calculate the number of iterations here to make sure we cover all provided constellations.
         // Iterating over the array and manually checking for each version could result in constellations
         // being skipped if there are any gaps in the array
-        final  int firstTestedVersion = HomeConfigPrefsBackend.VERSION - (migrationConstellations.size() - 1);
+        final int firstTestedVersion = HomeConfigPrefsBackend.VERSION - (migrationConstellations.size() - 1);
 
         // The last constellation is only used for the counts / expected outputs, hence we start
         // with the second-last constellation
         for (int testVersion = HomeConfigPrefsBackend.VERSION - 1; testVersion >= firstTestedVersion; testVersion--) {
 
             final Pair<PanelType[], PanelType[]> currentConstellation = migrationConstellations.get(testVersion);
             assertNotNull("No constellation for version " + testVersion + " - you must provide a constellation for every version upgrade in the list",
                     currentConstellation);
@@ -191,9 +218,47 @@ public class TestHomeConfigPrefsBackendM
 
                 assertEquals("Number of panels after migration doesn't match expected count",
                         jsonPanels.length(), expectedOutputList.length);
 
                 checkListContainsExpectedPanels(jsonPanels, expectedOutputList);
             }
         }
     }
+
+    // Test that if all panels are disabled, the migration retains all panels as being disabled
+    // (in addition to correctly removing panels as necessary).
+    @Test
+    public void testMigrationRetainsAllPanelsHiddenAfter6() throws JSONException {
+        final Context context = RuntimeEnvironment.application;
+
+        final Pair<PanelType[], PanelType[]> finalConstellation = migrationConstellations.get(HomeConfigPrefsBackend.VERSION);
+        assertNotNull("It looks like you added a HomeConfig migration, please add an appropriate entry to migrationConstellations",
+                finalConstellation);
+
+        final int firstTestedVersion = HomeConfigPrefsBackend.VERSION - (migrationConstellations.size() - 1);
+
+        for (int testVersion = HomeConfigPrefsBackend.VERSION - 1; testVersion >= firstTestedVersion; testVersion--) {
+            final Pair<PanelType[], PanelType[]> currentConstellation = migrationConstellations.get(testVersion);
+            assertNotNull("No constellation for version " + testVersion + " - you must provide a constellation for every version upgrade in the list",
+                    currentConstellation);
+
+            final PanelType[] inputList = currentConstellation.first;
+
+            JSONArray jsonPanels = createDisabledConfigsForList(context, inputList);
+
+            jsonPanels = HomeConfigPrefsBackend.migratePrefsFromVersionToVersion(context, testVersion, testVersion + 1, jsonPanels, null);
+
+            // All panels should remain disabled after the migration
+            checkAllPanelsAreDisabled(jsonPanels);
+
+            // Duplicated from previous test:
+            // Verify that the panels remaining after the migration correspond to the input panels
+            // for the next migration
+            final PanelType[] expectedOutputList = migrationConstellations.get(testVersion + 1).first;
+
+            assertEquals("Number of panels after migration doesn't match expected count",
+                    jsonPanels.length(), expectedOutputList.length);
+
+            checkListContainsExpectedPanels(jsonPanels, expectedOutputList);
+        }
+    }
 }