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

--HG--
extra : rebase_source : 30820e83d91df185cc70155bb528c4bbe9316869
This commit is contained in:
Julian_Chu 2017-02-02 11:56:37 +08:00
Родитель 40d3b48040
Коммит b779ec64b8
2 изменённых файлов: 154 добавлений и 0 удалений

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

@ -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());
}
}

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

@ -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));
}
}