Commit 885cf1f6 authored by Michael Thiessen's avatar Michael Thiessen Committed by Commit Bot

Batch device_dialog tests together

Introduces BlankCTATabInitialState.java, which manages initial state
across test suites. No post-batch cleanup is required as the only state
carried across test suites is the Activity, which is finished by the
test runner.

Also batches all device_dialog instrumentation tests together,
dramatically speeding them up.

Bug: 989569
Change-Id: I66fa56da157491defe1dca9b4ce9464a3d308961
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2307650Reviewed-by: default avatarYaron Friedman <yfriedman@chromium.org>
Commit-Queue: Michael Thiessen <mthiesse@chromium.org>
Cr-Commit-Position: refs/heads/master@{#792242}
parent 21e6c4e1
......@@ -38,6 +38,7 @@ import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
......@@ -78,7 +79,6 @@ import org.chromium.chrome.browser.contextualsearch.ContextualSearchInternalStat
import org.chromium.chrome.browser.contextualsearch.ResolvedSearchTerm.CardTag;
import org.chromium.chrome.browser.externalnav.ExternalNavigationDelegateImpl;
import org.chromium.chrome.browser.findinpage.FindToolbar;
import org.chromium.chrome.browser.firstrun.FirstRunStatus;
import org.chromium.chrome.browser.flags.ChromeFeatureList;
import org.chromium.chrome.browser.flags.ChromeSwitches;
import org.chromium.chrome.browser.gsa.GSAContextDisplaySelection;
......@@ -92,6 +92,7 @@ import org.chromium.chrome.browser.tabmodel.TabModelSelectorObserver;
import org.chromium.chrome.browser.tabmodel.TabModelUtils;
import org.chromium.chrome.test.ChromeActivityTestRule;
import org.chromium.chrome.test.ChromeJUnit4RunnerDelegate;
import org.chromium.chrome.test.batch.BlankCTATabInitialStateRule;
import org.chromium.chrome.test.util.ApplicationTestUtils;
import org.chromium.chrome.test.util.ChromeTabUtils;
import org.chromium.chrome.test.util.FullscreenTestUtils;
......@@ -145,38 +146,13 @@ import java.util.concurrent.TimeoutException;
@Restriction(RESTRICTION_TYPE_NON_LOW_END_DEVICE)
@Batch(Batch.PER_CLASS)
public class ContextualSearchManagerTest {
// This class batches with multiple paramaterized versions of itself, and therefore cannot use
// @BeforeClass/@AfterClass as these would be evaluated once for each parameterized class.
// TODO(crbug.com/1090043): Use @BeforeClass once this is fixed.
private static class SharedStaticState {
private static ChromeActivityTestRule<ChromeActivity> sActivityTestRule;
private static boolean sActivityStarted;
public static ChromeActivityTestRule<ChromeActivity> getActivityTestRule() {
if (sActivityTestRule == null) {
sActivityTestRule = new ChromeActivityTestRule<>(ChromeActivity.class);
}
return sActivityTestRule;
}
public static void ensureActivityStarted() {
if (sActivityStarted) return;
sActivityStarted = true;
TestThreadUtils.runOnUiThreadBlocking(() -> {
FirstRunStatus.setFirstRunFlowComplete(true);
LocaleManager.setInstanceForTest(new LocaleManager() {
@Override
public boolean needToCheckForSearchEnginePromo() {
return false;
}
});
});
sActivityTestRule.startMainActivityOnBlankPage();
}
}
@ClassRule
public static ChromeActivityTestRule<ChromeActivity> mActivityTestRule =
SharedStaticState.getActivityTestRule();
public static final ChromeActivityTestRule<ChromeActivity> sActivityTestRule =
new ChromeActivityTestRule(ChromeActivity.class);
@Rule
public final BlankCTATabInitialStateRule mInitialStateRule =
new BlankCTATabInitialStateRule(sActivityTestRule, false);
/** Parameter provider for enabling/disabling triggering-related Features. */
public static class FeatureParamProvider implements ParameterProvider {
......@@ -247,18 +223,18 @@ public class ContextualSearchManagerTest {
@Before
public void setUp() throws Exception {
SharedStaticState.ensureActivityStarted();
LocaleManager.setInstanceForTest(new LocaleManager() {
@Override
public boolean needToCheckForSearchEnginePromo() {
return false;
}
});
// TODO(crbug.com/989569): Allow TestServer reuse across tests to avoid waiting on test
// server startup for each test.
mTestServer = mActivityTestRule.getTestServer();
mTestServer = sActivityTestRule.getTestServer();
// Since we re-use the tab across tests, if the test page is already loaded, it's difficult
// to reload it without being racy, so we navigate to about:blank first.
mActivityTestRule.loadUrl("about:blank");
mActivityTestRule.loadUrl(mTestServer.getURL(TEST_PAGE));
sActivityTestRule.loadUrl(mTestServer.getURL(TEST_PAGE));
mManager = mActivityTestRule.getActivity().getContextualSearchManager();
mManager = sActivityTestRule.getActivity().getContextualSearchManager();
Assert.assertNotNull(mManager);
mPanel = mManager.getContextualSearchPanel();
......@@ -271,7 +247,7 @@ public class ContextualSearchManagerTest {
mFakeServer = new ContextualSearchFakeServer(mPolicy, this, mManager,
mManager.getOverlayContentDelegate(), new OverlayContentProgressObserver(),
mActivityTestRule.getActivity());
sActivityTestRule.getActivity());
mPanel.setOverlayPanelContentFactory(mFakeServer);
mManager.setNetworkCommunicator(mFakeServer);
......@@ -284,7 +260,7 @@ public class ContextualSearchManagerTest {
mActivityMonitor = InstrumentationRegistry.getInstrumentation().addMonitor(
filter, new Instrumentation.ActivityResult(Activity.RESULT_OK, null), true);
mDpToPx = mActivityTestRule.getActivity().getResources().getDisplayMetrics().density;
mDpToPx = sActivityTestRule.getActivity().getResources().getDisplayMetrics().density;
// Set the test Features map for all tests regardless of whether they are parameterized.
// Non-parameterized tests typically override this setting by calling setTestFeatures
......@@ -309,10 +285,6 @@ public class ContextualSearchManagerTest {
@After
public void tearDown() throws Exception {
TestThreadUtils.runOnUiThreadBlocking(() -> {
// Close all but the first tab as these tests expect to start with a single tab.
while (TabModelUtils.closeTabByIndex(
mActivityTestRule.getActivity().getCurrentTabModel(), 1)) {
}
mManager.dismissContextualSearchBar();
mPanel.closePanel(StateChangeReason.UNKNOWN, false);
});
......@@ -374,7 +346,7 @@ public class ContextualSearchManagerTest {
private void setOnlineStatusAndReload(boolean isOnline) {
mFakeServer.setIsOnline(isOnline);
final String testUrl = mTestServer.getURL(TEST_PAGE);
final Tab tab = mActivityTestRule.getActivity().getActivityTab();
final Tab tab = sActivityTestRule.getActivity().getActivityTab();
TestThreadUtils.runOnUiThreadBlocking(() -> tab.reload());
// Make sure the page is fully loaded.
ChromeTabUtils.waitForTabPageLoaded(tab, testUrl);
......@@ -413,7 +385,7 @@ public class ContextualSearchManagerTest {
private void clearSelection() {
ThreadUtils.runOnUiThreadBlocking(() -> {
SelectionPopupController.fromWebContents(mActivityTestRule.getWebContents())
SelectionPopupController.fromWebContents(sActivityTestRule.getWebContents())
.clearSelection();
});
}
......@@ -427,7 +399,7 @@ public class ContextualSearchManagerTest {
* @param nodeId A string containing the node ID.
*/
public void longPressNodeWithoutWaiting(String nodeId) throws TimeoutException {
Tab tab = mActivityTestRule.getActivity().getActivityTab();
Tab tab = sActivityTestRule.getActivity().getActivityTab();
DOMUtils.longPressNode(tab.getWebContents(), nodeId);
}
......@@ -474,7 +446,7 @@ public class ContextualSearchManagerTest {
*/
public long longPressNodeWithoutUp(String nodeId) throws TimeoutException {
long downTime = SystemClock.uptimeMillis();
Tab tab = mActivityTestRule.getActivity().getActivityTab();
Tab tab = sActivityTestRule.getActivity().getActivityTab();
DOMUtils.longPressNodeWithoutUp(tab.getWebContents(), nodeId, downTime);
waitForSelectActionBarVisible();
waitForPanelToPeek();
......@@ -497,7 +469,7 @@ public class ContextualSearchManagerTest {
// Drag to the specified position by a DOM node id.
int stepCount = 100;
Tab tab = mActivityTestRule.getActivity().getActivityTab();
Tab tab = sActivityTestRule.getActivity().getActivityTab();
DOMUtils.dragNodeTo(tab.getWebContents(), startNodeId, endNodeId, stepCount, downTime);
DOMUtils.dragNodeEnd(tab.getWebContents(), endNodeId, downTime);
......@@ -517,7 +489,7 @@ public class ContextualSearchManagerTest {
* @param nodeId A string containing the node ID.
*/
public void clickNode(String nodeId) throws TimeoutException {
Tab tab = mActivityTestRule.getActivity().getActivityTab();
Tab tab = sActivityTestRule.getActivity().getActivityTab();
DOMUtils.clickNode(tab.getWebContents(), nodeId);
}
......@@ -810,7 +782,7 @@ public class ContextualSearchManagerTest {
*/
private void pressKey(int keycode) {
KeyUtils.singleKeyEventActivity(InstrumentationRegistry.getInstrumentation(),
mActivityTestRule.getActivity(), keycode);
sActivityTestRule.getActivity(), keycode);
}
/**
......@@ -1064,16 +1036,16 @@ public class ContextualSearchManagerTest {
*/
private void fling(float startX, float startY, float endX, float endY, int stepCount) {
Point size = new Point();
mActivityTestRule.getActivity().getWindowManager().getDefaultDisplay().getSize(size);
sActivityTestRule.getActivity().getWindowManager().getDefaultDisplay().getSize(size);
float dragStartX = size.x * startX;
float dragEndX = size.x * endX;
float dragStartY = size.y * startY;
float dragEndY = size.y * endY;
long downTime = SystemClock.uptimeMillis();
TouchCommon.dragStart(mActivityTestRule.getActivity(), dragStartX, dragStartY, downTime);
TouchCommon.dragTo(mActivityTestRule.getActivity(), dragStartX, dragEndX, dragStartY,
TouchCommon.dragStart(sActivityTestRule.getActivity(), dragStartX, dragStartY, downTime);
TouchCommon.dragTo(sActivityTestRule.getActivity(), dragStartX, dragEndX, dragStartY,
dragEndY, stepCount, downTime);
TouchCommon.dragEnd(mActivityTestRule.getActivity(), dragEndX, dragEndY, downTime);
TouchCommon.dragEnd(sActivityTestRule.getActivity(), dragEndX, dragEndY, downTime);
}
/**
......@@ -1082,21 +1054,21 @@ public class ContextualSearchManagerTest {
*/
private void swipe(float startX, float startY, float endX, float endY, int stepCount) {
Point size = new Point();
mActivityTestRule.getActivity().getWindowManager().getDefaultDisplay().getSize(size);
sActivityTestRule.getActivity().getWindowManager().getDefaultDisplay().getSize(size);
float dragStartX = size.x * startX;
float dragEndX = size.x * endX;
float dragStartY = size.y * startY;
float dragEndY = size.y * endY;
int halfCount = stepCount / 2;
long downTime = SystemClock.uptimeMillis();
TouchCommon.dragStart(mActivityTestRule.getActivity(), dragStartX, dragStartY, downTime);
TouchCommon.dragTo(mActivityTestRule.getActivity(), dragStartX, dragEndX, dragStartY,
TouchCommon.dragStart(sActivityTestRule.getActivity(), dragStartX, dragStartY, downTime);
TouchCommon.dragTo(sActivityTestRule.getActivity(), dragStartX, dragEndX, dragStartY,
dragEndY, halfCount, downTime);
// Generate events in the stationary end position in order to simulate a "pause" in
// the movement, therefore preventing this gesture from being interpreted as a fling.
TouchCommon.dragTo(mActivityTestRule.getActivity(), dragEndX, dragEndX, dragEndY, dragEndY,
TouchCommon.dragTo(sActivityTestRule.getActivity(), dragEndX, dragEndX, dragEndY, dragEndY,
halfCount, downTime);
TouchCommon.dragEnd(mActivityTestRule.getActivity(), dragEndX, dragEndY, downTime);
TouchCommon.dragEnd(sActivityTestRule.getActivity(), dragEndX, dragEndY, downTime);
}
/**
......@@ -1142,7 +1114,7 @@ public class ContextualSearchManagerTest {
* Taps the base page at the given x, y position.
*/
private void tapBasePage(float x, float y) {
View root = mActivityTestRule.getActivity().getWindow().getDecorView().getRootView();
View root = sActivityTestRule.getActivity().getWindow().getDecorView().getRootView();
x *= root.getWidth();
y *= root.getHeight();
TouchCommon.singleClickView(root, (int) x, (int) y);
......@@ -1170,7 +1142,7 @@ public class ContextualSearchManagerTest {
* TODO(donnd): Replace this method with panelBarClick since this appears to be unreliable.
*/
private void clickPanelBar() {
View root = mActivityTestRule.getActivity().getWindow().getDecorView().getRootView();
View root = sActivityTestRule.getActivity().getWindow().getDecorView().getRootView();
float tapX = ((mPanel.getOffsetX() + mPanel.getWidth()) / 2f) * mDpToPx;
float tapY = (mPanel.getOffsetY() + (mPanel.getBarContainerHeight() / 2f)) * mDpToPx;
......@@ -1357,7 +1329,7 @@ public class ContextualSearchManagerTest {
waitForPanelToPeek();
OmniboxTestUtils.toggleUrlBarFocus(
(UrlBar) mActivityTestRule.getActivity().findViewById(R.id.url_bar), true);
(UrlBar) sActivityTestRule.getActivity().findViewById(R.id.url_bar), true);
assertPanelClosedOrUndefined();
}
......@@ -1851,7 +1823,7 @@ public class ContextualSearchManagerTest {
PostTask.runOrPostTask(UiThreadTaskTraits.DEFAULT, () -> {
ChromeTabUtils.simulateRendererKilledForTesting(
mActivityTestRule.getActivity().getActivityTab(), true);
sActivityTestRule.getActivity().getActivityTab(), true);
});
// Give the panelState time to change
......@@ -1872,12 +1844,12 @@ public class ContextualSearchManagerTest {
public void testContextualSearchNotDismissedOnBackgroundTabCrash(
@EnabledFeature int enabledFeature) throws Exception {
ChromeTabUtils.newTabFromMenu(
InstrumentationRegistry.getInstrumentation(), mActivityTestRule.getActivity());
InstrumentationRegistry.getInstrumentation(), sActivityTestRule.getActivity());
final Tab tab2 =
TabModelUtils.getCurrentTab(mActivityTestRule.getActivity().getCurrentTabModel());
TabModelUtils.getCurrentTab(sActivityTestRule.getActivity().getCurrentTabModel());
TestThreadUtils.runOnUiThreadBlocking(() -> {
TabModelUtils.setIndex(mActivityTestRule.getActivity().getCurrentTabModel(), 0);
TabModelUtils.setIndex(sActivityTestRule.getActivity().getCurrentTabModel(), 0);
});
triggerResolve("states");
......@@ -1909,7 +1881,7 @@ public class ContextualSearchManagerTest {
tabCreatedHelper.notifyCalled();
}
};
mActivityTestRule.getActivity().getTabModelSelector().addObserver(observer);
sActivityTestRule.getActivity().getTabModelSelector().addObserver(observer);
// -------- TEST ---------
// Start a slow-resolve search and maximize the Panel.
......@@ -1936,7 +1908,7 @@ public class ContextualSearchManagerTest {
tabCreatedHelper.waitForCallback(tabCreatedHelperCallCount);
// -------- CLEAN UP ---------
mActivityTestRule.getActivity().getTabModelSelector().removeObserver(observer);
sActivityTestRule.getActivity().getTabModelSelector().removeObserver(observer);
}
//============================================================================================
......@@ -2109,7 +2081,7 @@ public class ContextualSearchManagerTest {
private void closeAppMenu() {
TestThreadUtils.runOnUiThreadBlocking(
() -> mActivityTestRule.getAppMenuCoordinator().getAppMenuHandler().hideAppMenu());
() -> sActivityTestRule.getAppMenuCoordinator().getAppMenuHandler().hideAppMenu());
}
/**
......@@ -2117,7 +2089,7 @@ public class ContextualSearchManagerTest {
*/
private void assertAppMenuVisibility(final boolean isVisible) {
CriteriaHelper.pollInstrumentationThread(() -> {
Criteria.checkThat(mActivityTestRule.getAppMenuCoordinator()
Criteria.checkThat(sActivityTestRule.getAppMenuCoordinator()
.getAppMenuHandler()
.isAppMenuShowing(),
Matchers.is(isVisible));
......@@ -2408,7 +2380,7 @@ public class ContextualSearchManagerTest {
}
private SelectionPopupController getSelectionPopupController() {
return SelectionPopupController.fromWebContents(mActivityTestRule.getWebContents());
return SelectionPopupController.fromWebContents(sActivityTestRule.getWebContents());
}
/**
......@@ -2500,7 +2472,7 @@ public class ContextualSearchManagerTest {
public void testExternalNavigationWithUserGesture(@EnabledFeature int enabledFeature) {
final ExternalNavigationHandler externalNavHandler =
new ExternalNavigationHandler(new ExternalNavigationDelegateImpl(
mActivityTestRule.getActivity().getActivityTab()));
sActivityTestRule.getActivity().getActivityTab()));
final NavigationParams navigationParams = new NavigationParams(
"intent://test/#Intent;scheme=test;package=com.chrome.test;end", "",
false /* isPost */, true /* hasUserGesture */, PageTransition.LINK,
......@@ -2510,7 +2482,7 @@ public class ContextualSearchManagerTest {
InstrumentationRegistry.getInstrumentation().runOnMainSync(new Runnable() {
@Override
public void run() {
mActivityTestRule.getActivity().onUserInteraction();
sActivityTestRule.getActivity().onUserInteraction();
Assert.assertFalse(mManager.getOverlayContentDelegate().shouldInterceptNavigation(
externalNavHandler, navigationParams));
}
......@@ -2530,7 +2502,7 @@ public class ContextualSearchManagerTest {
@EnabledFeature int enabledFeature) {
final ExternalNavigationHandler externalNavHandler =
new ExternalNavigationHandler(new ExternalNavigationDelegateImpl(
mActivityTestRule.getActivity().getActivityTab()));
sActivityTestRule.getActivity().getActivityTab()));
final NavigationParams initialNavigationParams = new NavigationParams("http://test.com", "",
false /* isPost */, true /* hasUserGesture */, PageTransition.LINK,
......@@ -2547,7 +2519,7 @@ public class ContextualSearchManagerTest {
InstrumentationRegistry.getInstrumentation().runOnMainSync(new Runnable() {
@Override
public void run() {
mActivityTestRule.getActivity().onUserInteraction();
sActivityTestRule.getActivity().onUserInteraction();
OverlayContentDelegate delegate = mManager.getOverlayContentDelegate();
Assert.assertTrue(delegate.shouldInterceptNavigation(
externalNavHandler, initialNavigationParams));
......@@ -2569,7 +2541,7 @@ public class ContextualSearchManagerTest {
public void testExternalNavigationWithoutUserGesture(@EnabledFeature int enabledFeature) {
final ExternalNavigationHandler externalNavHandler =
new ExternalNavigationHandler(new ExternalNavigationDelegateImpl(
mActivityTestRule.getActivity().getActivityTab()));
sActivityTestRule.getActivity().getActivityTab()));
final NavigationParams navigationParams = new NavigationParams(
"intent://test/#Intent;scheme=test;package=com.chrome.test;end", "",
false /* isPost */, false /* hasUserGesture */, PageTransition.LINK,
......@@ -2579,7 +2551,7 @@ public class ContextualSearchManagerTest {
InstrumentationRegistry.getInstrumentation().runOnMainSync(new Runnable() {
@Override
public void run() {
mActivityTestRule.getActivity().onUserInteraction();
sActivityTestRule.getActivity().onUserInteraction();
Assert.assertFalse(mManager.getOverlayContentDelegate().shouldInterceptNavigation(
externalNavHandler, navigationParams));
}
......@@ -3059,8 +3031,8 @@ public class ContextualSearchManagerTest {
throws Exception {
// Toggle tab to fulllscreen.
FullscreenTestUtils.togglePersistentFullscreenAndAssert(
mActivityTestRule.getActivity().getActivityTab(), true,
mActivityTestRule.getActivity());
sActivityTestRule.getActivity().getActivityTab(), true,
sActivityTestRule.getActivity());
// Simulate a resolving search and assert that the panel peeks.
simulateResolveSearch("search");
......@@ -3089,9 +3061,9 @@ public class ContextualSearchManagerTest {
simulateResolveSearch("search");
// Toggle tab to fullscreen.
Tab tab = mActivityTestRule.getActivity().getActivityTab();
Tab tab = sActivityTestRule.getActivity().getActivityTab();
FullscreenTestUtils.togglePersistentFullscreenAndAssert(
tab, true, mActivityTestRule.getActivity());
tab, true, sActivityTestRule.getActivity());
// Assert that the panel is closed.
waitForPanelToClose();
......@@ -3101,7 +3073,7 @@ public class ContextualSearchManagerTest {
// Toggle tab to non-fullscreen.
FullscreenTestUtils.togglePersistentFullscreenAndAssert(
tab, false, mActivityTestRule.getActivity());
tab, false, sActivityTestRule.getActivity());
// Assert that the panel is closed.
waitForPanelToClose();
......@@ -3193,7 +3165,7 @@ public class ContextualSearchManagerTest {
// Check that the peeking bar is showing the quick action data.
Assert.assertTrue(quickActionControl.hasQuickAction());
Assert.assertTrue(barControl.getCaptionVisible());
Assert.assertEquals(mActivityTestRule.getActivity().getResources().getString(
Assert.assertEquals(sActivityTestRule.getActivity().getResources().getString(
R.string.contextual_search_quick_action_caption_phone),
barControl.getCaptionText());
Assert.assertEquals(1.f, imageControl.getCustomImageVisibilityPercentage(), 0);
......@@ -3211,7 +3183,7 @@ public class ContextualSearchManagerTest {
// Assert that the quick action data is showing.
Assert.assertTrue(barControl.getCaptionVisible());
Assert.assertEquals(mActivityTestRule.getActivity().getResources().getString(
Assert.assertEquals(sActivityTestRule.getActivity().getResources().getString(
R.string.contextual_search_quick_action_caption_phone),
barControl.getCaptionText());
// TODO(donnd): figure out why we get ~0.65 on Oreo rather than 1. https://crbug.com/818515.
......@@ -3250,7 +3222,7 @@ public class ContextualSearchManagerTest {
-> mPanel.onSearchTermResolved("search", null, "tel:555-555-5555",
QuickActionCategory.PHONE, CardTag.CT_CONTACT));
mActivityTestRule.getActivity().onUserInteraction();
sActivityTestRule.getActivity().onUserInteraction();
retryPanelBarInteractions(() -> {
// Tap on the portion of the bar that should trigger the quick action intent to be
// fired.
......@@ -3286,7 +3258,7 @@ public class ContextualSearchManagerTest {
// Assert that the URL was loaded.
ChromeTabUtils.waitForTabPageLoaded(
mActivityTestRule.getActivity().getActivityTab(), testUrl);
sActivityTestRule.getActivity().getActivityTab(), testUrl);
}, false);
}
......@@ -3458,11 +3430,11 @@ public class ContextualSearchManagerTest {
public void testTriggeringContextualSearchHidesFindInPageOverlay(
@EnabledFeature int enabledFeature) throws Exception {
MenuUtils.invokeCustomMenuActionSync(InstrumentationRegistry.getInstrumentation(),
mActivityTestRule.getActivity(), R.id.find_in_page_id);
sActivityTestRule.getActivity(), R.id.find_in_page_id);
CriteriaHelper.pollUiThread(() -> {
FindToolbar findToolbar =
(FindToolbar) mActivityTestRule.getActivity().findViewById(R.id.find_toolbar);
(FindToolbar) sActivityTestRule.getActivity().findViewById(R.id.find_toolbar);
Criteria.checkThat(findToolbar, Matchers.notNullValue());
Criteria.checkThat(findToolbar.isShown(), Matchers.is(true));
Criteria.checkThat(findToolbar.isAnimating(), Matchers.is(false));
......@@ -3471,7 +3443,7 @@ public class ContextualSearchManagerTest {
// Don't type anything to Find because that may cause scrolling which makes clicking in the
// page flaky.
View findToolbar = mActivityTestRule.getActivity().findViewById(R.id.find_toolbar);
View findToolbar = sActivityTestRule.getActivity().findViewById(R.id.find_toolbar);
Assert.assertTrue(findToolbar.isShown());
simulateResolveSearch("search");
......@@ -3554,7 +3526,7 @@ public class ContextualSearchManagerTest {
@ParameterAnnotations.UseMethodParameter(FeatureParamProvider.class)
public void testTabReparenting(@EnabledFeature int enabledFeature) throws Exception {
// Move our "tap_test" tab to another activity.
final ChromeActivity ca = mActivityTestRule.getActivity();
final ChromeActivity ca = sActivityTestRule.getActivity();
// Create a new tab so |ca| isn't destroyed.
ChromeTabUtils.newTabFromMenu(InstrumentationRegistry.getInstrumentation(), ca);
......
......@@ -13,16 +13,18 @@ import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import androidx.test.filters.LargeTest;
import androidx.test.filters.SmallTest;
import org.hamcrest.Matchers;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.chromium.base.test.util.Batch;
import org.chromium.base.test.util.CommandLineFlags;
import org.chromium.base.test.util.JniMocker;
import org.chromium.chrome.R;
......@@ -30,6 +32,7 @@ import org.chromium.chrome.browser.ChromeActivity;
import org.chromium.chrome.browser.flags.ChromeSwitches;
import org.chromium.chrome.test.ChromeActivityTestRule;
import org.chromium.chrome.test.ChromeJUnit4ClassRunner;
import org.chromium.chrome.test.batch.BlankCTATabInitialStateRule;
import org.chromium.components.location.LocationUtils;
import org.chromium.components.security_state.ConnectionSecurityLevel;
import org.chromium.content_public.browser.test.util.Criteria;
......@@ -48,11 +51,18 @@ import java.util.concurrent.Callable;
*/
@RunWith(ChromeJUnit4ClassRunner.class)
@CommandLineFlags.Add({ChromeSwitches.DISABLE_FIRST_RUN_EXPERIENCE})
@Batch(BluetoothChooserDialogTest.DEVICE_DIALOG_BATCH_NAME)
public class BluetoothChooserDialogTest {
@Rule
public ChromeActivityTestRule<ChromeActivity> mActivityTestRule =
public static final String DEVICE_DIALOG_BATCH_NAME = "device_dialog";
@ClassRule
public static final ChromeActivityTestRule<ChromeActivity> sActivityTestRule =
new ChromeActivityTestRule<>(ChromeActivity.class);
@Rule
public final BlankCTATabInitialStateRule mInitialStateRule =
new BlankCTATabInitialStateRule(sActivityTestRule, false);
@Rule
public JniMocker mocker = new JniMocker();
......@@ -110,7 +120,6 @@ public class BluetoothChooserDialogTest {
@Before
public void setUp() throws Exception {
mActivityTestRule.startMainActivityOnBlankPage();
mLocationUtils = new FakeLocationUtils();
LocationUtils.setFactory(() -> mLocationUtils);
mChooserDialog = createDialog();
......@@ -126,7 +135,7 @@ public class BluetoothChooserDialogTest {
private BluetoothChooserDialog createDialog() {
return TestThreadUtils.runOnUiThreadBlockingNoException(
() -> {
mWindowAndroid = mActivityTestRule.getActivity().getWindowAndroid();
mWindowAndroid = sActivityTestRule.getActivity().getWindowAndroid();
BluetoothChooserDialog dialog = new BluetoothChooserDialog(mWindowAndroid,
"https://origin.example.com/", ConnectionSecurityLevel.SECURE,
/*nativeBluetoothChooserDialogPtr=*/42);
......@@ -178,7 +187,7 @@ public class BluetoothChooserDialogTest {
}
@Test
@LargeTest
@SmallTest
public void testCancel() {
ItemChooserDialog itemChooser = mChooserDialog.mItemChooserDialog;
Dialog dialog = itemChooser.getDialogForTesting();
......@@ -191,7 +200,7 @@ public class BluetoothChooserDialogTest {
// Before we add items to the dialog, the 'searching' message should be
// showing, the Commit button should be disabled and the list view hidden.
Assert.assertEquals(removeLinkTags(mActivityTestRule.getActivity().getString(
Assert.assertEquals(removeLinkTags(sActivityTestRule.getActivity().getString(
R.string.bluetooth_searching)),
statusView.getText().toString());
Assert.assertFalse(button.isEnabled());
......@@ -206,7 +215,7 @@ public class BluetoothChooserDialogTest {
}
@Test
@LargeTest
@SmallTest
public void testSelectItem() {
Dialog dialog = mChooserDialog.mItemChooserDialog.getDialogForTesting();
......@@ -235,7 +244,7 @@ public class BluetoothChooserDialogTest {
// the progress spinner should disappear, the Commit button should still
// be disabled (since nothing's selected), and the list view should
// show.
Assert.assertEquals(removeLinkTags(mActivityTestRule.getActivity().getString(
Assert.assertEquals(removeLinkTags(sActivityTestRule.getActivity().getString(
R.string.bluetooth_searching)),
statusView.getText().toString());
Assert.assertFalse(button.isEnabled());
......@@ -250,7 +259,7 @@ public class BluetoothChooserDialogTest {
mChooserDialog.mConnectedIcon, mChooserDialog.mConnectedIconDescription));
Assert.assertTrue(itemAdapter.getItem(2).hasSameContents("id-3", "Name 3",
mChooserDialog.mSignalStrengthLevelIcon[1],
mActivityTestRule.getActivity().getResources().getQuantityString(
sActivityTestRule.getActivity().getResources().getQuantityString(
R.plurals.signal_strength_level_n_bars, 1, 1)));
// We show the connected icon even if the device has a signal strength.
Assert.assertTrue(itemAdapter.getItem(3).hasSameContents("id-4", "Name 4",
......@@ -263,7 +272,7 @@ public class BluetoothChooserDialogTest {
}
@Test
@LargeTest
@SmallTest
public void testNoLocationPermission() {
ItemChooserDialog itemChooser = mChooserDialog.mItemChooserDialog;
Dialog dialog = itemChooser.getDialogForTesting();
......@@ -285,10 +294,10 @@ public class BluetoothChooserDialogTest {
() -> mChooserDialog.notifyDiscoveryState(
BluetoothChooserDialog.DiscoveryMode.DISCOVERY_FAILED_TO_START));
Assert.assertEquals(removeLinkTags(mActivityTestRule.getActivity().getString(
Assert.assertEquals(removeLinkTags(sActivityTestRule.getActivity().getString(
R.string.bluetooth_need_location_permission)),
errorView.getText().toString());
Assert.assertEquals(removeLinkTags(mActivityTestRule.getActivity().getString(
Assert.assertEquals(removeLinkTags(sActivityTestRule.getActivity().getString(
R.string.bluetooth_adapter_off_help)),
statusView.getText().toString());
Assert.assertFalse(button.isEnabled());
......@@ -312,14 +321,14 @@ public class BluetoothChooserDialogTest {
new int[] {PackageManager.PERMISSION_GRANTED}));
Assert.assertEquals(1, mRestartSearchCount);
Assert.assertEquals(removeLinkTags(mActivityTestRule.getActivity().getString(
Assert.assertEquals(removeLinkTags(sActivityTestRule.getActivity().getString(
R.string.bluetooth_searching)),
statusView.getText().toString());
mChooserDialog.closeDialog();
}
@Test
@LargeTest
@SmallTest
public void testNoLocationServices() {
ItemChooserDialog itemChooser = mChooserDialog.mItemChooserDialog;
Dialog dialog = itemChooser.getDialogForTesting();
......@@ -345,10 +354,10 @@ public class BluetoothChooserDialogTest {
() -> mChooserDialog.notifyDiscoveryState(
BluetoothChooserDialog.DiscoveryMode.DISCOVERY_FAILED_TO_START));
Assert.assertEquals(removeLinkTags(mActivityTestRule.getActivity().getString(
Assert.assertEquals(removeLinkTags(sActivityTestRule.getActivity().getString(
R.string.bluetooth_need_location_services_on)),
errorView.getText().toString());
Assert.assertEquals(removeLinkTags(mActivityTestRule.getActivity().getString(
Assert.assertEquals(removeLinkTags(sActivityTestRule.getActivity().getString(
R.string.bluetooth_need_location_permission_help)),
statusView.getText().toString());
Assert.assertFalse(button.isEnabled());
......@@ -359,12 +368,13 @@ public class BluetoothChooserDialogTest {
// Turn on Location Services.
mLocationUtils.mSystemLocationSettingsEnabled = true;
TestThreadUtils.runOnUiThreadBlocking(
() -> mChooserDialog.mLocationModeBroadcastReceiver.onReceive(
mActivityTestRule.getActivity(),
()
-> mChooserDialog.mLocationModeBroadcastReceiver.onReceive(
sActivityTestRule.getActivity(),
new Intent(LocationManager.MODE_CHANGED_ACTION)));
Assert.assertEquals(1, mRestartSearchCount);
Assert.assertEquals(removeLinkTags(mActivityTestRule.getActivity().getString(
Assert.assertEquals(removeLinkTags(sActivityTestRule.getActivity().getString(
R.string.bluetooth_searching)),
statusView.getText().toString());
......@@ -374,7 +384,7 @@ public class BluetoothChooserDialogTest {
// TODO(jyasskin): Test when the user denies Chrome the ability to ask for permission.
@Test
@LargeTest
@SmallTest
public void testTurnOnAdapter() {
final ItemChooserDialog itemChooser = mChooserDialog.mItemChooserDialog;
Dialog dialog = itemChooser.getDialogForTesting();
......@@ -391,10 +401,10 @@ public class BluetoothChooserDialogTest {
// Turn off adapter.
TestThreadUtils.runOnUiThreadBlocking(() -> mChooserDialog.notifyAdapterTurnedOff());
Assert.assertEquals(removeLinkTags(mActivityTestRule.getActivity().getString(
Assert.assertEquals(removeLinkTags(sActivityTestRule.getActivity().getString(
R.string.bluetooth_adapter_off)),
errorView.getText().toString());
Assert.assertEquals(removeLinkTags(mActivityTestRule.getActivity().getString(
Assert.assertEquals(removeLinkTags(sActivityTestRule.getActivity().getString(
R.string.bluetooth_adapter_off_help)),
statusView.getText().toString());
Assert.assertFalse(button.isEnabled());
......
......@@ -9,15 +9,17 @@ import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import androidx.test.filters.LargeTest;
import androidx.test.filters.SmallTest;
import org.hamcrest.Matchers;
import org.junit.Assert;
import org.junit.Before;
import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.chromium.base.test.util.Batch;
import org.chromium.base.test.util.CommandLineFlags;
import org.chromium.base.test.util.JniMocker;
import org.chromium.chrome.R;
......@@ -25,6 +27,7 @@ import org.chromium.chrome.browser.ChromeActivity;
import org.chromium.chrome.browser.flags.ChromeSwitches;
import org.chromium.chrome.test.ChromeActivityTestRule;
import org.chromium.chrome.test.ChromeJUnit4ClassRunner;
import org.chromium.chrome.test.batch.BlankCTATabInitialStateRule;
import org.chromium.components.security_state.ConnectionSecurityLevel;
import org.chromium.content_public.browser.bluetooth_scanning.Event;
import org.chromium.content_public.browser.test.util.Criteria;
......@@ -37,11 +40,16 @@ import org.chromium.ui.base.ActivityWindowAndroid;
*/
@RunWith(ChromeJUnit4ClassRunner.class)
@CommandLineFlags.Add({ChromeSwitches.DISABLE_FIRST_RUN_EXPERIENCE})
@Batch(BluetoothChooserDialogTest.DEVICE_DIALOG_BATCH_NAME)
public class BluetoothScanningPermissionDialogTest {
@Rule
public ChromeActivityTestRule<ChromeActivity> mActivityTestRule =
@ClassRule
public static final ChromeActivityTestRule<ChromeActivity> sActivityTestRule =
new ChromeActivityTestRule<>(ChromeActivity.class);
@Rule
public final BlankCTATabInitialStateRule mInitialStateRule =
new BlankCTATabInitialStateRule(sActivityTestRule, false);
@Rule
public JniMocker mocker = new JniMocker();
......@@ -62,13 +70,12 @@ public class BluetoothScanningPermissionDialogTest {
public void setUp() throws Exception {
mocker.mock(BluetoothScanningPermissionDialogJni.TEST_HOOKS,
new TestBluetoothScanningPermissionDialogJni());
mActivityTestRule.startMainActivityOnBlankPage();
mPermissionDialog = createDialog();
}
private BluetoothScanningPermissionDialog createDialog() {
return TestThreadUtils.runOnUiThreadBlockingNoException(() -> {
mWindowAndroid = mActivityTestRule.getActivity().getWindowAndroid();
mWindowAndroid = sActivityTestRule.getActivity().getWindowAndroid();
BluetoothScanningPermissionDialog dialog = new BluetoothScanningPermissionDialog(
mWindowAndroid, "https://origin.example.com/", ConnectionSecurityLevel.SECURE,
/*nativeBluetoothScanningPermissionDialogPtr=*/42);
......@@ -77,7 +84,7 @@ public class BluetoothScanningPermissionDialogTest {
}
@Test
@LargeTest
@SmallTest
public void testAddDevice() {
Dialog dialog = mPermissionDialog.getDialogForTesting();
......@@ -117,7 +124,7 @@ public class BluetoothScanningPermissionDialogTest {
}
@Test
@LargeTest
@SmallTest
public void testCancelPermissionDialogWithoutClickingAnyButton() {
Dialog dialog = mPermissionDialog.getDialogForTesting();
......
......@@ -14,16 +14,19 @@ import android.widget.ListView;
import android.widget.TextView;
import androidx.core.util.ObjectsCompat;
import androidx.test.filters.LargeTest;
import androidx.test.filters.SmallTest;
import androidx.vectordrawable.graphics.drawable.VectorDrawableCompat;
import org.hamcrest.Matchers;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.chromium.base.test.util.Batch;
import org.chromium.base.test.util.CommandLineFlags;
import org.chromium.base.test.util.DisabledTest;
import org.chromium.chrome.R;
......@@ -31,6 +34,7 @@ import org.chromium.chrome.browser.ChromeActivity;
import org.chromium.chrome.browser.flags.ChromeSwitches;
import org.chromium.chrome.test.ChromeActivityTestRule;
import org.chromium.chrome.test.ChromeJUnit4ClassRunner;
import org.chromium.chrome.test.batch.BlankCTATabInitialStateRule;
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;
......@@ -42,11 +46,16 @@ import org.chromium.ui.widget.TextViewWithClickableSpans;
*/
@RunWith(ChromeJUnit4ClassRunner.class)
@CommandLineFlags.Add({ChromeSwitches.DISABLE_FIRST_RUN_EXPERIENCE})
@Batch(BluetoothChooserDialogTest.DEVICE_DIALOG_BATCH_NAME)
public class ItemChooserDialogTest implements ItemChooserDialog.ItemSelectedCallback {
@Rule
public ChromeActivityTestRule<ChromeActivity> mActivityTestRule =
@ClassRule
public static final ChromeActivityTestRule<ChromeActivity> sActivityTestRule =
new ChromeActivityTestRule<>(ChromeActivity.class);
@Rule
public final BlankCTATabInitialStateRule mInitialStateRule =
new BlankCTATabInitialStateRule(sActivityTestRule, false);
ItemChooserDialog mChooserDialog;
String mLastSelectedId = "None";
......@@ -59,7 +68,6 @@ public class ItemChooserDialogTest implements ItemChooserDialog.ItemSelectedCall
@Before
public void setUp() throws Exception {
mActivityTestRule.startMainActivityOnBlankPage();
mChooserDialog = createDialog();
mTestDrawable1 = getNewTestDrawable();
......@@ -71,6 +79,14 @@ public class ItemChooserDialogTest implements ItemChooserDialog.ItemSelectedCall
Assert.assertFalse(ObjectsCompat.equals(mTestDrawable1, mTestDrawable2));
}
@After
public void tearDown() {
TestThreadUtils.runOnUiThreadBlocking(() -> {
mChooserDialog.setIdleState();
mChooserDialog.dismiss();
});
}
// ItemChooserDialog.ItemSelectedCallback:
@Override
......@@ -80,7 +96,7 @@ public class ItemChooserDialogTest implements ItemChooserDialog.ItemSelectedCall
private Drawable getNewTestDrawable() {
Drawable drawable =
VectorDrawableCompat.create(mActivityTestRule.getActivity().getResources(),
VectorDrawableCompat.create(sActivityTestRule.getActivity().getResources(),
R.drawable.ic_bluetooth_connected, null);
// Calling mutate() on a Drawable should typically create a new ConstantState
// for that Drawable. Ensure the new drawable doesn't share a state with other
......@@ -101,7 +117,7 @@ public class ItemChooserDialogTest implements ItemChooserDialog.ItemSelectedCall
statusIdleNoneFound, statusIdleSomeFound, positiveButton);
ItemChooserDialog dialog = TestThreadUtils.runOnUiThreadBlockingNoException(() -> {
ItemChooserDialog dialog1 = new ItemChooserDialog(
mActivityTestRule.getActivity(), ItemChooserDialogTest.this, labels);
sActivityTestRule.getActivity(), ItemChooserDialogTest.this, labels);
return dialog1;
});
return dialog;
......@@ -152,7 +168,7 @@ public class ItemChooserDialogTest implements ItemChooserDialog.ItemSelectedCall
}
@Test
@LargeTest
@SmallTest
public void testAddItemsWithNoIcons() {
TestThreadUtils.runOnUiThreadBlocking(() -> {
Dialog dialog = mChooserDialog.getDialogForTesting();
......@@ -173,14 +189,11 @@ public class ItemChooserDialogTest implements ItemChooserDialog.ItemSelectedCall
Assert.assertEquals(View.GONE, icon2.getVisibility());
Assert.assertEquals(null, icon2.getDrawable());
}
mChooserDialog.setIdleState();
mChooserDialog.dismiss();
});
}
@Test
@LargeTest
@SmallTest
public void testAddItemsWithIcons() {
TestThreadUtils.runOnUiThreadBlocking(() -> {
Dialog dialog = mChooserDialog.getDialogForTesting();
......@@ -209,14 +222,11 @@ public class ItemChooserDialogTest implements ItemChooserDialog.ItemSelectedCall
Assert.assertEquals(mTestDrawable2, icon2.getDrawable());
Assert.assertEquals(mTestDrawableDescription2, icon2.getContentDescription());
}
mChooserDialog.setIdleState();
mChooserDialog.dismiss();
});
}
@Test
@LargeTest
@SmallTest
public void testAddItemWithIconAfterItemWithNoIcon() {
TestThreadUtils.runOnUiThreadBlocking(() -> {
Dialog dialog = mChooserDialog.getDialogForTesting();
......@@ -240,14 +250,11 @@ public class ItemChooserDialogTest implements ItemChooserDialog.ItemSelectedCall
Assert.assertEquals(View.VISIBLE, icon2.getVisibility());
Assert.assertEquals(mTestDrawable2, icon2.getDrawable());
}
mChooserDialog.setIdleState();
mChooserDialog.dismiss();
});
}
@Test
@LargeTest
@SmallTest
public void testAddItemWithNoIconAfterItemWithIcon() {
TestThreadUtils.runOnUiThreadBlocking(() -> {
Dialog dialog = mChooserDialog.getDialogForTesting();
......@@ -271,14 +278,11 @@ public class ItemChooserDialogTest implements ItemChooserDialog.ItemSelectedCall
Assert.assertEquals(mTestDrawable1, icon1.getDrawable());
Assert.assertEquals(View.INVISIBLE, icon2.getVisibility());
}
mChooserDialog.setIdleState();
mChooserDialog.dismiss();
});
}
@Test
@LargeTest
@SmallTest
public void testRemoveItemWithIconNoItemsWithIconsLeft() {
TestThreadUtils.runOnUiThreadBlocking(() -> {
Dialog dialog = mChooserDialog.getDialogForTesting();
......@@ -308,14 +312,11 @@ public class ItemChooserDialogTest implements ItemChooserDialog.ItemSelectedCall
ImageView icon2 = getIconImageView(dialog, 1);
Assert.assertEquals(View.GONE, icon2.getVisibility());
}
mChooserDialog.setIdleState();
mChooserDialog.dismiss();
});
}
@Test
@LargeTest
@SmallTest
public void testRemoveItemWithIconOneItemWithIconLeft() {
TestThreadUtils.runOnUiThreadBlocking(() -> {
Dialog dialog = mChooserDialog.getDialogForTesting();
......@@ -358,14 +359,11 @@ public class ItemChooserDialogTest implements ItemChooserDialog.ItemSelectedCall
Assert.assertEquals(mTestDrawable2, icon2.getDrawable());
Assert.assertEquals(View.INVISIBLE, icon3.getVisibility());
}
mChooserDialog.setIdleState();
mChooserDialog.dismiss();
});
}
@Test
@LargeTest
@SmallTest
public void testUpdateItemWithIconToNoIcon() {
TestThreadUtils.runOnUiThreadBlocking(() -> {
Dialog dialog = mChooserDialog.getDialogForTesting();
......@@ -392,14 +390,11 @@ public class ItemChooserDialogTest implements ItemChooserDialog.ItemSelectedCall
Assert.assertTrue(itemAdapter.getItem(0).hasSameContents(
"key1", "desc1", null /* icon */, null /* iconDescription */));
}
mChooserDialog.setIdleState();
mChooserDialog.dismiss();
});
}
@Test
@LargeTest
@SmallTest
public void testUpdateItemWithNoIconToIcon() {
TestThreadUtils.runOnUiThreadBlocking(() -> {
Dialog dialog = mChooserDialog.getDialogForTesting();
......@@ -426,14 +421,11 @@ public class ItemChooserDialogTest implements ItemChooserDialog.ItemSelectedCall
Assert.assertTrue(itemAdapter.getItem(0).hasSameContents(
"key1", "desc1", mTestDrawable1, mTestDrawableDescription1));
}
mChooserDialog.setIdleState();
mChooserDialog.dismiss();
});
}
@Test
@LargeTest
@SmallTest
public void testUpdateItemIcon() {
TestThreadUtils.runOnUiThreadBlocking(() -> {
Dialog dialog = mChooserDialog.getDialogForTesting();
......@@ -463,14 +455,11 @@ public class ItemChooserDialogTest implements ItemChooserDialog.ItemSelectedCall
Assert.assertTrue(itemAdapter.getItem(0).hasSameContents(
"key1", "desc1", mTestDrawable2, mTestDrawableDescription2));
}
mChooserDialog.setIdleState();
mChooserDialog.dismiss();
});
}
@Test
@LargeTest
@SmallTest
@DisabledTest(message = "crbug.com/958558")
public void testSimpleItemSelection() {
Dialog dialog = mChooserDialog.getDialogForTesting();
......@@ -512,12 +501,10 @@ public class ItemChooserDialogTest implements ItemChooserDialog.ItemSelectedCall
// Select the first item and verify it got selected.
selectItem(dialog, 1, "key1", true);
Assert.assertTrue(getDescriptionTextView(dialog, 1).isSelected());
mChooserDialog.dismiss();
}
@Test
@LargeTest
@SmallTest
public void testNoItemsAddedDiscoveryIdle() {
Dialog dialog = mChooserDialog.getDialogForTesting();
Assert.assertTrue(dialog.isShowing());
......@@ -541,12 +528,10 @@ public class ItemChooserDialogTest implements ItemChooserDialog.ItemSelectedCall
Assert.assertEquals(View.VISIBLE, items.getEmptyView().getVisibility());
Assert.assertEquals("statusIdleNoneFound", statusView.getText().toString());
Assert.assertFalse(button.isEnabled());
mChooserDialog.dismiss();
}
@Test
@LargeTest
@SmallTest
public void testPairButtonDisabledAfterSelectedItemRemoved() throws Throwable {
final Dialog dialog = TestThreadUtils.runOnUiThreadBlocking(() -> {
Dialog dialog1 = mChooserDialog.getDialogForTesting();
......@@ -566,13 +551,11 @@ public class ItemChooserDialogTest implements ItemChooserDialog.ItemSelectedCall
mChooserDialog.removeItemFromList("key1");
Assert.assertFalse(button.isEnabled());
mChooserDialog.dismiss();
});
}
@Test
@LargeTest
@SmallTest
public void testSelectAnItemAndRemoveAnotherItem() throws Throwable {
final Dialog dialog = TestThreadUtils.runOnUiThreadBlocking(() -> {
Dialog dialog1 = mChooserDialog.getDialogForTesting();
......@@ -599,13 +582,11 @@ public class ItemChooserDialogTest implements ItemChooserDialog.ItemSelectedCall
mChooserDialog.removeItemFromList("key3");
Assert.assertTrue(button.isEnabled());
Assert.assertEquals("key2", itemAdapter.getSelectedItemKey());
mChooserDialog.dismiss();
});
}
@Test
@LargeTest
@SmallTest
public void testSelectAnItemAndRemoveTheSelectedItem() throws Throwable {
final Dialog dialog = TestThreadUtils.runOnUiThreadBlocking(() -> {
Dialog dialog1 = mChooserDialog.getDialogForTesting();
......@@ -627,13 +608,11 @@ public class ItemChooserDialogTest implements ItemChooserDialog.ItemSelectedCall
mChooserDialog.removeItemFromList("key2");
Assert.assertFalse(button.isEnabled());
Assert.assertEquals("", itemAdapter.getSelectedItemKey());
mChooserDialog.dismiss();
});
}
@Test
@LargeTest
@SmallTest
public void testUpdateItemAndRemoveItemFromList() {
TestThreadUtils.runOnUiThreadBlocking(() -> {
Dialog dialog = mChooserDialog.getDialogForTesting();
......@@ -679,13 +658,11 @@ public class ItemChooserDialogTest implements ItemChooserDialog.ItemSelectedCall
Assert.assertEquals(View.VISIBLE, items.getEmptyView().getVisibility());
Assert.assertEquals("statusIdleNoneFound", statusView.getText().toString());
Assert.assertFalse(button.isEnabled());
mChooserDialog.dismiss();
});
}
@Test
@LargeTest
@SmallTest
public void testAddItemAndRemoveItemFromList() {
TestThreadUtils.runOnUiThreadBlocking(() -> {
Dialog dialog = mChooserDialog.getDialogForTesting();
......@@ -751,13 +728,11 @@ public class ItemChooserDialogTest implements ItemChooserDialog.ItemSelectedCall
Assert.assertEquals(View.VISIBLE, items.getEmptyView().getVisibility());
Assert.assertEquals("statusIdleNoneFound", statusView.getText().toString());
Assert.assertFalse(button.isEnabled());
mChooserDialog.dismiss();
});
}
@Test
@LargeTest
@SmallTest
public void testAddItemWithSameNameToListAndRemoveItemFromList() {
TestThreadUtils.runOnUiThreadBlocking(() -> {
Dialog dialog = mChooserDialog.getDialogForTesting();
......@@ -807,13 +782,11 @@ public class ItemChooserDialogTest implements ItemChooserDialog.ItemSelectedCall
// After removing item 1, item 3 is the only remaining item, so its display text
// also changed to its original description.
Assert.assertEquals("desc1", itemAdapter.getDisplayText(0));
mChooserDialog.dismiss();
});
}
@Test
@LargeTest
@SmallTest
public void testListHeight() {
// 500 * .3 is 150, which is 48 * 3.125. 48 * 3.5 is 168.
Assert.assertEquals(168, ItemChooserDialog.getListHeight(500, 1.0f));
......
......@@ -9,15 +9,17 @@ import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import androidx.test.filters.LargeTest;
import androidx.test.filters.SmallTest;
import org.hamcrest.Matchers;
import org.junit.Assert;
import org.junit.Before;
import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.chromium.base.test.util.Batch;
import org.chromium.base.test.util.CommandLineFlags;
import org.chromium.base.test.util.JniMocker;
import org.chromium.chrome.R;
......@@ -25,6 +27,7 @@ import org.chromium.chrome.browser.ChromeActivity;
import org.chromium.chrome.browser.flags.ChromeSwitches;
import org.chromium.chrome.test.ChromeActivityTestRule;
import org.chromium.chrome.test.ChromeJUnit4ClassRunner;
import org.chromium.chrome.test.batch.BlankCTATabInitialStateRule;
import org.chromium.components.security_state.ConnectionSecurityLevel;
import org.chromium.content_public.browser.test.util.Criteria;
import org.chromium.content_public.browser.test.util.CriteriaHelper;
......@@ -37,11 +40,16 @@ import org.chromium.ui.widget.TextViewWithClickableSpans;
*/
@RunWith(ChromeJUnit4ClassRunner.class)
@CommandLineFlags.Add({ChromeSwitches.DISABLE_FIRST_RUN_EXPERIENCE})
@Batch(BluetoothChooserDialogTest.DEVICE_DIALOG_BATCH_NAME)
public class UsbChooserDialogTest {
@Rule
public ChromeActivityTestRule<ChromeActivity> mActivityTestRule =
@ClassRule
public static final ChromeActivityTestRule<ChromeActivity> sActivityTestRule =
new ChromeActivityTestRule<>(ChromeActivity.class);
@Rule
public final BlankCTATabInitialStateRule mInitialStateRule =
new BlankCTATabInitialStateRule(sActivityTestRule, false);
@Rule
public JniMocker mocker = new JniMocker();
......@@ -65,14 +73,13 @@ public class UsbChooserDialogTest {
@Before
public void setUp() throws Exception {
mocker.mock(UsbChooserDialogJni.TEST_HOOKS, new TestUsbChooserDialogJni());
mActivityTestRule.startMainActivityOnBlankPage();
mChooserDialog = createDialog();
}
private UsbChooserDialog createDialog() {
return TestThreadUtils.runOnUiThreadBlockingNoException(() -> {
UsbChooserDialog dialog = new UsbChooserDialog(/*nativeUsbChooserDialogPtr=*/42);
dialog.show(mActivityTestRule.getActivity(), "https://origin.example.com/",
dialog.show(sActivityTestRule.getActivity(), "https://origin.example.com/",
ConnectionSecurityLevel.SECURE);
return dialog;
});
......@@ -110,7 +117,7 @@ public class UsbChooserDialogTest {
}
@Test
@LargeTest
@SmallTest
public void testCancel() {
Dialog dialog = mChooserDialog.mItemChooserDialog.getDialogForTesting();
Assert.assertTrue(dialog.isShowing());
......@@ -129,7 +136,7 @@ public class UsbChooserDialogTest {
}
@Test
@LargeTest
@SmallTest
public void testSelectItem() {
Dialog dialog = mChooserDialog.mItemChooserDialog.getDialogForTesting();
......@@ -151,7 +158,7 @@ public class UsbChooserDialogTest {
// After adding items to the dialog, the help message should be showing,
// the 'Connect' button should still be disabled (since nothing's selected),
// and the list view should show.
Assert.assertEquals(removeLinkTags(mActivityTestRule.getActivity().getString(
Assert.assertEquals(removeLinkTags(sActivityTestRule.getActivity().getString(
R.string.usb_chooser_dialog_footnote_text)),
statusView.getText().toString());
Assert.assertFalse(button.isEnabled());
......
......@@ -193,6 +193,7 @@ android_library("chrome_java_test_support") {
"javatests/src/org/chromium/chrome/test/MultiActivityTestRule.java",
"javatests/src/org/chromium/chrome/test/ReducedModeNativeTestRule.java",
"javatests/src/org/chromium/chrome/test/TestContentProvider.java",
"javatests/src/org/chromium/chrome/test/batch/BlankCTATabInitialStateRule.java",
"javatests/src/org/chromium/chrome/test/gcore/MockChromeGoogleApiClient.java",
"javatests/src/org/chromium/chrome/test/invalidation/IntentSavingContext.java",
"javatests/src/org/chromium/chrome/test/omaha/AttributeFinder.java",
......
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package org.chromium.chrome.test.batch;
import org.junit.rules.TestRule;
import org.junit.runner.Description;
import org.junit.runners.model.Statement;
import org.chromium.base.test.util.Batch;
import org.chromium.chrome.browser.ChromeActivity;
import org.chromium.chrome.browser.firstrun.FirstRunStatus;
import org.chromium.chrome.browser.tab.TabLaunchType;
import org.chromium.chrome.browser.tabmodel.TabModelUtils;
import org.chromium.chrome.test.ChromeActivityTestRule;
import org.chromium.content_public.browser.test.util.TestThreadUtils;
/**
* To be used by batched tests that would like to reset to a single blank tab open in
* ChromeTabbedActivity between each test, without restarting the Activity.
*
* State is stored statically, and so the Activity may be reused across multiple test suites within
* the same {@link Batch}.
*/
public class BlankCTATabInitialStateRule implements TestRule {
private static ChromeActivity sActivity;
private final ChromeActivityTestRule<ChromeActivity> mActivityTestRule;
private final boolean mClearAllTabState;
/**
* @param activityTestRule The ActivityTestRule to be reset to initial state between each test.
* @param clearAllTabState More thoroughly clears all tab state, at the cost of restarting the
* renderer process between each test.
*/
public BlankCTATabInitialStateRule(
ChromeActivityTestRule<ChromeActivity> activityTestRule, boolean clearAllTabState) {
super();
mActivityTestRule = activityTestRule;
mClearAllTabState = clearAllTabState;
}
@Override
public Statement apply(Statement base, Description description) {
return new Statement() {
@Override
public void evaluate() throws Throwable {
if (sActivity == null) {
TestThreadUtils.runOnUiThreadBlocking(
() -> { FirstRunStatus.setFirstRunFlowComplete(true); });
mActivityTestRule.startMainActivityOnBlankPage();
sActivity = mActivityTestRule.getActivity();
} else {
mActivityTestRule.setActivity(sActivity);
if (mClearAllTabState) {
resetTabStateThorough();
} else {
resetTabStateFast();
}
}
base.evaluate();
}
};
}
// Avoids closing the primary tab (and killing the renderer) in order to reset tab state
// quickly, at the cost of thoroughness. This should be adequate for most tests.
private void resetTabStateFast() {
TestThreadUtils.runOnUiThreadBlocking(() -> {
// Close all but the first tab as these tests expect to start with a single
// tab.
while (TabModelUtils.closeTabByIndex(sActivity.getCurrentTabModel(), 1)) {
}
});
mActivityTestRule.loadUrl("about:blank");
TestThreadUtils.runOnUiThreadBlocking(() -> {
sActivity.getCurrentWebContents().getNavigationController().clearHistory();
});
}
// Thoroughly resets tab state by closing all tabs before restoring the primary tab to
// about:blank state.
private void resetTabStateThorough() {
TestThreadUtils.runOnUiThreadBlocking(() -> {
sActivity.getTabModelSelector().closeAllTabs();
sActivity.getTabCreator(false).launchUrl("about:blank", TabLaunchType.FROM_CHROME_UI);
});
}
}
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