Commit 44bc5572 authored by Yaron Friedman's avatar Yaron Friedman Committed by Chromium LUCI CQ

[Flaky tests] Replace testNewTabWhenPageEdited with unit tests.

Not quite the same coverage but focuses on logic in
TabAssociatedApp class. Other test in TabsOpenedFromExternalAppTest can
serve as integration tests. Also ported over a related test.

Bug: 147853
Change-Id: I51ddc8cc022bc9b44689be52b8daa68c66bcde51
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2637261
Commit-Queue: Yaron Friedman <yfriedman@chromium.org>
Reviewed-by: default avatarTed Choc <tedchoc@chromium.org>
Cr-Commit-Position: refs/heads/master@{#844887}
parent f1b35ff6
......@@ -218,6 +218,7 @@ chrome_junit_test_java_sources = [
"junit/src/org/chromium/chrome/browser/suggestions/SuggestionsImageFetcherTest.java",
"junit/src/org/chromium/chrome/browser/survey/ChromeSurveyControllerTest.java",
"junit/src/org/chromium/chrome/browser/tab/HistoricalTabSaverUnitTest.java",
"junit/src/org/chromium/chrome/browser/tab/TabAssociatedAppTest.java",
"junit/src/org/chromium/chrome/browser/tab/TabAttributesTest.java",
"junit/src/org/chromium/chrome/browser/tab/TabBrowserControlsConstraintsHelperTest.java",
"junit/src/org/chromium/chrome/browser/tab/TabBrowserControlsOffsetHelperTest.java",
......
......@@ -574,85 +574,6 @@ public class TabsOpenedFromExternalAppTest {
mActivityTestRule.getActivity().getActivityTab()));
}
/**
* Tests that a tab is not reused when launched from the same app as an already opened tab and
* when the user has navigated elsewhere manually in the same tab.
*/
@Test
@LargeTest
@Feature({"Navigation"})
public void testNewTabAfterNavigation() {
mActivityTestRule.startMainActivityOnBlankPage();
String url1 = mTestServer.getURL("/chrome/test/data/android/google.html");
String url2 = mTestServer.getURL("/chrome/test/data/android/about.html");
String url3 = mTestServer.getURL("/chrome/test/data/android/test.html");
// Launch a first URL from an app.
launchUrlFromExternalApp(url1, EXTERNAL_APP_1_ID, false);
// Now simulate the user manually navigating to another URL.
mActivityTestRule.loadUrl(url3);
// Launch a second URL from the same app, it should open in a new tab.
int originalTabCount = ChromeTabUtils.getNumOpenTabs(mActivityTestRule.getActivity());
launchUrlFromExternalApp(url2, EXTERNAL_APP_1_ID, false);
int newTabCount = ChromeTabUtils.getNumOpenTabs(mActivityTestRule.getActivity());
Assert.assertEquals("Incorrect number of tabs open", originalTabCount + 1, newTabCount);
Assert.assertEquals("Selected tab is not on the right URL.", url2,
ChromeTabUtils.getUrlStringOnUiThread(
mActivityTestRule.getActivity().getActivityTab()));
}
/**
* Tests that a tab is not reused when launched from the same app as an already opened tab and
* when the user has entered text in the page.
* @throws InterruptedException
*/
/**
* @LargeTest
* @Feature({"Navigation"})
*/
@Test
@FlakyTest(message = "http://crbug.com/6467101")
public void testNewTabWhenPageEdited() throws TimeoutException {
mActivityTestRule.startMainActivityOnBlankPage();
String url1 = mTestServer.getURL("/chrome/test/data/android/google.html");
String url2 = mTestServer.getURL("/chrome/test/data/android/about.html");
// Launch a first URL from an app.
launchUrlFromExternalApp(url1, EXTERNAL_APP_1_ID, false);
// Focus the text-field and type something.
Tab tab = mActivityTestRule.getActivity().getActivityTab();
DOMUtils.focusNode(tab.getWebContents(), "textField");
// Some processing needs to happen before the test-field has the focus.
CriteriaHelper.pollInstrumentationThread(
new ElementFocusedCriteria(
mActivityTestRule.getActivity().getActivityTab(), "textField"),
2000, 200);
// Now type something.
InstrumentationRegistry.getInstrumentation().sendStringSync("banana");
// We also have to wait for the text to happen in the page.
CriteriaHelper.pollInstrumentationThread(
new ElementTextIsCriteria(
mActivityTestRule.getActivity().getActivityTab(), "textField", "banana"),
2000, 200);
// Launch a second URL from the same app, it should open in a new tab.
int originalTabCount = ChromeTabUtils.getNumOpenTabs(mActivityTestRule.getActivity());
launchUrlFromExternalApp(url2, EXTERNAL_APP_1_ID, false);
int newTabCount = ChromeTabUtils.getNumOpenTabs(mActivityTestRule.getActivity());
Assert.assertEquals("Incorrect number of tabs open", originalTabCount + 1, newTabCount);
Assert.assertEquals("Selected tab is not on the right URL.", url2,
ChromeTabUtils.getUrlStringOnUiThread(
mActivityTestRule.getActivity().getActivityTab()));
}
/**
* Catches regressions for https://crbug.com/495877.
*/
......
// Copyright 2021 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.browser.tab;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.when;
import android.support.test.filters.SmallTest;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.annotation.Config;
import org.chromium.base.UserDataHost;
import org.chromium.base.test.BaseRobolectricTestRunner;
import org.chromium.chrome.browser.TabLoadStatus;
import org.chromium.content_public.browser.LoadUrlParams;
import org.chromium.ui.base.PageTransition;
/**
* Tests for {@link TabAttributes}.
*/
@RunWith(BaseRobolectricTestRunner.class)
@Config(manifest = Config.NONE)
public class TabAssociatedAppTest {
private static final String APP_ID = "magicApp";
@Mock
private Tab mTab;
@Captor
ArgumentCaptor<TabObserver> mTabObserverCaptor;
// Hosts the TabAssociatedApp
private final UserDataHost mUserDataHost = new UserDataHost();
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
when(mTab.getUserDataHost()).thenReturn(mUserDataHost);
doNothing().when(mTab).addObserver(mTabObserverCaptor.capture());
}
@Test
@SmallTest
public void testDisassociatesOnInputEvent() {
TabAssociatedApp tabAssociatedApp = TabAssociatedApp.from(mTab);
tabAssociatedApp.setAppId(APP_ID);
Assert.assertEquals(APP_ID, tabAssociatedApp.getAppId());
// Simulate an event (without going through input system)
tabAssociatedApp.onImeEvent();
Assert.assertNull(tabAssociatedApp.getAppId());
}
@Test
@SmallTest
public void testDisassociatesOnOmniboxPageLoad() {
TabAssociatedApp tabAssociatedApp = TabAssociatedApp.from(mTab);
mTabObserverCaptor.getValue().onInitialized(mTab, APP_ID);
Assert.assertEquals(APP_ID, tabAssociatedApp.getAppId());
mTabObserverCaptor.getValue().onLoadUrl(mTab,
new LoadUrlParams("foobar.com", PageTransition.FROM_ADDRESS_BAR),
TabLoadStatus.DEFAULT_PAGE_LOAD);
Assert.assertNull(tabAssociatedApp.getAppId());
}
@Test
@SmallTest
public void testDoesNotDisassociateOnNormalPageLoad() {
TabAssociatedApp tabAssociatedApp = TabAssociatedApp.from(mTab);
mTabObserverCaptor.getValue().onInitialized(mTab, APP_ID);
Assert.assertEquals(APP_ID, tabAssociatedApp.getAppId());
mTabObserverCaptor.getValue().onLoadUrl(mTab,
new LoadUrlParams("foobar.com", PageTransition.LINK),
TabLoadStatus.DEFAULT_PAGE_LOAD);
Assert.assertEquals(APP_ID, tabAssociatedApp.getAppId());
}
}
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