Bug 1270191 - Add IntentUtils.getEnvVarMap. r=grisha

MozReview-Commit-ID: 7ojczu4rOnD

--HG--
extra : rebase_source : f33965372fdfdb801d5f9b999c91dd591f6c856c
This commit is contained in:
Michael Comella 2016-05-31 16:48:03 -07:00
Родитель 9939c69b11
Коммит b50b3795da
3 изменённых файлов: 132 добавлений и 0 удалений

Просмотреть файл

@ -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<String, String> getEnvVarMap(@NonNull final Intent intent) {
final HashMap<String, String> 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;
}
}

Просмотреть файл

@ -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',

Просмотреть файл

@ -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<String, String> TEST_ENV_VAR_MAP;
static {
final HashMap<String, String> 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<String, String> 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);
}
}
}