Commit 5029d57d authored by Rohit Agarwal's avatar Rohit Agarwal Committed by Commit Bot

Add browser tests for Incognito CCTs

Bug: 1023759
Change-Id: I33aec2a344314ad1576776a494d97f1a9cef38e6
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2039511
Commit-Queue: Rohit Agarwal <roagarwal@chromium.org>
Reviewed-by: default avatarPeter Conn <peconn@chromium.org>
Cr-Commit-Position: refs/heads/master@{#744991}
parent 4fdee839
...@@ -4,6 +4,10 @@ ...@@ -4,6 +4,10 @@
package org.chromium.chrome.browser.customtabs; package org.chromium.chrome.browser.customtabs;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import android.content.Intent; import android.content.Intent;
import android.graphics.Color; import android.graphics.Color;
import android.support.test.InstrumentationRegistry; import android.support.test.InstrumentationRegistry;
...@@ -12,13 +16,14 @@ import android.view.MenuItem; ...@@ -12,13 +16,14 @@ import android.view.MenuItem;
import androidx.browser.customtabs.CustomTabsIntent; import androidx.browser.customtabs.CustomTabsIntent;
import org.junit.Assert;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
import org.junit.rules.TestRule;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.chromium.base.Callback; import org.chromium.base.Callback;
import org.chromium.base.test.util.CommandLineFlags; import org.chromium.base.test.util.CommandLineFlags;
import org.chromium.base.test.util.DisabledTest;
import org.chromium.base.test.util.Feature; import org.chromium.base.test.util.Feature;
import org.chromium.chrome.R; import org.chromium.chrome.R;
import org.chromium.chrome.browser.ChromeSwitches; import org.chromium.chrome.browser.ChromeSwitches;
...@@ -27,6 +32,7 @@ import org.chromium.chrome.browser.flags.ChromeFeatureList; ...@@ -27,6 +32,7 @@ import org.chromium.chrome.browser.flags.ChromeFeatureList;
import org.chromium.chrome.browser.incognito.IncognitoNotificationService; import org.chromium.chrome.browser.incognito.IncognitoNotificationService;
import org.chromium.chrome.browser.toolbar.top.CustomTabToolbar; import org.chromium.chrome.browser.toolbar.top.CustomTabToolbar;
import org.chromium.chrome.test.ChromeJUnit4ClassRunner; import org.chromium.chrome.test.ChromeJUnit4ClassRunner;
import org.chromium.chrome.test.util.browser.Features;
import org.chromium.components.browser_ui.styles.ChromeColors; import org.chromium.components.browser_ui.styles.ChromeColors;
import org.chromium.content_public.browser.test.util.CriteriaHelper; import org.chromium.content_public.browser.test.util.CriteriaHelper;
import org.chromium.content_public.browser.test.util.TestThreadUtils; import org.chromium.content_public.browser.test.util.TestThreadUtils;
...@@ -42,90 +48,101 @@ public class CustomTabActivityIncognitoTest { ...@@ -42,90 +48,101 @@ public class CustomTabActivityIncognitoTest {
@Rule @Rule
public CustomTabActivityTestRule mCustomTabActivityTestRule = new CustomTabActivityTestRule(); public CustomTabActivityTestRule mCustomTabActivityTestRule = new CustomTabActivityTestRule();
private CustomTabActivity mActivity; @Rule
public TestRule mJUnitProcessor = new Features.JUnitProcessor();
// TODO(https://crbug.com/1023759): Update all test to cover both cases @Test
// where CCT_INCOGNITO is enabled and disabled. @SmallTest
public boolean isEnabled() { @Features.EnableFeatures({ChromeFeatureList.CCT_INCOGNITO})
return ChromeFeatureList.isInitialized() public void launchesIncognitoWhenEnabled() throws Exception {
&& ChromeFeatureList.isEnabled(ChromeFeatureList.CCT_INCOGNITO); CustomTabActivity activity = launchIncognitoCustomTab();
assertTrue(activity.getActivityTab().isIncognito());
} }
@Test @Test
@SmallTest @SmallTest
public void tabIsIncognito() throws Exception { @Features.DisableFeatures({ChromeFeatureList.CCT_INCOGNITO})
if (!isEnabled()) return; public void doesntLaunchIncognitoWhenDisabled() throws Exception {
launchIncognitoCustomTab(); CustomTabActivity activity = launchIncognitoCustomTab();
assertFalse(activity.getActivityTab().isIncognito());
Assert.assertTrue(mActivity.getActivityTab().isIncognito());
} }
@Test @Test
@SmallTest @SmallTest
@Feature({"UiCatalogue"}) @Feature({"UiCatalogue"})
@Features.EnableFeatures({ChromeFeatureList.CCT_INCOGNITO})
public void toolbarHasIncognitoThemeColor() throws Exception { public void toolbarHasIncognitoThemeColor() throws Exception {
if (!isEnabled()) return; CustomTabActivity activity = launchIncognitoCustomTab();
launchIncognitoCustomTab(); assertEquals(getIncognitoThemeColor(activity), getToolbarColor(activity));
Assert.assertEquals(getIncognitoThemeColor(), getToolbarColor());
} }
@Test @Test
@SmallTest @SmallTest
@Features.EnableFeatures({ChromeFeatureList.CCT_INCOGNITO})
public void ignoresCustomizedToolbarColor() throws Exception { public void ignoresCustomizedToolbarColor() throws Exception {
if (!isEnabled()) return; CustomTabActivity activity = launchIncognitoCustomTab(
launchIncognitoCustomTab(
intent -> intent.putExtra(CustomTabsIntent.EXTRA_TOOLBAR_COLOR, Color.RED)); intent -> intent.putExtra(CustomTabsIntent.EXTRA_TOOLBAR_COLOR, Color.RED));
assertEquals(getIncognitoThemeColor(activity), getToolbarColor(activity));
Assert.assertEquals(getIncognitoThemeColor(), getToolbarColor());
} }
@Test @Test
@SmallTest @SmallTest
@Feature({"UiCatalogue"}) @Feature({"UiCatalogue"})
@Features.EnableFeatures({ChromeFeatureList.CCT_INCOGNITO})
public void openInBrowserMenuItemHasCorrectTitle() throws Exception { public void openInBrowserMenuItemHasCorrectTitle() throws Exception {
if (!isEnabled()) return; CustomTabActivity activity = launchIncognitoCustomTab();
launchIncognitoCustomTab(); CustomTabsTestUtils.openAppMenuAndAssertMenuShown(activity);
CustomTabsTestUtils.openAppMenuAndAssertMenuShown(mActivity);
String menuTitle = mCustomTabActivityTestRule.getMenu() String menuTitle = mCustomTabActivityTestRule.getMenu()
.findItem(R.id.open_in_browser_id) .findItem(R.id.open_in_browser_id)
.getTitle() .getTitle()
.toString(); .toString();
Assert.assertEquals(mActivity.getString(R.string.menu_open_in_incognito_chrome), menuTitle); assertEquals(activity.getString(R.string.menu_open_in_incognito_chrome), menuTitle);
} }
@Test @Test
@SmallTest @SmallTest
public void incognitoNotificationClosesCustomTab() throws Exception { @Features.EnableFeatures({ChromeFeatureList.CCT_INCOGNITO})
if (!isEnabled()) return; @DisabledTest
launchIncognitoCustomTab(); // TODO(crbug.com/1023759) : The test crashes in Android Kitkat and is flaky on marshmallow.
// Need to investigate.
IncognitoNotificationService.getRemoveAllIncognitoTabsIntent(mActivity) public void incognitoNotificationClosesIncognitoCustomTab() throws Exception {
CustomTabActivity activity = launchIncognitoCustomTab();
IncognitoNotificationService.getRemoveAllIncognitoTabsIntent(activity)
.getPendingIntent() .getPendingIntent()
.send(); .send();
CriteriaHelper.pollUiThread(activity::isFinishing);
CriteriaHelper.pollUiThread(mActivity::isFinishing);
} }
@Test @Test
@SmallTest @SmallTest
@Features.EnableFeatures({ChromeFeatureList.CCT_INCOGNITO})
public void doesNotHaveAddToHomeScreenMenuItem() throws Exception { public void doesNotHaveAddToHomeScreenMenuItem() throws Exception {
if (!isEnabled()) return; CustomTabActivity activity = launchIncognitoCustomTab();
launchIncognitoCustomTab(); CustomTabsTestUtils.openAppMenuAndAssertMenuShown(activity);
CustomTabsTestUtils.openAppMenuAndAssertMenuShown(mActivity);
MenuItem item = mCustomTabActivityTestRule.getMenu().findItem(R.id.add_to_homescreen_id); MenuItem item = mCustomTabActivityTestRule.getMenu().findItem(R.id.add_to_homescreen_id);
Assert.assertTrue(item == null || !item.isVisible()); assertTrue(item == null || !item.isVisible());
} }
private void launchIncognitoCustomTab() throws InterruptedException { private static int getIncognitoThemeColor(CustomTabActivity activity)
launchIncognitoCustomTab(null); throws ExecutionException {
return TestThreadUtils.runOnUiThreadBlocking(
() -> ChromeColors.getDefaultThemeColor(activity.getResources(), true));
} }
private void launchIncognitoCustomTab(Callback<Intent> additionalIntentModifications) private static int getToolbarColor(CustomTabActivity activity) throws ExecutionException {
throws InterruptedException { return TestThreadUtils.runOnUiThreadBlocking(() -> {
CustomTabToolbar toolbar = activity.findViewById(R.id.toolbar);
return toolbar.getBackground().getColor();
});
}
private CustomTabActivity launchIncognitoCustomTab() throws InterruptedException {
return launchIncognitoCustomTab(null);
}
private CustomTabActivity launchIncognitoCustomTab(
Callback<Intent> additionalIntentModifications) throws InterruptedException {
Intent intent = CustomTabsTestUtils.createMinimalCustomTabIntent( Intent intent = CustomTabsTestUtils.createMinimalCustomTabIntent(
InstrumentationRegistry.getTargetContext(), "http://www.google.com"); InstrumentationRegistry.getTargetContext(), "http://www.google.com");
...@@ -136,18 +153,6 @@ public class CustomTabActivityIncognitoTest { ...@@ -136,18 +153,6 @@ public class CustomTabActivityIncognitoTest {
mCustomTabActivityTestRule.startCustomTabActivityWithIntent(intent); mCustomTabActivityTestRule.startCustomTabActivityWithIntent(intent);
mActivity = mCustomTabActivityTestRule.getActivity(); return mCustomTabActivityTestRule.getActivity();
}
private int getIncognitoThemeColor() throws ExecutionException {
return TestThreadUtils.runOnUiThreadBlocking(
() -> ChromeColors.getDefaultThemeColor(mActivity.getResources(), true));
}
private int getToolbarColor() throws ExecutionException {
return TestThreadUtils.runOnUiThreadBlocking(() -> {
CustomTabToolbar toolbar = mActivity.findViewById(R.id.toolbar);
return toolbar.getBackground().getColor();
});
} }
} }
\ No newline at end of file
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