Bug 1261713 - (Part 2) Refactor isInExperiment and add unit test. r=sebastian
authorMargaret Leibovic <margaret.leibovic@gmail.com>
Sun, 03 Apr 2016 18:08:15 -0400
changeset 291792 ae1f2e223f22a0961192e4acf85280fdb445621e
parent 291791 ac533e9899b77da4dcb3683440a789cdf06b4fab
child 291793 27bd269e54937b99b0eef25ce849400a29c48fdb
push id74679
push userkwierso@gmail.com
push dateTue, 05 Apr 2016 23:39:26 +0000
treeherdermozilla-inbound@b70ae970d45d [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerssebastian
bugs1261713
milestone48.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 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>();