Commit ef37a4f2 authored by Gang Wu's avatar Gang Wu Committed by Commit Bot

Reland "TabModel type should same as Tab type when switching tab"

This is a reland of a1ee3eea

Original change's description:
> TabModel type should same as Tab type when switching tab
> 
> We were use getCurrentModel() to switch tab, and this will be a problem
> in multi window mode. We should choose the model(incognito or not) same
> as the tab which we will switch to.
> 
> Bug: 1121102
> Change-Id: Ie9742fb4a1b38f921c01d7f8ffd47a813b1b5a14
> Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2379093
> Reviewed-by: Ted Choc <tedchoc@chromium.org>
> Commit-Queue: Gang Wu <gangwu@chromium.org>
> Cr-Commit-Position: refs/heads/master@{#802295}

Bug: 1121102
Change-Id: I5d943085b61f7d8a74bec78c1f9400404ed03de2
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2382211
Commit-Queue: Gang Wu <gangwu@chromium.org>
Reviewed-by: default avatarTed Choc <tedchoc@chromium.org>
Cr-Commit-Position: refs/heads/master@{#802916}
parent 767ae8de
......@@ -576,7 +576,9 @@ public class LayoutManagerChrome
Tab lastTab = getTabById(lastTabId);
if (NewTabPage.isNTPUrl(lastTab.getUrl()) && !lastTab.canGoBack()
&& !lastTab.canGoForward()) {
getTabModelSelector().getCurrentModel().closeTab(lastTab, tab, false, false, false);
getTabModelSelector()
.getModel(lastTab.isIncognito())
.closeTab(lastTab, tab, false, false, false);
}
}
}
......@@ -595,9 +595,10 @@ class AutocompleteMediator implements OnSuggestionsReceivedListener, StartStopWi
ChromeActivity chromeActivity =
(ChromeActivity) tab.getWindowAndroid().getActivity().get();
int tabIndex = TabModelUtils.getTabIndexById(
chromeActivity.getTabModelSelector().getCurrentModel(), tab.getId());
chromeActivity.getTabModelSelector().getCurrentModel().setIndex(
tabIndex, TabSelectionType.FROM_OMNIBOX);
chromeActivity.getTabModelSelector().getModel(tab.isIncognito()), tab.getId());
chromeActivity.getTabModelSelector()
.getModel(tab.isIncognito())
.setIndex(tabIndex, TabSelectionType.FROM_OMNIBOX);
} else {
// Browser is in background, bring to to foreground and switch to the tab.
Intent newIntent = ChromeIntentUtil.createBringTabToFrontIntent(tab.getId());
......
......@@ -4,10 +4,14 @@
package org.chromium.chrome.browser.omnibox.suggestions;
import static org.chromium.chrome.browser.multiwindow.MultiWindowTestHelper.moveActivityToFront;
import static org.chromium.chrome.browser.multiwindow.MultiWindowTestHelper.waitForSecondChromeTabbedActivity;
import android.app.Activity;
import android.app.Instrumentation;
import android.app.Instrumentation.ActivityMonitor;
import android.content.Intent;
import android.os.Build;
import android.support.test.InstrumentationRegistry;
import android.text.TextUtils;
import android.view.ViewGroup;
......@@ -28,9 +32,12 @@ import org.chromium.base.ActivityState;
import org.chromium.base.ThreadUtils;
import org.chromium.base.test.util.CommandLineFlags;
import org.chromium.base.test.util.DisabledTest;
import org.chromium.base.test.util.MinAndroidSdkLevel;
import org.chromium.chrome.R;
import org.chromium.chrome.browser.app.ChromeActivity;
import org.chromium.chrome.browser.ChromeTabbedActivity;
import org.chromium.chrome.browser.ChromeTabbedActivity2;
import org.chromium.chrome.browser.flags.ChromeSwitches;
import org.chromium.chrome.browser.multiwindow.MultiWindowUtils;
import org.chromium.chrome.browser.omnibox.LocationBarLayout;
import org.chromium.chrome.browser.omnibox.UrlBar;
import org.chromium.chrome.browser.omnibox.suggestions.base.BaseSuggestionView;
......@@ -39,7 +46,9 @@ import org.chromium.chrome.browser.searchwidget.SearchWidgetProvider;
import org.chromium.chrome.browser.tab.Tab;
import org.chromium.chrome.test.ChromeActivityTestRule;
import org.chromium.chrome.test.ChromeJUnit4ClassRunner;
import org.chromium.chrome.test.ChromeTabbedActivityTestRule;
import org.chromium.chrome.test.util.ChromeTabUtils;
import org.chromium.chrome.test.util.MenuUtils;
import org.chromium.chrome.test.util.OmniboxTestUtils;
import org.chromium.chrome.test.util.WaitForFocusHelper;
import org.chromium.chrome.test.util.browser.Features.EnableFeatures;
......@@ -60,8 +69,7 @@ import java.util.List;
@CommandLineFlags.Add({ChromeSwitches.DISABLE_FIRST_RUN_EXPERIENCE})
public class SwitchToTabTest {
@Rule
public ChromeActivityTestRule<ChromeActivity> mActivityTestRule =
new ChromeActivityTestRule<>(ChromeActivity.class);
public ChromeTabbedActivityTestRule mActivityTestRule = new ChromeTabbedActivityTestRule();
private static final int INVALID_INDEX = -1;
private EmbeddedTestServer mTestServer;
......@@ -89,6 +97,7 @@ public class SwitchToTabTest {
Assert.assertNotNull(urlBar);
WaitForFocusHelper.acquireFocusForView(urlBar);
OmniboxTestUtils.waitForFocusAndKeyboardActive(urlBar, true);
TestThreadUtils.runOnUiThreadBlocking(() -> { urlBar.setText(text); });
}
......@@ -266,9 +275,46 @@ public class SwitchToTabTest {
Tab tab = mActivityTestRule.getActivity().getActivityTab();
Criteria.checkThat(tab, Matchers.notNullValue());
Criteria.checkThat(tab, Matchers.is(aboutTab));
// Make sure tab is in either upload page or result page. cannot only verify one of
// them since on fast device tab jump to result page really quick but on slow device
// may stay on upload page for a really long time.
Criteria.checkThat(tab.getUrlString(), Matchers.is(testHttpsUrl1));
});
}
@Test
@MediumTest
@MinAndroidSdkLevel(Build.VERSION_CODES.N)
@EnableFeatures("OmniboxTabSwitchSuggestions")
@CommandLineFlags.Add(ChromeSwitches.DISABLE_TAB_MERGING_FOR_TESTING)
public void testSwitchToTabSuggestionWhenIncognitoTabOnTop() throws InterruptedException {
mTestServer = EmbeddedTestServer.createAndStartHTTPSServer(
InstrumentationRegistry.getInstrumentation().getContext(),
ServerCertificate.CERT_OK);
final String testHttpsUrl1 = mTestServer.getURL("/chrome/test/data/android/about.html");
final String testHttpsUrl2 = mTestServer.getURL("/chrome/test/data/android/ok.txt");
final String testHttpsUrl3 = mTestServer.getURL("/chrome/test/data/android/test.html");
mActivityTestRule.loadUrlInNewTab(testHttpsUrl2);
mActivityTestRule.loadUrlInNewTab(testHttpsUrl3);
final Tab aboutTab = mActivityTestRule.loadUrlInNewTab(testHttpsUrl1);
// Move "about.html" page to cta2 and create an incognito tab on top of "about.html".
final ChromeTabbedActivity cta1 = mActivityTestRule.getActivity();
MultiWindowUtils.getInstance().setIsInMultiWindowModeForTesting(true);
MenuUtils.invokeCustomMenuActionSync(InstrumentationRegistry.getInstrumentation(), cta1,
R.id.move_to_other_window_menu_id);
final ChromeTabbedActivity2 cta2 = waitForSecondChromeTabbedActivity();
ChromeActivityTestRule.waitForActivityNativeInitializationComplete(cta2);
ChromeTabUtils.newTabFromMenu(InstrumentationRegistry.getInstrumentation(), cta2,
true /*incognito*/, false /*waitForNtpLoad*/);
moveActivityToFront(cta1);
// Switch back to cta1, and try to switch to "about.html" in cta2.
LocationBarLayout locationBarLayout =
(LocationBarLayout) cta1.findViewById(R.id.location_bar);
typeAndClickMatchingTabMatchSuggestion(cta1, locationBarLayout, aboutTab);
CriteriaHelper.pollUiThread(() -> {
Tab tab = cta2.getActivityTab();
Criteria.checkThat(tab, Matchers.notNullValue());
Criteria.checkThat(tab, Matchers.is(aboutTab));
Criteria.checkThat(tab.getUrlString(), Matchers.is(testHttpsUrl1));
});
}
......
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