Bug 1329145 - Part3: add test cases r=sebastian
authorJulian_Chu <walkingice0204@gmail.com>
Thu, 02 Feb 2017 11:56:37 +0800
changeset 469924 f10e4c47689971e36b8d38525043cfd353db0172
parent 469923 e627854c8ae505173fa951587b5cc0691f5fc9e4
child 469925 bcabeb8c81a266a76bea170b3482bde25ca65c8a
push id43881
push userbmo:gps@mozilla.com
push dateThu, 02 Feb 2017 23:49:03 +0000
reviewerssebastian
bugs1329145
milestone54.0a1
Bug 1329145 - Part3: add test cases r=sebastian To test different situations * Intent has custom animation * Intent has no custom animation MozReview-Commit-ID: 9KqBJu9x4nw
mobile/android/tests/background/junit4/src/org/mozilla/gecko/customtabs/TestCustomTabsActivity.java
mobile/android/tests/background/junit4/src/org/mozilla/gecko/customtabs/TestIntentUtil.java
new file mode 100644
--- /dev/null
+++ b/mobile/android/tests/background/junit4/src/org/mozilla/gecko/customtabs/TestCustomTabsActivity.java
@@ -0,0 +1,94 @@
+/* Any copyright is dedicated to the Public Domain.
+   http://creativecommons.org/publicdomain/zero/1.0/ */
+
+package org.mozilla.gecko.customtabs;
+
+import android.content.Context;
+import android.content.Intent;
+import android.support.annotation.AnimRes;
+import android.support.customtabs.CustomTabsIntent;
+
+import junit.framework.Assert;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.internal.util.reflection.Whitebox;
+import org.mozilla.gecko.background.testhelpers.TestRunner;
+import org.robolectric.RuntimeEnvironment;
+
+import static org.mockito.Matchers.anyInt;
+import static org.mockito.Matchers.eq;
+import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.spy;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+
+@RunWith(TestRunner.class)
+public class TestCustomTabsActivity {
+
+    private static final String THIRD_PARTY_PACKAGE_NAME = "mozilla.unit.test";
+    private Context spyContext;  // 3rd party app context
+    private CustomTabsActivity spyActivity;
+
+    @AnimRes
+    private final int enterRes = 0x123; // arbitrary number as animation resource id
+    @AnimRes
+    private final int exitRes = 0x456; // arbitrary number as animation resource id
+
+    @Before
+    public void setUp() {
+        spyContext = spy(RuntimeEnvironment.application);
+        doReturn(THIRD_PARTY_PACKAGE_NAME).when(spyContext).getPackageName();
+
+        spyActivity = spy(new CustomTabsActivity());
+
+        final CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder();
+        builder.setExitAnimations(spyContext, enterRes, exitRes);
+        final Intent i = builder.build().intent;
+    }
+
+    /**
+     * Activity should not call overridePendingTransition if custom animation does not exist.
+     */
+    @Test
+    public void testFinishWithoutCustomAnimation() {
+        final CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder();
+        final Intent i = builder.build().intent;
+
+        doReturn(i).when(spyActivity).getIntent();
+
+        spyActivity.finish();
+        verify(spyActivity, times(0)).overridePendingTransition(anyInt(), anyInt());
+    }
+
+    /**
+     * Activity should call overridePendingTransition if custom animation exists.
+     */
+    @Test
+    public void testFinish() {
+        final CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder();
+        builder.setExitAnimations(spyContext, enterRes, exitRes);
+        final Intent i = builder.build().intent;
+
+        doReturn(i).when(spyActivity).getIntent();
+
+        spyActivity.finish();
+        verify(spyActivity, times(1)).overridePendingTransition(eq(enterRes), eq(exitRes));
+    }
+
+    /**
+     * To get 3rd party app's package name, if custom animation exists.
+     */
+    @Test
+    public void testGetPackageName() {
+        final CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder();
+        builder.setExitAnimations(spyContext, enterRes, exitRes);
+        final Intent i = builder.build().intent;
+
+        doReturn(i).when(spyActivity).getIntent();
+        Whitebox.setInternalState(spyActivity, "usingCustomAnimation", true);
+
+        Assert.assertEquals(THIRD_PARTY_PACKAGE_NAME, spyActivity.getPackageName());
+    }
+}
\ No newline at end of file
new file mode 100644
--- /dev/null
+++ b/mobile/android/tests/background/junit4/src/org/mozilla/gecko/customtabs/TestIntentUtil.java
@@ -0,0 +1,60 @@
+/* Any copyright is dedicated to the Public Domain.
+   http://creativecommons.org/publicdomain/zero/1.0/ */
+
+package org.mozilla.gecko.customtabs;
+
+import android.content.Context;
+import android.content.Intent;
+import android.support.annotation.AnimRes;
+import android.support.customtabs.CustomTabsIntent;
+
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mozilla.gecko.background.testhelpers.TestRunner;
+import org.robolectric.RuntimeEnvironment;
+
+import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.spy;
+
+@RunWith(TestRunner.class)
+public class TestIntentUtil {
+
+    private static final String THIRD_PARTY_PACKAGE_NAME = "mozilla.unit.test";
+    private Context spyContext;  // 3rd party app context
+
+    @Before
+    public void setUp() {
+        spyContext = spy(RuntimeEnvironment.application);
+        doReturn(THIRD_PARTY_PACKAGE_NAME).when(spyContext).getPackageName();
+    }
+
+    @Test
+    public void testIntentWithCustomAnimation() {
+        @AnimRes final int enterRes = 0x123; // arbitrary number as animation resource id
+        @AnimRes final int exitRes = 0x456; // arbitrary number as animation resource id
+
+        final CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder();
+        builder.setExitAnimations(spyContext, enterRes, exitRes);
+        final Intent i = builder.build().intent;
+
+        Assert.assertEquals(true, IntentUtil.hasExitAnimation(i));
+        Assert.assertEquals(THIRD_PARTY_PACKAGE_NAME, IntentUtil.getAnimationPackageName(i));
+        Assert.assertEquals(enterRes, IntentUtil.getEnterAnimationRes(i));
+        Assert.assertEquals(exitRes, IntentUtil.getExitAnimationRes(i));
+    }
+
+    @Test
+    public void testIntentWithoutCustomAnimation() {
+        final CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder();
+        final Intent i = builder.build().intent;
+
+        Assert.assertEquals(false, IntentUtil.hasExitAnimation(i));
+        Assert.assertEquals(null, IntentUtil.getAnimationPackageName(i));
+        Assert.assertEquals(IntentUtil.NO_ANIMATION_RESOURCE,
+                IntentUtil.getEnterAnimationRes(i));
+        Assert.assertEquals(IntentUtil.NO_ANIMATION_RESOURCE,
+                IntentUtil.getExitAnimationRes(i));
+    }
+}