| Index: chrome/android/javatests/src/org/chromium/chrome/browser/tabmodel/UndoTabModelTest.java
|
| diff --git a/chrome/android/javatests/src/org/chromium/chrome/browser/tabmodel/UndoTabModelTest.java b/chrome/android/javatests/src/org/chromium/chrome/browser/tabmodel/UndoTabModelTest.java
|
| index 450008e09e7430010450826c8935ed3bfbc5039d..4570879a0f5da316cf2f2651111cec7fbeed87af 100644
|
| --- a/chrome/android/javatests/src/org/chromium/chrome/browser/tabmodel/UndoTabModelTest.java
|
| +++ b/chrome/android/javatests/src/org/chromium/chrome/browser/tabmodel/UndoTabModelTest.java
|
| @@ -4,11 +4,18 @@
|
|
|
| package org.chromium.chrome.browser.tabmodel;
|
|
|
| +import android.annotation.TargetApi;
|
| +import android.os.Build;
|
| import android.test.suitebuilder.annotation.MediumTest;
|
|
|
| import org.chromium.base.ThreadUtils;
|
| import org.chromium.base.test.util.FlakyTest;
|
| +import org.chromium.base.test.util.MinAndroidSdkLevel;
|
| import org.chromium.base.test.util.Restriction;
|
| +import org.chromium.base.test.util.UrlUtils;
|
| +import org.chromium.chrome.browser.ChromeTabbedActivity2;
|
| +import org.chromium.chrome.browser.multiwindow.MultiWindowUtilsTest;
|
| +import org.chromium.chrome.browser.tab.EmptyTabObserver;
|
| import org.chromium.chrome.browser.tab.Tab;
|
| import org.chromium.chrome.browser.tabmodel.TabModel.TabLaunchType;
|
| import org.chromium.chrome.browser.tabmodel.TabModel.TabSelectionType;
|
| @@ -24,6 +31,7 @@ import java.util.concurrent.TimeoutException;
|
| */
|
| public class UndoTabModelTest extends ChromeTabbedActivityTestBase {
|
| private static final Tab[] EMPTY = new Tab[] { };
|
| + private static final String TEST_URL = UrlUtils.encodeHtmlDataUri("<html>poit.</html>");
|
|
|
| @Override
|
| public void startMainActivity() throws InterruptedException {
|
| @@ -321,6 +329,28 @@ public class UndoTabModelTest extends ChromeTabbedActivityTestBase {
|
| }
|
| }
|
|
|
| + private void openMostRecentlyClosedTabOnUiThread(final TabModelSelector selector) {
|
| + ThreadUtils.runOnUiThreadBlocking(new Runnable() {
|
| + @Override
|
| + public void run() {
|
| + ((TabModelSelectorImpl) selector).getCurrentModel().openMostRecentlyClosedTab();
|
| + }
|
| + });
|
| + }
|
| +
|
| + // Helper class that notifies when a page load is finished.
|
| + private static class TestTabObserver extends EmptyTabObserver {
|
| + private CallbackHelper mCallbackHelper;
|
| + public TestTabObserver(CallbackHelper callbackHelper) {
|
| + super();
|
| + mCallbackHelper = callbackHelper;
|
| + }
|
| + @Override
|
| + public void onPageLoadFinished(Tab tab) {
|
| + mCallbackHelper.notifyCalled();
|
| + }
|
| + }
|
| +
|
| /**
|
| * Test undo with a single tab with the following actions/expected states:
|
| * Action Model List Close List Comprehensive List
|
| @@ -1410,4 +1440,138 @@ public class UndoTabModelTest extends ChromeTabbedActivityTestBase {
|
| assertTrue(tab0.isClosing());
|
| assertFalse(tab0.isInitialized());
|
| }
|
| +
|
| + /**
|
| + * Test opening recently closed tabs using the rewound list in Java
|
| + * @throws InterruptedException
|
| + */
|
| + @MediumTest
|
| + public void testOpenRecentlyClosedTab() throws InterruptedException {
|
| + TabModelSelector selector = getActivity().getTabModelSelector();
|
| + TabModel model = selector.getModel(false);
|
| + ChromeTabCreator tabCreator = getActivity().getTabCreator(false);
|
| +
|
| + createTabOnUiThread(tabCreator);
|
| +
|
| + Tab tab0 = model.getTabAt(0);
|
| + Tab tab1 = model.getTabAt(1);
|
| + Tab[] allTabs = new Tab[]{tab0, tab1};
|
| +
|
| + closeTabOnUiThread(model, tab1, true);
|
| + checkState(model, new Tab[]{tab0}, tab0, new Tab[]{tab1}, allTabs, tab0);
|
| +
|
| + // Ensure tab recovery, ordering, and reuse of {@link Tab} objects in java.
|
| + openMostRecentlyClosedTabOnUiThread(selector);
|
| + checkState(model, allTabs, tab0, EMPTY, allTabs, tab0);
|
| + }
|
| +
|
| + /**
|
| + * Test opening recently closed tab using native tab restore service.
|
| + * @throws InterruptedException
|
| + */
|
| + @MediumTest
|
| + public void testOpenRecentlyClosedTabNative() throws InterruptedException {
|
| + final TabModelSelector selector = getActivity().getTabModelSelector();
|
| + final TabModel model = selector.getModel(false);
|
| + final ChromeTabCreator tabCreator = getActivity().getTabCreator(false);
|
| + final CallbackHelper tabCallbackHelper = new CallbackHelper();
|
| + final TestTabObserver observer = new TestTabObserver(tabCallbackHelper);
|
| +
|
| + // Create new tab and attach observer to listen to loaded event.
|
| + // We need to listen for page load finished events, then it has navigation
|
| + // history, then native code can successfully recover the page.
|
| + ThreadUtils.runOnUiThreadBlocking(new Runnable() {
|
| + @Override
|
| + public void run() {
|
| + tabCreator.createNewTab(new LoadUrlParams(TEST_URL),
|
| + TabLaunchType.FROM_CHROME_UI, null).addObserver(observer);
|
| + }
|
| + });
|
| +
|
| + // Wait for the page to be fully loaded.
|
| + try {
|
| + tabCallbackHelper.waitForCallback(0);
|
| + } catch (TimeoutException e) {
|
| + fail("Failed to load the tab.");
|
| + }
|
| +
|
| + // Close the tab, and commit pending closure.
|
| + assertEquals(model.getCount(), 2);
|
| + closeTabOnUiThread(model, model.getTabAt(1), false);
|
| + assertEquals(1, model.getCount());
|
| +
|
| + // Recover the page.
|
| + openMostRecentlyClosedTabOnUiThread(selector);
|
| +
|
| + assertEquals(2, model.getCount());
|
| + Tab newTab = model.getTabAt(1);
|
| + assertEquals(TEST_URL, newTab.getUrl());
|
| + Tab[] tabs = new Tab[]{model.getTabAt(0), newTab};
|
| + checkState(model, tabs, newTab, EMPTY, tabs, newTab);
|
| + }
|
| +
|
| + /**
|
| + * Test opening recently closed tab when we have multiple window.
|
| + * @throws InterruptedException
|
| + */
|
| + @MediumTest
|
| + @MinAndroidSdkLevel(24)
|
| + @TargetApi(Build.VERSION_CODES.LOLLIPOP)
|
| + public void testOpenRecentlyClosedTabMultiWindow() throws InterruptedException {
|
| + ChromeTabbedActivity2 secondWindow =
|
| + MultiWindowUtilsTest.createSecondChromeTabbedActivity(getActivity());
|
| +
|
| + // first window context.
|
| + final TabModelSelector firstSelector = getActivity().getTabModelSelector();
|
| + final TabModel firstModel = firstSelector.getModel(false);
|
| + final ChromeTabCreator firstTabCreator = getActivity().getTabCreator(false);
|
| +
|
| + // second window context.
|
| + final TabModelSelector secondSelector = secondWindow.getTabModelSelector();
|
| + final TabModel secondModel = secondSelector.getModel(false);
|
| +
|
| + final CallbackHelper tabCallbackHelper = new CallbackHelper();
|
| + final TestTabObserver observer = new TestTabObserver(tabCallbackHelper);
|
| +
|
| + // Assert first window has 1 tab and second window has no tabs.
|
| + assertEquals("Unexpecetd number of tabs in first window.", firstModel.getCount(), 1);
|
| + assertEquals("Unexpecetd number of tabs in second window.", 0, secondModel.getCount());
|
| +
|
| + // TODO(xingliu): Another test case to create tab on second window,
|
| + // currently hit exception while do stuff in second window.
|
| + ThreadUtils.runOnUiThreadBlocking(new Runnable() {
|
| + @Override
|
| + public void run() {
|
| + firstTabCreator.createNewTab(new LoadUrlParams(TEST_URL),
|
| + TabLaunchType.FROM_CHROME_UI, null).addObserver(observer);
|
| + }
|
| + });
|
| +
|
| + // Must wait for the page to be fully loaded.
|
| + try {
|
| + tabCallbackHelper.waitForCallback(0);
|
| + } catch (TimeoutException e) {
|
| + fail("Failed to load the tab.");
|
| + }
|
| + assertEquals("Unexpecetd number of tabs in first window.", 2, firstModel.getCount());
|
| + assertEquals("Unexpecetd number of tabs in second window.", 0, secondModel.getCount());
|
| +
|
| + closeTabOnUiThread(firstModel, firstModel.getTabAt(1), false);
|
| + assertEquals("Unexpecetd number of tabs in first window.", 1, firstModel.getCount());
|
| + assertEquals("Unexpecetd number of tabs in second window.", 0, secondModel.getCount());
|
| +
|
| + // Recover the page.
|
| + openMostRecentlyClosedTabOnUiThread(firstSelector);
|
| + assertEquals(2, firstModel.getCount());
|
| + assertEquals(0, secondModel.getCount());
|
| +
|
| + Tab[] firstWindowTabs = new Tab[]{firstModel.getTabAt(0), firstModel.getTabAt(1)};
|
| +
|
| + checkState(firstModel, firstWindowTabs, firstModel.getTabAt(1), EMPTY, firstWindowTabs,
|
| + firstModel.getTabAt(1));
|
| + checkState(secondModel, EMPTY, null, EMPTY, EMPTY, null);
|
| +
|
| + // Close the second window.
|
| + secondWindow.finishAndRemoveTask();
|
| + }
|
| }
|
|
|