Bug 1335355 - Switchboard: Ignore empty matchers in experiments configuration. r=ahunt
authorSebastian Kaspari <s.kaspari@gmail.com>
Tue, 31 Jan 2017 14:26:01 +0100
changeset 331977 5e899d7711f6194866ae2e1aa18ab9b0de785d92
parent 331976 bfd5466351b0aacb010d203e57a5b97b82d9bccb
child 331978 8c752414c834c3bdad86489d5784f5f95e0a1fca
push id31293
push userkwierso@gmail.com
push dateThu, 02 Feb 2017 00:07:12 +0000
treeherdermozilla-central@8196774c6b8a [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersahunt
bugs1335355
milestone54.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 1335355 - Switchboard: Ignore empty matchers in experiments configuration. r=ahunt MozReview-Commit-ID: 10T90TjJlGY
mobile/android/base/java/org/mozilla/gecko/switchboard/SwitchBoard.java
--- a/mobile/android/base/java/org/mozilla/gecko/switchboard/SwitchBoard.java
+++ b/mobile/android/base/java/org/mozilla/gecko/switchboard/SwitchBoard.java
@@ -32,16 +32,17 @@ import org.json.JSONException;
 import org.json.JSONObject;
 import org.json.JSONArray;
 
 import android.content.Context;
 import android.content.pm.PackageManager.NameNotFoundException;
 import android.os.Build;
 import android.support.annotation.NonNull;
 import android.support.annotation.Nullable;
+import android.text.TextUtils;
 import android.util.Log;
 
 
 /**
  * SwitchBoard is the core class of the KeepSafe Switchboard mobile A/B testing framework.
  * This class provides a bunch of static methods that can be used in your app to run A/B tests.
  *
  * The SwitchBoard supports production and staging environment.
@@ -194,80 +195,102 @@ public class SwitchBoard {
             if (entry.getString(KEY_NAME).equals(experimentName)) {
                 experiment = entry;
                 break;
             }
         }
         return experiment;
     }
 
-    private static boolean isMatch(Context c, @Nullable JSONObject matchKeys) {
+    /**
+     * Return false if the match object contains any non-matching patterns. Otherwise returns true.
+     */
+    private static boolean isMatch(Context context, @Nullable JSONObject matchKeys) {
         // If no match keys are specified, default to enabling the experiment.
         if (matchKeys == null) {
             return true;
         }
 
         if (matchKeys.has(KEY_APP_ID)) {
-            final String packageName = c.getPackageName();
             try {
-                if (!packageName.matches(matchKeys.getString(KEY_APP_ID))) {
+                final String packageName = context.getPackageName();
+                final String expectedAppIdPattern = matchKeys.getString(KEY_APP_ID);
+
+                if (!TextUtils.isEmpty(expectedAppIdPattern)
+                        && !packageName.matches(expectedAppIdPattern)) {
                     return false;
                 }
             } catch (JSONException e) {
                 Log.e(TAG, "Exception matching appId", e);
             }
         }
 
         if (matchKeys.has(KEY_COUNTRY)) {
             try {
                 final String country = Locale.getDefault().getISO3Country();
-                if (!country.matches(matchKeys.getString(KEY_COUNTRY))) {
+                final String expectedCountryPattern = matchKeys.getString(KEY_COUNTRY);
+
+                if (!TextUtils.isEmpty(expectedCountryPattern)
+                        && !country.matches(expectedCountryPattern)) {
                     return false;
                 }
             } catch (MissingResourceException | JSONException e) {
                 Log.e(TAG, "Exception matching country", e);
             }
         }
 
         if (matchKeys.has(KEY_DEVICE)) {
-            final String device = Build.DEVICE;
             try {
-                if (!device.matches(matchKeys.getString(KEY_DEVICE))) {
+                final String device = Build.DEVICE;
+                final String expectedDevicePattern = matchKeys.getString(KEY_DEVICE);
+
+                if (!TextUtils.isEmpty(expectedDevicePattern)
+                        && !device.matches(expectedDevicePattern)) {
                     return false;
                 }
             } catch (JSONException e) {
                 Log.e(TAG, "Exception matching device", e);
             }
+        }
 
-        }
         if (matchKeys.has(KEY_LANG)) {
             try {
                 final String lang = Locale.getDefault().getISO3Language();
-                if (!lang.matches(matchKeys.getString(KEY_LANG))) {
+                final String expectedLanguagePattern = matchKeys.getString(KEY_LANG);
+
+                if (!TextUtils.isEmpty(expectedLanguagePattern)
+                        && !lang.matches(expectedLanguagePattern)) {
                     return false;
                 }
             } catch (MissingResourceException | JSONException e) {
                 Log.e(TAG, "Exception matching lang", e);
             }
         }
+
         if (matchKeys.has(KEY_MANUFACTURER)) {
-            final String manufacturer = Build.MANUFACTURER;
             try {
-                if (!manufacturer.matches(matchKeys.getString(KEY_MANUFACTURER))) {
+                final String manufacturer = Build.MANUFACTURER;
+                final String expectedManufacturerPattern = matchKeys.getString(KEY_MANUFACTURER);
+
+                if (!TextUtils.isEmpty(expectedManufacturerPattern)
+                        && !manufacturer.matches(expectedManufacturerPattern)) {
                     return false;
                 }
             } catch (JSONException e) {
                 Log.e(TAG, "Exception matching manufacturer", e);
             }
         }
 
         if (matchKeys.has(KEY_VERSION)) {
             try {
-                final String version = c.getPackageManager().getPackageInfo(c.getPackageName(), 0).versionName;
-                if (!version.matches(matchKeys.getString(KEY_VERSION))) {
+                final String version = context.getPackageManager().getPackageInfo(context.getPackageName(), 0).versionName;
+                final String expectedVersionPattern = matchKeys.getString(KEY_VERSION);
+
+                if (!TextUtils.isEmpty(expectedVersionPattern)
+                        && !version.matches(expectedVersionPattern)) {
                     return false;
                 }
             } catch (NameNotFoundException | JSONException e) {
                 Log.e(TAG, "Exception matching version", e);
             }
         }
 
         // Default to return true if no matches failed.