Bug 1329145 - Part3: add test cases draft
authorJulian_Chu <walkingice0204@gmail.com>
Thu, 02 Feb 2017 11:56:37 +0800
changeset 469533 7f5845146485a05113326e755b37a15165eddce1
parent 469532 0118aafb57323ac3335d2b3b682be18a2c87987e
child 544227 55acea01018f36666710ba63d1ac402cfbbb2f61
push id43756
push userbmo:walkingice0204@gmail.com
push dateThu, 02 Feb 2017 08:43:17 +0000
bugs1329145
milestone53.0a1
Bug 1329145 - Part3: add test cases 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));
+    }
+}