summaryrefslogtreecommitdiffstats
path: root/mobile/android/geckoview/src/test/java
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-28 14:29:10 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-28 14:29:10 +0000
commit2aa4a82499d4becd2284cdb482213d541b8804dd (patch)
treeb80bf8bf13c3766139fbacc530efd0dd9d54394c /mobile/android/geckoview/src/test/java
parentInitial commit. (diff)
downloadfirefox-2aa4a82499d4becd2284cdb482213d541b8804dd.tar.xz
firefox-2aa4a82499d4becd2284cdb482213d541b8804dd.zip
Adding upstream version 86.0.1.upstream/86.0.1upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'mobile/android/geckoview/src/test/java')
-rw-r--r--mobile/android/geckoview/src/test/java/org/mozilla/gecko/util/GeckoBundleTest.java678
-rw-r--r--mobile/android/geckoview/src/test/java/org/mozilla/gecko/util/IntentUtilsTest.java61
-rw-r--r--mobile/android/geckoview/src/test/java/org/mozilla/gecko/util/NetworkUtilsTest.java189
-rw-r--r--mobile/android/geckoview/src/test/java/org/mozilla/gecko/util/TestDateUtil.java86
-rw-r--r--mobile/android/geckoview/src/test/java/org/mozilla/gecko/util/TestFileUtils.java360
-rw-r--r--mobile/android/geckoview/src/test/java/org/mozilla/gecko/util/TestFloatUtils.java51
-rw-r--r--mobile/android/geckoview/src/test/java/org/mozilla/gecko/util/TestIntentUtils.java74
-rw-r--r--mobile/android/geckoview/src/test/java/org/mozilla/gecko/util/TestStringUtils.java174
-rw-r--r--mobile/android/geckoview/src/test/java/org/mozilla/gecko/util/TestUUIDUtil.java48
9 files changed, 1721 insertions, 0 deletions
diff --git a/mobile/android/geckoview/src/test/java/org/mozilla/gecko/util/GeckoBundleTest.java b/mobile/android/geckoview/src/test/java/org/mozilla/gecko/util/GeckoBundleTest.java
new file mode 100644
index 0000000000..e76d9b8311
--- /dev/null
+++ b/mobile/android/geckoview/src/test/java/org/mozilla/gecko/util/GeckoBundleTest.java
@@ -0,0 +1,678 @@
+/* Any copyright is dedicated to the Public Domain.
+ http://creativecommons.org/publicdomain/zero/1.0/ */
+
+package org.mozilla.gecko.util;
+
+import static org.junit.Assert.*;
+
+import android.os.Parcel;
+import android.test.suitebuilder.annotation.SmallTest;
+
+import org.json.JSONException;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.robolectric.RobolectricTestRunner;
+
+import java.util.Arrays;
+import java.util.List;
+
+@RunWith(RobolectricTestRunner.class)
+@SmallTest
+public class GeckoBundleTest {
+ private static final int INNER_BUNDLE_SIZE = 28;
+ private static final int OUTER_BUNDLE_SIZE = INNER_BUNDLE_SIZE + 6;
+
+ private static GeckoBundle createInnerBundle() {
+ final GeckoBundle bundle = new GeckoBundle();
+
+ bundle.putBoolean("boolean", true);
+ bundle.putBooleanArray("booleanArray", new boolean[] {false, true});
+
+ bundle.putInt("int", 1);
+ bundle.putIntArray("intArray", new int[] {2, 3});
+
+ bundle.putDouble("double", 0.5);
+ bundle.putDoubleArray("doubleArray", new double[] {1.5, 2.5});
+
+ bundle.putLong("long", 1L);
+ bundle.putLongArray("longArray", new long[] {2L, 3L});
+
+ bundle.putString("string", "foo");
+ bundle.putString("nullString", null);
+ bundle.putString("emptyString", "");
+ bundle.putStringArray("stringArray", new String[] {"bar", "baz"});
+ bundle.putStringArray("stringArrayOfNull", new String[2]);
+
+ bundle.putBooleanArray("emptyBooleanArray", new boolean[0]);
+ bundle.putIntArray("emptyIntArray", new int[0]);
+ bundle.putDoubleArray("emptyDoubleArray", new double[0]);
+ bundle.putLongArray("emptyLongArray", new long[0]);
+ bundle.putStringArray("emptyStringArray", new String[0]);
+
+ bundle.putBooleanArray("nullBooleanArray", (boolean[]) null);
+ bundle.putIntArray("nullIntArray", (int[]) null);
+ bundle.putDoubleArray("nullDoubleArray", (double[]) null);
+ bundle.putLongArray("nullLongArray", (long[]) null);
+ bundle.putStringArray("nullStringArray", (String[]) null);
+
+ bundle.putDoubleArray("mixedArray", new double[] {1.0, 1.5});
+
+ bundle.putInt("byte", 1);
+ bundle.putInt("short", 1);
+ bundle.putDouble("float", 0.5);
+ bundle.putString("char", "f");
+
+ return bundle;
+ }
+
+ private static GeckoBundle createBundle() {
+ final GeckoBundle outer = createInnerBundle();
+ final GeckoBundle inner = createInnerBundle();
+
+ outer.putBundle("object", inner);
+ outer.putBundle("nullObject", null);
+ outer.putBundleArray("objectArray", new GeckoBundle[] {null, inner});
+ outer.putBundleArray("objectArrayOfNull", new GeckoBundle[2]);
+ outer.putBundleArray("emptyObjectArray", new GeckoBundle[0]);
+ outer.putBundleArray("nullObjectArray", (GeckoBundle[]) null);
+
+ return outer;
+ }
+
+ private static void checkInnerBundle(final GeckoBundle bundle, final int expectedSize) {
+ assertEquals(expectedSize, bundle.size());
+
+ assertEquals(true, bundle.getBoolean("boolean"));
+ assertArrayEquals(new boolean[] {false, true}, bundle.getBooleanArray("booleanArray"));
+
+ assertEquals(1, bundle.getInt("int"));
+ assertArrayEquals(new int[] {2, 3}, bundle.getIntArray("intArray"));
+
+ assertEquals(0.5, bundle.getDouble("double"), 0.0);
+ assertArrayEquals(new double[] {1.5, 2.5}, bundle.getDoubleArray("doubleArray"), 0.0);
+
+ assertEquals(1L, bundle.getLong("long"));
+ assertArrayEquals(new long[] {2L, 3L}, bundle.getLongArray("longArray"));
+
+ assertEquals("foo", bundle.getString("string"));
+ assertEquals(null, bundle.getString("nullString"));
+ assertEquals("", bundle.getString("emptyString"));
+ assertArrayEquals(new String[] {"bar", "baz"}, bundle.getStringArray("stringArray"));
+ assertArrayEquals(new String[2], bundle.getStringArray("stringArrayOfNull"));
+
+ assertArrayEquals(new boolean[0], bundle.getBooleanArray("emptyBooleanArray"));
+ assertArrayEquals(new int[0], bundle.getIntArray("emptyIntArray"));
+ assertArrayEquals(new double[0], bundle.getDoubleArray("emptyDoubleArray"), 0.0);
+ assertArrayEquals(new long[0], bundle.getLongArray("emptyLongArray"));
+ assertArrayEquals(new String[0], bundle.getStringArray("emptyStringArray"));
+
+ assertArrayEquals(null, bundle.getBooleanArray("nullBooleanArray"));
+ assertArrayEquals(null, bundle.getIntArray("nullIntArray"));
+ assertArrayEquals(null, bundle.getDoubleArray("nullDoubleArray"), 0.0);
+ assertArrayEquals(null, bundle.getLongArray("nullLongArray"));
+ assertArrayEquals(null, bundle.getStringArray("nullStringArray"));
+
+ assertArrayEquals(new double[] {1.0, 1.5}, bundle.getDoubleArray("mixedArray"), 0.0);
+
+ assertEquals(1, bundle.getInt("byte"));
+ assertEquals(1, bundle.getInt("short"));
+ assertEquals(0.5, bundle.getDouble("float"), 0.0);
+ assertEquals("f", bundle.getString("char"));
+ }
+
+ private static void checkBundle(final GeckoBundle bundle) {
+ checkInnerBundle(bundle, OUTER_BUNDLE_SIZE);
+
+ checkInnerBundle(bundle.getBundle("object"), INNER_BUNDLE_SIZE);
+ assertEquals(null, bundle.getBundle("nullObject"));
+
+ final GeckoBundle[] array = bundle.getBundleArray("objectArray");
+ assertNotNull(array);
+ assertEquals(2, array.length);
+ assertEquals(null, array[0]);
+ checkInnerBundle(array[1], INNER_BUNDLE_SIZE);
+
+ assertArrayEquals(new GeckoBundle[2], bundle.getBundleArray("objectArrayOfNull"));
+ assertArrayEquals(new GeckoBundle[0], bundle.getBundleArray("emptyObjectArray"));
+ assertArrayEquals(null, bundle.getBundleArray("nullObjectArray"));
+ }
+
+ private GeckoBundle reference;
+
+ @Before
+ public void prepareReference() {
+ reference = createBundle();
+ }
+
+ @Test
+ public void canConstructWithCapacity() {
+ new GeckoBundle(0);
+ new GeckoBundle(1);
+ new GeckoBundle(42);
+
+ try {
+ new GeckoBundle(-1);
+ fail("Should throw with -1 capacity");
+ } catch (final Exception e) {
+ assertTrue(true);
+ }
+ }
+
+ @Test
+ public void canConstructWithBundle() {
+ assertEquals(reference, new GeckoBundle(reference));
+
+ try {
+ new GeckoBundle(null);
+ fail("Should throw with null bundle");
+ } catch (final Exception e) {
+ assertTrue(true);
+ }
+ }
+
+ @Test
+ public void referenceShouldBeCorrect() {
+ checkBundle(reference);
+ }
+
+ @Test
+ public void equalsShouldReturnCorrectResult() {
+ assertTrue(reference.equals(reference));
+ assertFalse(reference.equals(null));
+
+ assertTrue(reference.equals(new GeckoBundle(reference)));
+ assertFalse(reference.equals(new GeckoBundle()));
+ }
+
+ @Test
+ public void toStringShouldNotReturnEmptyString() {
+ assertNotNull(reference.toString());
+ assertNotEquals("", reference.toString());
+ }
+
+ @Test
+ public void hashCodeShouldNotReturnZero() {
+ assertNotEquals(0, reference.hashCode());
+ }
+
+ private static void testRemove(final GeckoBundle bundle, final String key) {
+ if (bundle.get(key) != null) {
+ assertTrue(String.format("%s should exist", key), bundle.containsKey(key));
+ } else {
+ assertFalse(String.format("%s should not exist", key), bundle.containsKey(key));
+ }
+ bundle.remove(key);
+ assertFalse(String.format("%s should not exist", key), bundle.containsKey(key));
+ }
+
+ @Test
+ public void containsKeyAndRemoveShouldWork() {
+ final GeckoBundle test = new GeckoBundle(reference);
+
+ testRemove(test, "nonexistent");
+ testRemove(test, "boolean");
+ testRemove(test, "booleanArray");
+ testRemove(test, "int");
+ testRemove(test, "intArray");
+ testRemove(test, "double");
+ testRemove(test, "doubleArray");
+ testRemove(test, "long");
+ testRemove(test, "longArray");
+ testRemove(test, "string");
+ testRemove(test, "nullString");
+ testRemove(test, "emptyString");
+ testRemove(test, "stringArray");
+ testRemove(test, "stringArrayOfNull");
+ testRemove(test, "emptyBooleanArray");
+ testRemove(test, "emptyIntArray");
+ testRemove(test, "emptyDoubleArray");
+ testRemove(test, "emptyLongArray");
+ testRemove(test, "emptyStringArray");
+ testRemove(test, "nullBooleanArray");
+ testRemove(test, "nullIntArray");
+ testRemove(test, "nullDoubleArray");
+ testRemove(test, "nullLongArray");
+ testRemove(test, "nullStringArray");
+ testRemove(test, "mixedArray");
+ testRemove(test, "byte");
+ testRemove(test, "short");
+ testRemove(test, "float");
+ testRemove(test, "char");
+ testRemove(test, "object");
+ testRemove(test, "nullObject");
+ testRemove(test, "objectArray");
+ testRemove(test, "objectArrayOfNull");
+ testRemove(test, "emptyObjectArray");
+ testRemove(test, "nullObjectArray");
+
+ assertEquals(0, test.size());
+ }
+
+ @Test
+ public void clearShouldWork() {
+ final GeckoBundle test = new GeckoBundle(reference);
+ assertNotEquals(0, test.size());
+ test.clear();
+ assertEquals(0, test.size());
+ }
+
+ @Test
+ public void keysShouldReturnCorrectResult() {
+ final String[] actual = reference.keys();
+ final String[] expected = new String[] {
+ "boolean", "booleanArray", "int", "intArray", "double", "doubleArray", "long",
+ "longArray", "string", "nullString", "emptyString", "stringArray", "stringArrayOfNull",
+ "emptyBooleanArray", "emptyIntArray", "emptyDoubleArray", "emptyLongArray",
+ "emptyStringArray", "nullBooleanArray", "nullIntArray", "nullDoubleArray",
+ "nullLongArray", "nullStringArray", "mixedArray", "byte", "short", "float", "char",
+ "object", "nullObject", "objectArray", "objectArrayOfNull", "emptyObjectArray",
+ "nullObjectArray"
+ };
+
+ Arrays.sort(expected);
+ Arrays.sort(actual);
+
+ assertArrayEquals(expected, actual);
+ }
+
+ @Test
+ public void isEmptyShouldReturnCorrectResult() {
+ assertFalse(reference.isEmpty());
+ assertTrue(new GeckoBundle().isEmpty());
+ }
+
+ @Test
+ public void getExistentKeysShouldNotReturnDefaultValues() {
+ assertNotEquals(false, reference.getBoolean("boolean", false));
+ assertNotEquals(0, reference.getInt("int", 0));
+ assertNotEquals(0.0, reference.getDouble("double", 0.0), 0.0);
+ assertNotEquals(0L, reference.getLong("long", 0L));
+ assertNotEquals("", reference.getString("string", ""));
+ }
+
+ private static void testDefaultValueForNull(final GeckoBundle bundle, final String key) {
+ // We return default values for null values.
+ assertEquals(true, bundle.getBoolean(key, true));
+ assertEquals(1, bundle.getInt(key, 1));
+ assertEquals(0.5, bundle.getDouble(key, 0.5), 0.0);
+ assertEquals("foo", bundle.getString(key, "foo"));
+ }
+
+ @Test
+ public void getNonexistentKeysShouldReturnDefaultValues() {
+ assertEquals(null, reference.get("nonexistent"));
+
+ assertEquals(false, reference.getBoolean("nonexistent"));
+ assertEquals(true, reference.getBoolean("nonexistent", true));
+ assertEquals(0, reference.getInt("nonexistent"));
+ assertEquals(1, reference.getInt("nonexistent", 1));
+ assertEquals(0.0, reference.getDouble("nonexistent"), 0.0);
+ assertEquals(0.5, reference.getDouble("nonexistent", 0.5), 0.0);
+ assertEquals(null, reference.getString("nonexistent"));
+ assertEquals("foo", reference.getString("nonexistent", "foo"));
+ assertEquals(null, reference.getBundle("nonexistent"));
+
+ assertArrayEquals(null, reference.getBooleanArray("nonexistent"));
+ assertArrayEquals(null, reference.getIntArray("nonexistent"));
+ assertArrayEquals(null, reference.getDoubleArray("nonexistent"), 0.0);
+ assertArrayEquals(null, reference.getLongArray("nonexistent"));
+ assertArrayEquals(null, reference.getStringArray("nonexistent"));
+ assertArrayEquals(null, reference.getBundleArray("nonexistent"));
+
+ // We return default values for null values.
+ testDefaultValueForNull(reference, "nullObject");
+ testDefaultValueForNull(reference, "nullString");
+ testDefaultValueForNull(reference, "nullBooleanArray");
+ testDefaultValueForNull(reference, "nullIntArray");
+ testDefaultValueForNull(reference, "nullDoubleArray");
+ testDefaultValueForNull(reference, "nullLongArray");
+ testDefaultValueForNull(reference, "nullStringArray");
+ testDefaultValueForNull(reference, "nullObjectArray");
+ }
+
+ @Test
+ public void bundleConversionShouldWork() {
+ assertEquals(reference, GeckoBundle.fromBundle(reference.toBundle()));
+ }
+
+ @Test
+ public void jsonConversionShouldWork() throws JSONException {
+ assertEquals(reference, GeckoBundle.fromJSONObject(reference.toJSONObject()));
+ }
+
+ @Test
+ public void parcelConversionShouldWork() {
+ final Parcel parcel = Parcel.obtain();
+
+ reference.writeToParcel(parcel, 0);
+ reference.writeToParcel(parcel, 0);
+ parcel.setDataPosition(0);
+
+ assertEquals(reference, GeckoBundle.CREATOR.createFromParcel(parcel));
+
+ final GeckoBundle test = new GeckoBundle();
+ test.readFromParcel(parcel);
+ assertEquals(reference, test);
+
+ parcel.recycle();
+ }
+
+ private static void testInvalidCoercions(final GeckoBundle bundle, final String key,
+ final String... exceptions) {
+ final List<String> allowed;
+ if (exceptions == null) {
+ allowed = Arrays.asList(key);
+ } else {
+ allowed = Arrays.asList(Arrays.copyOf(exceptions, exceptions.length + 1));
+ allowed.set(exceptions.length, key);
+ }
+
+ if (!allowed.contains("boolean")) {
+ try {
+ bundle.getBoolean(key);
+ fail(String.format("%s should not coerce to boolean", key));
+ } catch (final Exception e) {
+ assertTrue(true);
+ }
+ }
+
+ if (!allowed.contains("booleanArray") && !allowed.contains("emptyBooleanArray") &&
+ !allowed.contains("nullBooleanArray")) {
+ try {
+ bundle.getBooleanArray(key);
+ fail(String.format("%s should not coerce to boolean array", key));
+ } catch (final Exception e) {
+ assertTrue(true);
+ }
+ }
+
+ if (!allowed.contains("int")) {
+ try {
+ bundle.getInt(key);
+ fail(String.format("%s should not coerce to int", key));
+ } catch (final Exception e) {
+ assertTrue(true);
+ }
+ }
+
+ if (!allowed.contains("intArray") && !allowed.contains("emptyIntArray") &&
+ !allowed.contains("nullIntArray")) {
+ try {
+ bundle.getIntArray(key);
+ fail(String.format("%s should not coerce to int array", key));
+ } catch (final Exception e) {
+ assertTrue(true);
+ }
+ }
+
+ if (!allowed.contains("double")) {
+ try {
+ bundle.getDouble(key);
+ fail(String.format("%s should not coerce to double", key));
+ } catch (final Exception e) {
+ assertTrue(true);
+ }
+ }
+
+ if (!allowed.contains("doubleArray") && !allowed.contains("emptyDoubleArray") &&
+ !allowed.contains("nullDoubleArray")) {
+ try {
+ bundle.getDoubleArray(key);
+ fail(String.format("%s should not coerce to double array", key));
+ } catch (final Exception e) {
+ assertTrue(true);
+ }
+ }
+
+ if (!allowed.contains("long")) {
+ try {
+ bundle.getLong(key);
+ fail(String.format("%s should not coerce to long", key));
+ } catch (final Exception e) {
+ assertTrue(true);
+ }
+ }
+
+ if (!allowed.contains("longArray") && !allowed.contains("emptyLongArray") &&
+ !allowed.contains("nullLongArray")) {
+ try {
+ bundle.getLongArray(key);
+ fail(String.format("%s should not coerce to long array", key));
+ } catch (final Exception e) {
+ assertTrue(true);
+ }
+ }
+
+ if (!allowed.contains("string") && !allowed.contains("nullString")) {
+ try {
+ bundle.getString(key);
+ fail(String.format("%s should not coerce to string", key));
+ } catch (final Exception e) {
+ assertTrue(true);
+ }
+ }
+
+ if (!allowed.contains("stringArray") && !allowed.contains("emptyStringArray") &&
+ !allowed.contains("nullStringArray") && !allowed.contains("stringArrayOfNull")) {
+ try {
+ bundle.getStringArray(key);
+ fail(String.format("%s should not coerce to string array", key));
+ } catch (final Exception e) {
+ assertTrue(true);
+ }
+ }
+
+ if (!allowed.contains("object") && !allowed.contains("nullObject")) {
+ try {
+ bundle.getBundle(key);
+ fail(String.format("%s should not coerce to bundle", key));
+ } catch (final Exception e) {
+ assertTrue(true);
+ }
+ }
+
+ if (!allowed.contains("objectArray") && !allowed.contains("emptyObjectArray") &&
+ !allowed.contains("nullObjectArray") && !allowed.contains("objectArrayOfNull")) {
+ try {
+ bundle.getBundleArray(key);
+ fail(String.format("%s should not coerce to bundle array", key));
+ } catch (final Exception e) {
+ assertTrue(true);
+ }
+ }
+ }
+
+ @Test
+ public void booleanShouldNotCoerceToOtherTypes() {
+ testInvalidCoercions(reference, "boolean");
+ }
+
+ @Test
+ public void booleanArrayShouldNotCoerceToOtherTypes() {
+ testInvalidCoercions(reference, "booleanArray");
+ }
+
+ @Test
+ public void intShouldCoerceToDouble() {
+ assertEquals(1.0, reference.getDouble("int"), 0.0);
+ assertArrayEquals(new double[] {2.0, 3.0}, reference.getDoubleArray("intArray"), 0.0);
+ }
+
+ @Test
+ public void intShouldCoerceToLong() {
+ assertEquals(1L, reference.getLong("int"));
+ assertArrayEquals(new long[] {2L, 3L}, reference.getLongArray("intArray"));
+ }
+
+ @Test
+ public void intShouldNotCoerceToOtherTypes() {
+ testInvalidCoercions(reference, "int", /* except */ "double", "long");
+ testInvalidCoercions(reference, "intArray",
+ /* except */ "doubleArray", "longArray");
+ }
+
+ @Test
+ public void doubleShouldCoerceToInt() {
+ assertEquals(0, reference.getInt("double"));
+ assertArrayEquals(new int[] {1, 2}, reference.getIntArray("doubleArray"));
+ }
+
+ @Test
+ public void doubleShouldCoerceToLong() {
+ assertEquals(0L, reference.getLong("double"));
+ assertArrayEquals(new long[] {1L, 2L}, reference.getLongArray("doubleArray"));
+ }
+
+ @Test
+ public void doubleShouldNotCoerceToOtherTypes() {
+ testInvalidCoercions(reference, "double", /* except */ "int", "long");
+ testInvalidCoercions(reference, "doubleArray",
+ /* except */ "intArray", "longArray");
+ }
+
+ @Test
+ public void longShouldCoerceToInt() {
+ assertEquals(1, reference.getInt("long"));
+ assertArrayEquals(new int[] {2, 3}, reference.getIntArray("longArray"));
+ }
+
+ @Test
+ public void longShouldCoerceToDouble() {
+ assertEquals(1.0, reference.getDouble("long"), 0.0);
+ assertArrayEquals(new double[] {2.0, 3.0}, reference.getDoubleArray("longArray"), 0.0);
+ }
+
+ @Test
+ public void longShouldNotCoerceToOtherTypes() {
+ testInvalidCoercions(reference, "long", /* except */ "int", "double");
+ testInvalidCoercions(reference, "longArray",
+ /* except */ "intArray", "doubleArray");
+ }
+
+ @Test
+ public void nullStringShouldCoerceToBundle() {
+ assertEquals(null, reference.getBundle("nullString"));
+ assertArrayEquals(new GeckoBundle[2], reference.getBundleArray("stringArrayOfNull"));
+ }
+
+ @Test
+ public void nullStringShouldNotCoerceToOtherTypes() {
+ testInvalidCoercions(reference, "stringArrayOfNull", /* except */ "objectArrayOfNull");
+ }
+
+ @Test
+ public void nonNullStringShouldNotCoerceToOtherTypes() {
+ testInvalidCoercions(reference, "string");
+ }
+
+ @Test
+ public void nullBundleShouldCoerceToString() {
+ assertEquals(null, reference.getString("nullObject"));
+ assertArrayEquals(new String[2], reference.getStringArray("objectArrayOfNull"));
+ }
+
+ @Test
+ public void nullBundleShouldNotCoerceToOtherTypes() {
+ testInvalidCoercions(reference, "objectArrayOfNull", /* except */ "stringArrayOfNull");
+ }
+
+ @Test
+ public void nonNullBundleShouldNotCoerceToOtherTypes() {
+ testInvalidCoercions(reference, "object");
+ }
+
+ @Test
+ public void emptyArrayShouldCoerceToAnyArray() {
+ assertArrayEquals(new int[0], reference.getIntArray("emptyBooleanArray"));
+ assertArrayEquals(new double[0], reference.getDoubleArray("emptyBooleanArray"), 0.0);
+ assertArrayEquals(new long[0], reference.getLongArray("emptyBooleanArray"));
+ assertArrayEquals(new String[0], reference.getStringArray("emptyBooleanArray"));
+ assertArrayEquals(new GeckoBundle[0], reference.getBundleArray("emptyBooleanArray"));
+
+ assertArrayEquals(new boolean[0], reference.getBooleanArray("emptyIntArray"));
+ assertArrayEquals(new double[0], reference.getDoubleArray("emptyIntArray"), 0.0);
+ assertArrayEquals(new long[0], reference.getLongArray("emptyIntArray"));
+ assertArrayEquals(new String[0], reference.getStringArray("emptyIntArray"));
+ assertArrayEquals(new GeckoBundle[0], reference.getBundleArray("emptyIntArray"));
+
+ assertArrayEquals(new boolean[0], reference.getBooleanArray("emptyDoubleArray"));
+ assertArrayEquals(new int[0], reference.getIntArray("emptyDoubleArray"));
+ assertArrayEquals(new long[0], reference.getLongArray("emptyDoubleArray"));
+ assertArrayEquals(new String[0], reference.getStringArray("emptyDoubleArray"));
+ assertArrayEquals(new GeckoBundle[0], reference.getBundleArray("emptyDoubleArray"));
+
+ assertArrayEquals(new boolean[0], reference.getBooleanArray("emptyLongArray"));
+ assertArrayEquals(new int[0], reference.getIntArray("emptyLongArray"));
+ assertArrayEquals(new double[0], reference.getDoubleArray("emptyLongArray"), 0.0);
+ assertArrayEquals(new String[0], reference.getStringArray("emptyLongArray"));
+ assertArrayEquals(new GeckoBundle[0], reference.getBundleArray("emptyLongArray"));
+
+ assertArrayEquals(new boolean[0], reference.getBooleanArray("emptyStringArray"));
+ assertArrayEquals(new int[0], reference.getIntArray("emptyStringArray"));
+ assertArrayEquals(new double[0], reference.getDoubleArray("emptyStringArray"), 0.0);
+ assertArrayEquals(new long[0], reference.getLongArray("emptyStringArray"));
+ assertArrayEquals(new GeckoBundle[0], reference.getBundleArray("emptyStringArray"));
+
+ assertArrayEquals(new boolean[0], reference.getBooleanArray("emptyObjectArray"));
+ assertArrayEquals(new int[0], reference.getIntArray("emptyObjectArray"));
+ assertArrayEquals(new double[0], reference.getDoubleArray("emptyObjectArray"), 0.0);
+ assertArrayEquals(new long[0], reference.getLongArray("emptyObjectArray"));
+ assertArrayEquals(new String[0], reference.getStringArray("emptyObjectArray"));
+ }
+
+ @Test
+ public void emptyArrayShouldNotCoerceToOtherTypes() {
+ testInvalidCoercions(reference, "emptyBooleanArray", /* except */ "intArray",
+ "doubleArray", "longArray", "stringArray", "objectArray");
+ testInvalidCoercions(reference, "emptyIntArray", /* except */ "booleanArray",
+ "doubleArray", "longArray", "stringArray", "objectArray");
+ testInvalidCoercions(reference, "emptyDoubleArray", /* except */ "booleanArray",
+ "intArray", "longArray", "stringArray", "objectArray");
+ testInvalidCoercions(reference, "emptyLongArray", /* except */ "booleanArray",
+ "intArray", "doubleArray", "stringArray", "objectArray");
+ testInvalidCoercions(reference, "emptyStringArray", /* except */ "booleanArray",
+ "intArray", "doubleArray", "longArray", "objectArray");
+ testInvalidCoercions(reference, "emptyObjectArray", /* except */ "booleanArray",
+ "intArray", "doubleArray", "longArray", "stringArray");
+ }
+
+ @Test
+ public void nullArrayShouldCoerceToAnyArray() {
+ assertArrayEquals(null, reference.getIntArray("nullBooleanArray"));
+ assertArrayEquals(null, reference.getDoubleArray("nullBooleanArray"), 0.0);
+ assertArrayEquals(null, reference.getLongArray("nullBooleanArray"));
+ assertArrayEquals(null, reference.getStringArray("nullBooleanArray"));
+ assertArrayEquals(null, reference.getBundleArray("nullBooleanArray"));
+
+ assertArrayEquals(null, reference.getBooleanArray("nullIntArray"));
+ assertArrayEquals(null, reference.getDoubleArray("nullIntArray"), 0.0);
+ assertArrayEquals(null, reference.getLongArray("nullIntArray"));
+ assertArrayEquals(null, reference.getStringArray("nullIntArray"));
+ assertArrayEquals(null, reference.getBundleArray("nullIntArray"));
+
+ assertArrayEquals(null, reference.getBooleanArray("nullDoubleArray"));
+ assertArrayEquals(null, reference.getIntArray("nullDoubleArray"));
+ assertArrayEquals(null, reference.getLongArray("nullDoubleArray"));
+ assertArrayEquals(null, reference.getStringArray("nullDoubleArray"));
+ assertArrayEquals(null, reference.getBundleArray("nullDoubleArray"));
+
+ assertArrayEquals(null, reference.getBooleanArray("nullLongArray"));
+ assertArrayEquals(null, reference.getIntArray("nullLongArray"));
+ assertArrayEquals(null, reference.getDoubleArray("nullLongArray"), 0.0);
+ assertArrayEquals(null, reference.getStringArray("nullLongArray"));
+ assertArrayEquals(null, reference.getBundleArray("nullLongArray"));
+
+ assertArrayEquals(null, reference.getBooleanArray("nullStringArray"));
+ assertArrayEquals(null, reference.getIntArray("nullStringArray"));
+ assertArrayEquals(null, reference.getDoubleArray("nullStringArray"), 0.0);
+ assertArrayEquals(null, reference.getLongArray("nullStringArray"));
+ assertArrayEquals(null, reference.getBundleArray("nullStringArray"));
+
+ assertArrayEquals(null, reference.getBooleanArray("nullObjectArray"));
+ assertArrayEquals(null, reference.getIntArray("nullObjectArray"));
+ assertArrayEquals(null, reference.getDoubleArray("nullObjectArray"), 0.0);
+ assertArrayEquals(null, reference.getLongArray("nullObjectArray"));
+ assertArrayEquals(null, reference.getStringArray("nullObjectArray"));
+ }
+}
diff --git a/mobile/android/geckoview/src/test/java/org/mozilla/gecko/util/IntentUtilsTest.java b/mobile/android/geckoview/src/test/java/org/mozilla/gecko/util/IntentUtilsTest.java
new file mode 100644
index 0000000000..148d30d44e
--- /dev/null
+++ b/mobile/android/geckoview/src/test/java/org/mozilla/gecko/util/IntentUtilsTest.java
@@ -0,0 +1,61 @@
+/* Any copyright is dedicated to the Public Domain.
+ http://creativecommons.org/publicdomain/zero/1.0/ */
+
+package org.mozilla.gecko.util;
+
+import static org.junit.Assert.*;
+
+import android.net.Uri;
+import android.os.Parcel;
+import android.test.suitebuilder.annotation.SmallTest;
+
+import org.json.JSONException;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.robolectric.RobolectricTestRunner;
+
+import java.util.Arrays;
+import java.util.List;
+
+@RunWith(RobolectricTestRunner.class)
+@SmallTest
+public class IntentUtilsTest {
+
+ @Test
+ public void shouldNormalizeUri() {
+ final String uri = "HTTPS://mozilla.org";
+ final Uri normUri = IntentUtils.normalizeUri(uri);
+ assertEquals("https://mozilla.org", normUri.toString());
+ }
+
+ @Test
+ public void safeHttpUri() {
+ final String uri = "https://mozilla.org";
+ assertTrue(IntentUtils.isUriSafeForScheme(uri));
+ }
+
+ @Test
+ public void safeIntentUri() {
+ final String uri = "intent:https://mozilla.org#Intent;end;";
+ assertTrue(IntentUtils.isUriSafeForScheme(uri));
+ }
+
+ @Test
+ public void unsafeIntentUri() {
+ final String uri = "intent:file:///storage/emulated/0/Download#Intent;end";
+ assertFalse(IntentUtils.isUriSafeForScheme(uri));
+ }
+
+ @Test
+ public void safeTelUri() {
+ final String uri = "tel:12345678";
+ assertTrue(IntentUtils.isUriSafeForScheme(uri));
+ }
+
+ @Test
+ public void unsafeTelUri() {
+ final String uri = "tel:#12345678";
+ assertFalse(IntentUtils.isUriSafeForScheme(uri));
+ }
+}
diff --git a/mobile/android/geckoview/src/test/java/org/mozilla/gecko/util/NetworkUtilsTest.java b/mobile/android/geckoview/src/test/java/org/mozilla/gecko/util/NetworkUtilsTest.java
new file mode 100644
index 0000000000..8db4eb3613
--- /dev/null
+++ b/mobile/android/geckoview/src/test/java/org/mozilla/gecko/util/NetworkUtilsTest.java
@@ -0,0 +1,189 @@
+/* Any copyright is dedicated to the Public Domain.
+ http://creativecommons.org/publicdomain/zero/1.0/ */
+
+package org.mozilla.gecko.util;
+
+import android.content.Context;
+import android.net.ConnectivityManager;
+import android.net.NetworkInfo;
+import android.telephony.TelephonyManager;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mozilla.gecko.util.NetworkUtils.ConnectionSubType;
+import org.mozilla.gecko.util.NetworkUtils.ConnectionType;
+import org.mozilla.gecko.util.NetworkUtils.NetworkStatus;
+import org.robolectric.RobolectricTestRunner;
+import org.robolectric.RuntimeEnvironment;
+import org.robolectric.shadow.api.Shadow;
+import org.robolectric.shadows.ShadowConnectivityManager;
+import org.robolectric.shadows.ShadowNetworkInfo;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+@RunWith(RobolectricTestRunner.class)
+public class NetworkUtilsTest {
+ private ConnectivityManager connectivityManager;
+ private ShadowConnectivityManager shadowConnectivityManager;
+
+ @Before
+ public void setUp() {
+ connectivityManager = (ConnectivityManager) RuntimeEnvironment.application.getSystemService(Context.CONNECTIVITY_SERVICE);
+
+ // Not using Shadows.shadowOf(connectivityManager) because of Robolectric bug when using API23+
+ // See: https://github.com/robolectric/robolectric/issues/1862
+ shadowConnectivityManager = (ShadowConnectivityManager) Shadow.extract(connectivityManager);
+ }
+
+ @Test
+ public void testIsConnected() throws Exception {
+ assertFalse(NetworkUtils.isConnected((ConnectivityManager) null));
+
+ shadowConnectivityManager.setActiveNetworkInfo(null);
+ assertFalse(NetworkUtils.isConnected(connectivityManager));
+
+ shadowConnectivityManager.setActiveNetworkInfo(
+ ShadowNetworkInfo.newInstance(NetworkInfo.DetailedState.CONNECTED, ConnectivityManager.TYPE_WIFI, 0, true, true)
+ );
+ assertTrue(NetworkUtils.isConnected(connectivityManager));
+
+ shadowConnectivityManager.setActiveNetworkInfo(
+ ShadowNetworkInfo.newInstance(NetworkInfo.DetailedState.DISCONNECTED, ConnectivityManager.TYPE_WIFI, 0, true, false)
+ );
+ assertFalse(NetworkUtils.isConnected(connectivityManager));
+ }
+
+ @Test
+ public void testGetConnectionSubType() throws Exception {
+ assertEquals(ConnectionSubType.UNKNOWN, NetworkUtils.getConnectionSubType(null));
+
+ shadowConnectivityManager.setActiveNetworkInfo(null);
+ assertEquals(ConnectionSubType.UNKNOWN, NetworkUtils.getConnectionSubType(connectivityManager));
+
+ // We don't seem to care about figuring out all connection types. So...
+ shadowConnectivityManager.setActiveNetworkInfo(
+ ShadowNetworkInfo.newInstance(NetworkInfo.DetailedState.CONNECTED, ConnectivityManager.TYPE_VPN, 0, true, true)
+ );
+ assertEquals(ConnectionSubType.UNKNOWN, NetworkUtils.getConnectionSubType(connectivityManager));
+
+ // But anything below we should recognize.
+ shadowConnectivityManager.setActiveNetworkInfo(
+ ShadowNetworkInfo.newInstance(NetworkInfo.DetailedState.CONNECTED, ConnectivityManager.TYPE_ETHERNET, 0, true, true)
+ );
+ assertEquals(ConnectionSubType.ETHERNET, NetworkUtils.getConnectionSubType(connectivityManager));
+
+ shadowConnectivityManager.setActiveNetworkInfo(
+ ShadowNetworkInfo.newInstance(NetworkInfo.DetailedState.CONNECTED, ConnectivityManager.TYPE_WIFI, 0, true, true)
+ );
+ assertEquals(ConnectionSubType.WIFI, NetworkUtils.getConnectionSubType(connectivityManager));
+
+ shadowConnectivityManager.setActiveNetworkInfo(
+ ShadowNetworkInfo.newInstance(NetworkInfo.DetailedState.CONNECTED, ConnectivityManager.TYPE_WIMAX, 0, true, true)
+ );
+ assertEquals(ConnectionSubType.WIMAX, NetworkUtils.getConnectionSubType(connectivityManager));
+
+ // Unknown mobile
+ shadowConnectivityManager.setActiveNetworkInfo(
+ ShadowNetworkInfo.newInstance(NetworkInfo.DetailedState.CONNECTED, ConnectivityManager.TYPE_MOBILE, TelephonyManager.NETWORK_TYPE_UNKNOWN, true, true)
+ );
+ assertEquals(ConnectionSubType.UNKNOWN, NetworkUtils.getConnectionSubType(connectivityManager));
+
+ // 2G mobile types
+ int[] cell2gTypes = new int[] {
+ TelephonyManager.NETWORK_TYPE_GPRS,
+ TelephonyManager.NETWORK_TYPE_EDGE,
+ TelephonyManager.NETWORK_TYPE_CDMA,
+ TelephonyManager.NETWORK_TYPE_1xRTT,
+ TelephonyManager.NETWORK_TYPE_IDEN
+ };
+ for (int i = 0; i < cell2gTypes.length; i++) {
+ shadowConnectivityManager.setActiveNetworkInfo(
+ ShadowNetworkInfo.newInstance(NetworkInfo.DetailedState.CONNECTED, ConnectivityManager.TYPE_MOBILE, cell2gTypes[i], true, true)
+ );
+ assertEquals(ConnectionSubType.CELL_2G, NetworkUtils.getConnectionSubType(connectivityManager));
+ }
+
+ // 3G mobile types
+ int[] cell3gTypes = new int[] {
+ TelephonyManager.NETWORK_TYPE_UMTS,
+ TelephonyManager.NETWORK_TYPE_EVDO_0,
+ TelephonyManager.NETWORK_TYPE_EVDO_A,
+ TelephonyManager.NETWORK_TYPE_HSDPA,
+ TelephonyManager.NETWORK_TYPE_HSUPA,
+ TelephonyManager.NETWORK_TYPE_HSPA,
+ TelephonyManager.NETWORK_TYPE_EVDO_B,
+ TelephonyManager.NETWORK_TYPE_EHRPD,
+ TelephonyManager.NETWORK_TYPE_HSPAP
+ };
+ for (int i = 0; i < cell3gTypes.length; i++) {
+ shadowConnectivityManager.setActiveNetworkInfo(
+ ShadowNetworkInfo.newInstance(NetworkInfo.DetailedState.CONNECTED, ConnectivityManager.TYPE_MOBILE, cell3gTypes[i], true, true)
+ );
+ assertEquals(ConnectionSubType.CELL_3G, NetworkUtils.getConnectionSubType(connectivityManager));
+ }
+
+ // 4G mobile type
+ shadowConnectivityManager.setActiveNetworkInfo(
+ ShadowNetworkInfo.newInstance(NetworkInfo.DetailedState.CONNECTED, ConnectivityManager.TYPE_MOBILE, TelephonyManager.NETWORK_TYPE_LTE, true, true)
+ );
+ assertEquals(ConnectionSubType.CELL_4G, NetworkUtils.getConnectionSubType(connectivityManager));
+ }
+
+ @Test
+ public void testGetConnectionType() {
+ shadowConnectivityManager.setActiveNetworkInfo(null);
+ assertEquals(ConnectionType.NONE, NetworkUtils.getConnectionType(connectivityManager));
+ assertEquals(ConnectionType.NONE, NetworkUtils.getConnectionType(null));
+
+ shadowConnectivityManager.setActiveNetworkInfo(
+ ShadowNetworkInfo.newInstance(NetworkInfo.DetailedState.CONNECTED, ConnectivityManager.TYPE_VPN, 0, true, true)
+ );
+ assertEquals(ConnectionType.OTHER, NetworkUtils.getConnectionType(connectivityManager));
+
+ shadowConnectivityManager.setActiveNetworkInfo(
+ ShadowNetworkInfo.newInstance(NetworkInfo.DetailedState.CONNECTED, ConnectivityManager.TYPE_WIFI, 0, true, true)
+ );
+ assertEquals(ConnectionType.WIFI, NetworkUtils.getConnectionType(connectivityManager));
+
+ shadowConnectivityManager.setActiveNetworkInfo(
+ ShadowNetworkInfo.newInstance(NetworkInfo.DetailedState.CONNECTED, ConnectivityManager.TYPE_MOBILE, 0, true, true)
+ );
+ assertEquals(ConnectionType.CELLULAR, NetworkUtils.getConnectionType(connectivityManager));
+
+ shadowConnectivityManager.setActiveNetworkInfo(
+ ShadowNetworkInfo.newInstance(NetworkInfo.DetailedState.CONNECTED, ConnectivityManager.TYPE_ETHERNET, 0, true, true)
+ );
+ assertEquals(ConnectionType.ETHERNET, NetworkUtils.getConnectionType(connectivityManager));
+
+ shadowConnectivityManager.setActiveNetworkInfo(
+ ShadowNetworkInfo.newInstance(NetworkInfo.DetailedState.CONNECTED, ConnectivityManager.TYPE_BLUETOOTH, 0, true, true)
+ );
+ assertEquals(ConnectionType.BLUETOOTH, NetworkUtils.getConnectionType(connectivityManager));
+
+ shadowConnectivityManager.setActiveNetworkInfo(
+ ShadowNetworkInfo.newInstance(NetworkInfo.DetailedState.CONNECTED, ConnectivityManager.TYPE_WIMAX, 0, true, true)
+ );
+ assertEquals(ConnectionType.CELLULAR, NetworkUtils.getConnectionType(connectivityManager));
+ }
+
+ @Test
+ public void testGetNetworkStatus() {
+ assertEquals(NetworkStatus.UNKNOWN, NetworkUtils.getNetworkStatus(null));
+
+ shadowConnectivityManager.setActiveNetworkInfo(null);
+ assertEquals(NetworkStatus.DOWN, NetworkUtils.getNetworkStatus(connectivityManager));
+
+ shadowConnectivityManager.setActiveNetworkInfo(
+ ShadowNetworkInfo.newInstance(NetworkInfo.DetailedState.CONNECTING, ConnectivityManager.TYPE_MOBILE, 0, true, false)
+ );
+ assertEquals(NetworkStatus.DOWN, NetworkUtils.getNetworkStatus(connectivityManager));
+
+ shadowConnectivityManager.setActiveNetworkInfo(
+ ShadowNetworkInfo.newInstance(NetworkInfo.DetailedState.CONNECTED, ConnectivityManager.TYPE_MOBILE, 0, true, true)
+ );
+ assertEquals(NetworkStatus.UP, NetworkUtils.getNetworkStatus(connectivityManager));
+ }
+}
diff --git a/mobile/android/geckoview/src/test/java/org/mozilla/gecko/util/TestDateUtil.java b/mobile/android/geckoview/src/test/java/org/mozilla/gecko/util/TestDateUtil.java
new file mode 100644
index 0000000000..7f01d88646
--- /dev/null
+++ b/mobile/android/geckoview/src/test/java/org/mozilla/gecko/util/TestDateUtil.java
@@ -0,0 +1,86 @@
+/*
+ * 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 org.junit.Test;
+
+import java.util.Calendar;
+import java.util.GregorianCalendar;
+import java.util.Locale;
+import java.util.TimeZone;
+
+import static org.junit.Assert.assertEquals;
+
+/**
+ * Unit tests for date utilities.
+ */
+public class TestDateUtil {
+ @Test
+ public void testGetDateInHTTPFormatGMT() {
+ final TimeZone gmt = TimeZone.getTimeZone("GMT");
+ final GregorianCalendar calendar = new GregorianCalendar(gmt, Locale.US);
+ calendar.set(2011, Calendar.FEBRUARY, 1, 14, 0, 0);
+ final String expectedDate = "Tue, 01 Feb 2011 14:00:00 GMT";
+
+ final String actualDate = DateUtil.getDateInHTTPFormat(calendar.getTime());
+ assertEquals("Returned date is expected", expectedDate, actualDate);
+ }
+
+ @Test
+ public void testGetDateInHTTPFormatNonGMT() {
+ final TimeZone kst = TimeZone.getTimeZone("Asia/Seoul"); // no daylight savings time.
+ final GregorianCalendar calendar = new GregorianCalendar(kst, Locale.US);
+ calendar.set(2011, Calendar.FEBRUARY, 1, 14, 0, 0);
+ final String expectedDate = "Tue, 01 Feb 2011 05:00:00 GMT";
+
+ final String actualDate = DateUtil.getDateInHTTPFormat(calendar.getTime());
+ assertEquals("Returned date is expected", expectedDate, actualDate);
+ }
+
+ @Test
+ public void testGetTimezoneOffsetInMinutes() {
+ assertEquals("GMT has no offset", 0, DateUtil.getTimezoneOffsetInMinutes(TimeZone.getTimeZone("GMT")));
+
+ // We use custom timezones because they don't have daylight savings time.
+ assertEquals("Offset for GMT-8 is correct",
+ -480, DateUtil.getTimezoneOffsetInMinutes(TimeZone.getTimeZone("GMT-8")));
+ assertEquals("Offset for GMT+12:45 is correct",
+ 765, DateUtil.getTimezoneOffsetInMinutes(TimeZone.getTimeZone("GMT+12:45")));
+
+ // We use a non-custom timezone without DST.
+ assertEquals("Offset for KST is correct",
+ 540, DateUtil.getTimezoneOffsetInMinutes(TimeZone.getTimeZone("Asia/Seoul")));
+ }
+
+ @Test
+ public void testGetTimezoneOffsetInMinutesForGivenDateNoDaylightSavingsTime() {
+ final TimeZone kst = TimeZone.getTimeZone("Asia/Seoul");
+ final Calendar[] calendars =
+ new Calendar[] { getCalendarForMonth(Calendar.DECEMBER), getCalendarForMonth(Calendar.AUGUST) };
+ for (final Calendar cal : calendars) {
+ cal.setTimeZone(kst);
+ assertEquals("Offset for KST does not change with daylight savings time",
+ 540, DateUtil.getTimezoneOffsetInMinutesForGivenDate(cal));
+ }
+ }
+
+ @Test
+ public void testGetTimezoneOffsetInMinutesForGivenDateDaylightSavingsTime() {
+ final TimeZone pacificTimeZone = TimeZone.getTimeZone("America/Los_Angeles");
+ final Calendar pstCalendar = getCalendarForMonth(Calendar.DECEMBER);
+ final Calendar pdtCalendar = getCalendarForMonth(Calendar.AUGUST);
+ pstCalendar.setTimeZone(pacificTimeZone);
+ pdtCalendar.setTimeZone(pacificTimeZone);
+ assertEquals("Offset for PST is correct", -480, DateUtil.getTimezoneOffsetInMinutesForGivenDate(pstCalendar));
+ assertEquals("Offset for PDT is correct", -420, DateUtil.getTimezoneOffsetInMinutesForGivenDate(pdtCalendar));
+
+ }
+
+ private Calendar getCalendarForMonth(final int month) {
+ return new GregorianCalendar(2000, month, 1);
+ }
+}
diff --git a/mobile/android/geckoview/src/test/java/org/mozilla/gecko/util/TestFileUtils.java b/mobile/android/geckoview/src/test/java/org/mozilla/gecko/util/TestFileUtils.java
new file mode 100644
index 0000000000..549e433ce3
--- /dev/null
+++ b/mobile/android/geckoview/src/test/java/org/mozilla/gecko/util/TestFileUtils.java
@@ -0,0 +1,360 @@
+/*
+ * 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 org.json.JSONException;
+import org.json.JSONObject;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.TemporaryFolder;
+import org.junit.runner.RunWith;
+import org.mockito.Matchers;
+import org.mozilla.gecko.util.FileUtils.FileLastModifiedComparator;
+import org.mozilla.gecko.util.FileUtils.FilenameRegexFilter;
+import org.mozilla.gecko.util.FileUtils.FilenameWhitelistFilter;
+import org.robolectric.RobolectricTestRunner;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.io.OutputStreamWriter;
+import java.nio.charset.Charset;
+import java.util.Arrays;
+import java.util.HashSet;
+import java.util.Set;
+import java.util.regex.Pattern;
+
+import static junit.framework.Assert.*;
+import static org.hamcrest.Matchers.is;
+import static org.hamcrest.Matchers.not;
+import static org.hamcrest.Matchers.nullValue;
+import static org.junit.Assert.assertThat;
+import static org.mockito.Matchers.notNull;
+import static org.mockito.Mockito.*;
+
+/**
+ * Tests the utilities in {@link FileUtils}.
+ */
+@RunWith(RobolectricTestRunner.class)
+public class TestFileUtils {
+
+ private static final Charset CHARSET = Charset.forName("UTF-8");
+
+ @Rule
+ public TemporaryFolder tempDir = new TemporaryFolder();
+ public File testFile;
+ public File nonExistentFile;
+
+ @Before
+ public void setUp() throws Exception {
+ testFile = tempDir.newFile();
+ nonExistentFile = new File(tempDir.getRoot(), "non-existent-file");
+ }
+
+ @Test
+ public void testReadJSONObjectFromFile() throws Exception {
+ final JSONObject expected = new JSONObject("{\"str\": \"some str\"}");
+ writeStringToFile(testFile, expected.toString());
+
+ final JSONObject actual = FileUtils.readJSONObjectFromFile(testFile);
+ assertEquals("JSON contains expected str", expected.getString("str"), actual.getString("str"));
+ }
+
+ @Test(expected=IOException.class)
+ public void testReadJSONObjectFromFileEmptyFile() throws Exception {
+ assertEquals("Test file is empty", 0, testFile.length());
+ FileUtils.readJSONObjectFromFile(testFile); // expected to throw
+ }
+
+ @Test(expected=JSONException.class)
+ public void testReadJSONObjectFromFileInvalidJSON() throws Exception {
+ writeStringToFile(testFile, "not a json str");
+ FileUtils.readJSONObjectFromFile(testFile); // expected to throw
+ }
+
+ @Test
+ public void testReadStringFromFileReadsData() throws Exception {
+ final String expected = "String to write contains hard characters: !\n \\s..\"'\u00f1";
+ writeStringToFile(testFile, expected);
+
+ final String actual = FileUtils.readStringFromFile(testFile);
+ assertEquals("Read content matches written content", expected, actual);
+ }
+
+ @Test
+ public void testReadStringFromFileEmptyFile() throws Exception {
+ assertEquals("Test file is empty", 0, testFile.length());
+
+ final String actual = FileUtils.readStringFromFile(testFile);
+ assertEquals("Read content is empty", "", actual);
+ }
+
+ @Test(expected=FileNotFoundException.class)
+ public void testReadStringFromNonExistentFile() throws Exception {
+ assertFalse("File does not exist", nonExistentFile.exists());
+ FileUtils.readStringFromFile(nonExistentFile);
+ }
+
+ @Test
+ public void testReadStringFromInputStreamAndCloseStreamBufferLenIsFileLen() throws Exception {
+ final String expected = "String to write contains hard characters: !\n \\s..\"'\u00f1";
+ writeStringToFile(testFile, expected);
+
+ final FileInputStream stream = new FileInputStream(testFile);
+ final String actual = FileUtils.readStringFromInputStreamAndCloseStream(stream, expected.length());
+ assertEquals("Read content matches written content", expected, actual);
+ }
+
+ @Test
+ public void testReadStringFromInputStreamAndCloseStreamBufferLenIsBiggerThanFile() throws Exception {
+ final String expected = "aoeuhtns";
+ writeStringToFile(testFile, expected);
+
+ final FileInputStream stream = new FileInputStream(testFile);
+ final String actual = FileUtils.readStringFromInputStreamAndCloseStream(stream, expected.length() + 1024);
+ assertEquals("Read content matches written content", expected, actual);
+ }
+
+ @Test
+ public void testReadStringFromInputStreamAndCloseStreamBufferLenIsSmallerThanFile() throws Exception {
+ final String expected = "aoeuhtns aoeusth aoeusth aoeusnth aoeusth aoeusnth aoesuth";
+ writeStringToFile(testFile, expected);
+
+ final FileInputStream stream = new FileInputStream(testFile);
+ final String actual = FileUtils.readStringFromInputStreamAndCloseStream(stream, 8);
+ assertEquals("Read content matches written content", expected, actual);
+ }
+
+ @Test(expected=IllegalArgumentException.class)
+ public void testReadStringFromInputStreamAndCloseStreamBufferLenIsZero() throws Exception {
+ final String expected = "aoeuhtns aoeusth aoeusth aoeusnth aoeusth aoeusnth aoesuth";
+ writeStringToFile(testFile, expected);
+
+ final FileInputStream stream = new FileInputStream(testFile);
+ FileUtils.readStringFromInputStreamAndCloseStream(stream, 0); // expected to throw.
+ }
+
+ @Test
+ public void testReadStringFromInputStreamAndCloseStreamIsEmptyStream() throws Exception {
+ assertTrue("Test file exists", testFile.exists());
+ assertEquals("Test file is empty", 0, testFile.length());
+
+ final FileInputStream stream = new FileInputStream(testFile);
+ final String actual = FileUtils.readStringFromInputStreamAndCloseStream(stream, 8);
+ assertEquals("Read content from stream is empty", "", actual);
+ }
+
+ @Test(expected=IOException.class)
+ public void testReadStringFromInputStreamAndCloseStreamClosesStream() throws Exception {
+ final String expected = "String to write contains hard characters: !\n \\s..\"'\u00f1";
+ writeStringToFile(testFile, expected);
+
+ final FileInputStream stream = new FileInputStream(testFile);
+ try {
+ stream.read(); // should not throw because stream is open.
+ FileUtils.readStringFromInputStreamAndCloseStream(stream, expected.length());
+ } catch (final IOException e) {
+ fail("Did not expect method to throw when writing file: " + e);
+ }
+
+ stream.read(); // expected to throw because stream is closed.
+ }
+
+ @Test
+ public void testWriteStringToOutputStreamAndCloseStreamWritesData() throws Exception {
+ final String expected = "A string with some data in it! \u00f1 \n";
+ final FileOutputStream fos = new FileOutputStream(testFile, false);
+ FileUtils.writeStringToOutputStreamAndCloseStream(fos, expected);
+
+ assertTrue("Written file exists", testFile.exists());
+ assertEquals("Read data equals written data", expected, readStringFromFile(testFile, expected.length()));
+ }
+
+ @Test(expected=IOException.class)
+ public void testWriteStringToOutputStreamAndCloseStreamClosesStream() throws Exception {
+ final FileOutputStream fos = new FileOutputStream(testFile, false);
+ try {
+ fos.write('c'); // should not throw because stream is open.
+ FileUtils.writeStringToOutputStreamAndCloseStream(fos, "some string with data");
+ } catch (final IOException e) {
+ fail("Did not expect method to throw when writing file: " + e);
+ }
+
+ fos.write('c'); // expected to throw because stream is closed.
+ }
+
+ /**
+ * The Writer we wrap our stream in can throw in .close(), preventing the underlying stream from closing.
+ * I added code to prevent ensure we close if the writer .close() throws.
+ *
+ * I wrote this test to test that code, however, we'd have to mock the writer [1] and that isn't straight-forward.
+ * I left this test around because it's a good test of other code.
+ *
+ * [1]: We thought we could mock FileOutputStream.flush but it's only flushed if the Writer thinks it should be
+ * flushed. We can write directly to the Stream, but that doesn't change the Writer state and doesn't affect whether
+ * it thinks it should be flushed.
+ */
+ @Test(expected=IOException.class)
+ public void testWriteStringToOutputStreamAndCloseStreamClosesStreamIfWriterThrows() throws Exception {
+ final FileOutputStream fos = mock(FileOutputStream.class);
+ doThrow(IOException.class).when(fos).write(any(byte[].class), anyInt(), anyInt());
+ doThrow(IOException.class).when(fos).write(anyInt());
+ doThrow(IOException.class).when(fos).write(any(byte[].class));
+
+ boolean exceptionCaught = false;
+ try {
+ FileUtils.writeStringToOutputStreamAndCloseStream(fos, "some string with data");
+ } catch (final IOException e) {
+ exceptionCaught = true;
+ }
+ assertTrue("Exception caught during tested method", exceptionCaught); // not strictly necessary but documents assumptions
+
+ fos.write('c'); // expected to throw because stream is closed.
+ }
+
+ @Test
+ public void testWriteStringToFile() throws Exception {
+ final String expected = "String to write contains hard characters: !\n \\s..\"'\u00f1";
+ FileUtils.writeStringToFile(testFile, expected);
+
+ assertTrue("Written file exists", testFile.exists());
+ assertEquals("Read data equals written data", expected, readStringFromFile(testFile, expected.length()));
+ }
+
+ @Test
+ public void testWriteStringToFileEmptyString() throws Exception {
+ final String expected = "";
+ FileUtils.writeStringToFile(testFile, expected);
+
+ assertTrue("Written file exists", testFile.exists());
+ assertEquals("Written file is empty", 0, testFile.length());
+ assertEquals("Read data equals written (empty) data", expected, readStringFromFile(testFile, expected.length()));
+ }
+
+ @Test
+ public void testWriteStringToFileCreatesNewFile() throws Exception {
+ final String expected = "some str to write";
+ assertFalse("Non existent file does not exist", nonExistentFile.exists());
+ FileUtils.writeStringToFile(nonExistentFile, expected); // expected to create file
+
+ assertTrue("Written file was created", nonExistentFile.exists());
+ assertEquals("Read data equals written data", expected, readStringFromFile(nonExistentFile, (int) nonExistentFile.length()));
+ }
+
+ @Test
+ public void testWriteStringToFileOverwritesFile() throws Exception {
+ writeStringToFile(testFile, "data");
+
+ final String expected = "some str to write";
+ FileUtils.writeStringToFile(testFile, expected);
+
+ assertTrue("Written file was created", testFile.exists());
+ assertEquals("Read data equals written data", expected, readStringFromFile(testFile, (int) testFile.length()));
+ }
+
+ @Test
+ public void testWriteJSONObjectToFile() throws Exception {
+ final JSONObject expected = new JSONObject()
+ .put("int", 1)
+ .put("str", "1")
+ .put("bool", true)
+ .put("null", JSONObject.NULL)
+ .put("raw null", null);
+ FileUtils.writeJSONObjectToFile(testFile, expected);
+
+ assertTrue("Written file exists", testFile.exists());
+
+ // JSONObject.equals compares references so we have to assert each key individually. >:(
+ final JSONObject actual = new JSONObject(readStringFromFile(testFile, (int) testFile.length()));
+ assertEquals(1, actual.getInt("int"));
+ assertEquals("1", actual.getString("str"));
+ assertEquals(true, actual.getBoolean("bool"));
+ assertEquals(JSONObject.NULL, actual.get("null"));
+ assertFalse(actual.has("raw null"));
+ }
+
+ // Since the read methods may not be tested yet.
+ private static String readStringFromFile(final File file, final int bufferLen) throws IOException {
+ final char[] buffer = new char[bufferLen];
+ try (InputStreamReader reader = new InputStreamReader(new FileInputStream(file), Charset.forName("UTF-8"))) {
+ reader.read(buffer, 0, buffer.length);
+ }
+ return new String(buffer);
+ }
+
+ // Since the write methods may not be tested yet.
+ private static void writeStringToFile(final File file, final String str) throws IOException {
+ try (OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(file, false), CHARSET)) {
+ writer.write(str);
+ }
+ assertTrue("Written file from helper method exists", file.exists());
+ }
+
+ @Test
+ public void testFilenameWhitelistFilter() {
+ final String[] expectedToAccept = new String[] { "one", "two", "three" };
+ final Set<String> whitelist = new HashSet<>(Arrays.asList(expectedToAccept));
+ final FilenameWhitelistFilter testFilter = new FilenameWhitelistFilter(whitelist);
+ for (final String str : expectedToAccept) {
+ assertTrue("Filename, " + str + ", in whitelist is accepted", testFilter.accept(testFile, str));
+ }
+
+ final String[] notExpectedToAccept = new String[] { "not-in-whitelist", "meh", "whatever" };
+ for (final String str : notExpectedToAccept) {
+ assertFalse("Filename, " + str + ", not in whitelist is not accepted", testFilter.accept(testFile, str));
+ }
+ }
+
+ @Test
+ public void testFilenameRegexFilter() {
+ final Pattern pattern = Pattern.compile("[a-z]{1,6}");
+ final FilenameRegexFilter testFilter = new FilenameRegexFilter(pattern);
+ final String[] expectedToAccept = new String[] { "duckie", "goes", "quack" };
+ for (final String str : expectedToAccept) {
+ assertTrue("Filename, " + str + ", matching regex expected to accept", testFilter.accept(testFile, str));
+ }
+
+ final String[] notExpectedToAccept = new String[] { "DUCKIE", "1337", "2fast" };
+ for (final String str : notExpectedToAccept) {
+ assertFalse("Filename, " + str + ", not matching regex not expected to accept", testFilter.accept(testFile, str));
+ }
+ }
+
+ @Test
+ public void testFileLastModifiedComparator() {
+ final FileLastModifiedComparator testComparator = new FileLastModifiedComparator();
+ final File oldFile = mock(File.class);
+ final File newFile = mock(File.class);
+ final File equallyNewFile = mock(File.class);
+ when(oldFile.lastModified()).thenReturn(10L);
+ when(newFile.lastModified()).thenReturn(100L);
+ when(equallyNewFile.lastModified()).thenReturn(100L);
+
+ assertTrue("Old file is less than new file", testComparator.compare(oldFile, newFile) < 0);
+ assertTrue("New file is greater than old file", testComparator.compare(newFile, oldFile) > 0);
+ assertTrue("New files are equal", testComparator.compare(newFile, equallyNewFile) == 0);
+ }
+
+ @Test
+ public void testCreateTempDir() throws Exception {
+ String prefix = "tmp";
+ File directory = tempDir.newFolder();
+ File tempDir1 = FileUtils.createTempDir(directory, prefix);
+ File tempDir2 = FileUtils.createTempDir(directory, prefix);
+
+ assertThat(tempDir1, not(nullValue()));
+ assertThat(tempDir1.isDirectory(), is(true));
+ assertThat(tempDir2, not(nullValue()));
+ assertThat(tempDir2.isDirectory(), is(true));
+ assertThat(tempDir1.getAbsolutePath(), is(not(tempDir2.getAbsolutePath())));
+ }
+}
diff --git a/mobile/android/geckoview/src/test/java/org/mozilla/gecko/util/TestFloatUtils.java b/mobile/android/geckoview/src/test/java/org/mozilla/gecko/util/TestFloatUtils.java
new file mode 100644
index 0000000000..6540110eae
--- /dev/null
+++ b/mobile/android/geckoview/src/test/java/org/mozilla/gecko/util/TestFloatUtils.java
@@ -0,0 +1,51 @@
+/*
+ * 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 org.junit.Test;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotEquals;
+import static org.junit.Assert.assertTrue;
+
+
+/**
+ * Unit tests for float utilities.
+ */
+public class TestFloatUtils {
+
+ @Test
+ public void testEqualIfComparingZeros() {
+ assertTrue(FloatUtils.fuzzyEquals(0, 0));
+ }
+
+ @Test
+ public void testEqualFailIf5thDigitIsDifferent() {
+ assertFalse(FloatUtils.fuzzyEquals(0.00001f, 0.00002f));
+ }
+
+ @Test
+ public void testEqualSuccessIf6thDigitIsDifferent() {
+ assertTrue(FloatUtils.fuzzyEquals(0.000001f, 0.000002f));
+ }
+
+ @Test
+ public void testEqualFail() {
+ assertFalse(FloatUtils.fuzzyEquals(10, 0));
+ }
+
+ @Test
+ public void testEqualSuccessIfPromoted() {
+ assertTrue(FloatUtils.fuzzyEquals(5, 5));
+ }
+
+ @Test
+ public void testEqualSuccessIfUnPromoted() {
+ assertTrue(FloatUtils.fuzzyEquals(5.6f, 5.6f));
+ }
+}
diff --git a/mobile/android/geckoview/src/test/java/org/mozilla/gecko/util/TestIntentUtils.java b/mobile/android/geckoview/src/test/java/org/mozilla/gecko/util/TestIntentUtils.java
new file mode 100644
index 0000000000..b005d17e85
--- /dev/null
+++ b/mobile/android/geckoview/src/test/java/org/mozilla/gecko/util/TestIntentUtils.java
@@ -0,0 +1,74 @@
+/*
+ * 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.mozglue.SafeIntent;
+import org.robolectric.RobolectricTestRunner;
+
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
+
+import static org.junit.Assert.*;
+
+/**
+ * Tests for the Intent utilities.
+ */
+@RunWith(RobolectricTestRunner.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(new SafeIntent(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);
+ }
+ }
+}
diff --git a/mobile/android/geckoview/src/test/java/org/mozilla/gecko/util/TestStringUtils.java b/mobile/android/geckoview/src/test/java/org/mozilla/gecko/util/TestStringUtils.java
new file mode 100644
index 0000000000..af844c6e38
--- /dev/null
+++ b/mobile/android/geckoview/src/test/java/org/mozilla/gecko/util/TestStringUtils.java
@@ -0,0 +1,174 @@
+/* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*-
+ * 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 org.junit.Test;
+import org.junit.runner.RunWith;
+import org.robolectric.RobolectricTestRunner;
+
+import java.util.Arrays;
+import java.util.Collections;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+@RunWith(RobolectricTestRunner.class)
+public class TestStringUtils {
+ @Test
+ public void testIsHttpOrHttps() {
+ // No value
+ assertFalse(StringUtils.isHttpOrHttps(null));
+ assertFalse(StringUtils.isHttpOrHttps(""));
+
+ // Garbage
+ assertFalse(StringUtils.isHttpOrHttps("lksdjflasuf"));
+
+ // URLs with http/https
+ assertTrue(StringUtils.isHttpOrHttps("https://www.google.com"));
+ assertTrue(StringUtils.isHttpOrHttps("http://www.facebook.com"));
+ assertTrue(StringUtils.isHttpOrHttps("https://mozilla.org/en-US/firefox/products/"));
+
+ // IP addresses
+ assertTrue(StringUtils.isHttpOrHttps("https://192.168.0.1"));
+ assertTrue(StringUtils.isHttpOrHttps("http://63.245.215.20/en-US/firefox/products"));
+
+ // Other protocols
+ assertFalse(StringUtils.isHttpOrHttps("ftp://people.mozilla.org"));
+ assertFalse(StringUtils.isHttpOrHttps("javascript:window.google.com"));
+ assertFalse(StringUtils.isHttpOrHttps("tel://1234567890"));
+
+ // No scheme
+ assertFalse(StringUtils.isHttpOrHttps("google.com"));
+ assertFalse(StringUtils.isHttpOrHttps("git@github.com:mozilla/gecko-dev.git"));
+ }
+
+ @Test
+ public void testStripRef() {
+ assertEquals(StringUtils.stripRef(null), null);
+ assertEquals(StringUtils.stripRef(""), "");
+
+ assertEquals(StringUtils.stripRef("??AAABBBCCC"), "??AAABBBCCC");
+ assertEquals(StringUtils.stripRef("https://mozilla.org"), "https://mozilla.org");
+ assertEquals(StringUtils.stripRef("https://mozilla.org#BBBB"), "https://mozilla.org");
+ assertEquals(StringUtils.stripRef("https://mozilla.org/#BBBB"), "https://mozilla.org/");
+ }
+
+ @Test
+ public void testStripScheme() {
+ assertEquals("mozilla.org", StringUtils.stripScheme("http://mozilla.org"));
+ assertEquals("mozilla.org", StringUtils.stripScheme("http://mozilla.org/"));
+ assertEquals("https://mozilla.org", StringUtils.stripScheme("https://mozilla.org"));
+ assertEquals("https://mozilla.org", StringUtils.stripScheme("https://mozilla.org/"));
+ assertEquals("mozilla.org", StringUtils.stripScheme("https://mozilla.org/", StringUtils.UrlFlags.STRIP_HTTPS));
+ assertEquals("mozilla.org", StringUtils.stripScheme("https://mozilla.org", StringUtils.UrlFlags.STRIP_HTTPS));
+ assertEquals("", StringUtils.stripScheme("http://"));
+ assertEquals("", StringUtils.stripScheme("https://", StringUtils.UrlFlags.STRIP_HTTPS));
+ // This edge case is not handled properly yet
+// assertEquals(StringUtils.stripScheme("https://"), "");
+ assertEquals(null, StringUtils.stripScheme(null));
+ }
+
+ @Test
+ public void testIsRTL() {
+ assertFalse(StringUtils.isRTL("mozilla.org"));
+ assertFalse(StringUtils.isRTL("something.عربي"));
+
+ assertTrue(StringUtils.isRTL("عربي"));
+ assertTrue(StringUtils.isRTL("عربي.org"));
+
+ // Text with LTR mark
+ assertFalse(StringUtils.isRTL("\u200EHello"));
+ assertFalse(StringUtils.isRTL("\u200Eعربي"));
+ }
+
+ @Test
+ public void testForceLTR() {
+ assertFalse(StringUtils.isRTL(StringUtils.forceLTR("عربي")));
+ assertFalse(StringUtils.isRTL(StringUtils.forceLTR("عربي.org")));
+
+ // Strings that are already LTR are not modified
+ final String someLtrString = "HelloWorld";
+ assertEquals(someLtrString, StringUtils.forceLTR(someLtrString));
+
+ // We add the LTR mark only once
+ final String someRtlString = "عربي";
+ assertEquals(4, someRtlString.length());
+ final String forcedLtrString = StringUtils.forceLTR(someRtlString);
+ assertEquals(5, forcedLtrString.length());
+ final String forcedAgainLtrString = StringUtils.forceLTR(forcedLtrString);
+ assertEquals(5, forcedAgainLtrString.length());
+ }
+
+ @Test
+ public void testIsSearchQuery(){
+ boolean any = true;
+ // test trim
+ assertFalse(StringUtils.isSearchQuery("",false));
+ assertTrue(StringUtils.isSearchQuery("",true));
+
+ // test space
+ assertTrue(StringUtils.isSearchQuery(" apple pen ",any));
+ assertTrue(StringUtils.isSearchQuery("pineapple pen",any));
+ assertTrue(StringUtils.isSearchQuery(": :",any));
+ assertTrue(StringUtils.isSearchQuery(". .",any));
+ assertTrue(StringUtils.isSearchQuery("gcm site:stackoverflow.com",any));
+ assertTrue(StringUtils.isSearchQuery("/mnt/etc/resolv.conf does not exist",true));
+
+ // test colon
+ assertFalse(StringUtils.isSearchQuery(":",any));
+ assertFalse(StringUtils.isSearchQuery("site:stackoverflow.com",any));
+ assertFalse(StringUtils.isSearchQuery("http:mozilla.com",any));
+ assertFalse(StringUtils.isSearchQuery("http://mozilla.com",any));
+ assertFalse(StringUtils.isSearchQuery("http:/mozilla.com",any));
+
+ // test dot
+ assertFalse(StringUtils.isSearchQuery(".",any));
+ assertFalse(StringUtils.isSearchQuery("cd..",any));
+ assertFalse(StringUtils.isSearchQuery("cd...",any));
+ assertFalse(StringUtils.isSearchQuery("mozilla.com",any));
+
+
+ // test ambiguous
+ String ambiguous = "~!@#$%^&*()_+`34567890-=qwertyuiop[]\\QWERTYUIOP{}|asdfghjkl;'ASDFGHJKL:\"ZXCVBNM<>?zxcvbnm,./";
+ ambiguous = ambiguous.replace(" ","").replace(".","").replace(":","");
+ assertTrue(StringUtils.isSearchQuery(ambiguous,true));
+ assertFalse(StringUtils.isSearchQuery(ambiguous,false));
+
+
+ }
+
+ @Test
+ public void testQueryExists(){
+ // test empty
+ assertFalse(StringUtils.queryExists(""));
+
+ // test single
+ assertFalse(StringUtils.queryExists("mozilla.org"));
+ assertFalse(StringUtils.queryExists("https://www.google.com/"));
+ assertTrue(StringUtils.queryExists("https://www.google.com/search?q=%s"));
+ assertTrue(StringUtils.queryExists("https://www.google.com/search?q=%S"));
+ assertTrue(StringUtils.queryExists("%s"));
+ assertTrue(StringUtils.queryExists("%S"));
+
+ //test double
+ assertTrue(StringUtils.queryExists("%s%S"));
+ assertTrue(StringUtils.queryExists("https://www.google.com/search?q=%s%S"));
+ }
+
+ @Test
+ public void testPathStartIndex(){
+ // Tests without protocol
+ assertTrue(StringUtils.pathStartIndex("mozilla.org") == -1);
+ assertTrue(StringUtils.pathStartIndex("mozilla.org/en-US") == 11);
+
+ // Tests with protocol
+ assertTrue(StringUtils.pathStartIndex("https://mozilla.org") == -1);
+ assertTrue(StringUtils.pathStartIndex("https://mozilla.org/") == 19);
+ assertTrue(StringUtils.pathStartIndex("https://mozilla.org/en-US") == 19);
+
+ }
+}
diff --git a/mobile/android/geckoview/src/test/java/org/mozilla/gecko/util/TestUUIDUtil.java b/mobile/android/geckoview/src/test/java/org/mozilla/gecko/util/TestUUIDUtil.java
new file mode 100644
index 0000000000..b98e769d01
--- /dev/null
+++ b/mobile/android/geckoview/src/test/java/org/mozilla/gecko/util/TestUUIDUtil.java
@@ -0,0 +1,48 @@
+/*
+ * 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 org.junit.Test;
+
+import static org.junit.Assert.*;
+
+/**
+ * Tests for uuid utils.
+ */
+public class TestUUIDUtil {
+ private static final String[] validUUIDs = {
+ "904cd9f8-af63-4525-8ce0-b9127e5364fa",
+ "8d584bd2-00ea-4043-a617-ed4ce7018ed0",
+ "3abad327-2669-4f68-b9ef-7ace8c5314d6",
+ };
+
+ private static final String[] invalidUUIDs = {
+ "its-not-a-uuid-mate",
+ "904cd9f8-af63-4525-8ce0-b9127e5364falol",
+ "904cd9f8-af63-4525-8ce0-b9127e5364f",
+ };
+
+ @Test
+ public void testUUIDRegex() {
+ for (final String uuid : validUUIDs) {
+ assertTrue("Valid UUID matches UUID-regex", uuid.matches(UUIDUtil.UUID_REGEX));
+ }
+ for (final String uuid : invalidUUIDs) {
+ assertFalse("Invalid UUID does not match UUID-regex", uuid.matches(UUIDUtil.UUID_REGEX));
+ }
+ }
+
+ @Test
+ public void testUUIDPattern() {
+ for (final String uuid : validUUIDs) {
+ assertTrue("Valid UUID matches UUID-regex", UUIDUtil.UUID_PATTERN.matcher(uuid).matches());
+ }
+ for (final String uuid : invalidUUIDs) {
+ assertFalse("Invalid UUID does not match UUID-regex", UUIDUtil.UUID_PATTERN.matcher(uuid).matches());
+ }
+ }
+}