Commit f0289379 authored by Wei-Yin Chen (陳威尹)'s avatar Wei-Yin Chen (陳威尹) Committed by Commit Bot

Simplify ReturnToChromeTest by reusing test helpers

Simplify ReturnToChromeTest before adding more tests to it in
http://crrev.com/c/1903526.

Bug: 1022289
Change-Id: I5faa1814addfa58f3cb5729b7176025ae3092fc2
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1904958
Commit-Queue: Wei-Yin Chen (陳威尹) <wychen@chromium.org>
Reviewed-by: default avatarJustin DeWitt <dewittj@chromium.org>
Cr-Commit-Position: refs/heads/master@{#714020}
parent e317731e
......@@ -8,7 +8,6 @@ import static org.junit.Assert.assertEquals;
import static org.chromium.chrome.browser.tasks.ReturnToChromeExperimentsUtil.TAB_SWITCHER_ON_RETURN_MS;
import android.app.Activity;
import android.support.test.InstrumentationRegistry;
import android.support.test.filters.SmallTest;
......@@ -18,47 +17,47 @@ import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.chromium.base.ActivityState;
import org.chromium.base.ApplicationStatus;
import org.chromium.base.test.util.CallbackHelper;
import org.chromium.base.test.util.CommandLineFlags;
import org.chromium.base.test.util.Feature;
import org.chromium.base.test.util.Restriction;
import org.chromium.chrome.browser.ChromeFeatureList;
import org.chromium.chrome.browser.ChromeSwitches;
import org.chromium.chrome.browser.ChromeTabbedActivity;
import org.chromium.chrome.browser.flags.FeatureUtilities;
import org.chromium.chrome.browser.tasks.tab_management.TabUiTestHelper;
import org.chromium.chrome.test.ChromeJUnit4ClassRunner;
import org.chromium.chrome.test.ChromeTabbedActivityTestRule;
import org.chromium.chrome.test.util.MenuUtils;
import org.chromium.chrome.test.util.ApplicationTestUtils;
import org.chromium.content_public.browser.test.util.Criteria;
import org.chromium.content_public.browser.test.util.CriteriaHelper;
import org.chromium.content_public.browser.test.util.TestThreadUtils;
import org.chromium.net.test.EmbeddedTestServer;
import org.chromium.ui.test.util.UiRestriction;
import java.util.concurrent.TimeoutException;
/**
* Tests the functionality of return to chrome features that open overview mode if the timeout
* has passed.
*/
@RunWith(ChromeJUnit4ClassRunner.class)
@Restriction(UiRestriction.RESTRICTION_TYPE_PHONE)
@CommandLineFlags.Add({ChromeSwitches.DISABLE_FIRST_RUN_EXPERIENCE,
"enable-features=" + ChromeFeatureList.TAB_SWITCHER_ON_RETURN + "<Study",
"force-fieldtrials=Study/Group"})
public class ReturnToChromeTest {
private static final String BASE_PARAMS =
"force-fieldtrial-params=Study.Group:" + TAB_SWITCHER_ON_RETURN_MS + "/0";
@Rule
public ChromeTabbedActivityTestRule mActivityTestRule = new ChromeTabbedActivityTestRule();
private ChromeTabbedActivity mActivity;
private String mUrl;
@Before
public void setUp() {
FeatureUtilities.setGridTabSwitcherEnabledForTesting(true);
mActivityTestRule.startMainActivityOnBlankPage();
mActivity = mActivityTestRule.getActivity();
EmbeddedTestServer testServer =
EmbeddedTestServer.createAndStartServer(InstrumentationRegistry.getContext());
mUrl = testServer.getURL("/chrome/test/data/android/navigate/simple.html");
setupTabs();
mActivityTestRule.startMainActivityOnBlankPage();
}
/**
......@@ -68,19 +67,14 @@ public class ReturnToChromeTest {
@Test
@SmallTest
@Feature({"ReturnToChrome"})
@CommandLineFlags.Add({ChromeSwitches.DISABLE_FIRST_RUN_EXPERIENCE,
"enable-features=" + ChromeFeatureList.TAB_SWITCHER_ON_RETURN + "<FakeStudyName",
"force-fieldtrials=FakeStudyName/Enabled",
"force-fieldtrial-params=FakeStudyName.Enabled:" + TAB_SWITCHER_ON_RETURN_MS
+ "/100000"})
public void
testTabSwitcherModeNotTriggeredWithinThreshold() throws Exception {
finishActivityCompletely();
@CommandLineFlags.Add({BASE_PARAMS + "/" + TAB_SWITCHER_ON_RETURN_MS + "/100000"})
public void testTabSwitcherModeNotTriggeredWithinThreshold() throws Exception {
TabUiTestHelper.prepareTabsWithThumbnail(mActivityTestRule, 2, 0, mUrl);
ApplicationTestUtils.finishActivity(mActivityTestRule.getActivity());
mActivityTestRule.startMainActivityFromLauncher();
mActivity = mActivityTestRule.getActivity();
Assert.assertFalse(mActivity.getLayoutManager().overviewVisible());
Assert.assertFalse(mActivityTestRule.getActivity().getLayoutManager().overviewVisible());
}
/**
......@@ -90,18 +84,14 @@ public class ReturnToChromeTest {
@Test
@SmallTest
@Feature({"ReturnToChrome"})
@CommandLineFlags.Add({ChromeSwitches.DISABLE_FIRST_RUN_EXPERIENCE,
"enable-features=" + ChromeFeatureList.TAB_SWITCHER_ON_RETURN + "<FakeStudyName",
"force-fieldtrials=FakeStudyName/Enabled",
"force-fieldtrial-params=FakeStudyName.Enabled:" + TAB_SWITCHER_ON_RETURN_MS + "/0"})
public void
testTabSwitcherModeTriggeredBeyondThreshold() throws Exception {
finishActivityCompletely();
@CommandLineFlags.Add({BASE_PARAMS + "/" + TAB_SWITCHER_ON_RETURN_MS + "/0"})
public void testTabSwitcherModeTriggeredBeyondThreshold() throws Exception {
TabUiTestHelper.prepareTabsWithThumbnail(mActivityTestRule, 2, 0, mUrl);
ApplicationTestUtils.finishActivity(mActivityTestRule.getActivity());
mActivityTestRule.startMainActivityFromLauncher();
mActivity = mActivityTestRule.getActivity();
Assert.assertTrue(mActivity.getLayoutManager().overviewVisible());
Assert.assertTrue(mActivityTestRule.getActivity().getLayoutManager().overviewVisible());
CriteriaHelper.pollUiThread(Criteria.equals(true,
mActivityTestRule.getActivity()
......@@ -111,45 +101,4 @@ public class ReturnToChromeTest {
assertEquals(2, mActivityTestRule.getActivity().getTabModelSelector().getTotalTabCount());
}
private void setupTabs() {
TestThreadUtils.runOnUiThreadBlocking(
() -> mActivityTestRule.getActivity().getTabModelSelector().closeAllTabs());
EmbeddedTestServer testServer =
EmbeddedTestServer.createAndStartServer(InstrumentationRegistry.getContext());
final String url = testServer.getURL("/chrome/test/data/android/navigate/simple.html");
// Add 2 tabs.
MenuUtils.invokeCustomMenuActionSync(InstrumentationRegistry.getInstrumentation(),
mActivity, org.chromium.chrome.R.id.new_tab_menu_id);
mActivityTestRule.loadUrl(url);
MenuUtils.invokeCustomMenuActionSync(InstrumentationRegistry.getInstrumentation(),
mActivity, org.chromium.chrome.R.id.new_tab_menu_id);
mActivityTestRule.loadUrl(url);
assertEquals(2, mActivity.getTabModelSelector().getTotalTabCount());
}
private void finishActivityCompletely() throws TimeoutException {
final CallbackHelper activityCallback = new CallbackHelper();
ApplicationStatus.ActivityStateListener stateListener =
new ApplicationStatus.ActivityStateListener() {
@Override
public void onActivityStateChange(Activity activity, int newState) {
if (newState == ActivityState.STOPPED) {
activityCallback.notifyCalled();
ApplicationStatus.unregisterActivityStateListener(this);
}
}
};
ApplicationStatus.registerStateListenerForAllActivities(stateListener);
try {
mActivity.finish();
activityCallback.waitForCallback("Activity did not stop as expected", 0);
mActivityTestRule.setActivity(null);
} finally {
ApplicationStatus.unregisterActivityStateListener(stateListener);
}
}
}
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