From b50b3795da205de51184cdbbcf7322c04a84b126 Mon Sep 17 00:00:00 2001 From: Michael Comella Date: Tue, 31 May 2016 16:48:03 -0700 Subject: [PATCH] Bug 1270191 - Add IntentUtils.getEnvVarMap. r=grisha MozReview-Commit-ID: 7ojczu4rOnD --HG-- extra : rebase_source : f33965372fdfdb801d5f9b999c91dd591f6c856c --- .../org/mozilla/gecko/util/IntentUtils.java | 59 +++++++++++++++ mobile/android/base/moz.build | 1 + .../mozilla/gecko/util/TestIntentUtils.java | 72 +++++++++++++++++++ 3 files changed, 132 insertions(+) create mode 100644 mobile/android/base/java/org/mozilla/gecko/util/IntentUtils.java create mode 100644 mobile/android/tests/background/junit4/src/org/mozilla/gecko/util/TestIntentUtils.java diff --git a/mobile/android/base/java/org/mozilla/gecko/util/IntentUtils.java b/mobile/android/base/java/org/mozilla/gecko/util/IntentUtils.java new file mode 100644 index 000000000000..c802a613e467 --- /dev/null +++ b/mobile/android/base/java/org/mozilla/gecko/util/IntentUtils.java @@ -0,0 +1,59 @@ +/* + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, you can obtain one at http://mozilla.org/MPL/2.0/. + */ + +package org.mozilla.gecko.util; + +import android.content.Intent; +import android.support.annotation.NonNull; + +import java.util.HashMap; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +/** + * Utilities for Intents. + */ +public class IntentUtils { + private static final String ENV_VAR_REGEX = "(.+)=(.*)"; + + private IntentUtils() {} + + /** + * Returns a list of environment variables and their values. These are parsed from an Intent extra + * with the key -> value format: + * env# -> ENV_VAR=VALUE + * + * # in env# is expected to be increasing from 0. + * + * @return A Map of environment variable name to value, e.g. ENV_VAR -> VALUE + */ + public static HashMap getEnvVarMap(@NonNull final Intent intent) { + final HashMap out = new HashMap<>(); + final Pattern envVarPattern = Pattern.compile(ENV_VAR_REGEX); + Matcher matcher = null; + + String envValue = intent.getStringExtra("env0"); + int i = 1; + while (envValue != null) { + // Optimization to re-use matcher rather than creating new + // objects because this is used in the startup path. + if (matcher == null) { + matcher = envVarPattern.matcher(envValue); + } else { + matcher.reset(envValue); + } + + if (matcher.matches()) { + final String envVarName = matcher.group(1); + final String envVarValue = matcher.group(2); + out.put(envVarName, envVarValue); + } + envValue = intent.getStringExtra("env" + i); + i += 1; + } + return out; + } +} diff --git a/mobile/android/base/moz.build b/mobile/android/base/moz.build index 5f499f9ffbc6..1c5c8286b846 100644 --- a/mobile/android/base/moz.build +++ b/mobile/android/base/moz.build @@ -116,6 +116,7 @@ gujar.sources += ['java/org/mozilla/gecko/' + x for x in [ 'util/INIParser.java', 'util/INISection.java', 'util/InputOptionsUtils.java', + 'util/IntentUtils.java', 'util/IOUtils.java', 'util/JSONUtils.java', 'util/MenuUtils.java', diff --git a/mobile/android/tests/background/junit4/src/org/mozilla/gecko/util/TestIntentUtils.java b/mobile/android/tests/background/junit4/src/org/mozilla/gecko/util/TestIntentUtils.java new file mode 100644 index 000000000000..28698283f229 --- /dev/null +++ b/mobile/android/tests/background/junit4/src/org/mozilla/gecko/util/TestIntentUtils.java @@ -0,0 +1,72 @@ +/* + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, you can obtain one at http://mozilla.org/MPL/2.0/. + */ + +package org.mozilla.gecko.util; + +import android.content.Intent; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mozilla.gecko.background.testhelpers.TestRunner; + +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; + +import static org.junit.Assert.*; + +/** + * Tests for the Intent utilities. + */ +@RunWith(TestRunner.class) +public class TestIntentUtils { + + private static final Map TEST_ENV_VAR_MAP; + static { + final HashMap tempMap = new HashMap<>(); + tempMap.put("ZERO", "0"); + tempMap.put("ONE", "1"); + tempMap.put("STRING", "TEXT"); + tempMap.put("L_WHITESPACE", " LEFT"); + tempMap.put("R_WHITESPACE", "RIGHT "); + tempMap.put("ALL_WHITESPACE", " ALL "); + tempMap.put("WHITESPACE_IN_VALUE", "IN THE MIDDLE"); + tempMap.put("WHITESPACE IN KEY", "IS_PROBABLY_NOT_VALID_ANYWAY"); + tempMap.put("BLANK_VAL", ""); + TEST_ENV_VAR_MAP = Collections.unmodifiableMap(tempMap); + } + + private Intent testIntent; + + @Before + public void setUp() throws Exception { + testIntent = getIntentWithTestData(); + } + + private static Intent getIntentWithTestData() { + final Intent out = new Intent(Intent.ACTION_VIEW); + int i = 0; + for (final String key : TEST_ENV_VAR_MAP.keySet()) { + final String value = key + "=" + TEST_ENV_VAR_MAP.get(key); + out.putExtra("env" + i, value); + i += 1; + } + return out; + } + + @Test + public void testGetEnvVarMap() throws Exception { + final HashMap actual = IntentUtils.getEnvVarMap(testIntent); + for (final String actualEnvVarName : actual.keySet()) { + assertTrue("Actual key exists in test data: " + actualEnvVarName, + TEST_ENV_VAR_MAP.containsKey(actualEnvVarName)); + + final String expectedValue = TEST_ENV_VAR_MAP.get(actualEnvVarName); + final String actualValue = actual.get(actualEnvVarName); + assertEquals("Actual env var value matches test data", expectedValue, actualValue); + } + } +} \ No newline at end of file