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