OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 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. | |
4 | |
5 #include "base/strings/utf_string_conversions.h" | |
6 #include "chrome/browser/chrome_notification_types.h" | |
7 #include "chrome/browser/sessions/tab_restore_service.h" | |
8 #include "chrome/browser/sessions/tab_restore_service_delegate.h" | |
9 #include "chrome/browser/sessions/tab_restore_service_factory.h" | |
10 #include "chrome/browser/ui/browser.h" | |
11 #include "chrome/browser/ui/browser_commands.h" | |
12 #include "chrome/browser/ui/browser_list.h" | |
13 #include "chrome/browser/ui/tabs/tab_strip_model.h" | |
14 #include "chrome/browser/ui/toolbar/recent_tabs_sub_menu_model.h" | |
15 #include "chrome/test/base/in_process_browser_test.h" | |
16 #include "chrome/test/base/interactive_test_utils.h" | |
17 #include "content/public/browser/notification_service.h" | |
18 #include "content/public/browser/web_contents.h" | |
19 #include "content/public/test/browser_test_utils.h" | |
20 | |
21 typedef InProcessBrowserTest TabRestoreTest; | |
sky
2014/04/16 16:42:05
Make this match your file name, eg BrowserTabResto
| |
22 | |
23 void AwaitTabsReady(TabStripModel* tab_strip_model) { | |
24 for (int i = 0; i < tab_strip_model->count(); ++i) { | |
25 content::WebContents* contents = tab_strip_model->GetWebContentsAt(i); | |
26 std::string title = base::UTF16ToUTF8(contents->GetTitle()); | |
27 if (title == "about:blank") | |
28 continue; | |
29 bool window_ready = false; | |
30 const char kGetReadyJS[] = "window.domAutomationController.send(" | |
31 "window.ready == undefined ? false : window.ready);"; | |
32 EXPECT_TRUE(content::ExecuteScriptAndExtractBool( | |
33 contents, kGetReadyJS, &window_ready)); | |
34 if (title.empty() || !window_ready) { | |
35 base::PlatformThread::YieldCurrentThread(); | |
sky
2014/04/16 16:42:05
Don't do this. Add observers to wait for the condi
| |
36 i = -1; // Start over. | |
37 } | |
38 } | |
39 } | |
40 | |
41 void CheckVisbility(TabStripModel* tab_strip_model, int visible_index) { | |
42 for (int i = 0; i < tab_strip_model->count(); ++i) { | |
43 content::WebContents* contents = tab_strip_model->GetWebContentsAt(i); | |
44 std::string document_visibility_state; | |
45 const char kGetStateJS[] = "window.domAutomationController.send(" | |
46 "window.document.visibilityState);"; | |
47 EXPECT_TRUE(content::ExecuteScriptAndExtractString( | |
48 contents, kGetStateJS, &document_visibility_state)); | |
49 if (i == visible_index) { | |
50 EXPECT_EQ("visible", document_visibility_state); | |
51 } else { | |
52 EXPECT_EQ("hidden", document_visibility_state); | |
53 } | |
54 } | |
55 } | |
56 | |
57 void CreateTabs(Browser* browser) { | |
58 static GURL test_page(ui_test_utils::GetTestUrl(base::FilePath(), | |
59 base::FilePath(FILE_PATH_LITERAL("tab-restore-visibilty.html")))); | |
60 ui_test_utils::NavigateToURLWithDisposition( | |
61 browser, test_page, NEW_FOREGROUND_TAB, | |
62 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION); | |
63 ui_test_utils::NavigateToURLWithDisposition( | |
64 browser, test_page, NEW_BACKGROUND_TAB, | |
65 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION); | |
66 } | |
67 | |
68 void CloseBrowser(Browser* browser) { | |
69 content::WindowedNotificationObserver observer( | |
70 chrome::NOTIFICATION_BROWSER_CLOSED, | |
71 content::Source<Browser>(browser)); | |
72 chrome::CloseWindow(browser); | |
73 observer.Wait(); | |
74 } | |
75 | |
76 IN_PROC_BROWSER_TEST_F(TabRestoreTest, RecentTabsMenuTabDisposition) { | |
77 // Create tabs. | |
78 CreateTabs(browser()); | |
79 TabStripModel* tab_strip_model= browser()->tab_strip_model(); | |
80 ASSERT_EQ(3, tab_strip_model->count()); | |
81 AwaitTabsReady(tab_strip_model); | |
82 | |
83 // Create a new browser. | |
84 ui_test_utils::NavigateToURLWithDisposition( | |
85 browser(), GURL(), NEW_WINDOW, | |
86 ui_test_utils::BROWSER_TEST_WAIT_FOR_BROWSER); | |
87 BrowserList* active_browser_list = | |
88 BrowserList::GetInstance(chrome::GetActiveDesktop()); | |
89 ASSERT_EQ(2u, active_browser_list->size()); | |
90 | |
91 // Close the first browser. | |
92 CloseBrowser(browser()); | |
93 ASSERT_EQ(1u, active_browser_list->size()); | |
94 | |
95 // Restore tabs using current browser's recent tabs menu. | |
96 content::WindowedNotificationObserver browser_observer( | |
97 chrome::NOTIFICATION_BROWSER_WINDOW_READY, | |
98 content::NotificationService::AllSources()); | |
99 content::WindowedNotificationObserver tab_added_observer( | |
100 chrome::NOTIFICATION_TAB_PARENTED, | |
101 content::NotificationService::AllSources()); | |
102 content::WindowedNotificationObserver tab_loaded_observer( | |
103 content::NOTIFICATION_LOAD_STOP, | |
104 content::NotificationService::AllSources()); | |
105 | |
106 Browser* browser = active_browser_list->get(0); | |
107 RecentTabsSubMenuModel menu(NULL, browser, NULL); | |
108 menu.ExecuteCommand( | |
109 RecentTabsSubMenuModel::GetFirstRecentTabsCommandId(), 0); | |
110 | |
111 // Await a new browser containing the restored tabs. | |
112 browser_observer.Wait(); | |
113 tab_added_observer.Wait(); | |
114 tab_loaded_observer.Wait(); | |
115 ASSERT_EQ(2u, active_browser_list->size()); | |
116 | |
117 // The current browser should still have only 1 tab. | |
118 tab_strip_model = browser->tab_strip_model(); | |
119 ASSERT_EQ(1, tab_strip_model->count()); | |
120 | |
121 // There should be 3 restored tabs in the new browser. | |
122 browser = active_browser_list->get(1); | |
123 tab_strip_model = browser->tab_strip_model(); | |
124 ASSERT_EQ(3, tab_strip_model->count()); | |
125 | |
126 // Its middle tab only should have visibile disposition. | |
127 AwaitTabsReady(tab_strip_model); | |
128 CheckVisbility(tab_strip_model, 1); | |
129 } | |
130 | |
131 IN_PROC_BROWSER_TEST_F(TabRestoreTest, RestoreDelegateTabDisposition) { | |
132 // Create tabs. | |
133 CreateTabs(browser()); | |
134 TabStripModel* tab_strip_model= browser()->tab_strip_model(); | |
135 ASSERT_EQ(3, tab_strip_model->count()); | |
136 AwaitTabsReady(tab_strip_model); | |
137 | |
138 // Create a new browser. | |
139 ui_test_utils::NavigateToURLWithDisposition( | |
140 browser(), GURL(), NEW_WINDOW, | |
141 ui_test_utils::BROWSER_TEST_WAIT_FOR_BROWSER); | |
142 BrowserList* active_browser_list = | |
143 BrowserList::GetInstance(chrome::GetActiveDesktop()); | |
144 ASSERT_EQ(2u, active_browser_list->size()); | |
145 | |
146 // Close the first browser. | |
147 CloseBrowser(browser()); | |
148 ASSERT_EQ(1u, active_browser_list->size()); | |
149 | |
150 // Check the current browser has a delegated restore service. | |
151 Browser* browser = active_browser_list->get(0); | |
152 TabRestoreService* service = | |
153 TabRestoreServiceFactory::GetForProfile(browser->profile()); | |
154 bool has_tab_restore_service = !!service; | |
155 ASSERT_TRUE(has_tab_restore_service); | |
156 TabRestoreServiceDelegate* delegate = | |
157 TabRestoreServiceDelegate::FindDelegateForWebContents( | |
158 browser->tab_strip_model()->GetActiveWebContents()); | |
159 bool has_tab_restore_delegate = !!delegate; | |
160 ASSERT_TRUE(has_tab_restore_delegate); | |
161 | |
162 // Tab restore using the delegated restore service. | |
163 content::WindowedNotificationObserver browser_observer( | |
164 chrome::NOTIFICATION_BROWSER_WINDOW_READY, | |
165 content::NotificationService::AllSources()); | |
166 content::WindowedNotificationObserver tab_added_observer( | |
167 chrome::NOTIFICATION_TAB_PARENTED, | |
168 content::NotificationService::AllSources()); | |
169 content::WindowedNotificationObserver tab_loaded_observer( | |
170 content::NOTIFICATION_LOAD_STOP, | |
171 content::NotificationService::AllSources()); | |
172 | |
173 service->RestoreMostRecentEntry( | |
174 delegate, browser->host_desktop_type()); | |
175 | |
176 // Await a new browser containing the restored tabs. | |
177 browser_observer.Wait(); | |
178 tab_added_observer.Wait(); | |
179 tab_loaded_observer.Wait(); | |
180 ASSERT_EQ(2u, active_browser_list->size()); | |
181 | |
182 // The current browser should still have only 1 tab. | |
183 tab_strip_model = browser->tab_strip_model(); | |
184 ASSERT_EQ(1, tab_strip_model->count()); | |
185 | |
186 // There should be 3 restored tabs in the new browser. | |
187 browser = active_browser_list->get(1); | |
188 tab_strip_model = browser->tab_strip_model(); | |
189 ASSERT_EQ(3, tab_strip_model->count()); | |
190 | |
191 // Its middle tab only should have visibile disposition. | |
192 AwaitTabsReady(tab_strip_model); | |
193 CheckVisbility(tab_strip_model, 1); | |
194 } | |
OLD | NEW |