OLD | NEW |
---|---|
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package org.chromium.chrome.browser.tabmodel; | 5 package org.chromium.chrome.browser.tabmodel; |
6 | 6 |
7 import android.test.suitebuilder.annotation.MediumTest; | 7 import android.test.suitebuilder.annotation.MediumTest; |
8 | 8 |
9 import org.chromium.base.ThreadUtils; | 9 import org.chromium.base.ThreadUtils; |
10 import org.chromium.base.test.util.FlakyTest; | 10 import org.chromium.base.test.util.FlakyTest; |
11 import org.chromium.base.test.util.Restriction; | 11 import org.chromium.base.test.util.Restriction; |
12 import org.chromium.base.test.util.UrlUtils; | |
13 import org.chromium.chrome.browser.tab.EmptyTabObserver; | |
12 import org.chromium.chrome.browser.tab.Tab; | 14 import org.chromium.chrome.browser.tab.Tab; |
13 import org.chromium.chrome.browser.tabmodel.TabModel.TabLaunchType; | 15 import org.chromium.chrome.browser.tabmodel.TabModel.TabLaunchType; |
14 import org.chromium.chrome.browser.tabmodel.TabModel.TabSelectionType; | 16 import org.chromium.chrome.browser.tabmodel.TabModel.TabSelectionType; |
15 import org.chromium.chrome.test.ChromeTabbedActivityTestBase; | 17 import org.chromium.chrome.test.ChromeTabbedActivityTestBase; |
16 import org.chromium.chrome.test.util.ChromeRestriction; | 18 import org.chromium.chrome.test.util.ChromeRestriction; |
17 import org.chromium.content.browser.test.util.CallbackHelper; | 19 import org.chromium.content.browser.test.util.CallbackHelper; |
18 import org.chromium.content_public.browser.LoadUrlParams; | 20 import org.chromium.content_public.browser.LoadUrlParams; |
19 | 21 |
20 import java.util.concurrent.TimeoutException; | 22 import java.util.concurrent.TimeoutException; |
21 | 23 |
22 /** | 24 /** |
23 * Tests undo and restoring of tabs in a {@link TabModel}. | 25 * Tests undo and restoring of tabs in a {@link TabModel}. |
24 */ | 26 */ |
25 public class UndoTabModelTest extends ChromeTabbedActivityTestBase { | 27 public class UndoTabModelTest extends ChromeTabbedActivityTestBase { |
26 private static final Tab[] EMPTY = new Tab[] { }; | 28 private static final Tab[] EMPTY = new Tab[] { }; |
29 private static final String TEST_URL = UrlUtils.encodeHtmlDataUri("<html>poi t.</html>"); | |
27 | 30 |
28 @Override | 31 @Override |
29 public void startMainActivity() throws InterruptedException { | 32 public void startMainActivity() throws InterruptedException { |
30 startMainActivityOnBlankPage(); | 33 startMainActivityOnBlankPage(); |
31 } | 34 } |
32 | 35 |
33 private void checkState( | 36 private void checkState( |
34 final TabModel model, final Tab[] tabsList, final Tab selectedTab, | 37 final TabModel model, final Tab[] tabsList, final Tab selectedTab, |
35 final Tab[] closingTabs, final Tab[] fullTabsList, | 38 final Tab[] closingTabs, final Tab[] fullTabsList, |
36 final Tab fullSelectedTab) { | 39 final Tab fullSelectedTab) { |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
68 private void createTabOnUiThread(final ChromeTabCreator tabCreator) { | 71 private void createTabOnUiThread(final ChromeTabCreator tabCreator) { |
69 ThreadUtils.runOnUiThreadBlocking(new Runnable() { | 72 ThreadUtils.runOnUiThreadBlocking(new Runnable() { |
70 @Override | 73 @Override |
71 public void run() { | 74 public void run() { |
72 tabCreator.createNewTab(new LoadUrlParams("about:blank"), | 75 tabCreator.createNewTab(new LoadUrlParams("about:blank"), |
73 TabLaunchType.FROM_CHROME_UI, null); | 76 TabLaunchType.FROM_CHROME_UI, null); |
74 } | 77 } |
75 }); | 78 }); |
76 } | 79 } |
77 | 80 |
81 private void createTabOnUiThread(final ChromeTabCreator tabCreator, final St ring url) { | |
82 ThreadUtils.runOnUiThreadBlocking(new Runnable() { | |
83 @Override | |
84 public void run() { | |
85 tabCreator.createNewTab(new LoadUrlParams(url), | |
86 TabLaunchType.FROM_CHROME_UI, null); | |
87 } | |
88 }); | |
89 } | |
90 | |
91 | |
78 private void selectTabOnUiThread(final TabModel model, final Tab tab) { | 92 private void selectTabOnUiThread(final TabModel model, final Tab tab) { |
79 ThreadUtils.runOnUiThreadBlocking(new Runnable() { | 93 ThreadUtils.runOnUiThreadBlocking(new Runnable() { |
80 @Override | 94 @Override |
81 public void run() { | 95 public void run() { |
82 model.setIndex(model.indexOf(tab), TabSelectionType.FROM_USER); | 96 model.setIndex(model.indexOf(tab), TabSelectionType.FROM_USER); |
83 } | 97 } |
84 }); | 98 }); |
85 } | 99 } |
86 | 100 |
87 private void closeTabOnUiThread( | 101 private void closeTabOnUiThread( |
(...skipping 226 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
314 }); | 328 }); |
315 | 329 |
316 for (int i = 0; i < selector.getModels().size(); i++) { | 330 for (int i = 0; i < selector.getModels().size(); i++) { |
317 TabList tabs = selector.getModelAt(i).getComprehensiveModel(); | 331 TabList tabs = selector.getModelAt(i).getComprehensiveModel(); |
318 for (int j = 0; j < tabs.getCount(); j++) { | 332 for (int j = 0; j < tabs.getCount(); j++) { |
319 assertFalse(tabs.isClosurePending(tabs.getTabAt(j).getId())); | 333 assertFalse(tabs.isClosurePending(tabs.getTabAt(j).getId())); |
320 } | 334 } |
321 } | 335 } |
322 } | 336 } |
323 | 337 |
338 private void openRecentClosedTabOnUiThread(final TabModelSelector selector) { | |
Theresa
2016/06/29 23:53:29
nit: openRecentlyClosedTabOnUiThread
xingliu
2016/06/30 05:43:55
Done.
| |
339 ThreadUtils.runOnUiThreadBlocking(new Runnable() { | |
340 @Override | |
341 public void run() { | |
342 ((TabModelSelectorImpl) selector).getCurrentModel().openRecentCl osedTab(); | |
343 } | |
344 }); | |
345 } | |
346 | |
324 /** | 347 /** |
325 * Test undo with a single tab with the following actions/expected states: | 348 * Test undo with a single tab with the following actions/expected states: |
326 * Action Model List Close List Compr ehensive List | 349 * Action Model List Close List Compr ehensive List |
327 * 1. Initial State [ 0s ] - [ 0s ] | 350 * 1. Initial State [ 0s ] - [ 0s ] |
328 * 2. CloseTab(0, allow undo) - [ 0 ] [ 0s ] | 351 * 2. CloseTab(0, allow undo) - [ 0 ] [ 0s ] |
329 * 3. CancelClose(0) [ 0s ] - [ 0s ] | 352 * 3. CancelClose(0) [ 0s ] - [ 0s ] |
330 * 4. CloseTab(0, allow undo) - [ 0 ] [ 0s ] | 353 * 4. CloseTab(0, allow undo) - [ 0 ] [ 0s ] |
331 * 5. CommitClose(0) - - - | 354 * 5. CommitClose(0) - - - |
332 * 6. CreateTab(0) [ 0s ] - [ 0s ] | 355 * 6. CreateTab(0) [ 0s ] - [ 0s ] |
333 * 7. CloseTab(0, allow undo) - [ 0 ] [ 0s ] | 356 * 7. CloseTab(0, allow undo) - [ 0 ] [ 0s ] |
(...skipping 1069 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1403 closeTabOnUiThread(model, tab0, true); | 1426 closeTabOnUiThread(model, tab0, true); |
1404 checkState(model, new Tab[] { tab1 }, tab1, EMPTY, fullList, tab1); | 1427 checkState(model, new Tab[] { tab1 }, tab1, EMPTY, fullList, tab1); |
1405 | 1428 |
1406 // 3. | 1429 // 3. |
1407 saveStateOnUiThread(selector); | 1430 saveStateOnUiThread(selector); |
1408 fullList = new Tab[] { tab1 }; | 1431 fullList = new Tab[] { tab1 }; |
1409 checkState(model, new Tab[] { tab1 }, tab1, EMPTY, fullList, tab1); | 1432 checkState(model, new Tab[] { tab1 }, tab1, EMPTY, fullList, tab1); |
1410 assertTrue(tab0.isClosing()); | 1433 assertTrue(tab0.isClosing()); |
1411 assertFalse(tab0.isInitialized()); | 1434 assertFalse(tab0.isInitialized()); |
1412 } | 1435 } |
1436 | |
1437 /** | |
1438 * Test open recently closed tab which uses rewinding list in java. | |
Theresa
2016/06/29 23:53:29
nit: Test opening recently closed tabs using the r
xingliu
2016/06/30 05:43:55
Done.
| |
1439 * Open recently closed tab first uses rewind list in java, then try to use | |
1440 * native tab_restore_service if nothing in rewind list. | |
1441 * @throws InterruptedException | |
1442 */ | |
1443 @MediumTest | |
1444 public void testOpenRecentClosedTab() throws InterruptedException { | |
Theresa
2016/06/29 23:53:30
nit: testOpenRecentlyClosedTab()
xingliu
2016/06/30 05:43:56
Done.
| |
1445 TabModelSelector selector = getActivity().getTabModelSelector(); | |
1446 TabModel model = selector.getModel(false); | |
1447 ChromeTabCreator tabCreator = getActivity().getTabCreator(false); | |
1448 | |
1449 createTabOnUiThread(tabCreator, TEST_URL); | |
Theresa
2016/06/29 23:53:30
This test doesn't rely on native, so I think we sh
xingliu
2016/06/30 05:43:56
Done.
| |
1450 | |
1451 Tab tab0 = model.getTabAt(0); | |
1452 Tab tab1 = model.getTabAt(1); | |
1453 Tab[] allTabs = new Tab[]{tab0, tab1}; | |
1454 | |
1455 closeTabOnUiThread(model, tab1, true); | |
1456 checkState(model, new Tab[]{tab0}, tab0, new Tab[]{tab1}, allTabs, tab0) ; | |
1457 | |
1458 // ensure tab recovery, ordering, and reuse of {@link Tab} objects in ja va. | |
1459 openRecentClosedTabOnUiThread(selector); | |
1460 checkState(model, allTabs, tab0, EMPTY, allTabs, tab0); | |
1461 } | |
1462 | |
1463 // helper class to wait for fully loaded page. | |
Theresa
2016/06/29 23:53:30
nit: Helper class that notifies when a page load i
xingliu
2016/06/30 05:43:55
Done.
| |
1464 private class TestTabObserver extends EmptyTabObserver { | |
Theresa
2016/06/29 23:53:30
Let's put this by the rest of the private methods
xingliu
2016/06/30 05:43:56
Done.
| |
1465 private CallbackHelper mCallbackHelper; | |
1466 private TabModel mModel; | |
1467 public TestTabObserver(CallbackHelper callbackHelper, TabModel model) { | |
1468 super(); | |
1469 mModel = model; | |
1470 mCallbackHelper = callbackHelper; | |
1471 } | |
1472 @Override | |
1473 public void onPageLoadFinished(Tab tab) { | |
1474 // close the page after tab is loaded, close it and commit closure. | |
1475 try { | |
1476 assertEquals(mModel.getCount(), 2); | |
1477 UndoTabModelTest.this.closeTabOnUiThread(mModel, tab, false); | |
1478 assertEquals(1, mModel.getCount()); | |
1479 mCallbackHelper.notifyCalled(); | |
Theresa
2016/06/29 23:53:30
I think only the callback should be notified from
xingliu
2016/06/30 05:43:55
Done.
xingliu
2016/06/30 05:43:56
Make sense.
| |
1480 } catch (InterruptedException e) { | |
1481 // ignore interrupt. | |
1482 } | |
1483 } | |
1484 } | |
1485 | |
1486 /** | |
1487 * Test open recently closed tab which uses native code from tab restore ser vice. | |
1488 * Open recently closed tab first use rewind list in java, then try to use | |
1489 * native tab_restore_service if nothing in rewind list. | |
1490 * @throws InterruptedException | |
1491 */ | |
1492 @MediumTest | |
1493 public void testOpenRecentClosedTabNative() throws InterruptedException { | |
1494 final TabModelSelector selector = getActivity().getTabModelSelector(); | |
1495 final TabModel model = selector.getModel(false); | |
1496 final ChromeTabCreator tabCreator = getActivity().getTabCreator(false); | |
1497 final CallbackHelper tabCallbackHelper = new CallbackHelper(); | |
1498 final TestTabObserver observer = new TestTabObserver(tabCallbackHelper, model); | |
1499 | |
1500 // create new tab and attach observer to listen to loaded event. | |
1501 // reason to do this is we need to wait the page to be fully loaded, the n it has navigation | |
Theresa
2016/06/29 23:53:30
nit: I would drop the "reason to do this is" and j
xingliu
2016/06/30 05:43:55
Done.
| |
1502 // history, then native code can successfully recover the page. | |
1503 ThreadUtils.runOnUiThreadBlocking(new Runnable() { | |
1504 @Override | |
1505 public void run() { | |
1506 tabCreator.createNewTab(new LoadUrlParams(TEST_URL), | |
1507 TabLaunchType.FROM_CHROME_UI, null).addObserver(observer ); | |
1508 } | |
1509 }); | |
1510 | |
1511 // wait for the page to be fully loaded. | |
1512 try { | |
1513 tabCallbackHelper.waitForCallback(0); | |
1514 } catch (TimeoutException e) { | |
1515 fail(); | |
1516 } | |
1517 | |
1518 // recover the page. | |
1519 openRecentClosedTabOnUiThread(selector); | |
1520 | |
1521 assertEquals(2, model.getCount()); | |
1522 Tab newTab = model.getTabAt(1); | |
1523 assertEquals(TEST_URL, newTab.getUrl()); | |
1524 assertTrue(TabModelUtils.getCurrentTab(model) == newTab); | |
Theresa
2016/06/29 23:53:30
We should call checkState here too.
xingliu
2016/06/30 05:43:55
Done.
| |
1525 } | |
Theresa
2016/06/29 23:53:30
Can we add a test that makes sure tabs are restore
xingliu
2016/06/30 05:43:55
Implemented another test case in the patch. But hi
xingliu
2016/06/30 05:43:56
Done.
| |
1413 } | 1526 } |
OLD | NEW |