From 6bf0a5cb5034a7e684dcc3500e841785237ce2dd Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 7 Apr 2024 19:32:43 +0200 Subject: Adding upstream version 1:115.7.0. Signed-off-by: Daniel Baumann --- .../org/mozilla/gecko/util/GeckoBundleTest.java | 745 +++++++++++++++++++++ .../org/mozilla/gecko/util/IntentUtilsTest.java | 66 ++ .../org/mozilla/gecko/util/NetworkUtilsTest.java | 215 ++++++ 3 files changed, 1026 insertions(+) create mode 100644 mobile/android/geckoview/src/test/java/org/mozilla/gecko/util/GeckoBundleTest.java create mode 100644 mobile/android/geckoview/src/test/java/org/mozilla/gecko/util/IntentUtilsTest.java create mode 100644 mobile/android/geckoview/src/test/java/org/mozilla/gecko/util/NetworkUtilsTest.java (limited to 'mobile/android/geckoview/src/test') 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..8ef19ca696 --- /dev/null +++ b/mobile/android/geckoview/src/test/java/org/mozilla/gecko/util/GeckoBundleTest.java @@ -0,0 +1,745 @@ +/* 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 java.util.Arrays; +import java.util.List; +import org.json.JSONException; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.robolectric.RobolectricTestRunner; + +@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 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..24315ff585 --- /dev/null +++ b/mobile/android/geckoview/src/test/java/org/mozilla/gecko/util/IntentUtilsTest.java @@ -0,0 +1,66 @@ +/* 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.test.suitebuilder.annotation.SmallTest; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.robolectric.RobolectricTestRunner; + +@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)); + } + + @Test + public void unsafeHtmlEncodedTelUri() { + assertFalse(IntentUtils.isUriSafeForScheme("tel:*%2306%23")); + assertFalse(IntentUtils.isUriSafeForScheme("tel:%2A%2306%23")); + } + + @Test + public void intentDataWithoutScheme() { + final String uri = "intent:non_scheme_intent#Intent;end"; + assertTrue(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..f5033041e3 --- /dev/null +++ b/mobile/android/geckoview/src/test/java/org/mozilla/gecko/util/NetworkUtilsTest.java @@ -0,0 +1,215 @@ +/* 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.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +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; + +@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 + final 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 + final 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)); + } +} -- cgit v1.2.3