Chromium Code Reviews| Index: chrome/browser/ui/browser_tab_restore_uitest.cc |
| diff --git a/chrome/browser/ui/browser_tab_restore_uitest.cc b/chrome/browser/ui/browser_tab_restore_uitest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..e5873ba7a651dab093d3c4c3f6ff4c57c7d05ddd |
| --- /dev/null |
| +++ b/chrome/browser/ui/browser_tab_restore_uitest.cc |
| @@ -0,0 +1,194 @@ |
| +// Copyright (c) 2014 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "base/strings/utf_string_conversions.h" |
| +#include "chrome/browser/chrome_notification_types.h" |
| +#include "chrome/browser/sessions/tab_restore_service.h" |
| +#include "chrome/browser/sessions/tab_restore_service_delegate.h" |
| +#include "chrome/browser/sessions/tab_restore_service_factory.h" |
| +#include "chrome/browser/ui/browser.h" |
| +#include "chrome/browser/ui/browser_commands.h" |
| +#include "chrome/browser/ui/browser_list.h" |
| +#include "chrome/browser/ui/tabs/tab_strip_model.h" |
| +#include "chrome/browser/ui/toolbar/recent_tabs_sub_menu_model.h" |
| +#include "chrome/test/base/in_process_browser_test.h" |
| +#include "chrome/test/base/interactive_test_utils.h" |
| +#include "content/public/browser/notification_service.h" |
| +#include "content/public/browser/web_contents.h" |
| +#include "content/public/test/browser_test_utils.h" |
| + |
| +typedef InProcessBrowserTest TabRestoreTest; |
|
sky
2014/04/16 16:42:05
Make this match your file name, eg BrowserTabResto
|
| + |
| +void AwaitTabsReady(TabStripModel* tab_strip_model) { |
| + for (int i = 0; i < tab_strip_model->count(); ++i) { |
| + content::WebContents* contents = tab_strip_model->GetWebContentsAt(i); |
| + std::string title = base::UTF16ToUTF8(contents->GetTitle()); |
| + if (title == "about:blank") |
| + continue; |
| + bool window_ready = false; |
| + const char kGetReadyJS[] = "window.domAutomationController.send(" |
| + "window.ready == undefined ? false : window.ready);"; |
| + EXPECT_TRUE(content::ExecuteScriptAndExtractBool( |
| + contents, kGetReadyJS, &window_ready)); |
| + if (title.empty() || !window_ready) { |
| + base::PlatformThread::YieldCurrentThread(); |
|
sky
2014/04/16 16:42:05
Don't do this. Add observers to wait for the condi
|
| + i = -1; // Start over. |
| + } |
| + } |
| +} |
| + |
| +void CheckVisbility(TabStripModel* tab_strip_model, int visible_index) { |
| + for (int i = 0; i < tab_strip_model->count(); ++i) { |
| + content::WebContents* contents = tab_strip_model->GetWebContentsAt(i); |
| + std::string document_visibility_state; |
| + const char kGetStateJS[] = "window.domAutomationController.send(" |
| + "window.document.visibilityState);"; |
| + EXPECT_TRUE(content::ExecuteScriptAndExtractString( |
| + contents, kGetStateJS, &document_visibility_state)); |
| + if (i == visible_index) { |
| + EXPECT_EQ("visible", document_visibility_state); |
| + } else { |
| + EXPECT_EQ("hidden", document_visibility_state); |
| + } |
| + } |
| +} |
| + |
| +void CreateTabs(Browser* browser) { |
| + static GURL test_page(ui_test_utils::GetTestUrl(base::FilePath(), |
| + base::FilePath(FILE_PATH_LITERAL("tab-restore-visibilty.html")))); |
| + ui_test_utils::NavigateToURLWithDisposition( |
| + browser, test_page, NEW_FOREGROUND_TAB, |
| + ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION); |
| + ui_test_utils::NavigateToURLWithDisposition( |
| + browser, test_page, NEW_BACKGROUND_TAB, |
| + ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION); |
| +} |
| + |
| +void CloseBrowser(Browser* browser) { |
| + content::WindowedNotificationObserver observer( |
| + chrome::NOTIFICATION_BROWSER_CLOSED, |
| + content::Source<Browser>(browser)); |
| + chrome::CloseWindow(browser); |
| + observer.Wait(); |
| +} |
| + |
| +IN_PROC_BROWSER_TEST_F(TabRestoreTest, RecentTabsMenuTabDisposition) { |
| + // Create tabs. |
| + CreateTabs(browser()); |
| + TabStripModel* tab_strip_model= browser()->tab_strip_model(); |
| + ASSERT_EQ(3, tab_strip_model->count()); |
| + AwaitTabsReady(tab_strip_model); |
| + |
| + // Create a new browser. |
| + ui_test_utils::NavigateToURLWithDisposition( |
| + browser(), GURL(), NEW_WINDOW, |
| + ui_test_utils::BROWSER_TEST_WAIT_FOR_BROWSER); |
| + BrowserList* active_browser_list = |
| + BrowserList::GetInstance(chrome::GetActiveDesktop()); |
| + ASSERT_EQ(2u, active_browser_list->size()); |
| + |
| + // Close the first browser. |
| + CloseBrowser(browser()); |
| + ASSERT_EQ(1u, active_browser_list->size()); |
| + |
| + // Restore tabs using current browser's recent tabs menu. |
| + content::WindowedNotificationObserver browser_observer( |
| + chrome::NOTIFICATION_BROWSER_WINDOW_READY, |
| + content::NotificationService::AllSources()); |
| + content::WindowedNotificationObserver tab_added_observer( |
| + chrome::NOTIFICATION_TAB_PARENTED, |
| + content::NotificationService::AllSources()); |
| + content::WindowedNotificationObserver tab_loaded_observer( |
| + content::NOTIFICATION_LOAD_STOP, |
| + content::NotificationService::AllSources()); |
| + |
| + Browser* browser = active_browser_list->get(0); |
| + RecentTabsSubMenuModel menu(NULL, browser, NULL); |
| + menu.ExecuteCommand( |
| + RecentTabsSubMenuModel::GetFirstRecentTabsCommandId(), 0); |
| + |
| + // Await a new browser containing the restored tabs. |
| + browser_observer.Wait(); |
| + tab_added_observer.Wait(); |
| + tab_loaded_observer.Wait(); |
| + ASSERT_EQ(2u, active_browser_list->size()); |
| + |
| + // The current browser should still have only 1 tab. |
| + tab_strip_model = browser->tab_strip_model(); |
| + ASSERT_EQ(1, tab_strip_model->count()); |
| + |
| + // There should be 3 restored tabs in the new browser. |
| + browser = active_browser_list->get(1); |
| + tab_strip_model = browser->tab_strip_model(); |
| + ASSERT_EQ(3, tab_strip_model->count()); |
| + |
| + // Its middle tab only should have visibile disposition. |
| + AwaitTabsReady(tab_strip_model); |
| + CheckVisbility(tab_strip_model, 1); |
| +} |
| + |
| +IN_PROC_BROWSER_TEST_F(TabRestoreTest, RestoreDelegateTabDisposition) { |
| + // Create tabs. |
| + CreateTabs(browser()); |
| + TabStripModel* tab_strip_model= browser()->tab_strip_model(); |
| + ASSERT_EQ(3, tab_strip_model->count()); |
| + AwaitTabsReady(tab_strip_model); |
| + |
| + // Create a new browser. |
| + ui_test_utils::NavigateToURLWithDisposition( |
| + browser(), GURL(), NEW_WINDOW, |
| + ui_test_utils::BROWSER_TEST_WAIT_FOR_BROWSER); |
| + BrowserList* active_browser_list = |
| + BrowserList::GetInstance(chrome::GetActiveDesktop()); |
| + ASSERT_EQ(2u, active_browser_list->size()); |
| + |
| + // Close the first browser. |
| + CloseBrowser(browser()); |
| + ASSERT_EQ(1u, active_browser_list->size()); |
| + |
| + // Check the current browser has a delegated restore service. |
| + Browser* browser = active_browser_list->get(0); |
| + TabRestoreService* service = |
| + TabRestoreServiceFactory::GetForProfile(browser->profile()); |
| + bool has_tab_restore_service = !!service; |
| + ASSERT_TRUE(has_tab_restore_service); |
| + TabRestoreServiceDelegate* delegate = |
| + TabRestoreServiceDelegate::FindDelegateForWebContents( |
| + browser->tab_strip_model()->GetActiveWebContents()); |
| + bool has_tab_restore_delegate = !!delegate; |
| + ASSERT_TRUE(has_tab_restore_delegate); |
| + |
| + // Tab restore using the delegated restore service. |
| + content::WindowedNotificationObserver browser_observer( |
| + chrome::NOTIFICATION_BROWSER_WINDOW_READY, |
| + content::NotificationService::AllSources()); |
| + content::WindowedNotificationObserver tab_added_observer( |
| + chrome::NOTIFICATION_TAB_PARENTED, |
| + content::NotificationService::AllSources()); |
| + content::WindowedNotificationObserver tab_loaded_observer( |
| + content::NOTIFICATION_LOAD_STOP, |
| + content::NotificationService::AllSources()); |
| + |
| + service->RestoreMostRecentEntry( |
| + delegate, browser->host_desktop_type()); |
| + |
| + // Await a new browser containing the restored tabs. |
| + browser_observer.Wait(); |
| + tab_added_observer.Wait(); |
| + tab_loaded_observer.Wait(); |
| + ASSERT_EQ(2u, active_browser_list->size()); |
| + |
| + // The current browser should still have only 1 tab. |
| + tab_strip_model = browser->tab_strip_model(); |
| + ASSERT_EQ(1, tab_strip_model->count()); |
| + |
| + // There should be 3 restored tabs in the new browser. |
| + browser = active_browser_list->get(1); |
| + tab_strip_model = browser->tab_strip_model(); |
| + ASSERT_EQ(3, tab_strip_model->count()); |
| + |
| + // Its middle tab only should have visibile disposition. |
| + AwaitTabsReady(tab_strip_model); |
| + CheckVisbility(tab_strip_model, 1); |
| +} |