Commit 5dd7214f authored by Lijin Shen's avatar Lijin Shen Committed by Commit Bot

Add new unit tests for Default promo

Add new unit tests for testing whether Util can decide promo action
correctly based on the given external states by mocking Deps.

Bug: 1109255
Change-Id: I3e7f653e4c156b2fadcdbb605ce40ecda3a738f3
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2396601
Commit-Queue: Lijin Shen <lazzzis@google.com>
Reviewed-by: default avatarPavel Yatsuk <pavely@chromium.org>
Cr-Commit-Position: refs/heads/master@{#805106}
parent 37fd7712
...@@ -4,13 +4,18 @@ ...@@ -4,13 +4,18 @@
package org.chromium.chrome.browser.ui.default_browser_promo; package org.chromium.chrome.browser.ui.default_browser_promo;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.when;
import android.content.pm.ActivityInfo; import android.content.pm.ActivityInfo;
import android.content.pm.ResolveInfo; import android.content.pm.ResolveInfo;
import android.os.Build;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations; import org.mockito.MockitoAnnotations;
import org.robolectric.RuntimeEnvironment; import org.robolectric.RuntimeEnvironment;
import org.robolectric.Shadows; import org.robolectric.Shadows;
...@@ -20,6 +25,7 @@ import org.robolectric.shadows.ShadowPackageManager; ...@@ -20,6 +25,7 @@ import org.robolectric.shadows.ShadowPackageManager;
import org.chromium.base.ContextUtils; import org.chromium.base.ContextUtils;
import org.chromium.base.PackageManagerUtils; import org.chromium.base.PackageManagerUtils;
import org.chromium.base.test.BaseRobolectricTestRunner; import org.chromium.base.test.BaseRobolectricTestRunner;
import org.chromium.chrome.browser.ui.default_browser_promo.DefaultBrowserPromoUtils.DefaultBrowserPromoAction;
import org.chromium.chrome.browser.ui.default_browser_promo.DefaultBrowserPromoUtils.DefaultBrowserState; import org.chromium.chrome.browser.ui.default_browser_promo.DefaultBrowserPromoUtils.DefaultBrowserState;
import java.util.ArrayList; import java.util.ArrayList;
...@@ -31,6 +37,9 @@ import java.util.List; ...@@ -31,6 +37,9 @@ import java.util.List;
@RunWith(BaseRobolectricTestRunner.class) @RunWith(BaseRobolectricTestRunner.class)
@Config(manifest = Config.NONE) @Config(manifest = Config.NONE)
public class DefaultBrowserPromoUtilsTest { public class DefaultBrowserPromoUtilsTest {
@Mock
DefaultBrowserPromoDeps mDeps;
@Before @Before
public void setUp() { public void setUp() {
MockitoAnnotations.initMocks(this); MockitoAnnotations.initMocks(this);
...@@ -105,6 +114,167 @@ public class DefaultBrowserPromoUtilsTest { ...@@ -105,6 +114,167 @@ public class DefaultBrowserPromoUtilsTest {
createResolveInfo("com.android.chrome.random.string", 1))); createResolveInfo("com.android.chrome.random.string", 1)));
} }
@Test
public void testBasicPromo() {
setDepsMockWithDefaultValues();
Assert.assertEquals("Should promo disambiguation sheet on P.",
DefaultBrowserPromoAction.DISAMBIGUATION_SHEET,
DefaultBrowserPromoUtils.decideNextAction(mDeps, null));
}
// --- Q above ---
@Test
public void testPromo_Q_No_Default() {
setDepsMockWithDefaultValues();
when(mDeps.getSDKInt()).thenReturn(Build.VERSION_CODES.Q);
when(mDeps.isRoleAvailable(any())).thenReturn(true);
Assert.assertEquals("Should promo role manager when there is no default browser on Q+.",
DefaultBrowserPromoAction.ROLE_MANAGER,
DefaultBrowserPromoUtils.decideNextAction(mDeps, null));
}
@Test
public void testPromo_Q_Other_Default() {
setDepsMockWithDefaultValues();
when(mDeps.getSDKInt()).thenReturn(Build.VERSION_CODES.Q);
when(mDeps.isRoleAvailable(any())).thenReturn(true);
when(mDeps.getCurrentDefaultBrowserState(any()))
.thenReturn(DefaultBrowserState.OTHER_DEFAULT);
Assert.assertEquals(
"Should promo role manager when there is another default browser on Q+.",
DefaultBrowserPromoAction.ROLE_MANAGER,
DefaultBrowserPromoUtils.decideNextAction(mDeps, null));
}
// --- P below ---
@Test
public void testPromo_P_otherDefaultSystemSettings() {
setDepsMockWithDefaultValues();
when(mDeps.getCurrentDefaultBrowserState(any()))
.thenReturn(DefaultBrowserState.OTHER_DEFAULT);
when(mDeps.promoActionOnP()).thenReturn(DefaultBrowserPromoAction.SYSTEM_SETTINGS);
Assert.assertEquals(
"Should promo system settings when there is another default browser on P-.",
DefaultBrowserPromoAction.SYSTEM_SETTINGS,
DefaultBrowserPromoUtils.decideNextAction(mDeps, null));
}
@Test
public void testNoPromo_P_noDefaultNoSystemSettings() {
setDepsMockWithDefaultValues();
when(mDeps.getCurrentDefaultBrowserState(any())).thenReturn(DefaultBrowserState.NO_DEFAULT);
when(mDeps.promoActionOnP()).thenReturn(DefaultBrowserPromoAction.SYSTEM_SETTINGS);
when(mDeps.doesManageDefaultAppsSettingsActivityExist()).thenReturn(false);
Assert.assertEquals(
"Should not promo system settings on P- when target system setting is not available.",
DefaultBrowserPromoAction.NO_ACTION,
DefaultBrowserPromoUtils.decideNextAction(mDeps, null));
}
@Test
public void testNoPromo_P_noDefaultDisabled() {
setDepsMockWithDefaultValues();
when(mDeps.getCurrentDefaultBrowserState(any())).thenReturn(DefaultBrowserState.NO_DEFAULT);
when(mDeps.promoActionOnP()).thenReturn(DefaultBrowserPromoAction.NO_ACTION);
Assert.assertEquals(
"Should not promo on P- when promoing on \'no default\' scenario is disabled.",
DefaultBrowserPromoAction.NO_ACTION,
DefaultBrowserPromoUtils.decideNextAction(mDeps, null));
}
@Test
public void testNoPromo_otherDefault_M() {
setDepsMockWithDefaultValues();
when(mDeps.getSDKInt()).thenReturn(Build.VERSION_CODES.M);
when(mDeps.doesManageDefaultAppsSettingsActivityExist()).thenReturn(false);
when(mDeps.getCurrentDefaultBrowserState(any()))
.thenReturn(DefaultBrowserState.OTHER_DEFAULT);
Assert.assertEquals("Should not promo on M- when there is another default browser.",
DefaultBrowserPromoAction.NO_ACTION,
DefaultBrowserPromoUtils.decideNextAction(mDeps, null));
}
// --- prerequisites ---
@Test
public void testPromo_increasedPromoCount() {
setDepsMockWithDefaultValues();
when(mDeps.getMaxPromoCount()).thenReturn(100);
when(mDeps.getPromoCount()).thenReturn(99);
Assert.assertNotEquals("Should promo when promo count does not reach the upper limit.",
DefaultBrowserPromoAction.NO_ACTION,
DefaultBrowserPromoUtils.decideNextAction(mDeps, null));
}
@Test
public void testNoPromo_greaterThanMaxPromoCount() {
setDepsMockWithDefaultValues();
when(mDeps.getPromoCount()).thenReturn(1);
when(mDeps.getMaxPromoCount()).thenReturn(1);
Assert.assertEquals("Should not promo when promo count reaches the upper limit.",
DefaultBrowserPromoAction.NO_ACTION,
DefaultBrowserPromoUtils.decideNextAction(mDeps, null));
}
@Test
public void testNoPromo_featureDisabled() {
setDepsMockWithDefaultValues();
when(mDeps.isFeatureEnabled()).thenReturn(false);
Assert.assertEquals("Should not promo when the fearure is disabled.",
DefaultBrowserPromoAction.NO_ACTION,
DefaultBrowserPromoUtils.decideNextAction(mDeps, null));
}
@Test
public void testNoPromo_lessThanMinSessionCount() {
setDepsMockWithDefaultValues();
when(mDeps.getSessionCount()).thenReturn(1);
when(mDeps.getMinSessionCount()).thenReturn(3);
Assert.assertEquals(
"Should not promo when session count has not reached the required amount.",
DefaultBrowserPromoAction.NO_ACTION,
DefaultBrowserPromoUtils.decideNextAction(mDeps, null));
}
@Test
public void testNoPromo_isOtherChromeDefault() {
setDepsMockWithDefaultValues();
when(mDeps.isCurrentDefaultBrowserChrome(any())).thenReturn(true);
when(mDeps.getCurrentDefaultBrowserState(any()))
.thenReturn(DefaultBrowserState.OTHER_DEFAULT);
Assert.assertEquals(
"Should not promo when another chrome channel browser has been default.",
DefaultBrowserPromoAction.NO_ACTION,
DefaultBrowserPromoUtils.decideNextAction(mDeps, null));
}
@Test
public void testNoPromo_isCurrentChromeDefault() {
setDepsMockWithDefaultValues();
when(mDeps.isCurrentDefaultBrowserChrome(any())).thenReturn(true);
when(mDeps.getCurrentDefaultBrowserState(any()))
.thenReturn(DefaultBrowserState.CHROME_DEFAULT);
Assert.assertEquals("Should not promo when chrome has been default.",
DefaultBrowserPromoAction.NO_ACTION,
DefaultBrowserPromoUtils.decideNextAction(mDeps, null));
}
private void setDepsMockWithDefaultValues() {
when(mDeps.isFeatureEnabled()).thenReturn(true);
when(mDeps.getMinSessionCount()).thenReturn(3);
when(mDeps.getSessionCount()).thenReturn(10);
when(mDeps.doesManageDefaultAppsSettingsActivityExist()).thenReturn(true);
when(mDeps.getSDKInt()).thenReturn(Build.VERSION_CODES.P);
when(mDeps.promoActionOnP()).thenReturn(DefaultBrowserPromoAction.DISAMBIGUATION_SHEET);
when(mDeps.isChromeStable()).thenReturn(false);
when(mDeps.getPromoCount()).thenReturn(0);
when(mDeps.getMaxPromoCount()).thenReturn(1);
when(mDeps.getLastPromoInterval()).thenReturn(1000);
when(mDeps.getMinPromoInterval()).thenReturn(10);
when(mDeps.isChromePreStableInstalled()).thenReturn(false);
when(mDeps.isCurrentDefaultBrowserChrome(any())).thenReturn(false);
when(mDeps.getCurrentDefaultBrowserState(any())).thenReturn(DefaultBrowserState.NO_DEFAULT);
}
private ResolveInfo createResolveInfo(String packageName, int match) { private ResolveInfo createResolveInfo(String packageName, int match) {
ResolveInfo resolveInfo = new ResolveInfo(); ResolveInfo resolveInfo = new ResolveInfo();
ActivityInfo activityInfo = new ActivityInfo(); ActivityInfo activityInfo = new ActivityInfo();
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment