1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/chrome_notification_types.h"
6 #include "chrome/browser/sessions/tab_restore_service.h"
7 #include "chrome/browser/sessions/tab_restore_service_delegate.h"
8 #include "chrome/browser/sessions/tab_restore_service_factory.h"
9 #include "chrome/browser/ui/browser.h"
10 #include "chrome/browser/ui/browser_commands.h"
11 #include "chrome/browser/ui/browser_list.h"
12 #include "chrome/browser/ui/tabs/tab_strip_model.h"
13 #include "chrome/browser/ui/toolbar/recent_tabs_sub_menu_model.h"
14 #include "chrome/test/base/in_process_browser_test.h"
15 #include "chrome/test/base/interactive_test_utils.h"
16 #include "content/public/browser/web_contents.h"
17 #include "content/public/test/browser_test_utils.h"
19 typedef InProcessBrowserTest BrowserTabRestoreTest
;
21 void AwaitTabsReady(content::DOMMessageQueue
* message_queue
, int tabs
) {
22 for (int i
= 0; i
< tabs
; ++i
) {
24 EXPECT_TRUE(message_queue
->WaitForMessage(&message
));
25 EXPECT_EQ("\"READY\"", message
);
29 void CheckVisbility(TabStripModel
* tab_strip_model
, int visible_index
) {
30 for (int i
= 0; i
< tab_strip_model
->count(); ++i
) {
31 content::WebContents
* contents
= tab_strip_model
->GetWebContentsAt(i
);
32 std::string document_visibility_state
;
33 const char kGetStateJS
[] = "window.domAutomationController.send("
34 "window.document.visibilityState);";
35 EXPECT_TRUE(content::ExecuteScriptAndExtractString(
36 contents
, kGetStateJS
, &document_visibility_state
));
37 if (i
== visible_index
) {
38 EXPECT_EQ("visible", document_visibility_state
);
40 EXPECT_EQ("hidden", document_visibility_state
);
45 void CreateTestTabs(Browser
* browser
) {
46 GURL
test_page(ui_test_utils::GetTestUrl(base::FilePath(),
47 base::FilePath(FILE_PATH_LITERAL("tab-restore-visibilty.html"))));
48 ui_test_utils::NavigateToURLWithDisposition(
49 browser
, test_page
, NEW_FOREGROUND_TAB
,
50 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION
);
51 ui_test_utils::NavigateToURLWithDisposition(
52 browser
, test_page
, NEW_BACKGROUND_TAB
,
53 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION
);
56 void CloseBrowser(Browser
* browser
) {
57 content::WindowedNotificationObserver
close_observer(
58 chrome::NOTIFICATION_BROWSER_CLOSED
,
59 content::Source
<Browser
>(browser
));
60 chrome::CloseWindow(browser
);
61 close_observer
.Wait();
64 IN_PROC_BROWSER_TEST_F(BrowserTabRestoreTest
, RecentTabsMenuTabDisposition
) {
66 CreateTestTabs(browser());
67 EXPECT_EQ(3, browser()->tab_strip_model()->count());
69 // Create a new browser.
70 ui_test_utils::NavigateToURLWithDisposition(
71 browser(), GURL(), NEW_WINDOW
,
72 ui_test_utils::BROWSER_TEST_WAIT_FOR_BROWSER
);
73 BrowserList
* active_browser_list
=
74 BrowserList::GetInstance(browser()->host_desktop_type());
75 EXPECT_EQ(2u, active_browser_list
->size());
77 // Close the first browser.
78 CloseBrowser(browser());
79 EXPECT_EQ(1u, active_browser_list
->size());
81 // Restore tabs using the browser's recent tabs menu.
82 content::DOMMessageQueue queue
;
83 Browser
* browser
= active_browser_list
->get(0);
84 RecentTabsSubMenuModel
menu(NULL
, browser
, NULL
);
86 RecentTabsSubMenuModel::GetFirstRecentTabsCommandId(), 0);
87 AwaitTabsReady(&queue
, 2);
89 // There should be 3 restored tabs in the new browser.
90 EXPECT_EQ(2u, active_browser_list
->size());
91 browser
= active_browser_list
->get(1);
92 EXPECT_EQ(3, browser
->tab_strip_model()->count());
94 // The middle tab only should have visible disposition.
95 CheckVisbility(browser
->tab_strip_model(), 1);
98 IN_PROC_BROWSER_TEST_F(BrowserTabRestoreTest
, DelegateRestoreTabDisposition
) {
100 CreateTestTabs(browser());
101 EXPECT_EQ(3, browser()->tab_strip_model()->count());
103 // Create a new browser.
104 ui_test_utils::NavigateToURLWithDisposition(
105 browser(), GURL(), NEW_WINDOW
,
106 ui_test_utils::BROWSER_TEST_WAIT_FOR_BROWSER
);
107 BrowserList
* active_browser_list
=
108 BrowserList::GetInstance(browser()->host_desktop_type());
109 EXPECT_EQ(2u, active_browser_list
->size());
111 // Close the first browser.
112 CloseBrowser(browser());
113 EXPECT_EQ(1u, active_browser_list
->size());
115 // Check the browser has a delegated restore service.
116 Browser
* browser
= active_browser_list
->get(0);
117 TabRestoreService
* service
=
118 TabRestoreServiceFactory::GetForProfile(browser
->profile());
119 bool has_tab_restore_service
= !!service
;
120 ASSERT_TRUE(has_tab_restore_service
);
121 TabRestoreServiceDelegate
* delegate
=
122 TabRestoreServiceDelegate::FindDelegateForWebContents(
123 browser
->tab_strip_model()->GetActiveWebContents());
124 bool has_tab_restore_delegate
= !!delegate
;
125 ASSERT_TRUE(has_tab_restore_delegate
);
127 // Restore tabs using that delegated restore service.
128 content::DOMMessageQueue queue
;
129 service
->RestoreMostRecentEntry(
130 delegate
, browser
->host_desktop_type());
131 AwaitTabsReady(&queue
, 2);
133 // There should be 3 restored tabs in the new browser.
134 EXPECT_EQ(2u, active_browser_list
->size());
135 browser
= active_browser_list
->get(1);
136 EXPECT_EQ(3, browser
->tab_strip_model()->count());
138 // The middle tab only should have visible disposition.
139 CheckVisbility(browser
->tab_strip_model(), 1);