Bug 1261713 - (Part 2) Refactor isInExperiment and add unit test. r=sebastian draft
authorMargaret Leibovic <margaret.leibovic@gmail.com>
Sun, 03 Apr 2016 18:08:15 -0400
changeset 347227 5413dc0d35c3a7f59780a835eed3236a27e3c8f4
parent 347226 32030b578478639907541689b4f6af97793ffc0a
child 347228 e41b11103c6792071480e5118fa08a8d4c18f703
push id14518
push usermleibovic@mozilla.com
push dateSun, 03 Apr 2016 22:28:38 +0000
reviewerssebastian
bugs1261713
milestone48.0a1
Bug 1261713 - (Part 2) Refactor isInExperiment and add unit test. r=sebastian MozReview-Commit-ID: 7etdTcolf26
mobile/android/tests/background/junit4/src/com/keepsafe/switchboard/TestSwitchboard.java
mobile/android/thirdparty/com/keepsafe/switchboard/Switch.java
mobile/android/thirdparty/com/keepsafe/switchboard/SwitchBoard.java
--- a/mobile/android/tests/background/junit4/src/com/keepsafe/switchboard/TestSwitchboard.java
+++ b/mobile/android/tests/background/junit4/src/com/keepsafe/switchboard/TestSwitchboard.java
@@ -1,25 +1,45 @@
 package com.keepsafe.switchboard;
 
 import android.content.Context;
 
+import org.junit.Before;
 import org.junit.Test;
 import org.junit.runner.RunWith;
 import org.mozilla.gecko.background.testhelpers.TestRunner;
 import org.robolectric.RuntimeEnvironment;
 
+import java.io.IOException;
 import java.util.UUID;
 
 import static org.junit.Assert.*;
 
 @RunWith(TestRunner.class)
 public class TestSwitchboard {
 
+    private static final String TEST_JSON = "{\"active-experiment\":{\"isActive\":true,\"values\":null},\"inactive-experiment\":{\"isActive\":false,\"values\":null}}";
+
+    @Before
+    public void setUp() throws IOException {
+        final Context c = RuntimeEnvironment.application;
+
+        // Avoid hitting the network by setting a config directly.
+        Preferences.setDynamicConfigJson(c, TEST_JSON);
+    }
+
     @Test
     public void testDeviceUuidFactory() {
         final Context c = RuntimeEnvironment.application;
         final DeviceUuidFactory df = new DeviceUuidFactory(c);
         final UUID uuid = df.getDeviceUuid();
         assertNotNull("UUID is not null", uuid);
         assertEquals("DeviceUuidFactory always returns the same UUID", df.getDeviceUuid(), uuid);
     }
+
+    @Test
+    public void testIsInExperiment() {
+        final Context c = RuntimeEnvironment.application;
+        assertTrue("Active experiment is active", SwitchBoard.isInExperiment(c, "active-experiment"));
+        assertFalse("Inactive experiment is inactive", SwitchBoard.isInExperiment(c, "inactive-experiment"));
+    }
+
 }
--- a/mobile/android/thirdparty/com/keepsafe/switchboard/Switch.java
+++ b/mobile/android/thirdparty/com/keepsafe/switchboard/Switch.java
@@ -46,26 +46,16 @@ public class Switch {
      * Returns true if the experiment is active for this particular user.
      * @return Status of the experiment and false when experiment does not exist.
      */
     public boolean isActive() {
         return SwitchBoard.isInExperiment(context, experimentName);
     }
 
     /**
-     * Returns the status of the experiment or the given default value when experiment
-     * does not exist.
-     * @param defaultValue Value to return when experiment does not exist.
-     * @return Experiment status
-     */
-    public boolean isActive(boolean defaultValue) {
-        return SwitchBoard.isInExperiment(context, experimentName, defaultValue);
-    }
-
-    /**
      * Returns true if the experiment has aditional values.
      * @return true when values exist
      */
     public boolean hasValues() {
         return SwitchBoard.hasExperimentValues(context, experimentName);
     }
 
     /**
--- a/mobile/android/thirdparty/com/keepsafe/switchboard/SwitchBoard.java
+++ b/mobile/android/thirdparty/com/keepsafe/switchboard/SwitchBoard.java
@@ -173,55 +173,31 @@ public class SwitchBoard {
     /**
      * Looks up in config if user is in certain experiment. Returns false as a default value when experiment
      * does not exist.
      * Experiment names are defined server side as Key in array for return values.
      * @param experimentName Name of the experiment to lookup
      * @return returns value for experiment or false if experiment does not exist.
      */
     public static boolean isInExperiment(Context c, String experimentName) {
-        return isInExperiment(c, experimentName, false);
-    }
-
-    /**
-     * Looks up in config if user is in certain experiment.
-     * Experiment names are defined server side as Key in array for return values.
-     * @param experimentName Name of the experiment to lookup
-     * @param defaultReturnVal The return value that should be return when experiment does not exist
-     * @return returns value for experiment or defaultReturnVal if experiment does not exist.
-     */
-    public static boolean isInExperiment(Context c, String experimentName, boolean defaultReturnVal) {
-        //lookup experiment in config
-        String config = Preferences.getDynamicConfigJson(c);
-
-        //if it does not exist
-        if(config == null)
-            return false;
-        else {
+        final String config = Preferences.getDynamicConfigJson(c);
 
-            try {
-                JSONObject experiment = (JSONObject) new JSONObject(config).get(experimentName);
-                if(DEBUG) Log.d(TAG, "experiment " + experimentName + " JSON object: " + experiment.toString());
-                if(experiment == null)
-                    return defaultReturnVal;
-
-                boolean returnValue = defaultReturnVal;
-                returnValue = experiment.getBoolean(IS_EXPERIMENT_ACTIVE);
-
-                return returnValue;
-            } catch (JSONException e) {
-                Log.e(TAG, "Config: " + config);
-                e.printStackTrace();
-
-            }
-
-            //return false when JSON fails
-            return defaultReturnVal;
+        if (config == null) {
+            return false;
         }
 
+        try {
+            final JSONObject experiment = new JSONObject(config).getJSONObject(experimentName);
+            if(DEBUG) Log.d(TAG, "experiment " + experimentName + " JSON object: " + experiment.toString());
+
+            return experiment != null && experiment.getBoolean(IS_EXPERIMENT_ACTIVE);
+        } catch (JSONException e) {
+            Log.e(TAG, "Error getting experiment from config", e);
+            return false;
+        }
     }
 
     /**
      * @returns a list of all active experiments.
      */
     public static List<String> getActiveExperiments(Context c) {
         ArrayList<String> returnList = new ArrayList<String>();