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.
4 
5 #include "chrome/browser/ui/browser_tabrestore.h"
6 
7 #include "chrome/browser/chrome_notification_types.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/browser_live_tab_context.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 "chrome/test/base/ui_test_utils.h"
18 #include "components/sessions/core/tab_restore_service.h"
19 #include "content/public/browser/navigation_controller.h"
20 #include "content/public/browser/notification_service.h"
21 #include "content/public/browser/web_contents.h"
22 #include "content/public/common/url_constants.h"
23 #include "content/public/test/browser_test.h"
24 #include "content/public/test/browser_test_utils.h"
25 #include "content/public/test/test_utils.h"
26 
27 typedef InProcessBrowserTest BrowserTabRestoreTest;
28 
AwaitTabsReady(content::DOMMessageQueue * message_queue,int tabs)29 void AwaitTabsReady(content::DOMMessageQueue* message_queue, int tabs) {
30   for (int i = 0; i < tabs; ++i) {
31     std::string message;
32     EXPECT_TRUE(message_queue->WaitForMessage(&message));
33     EXPECT_EQ("\"READY\"", message);
34   }
35 }
36 
CheckVisbility(TabStripModel * tab_strip_model,int visible_index)37 void CheckVisbility(TabStripModel* tab_strip_model, int visible_index) {
38   for (int i = 0; i < tab_strip_model->count(); ++i) {
39     content::WebContents* contents = tab_strip_model->GetWebContentsAt(i);
40     std::string document_visibility_state;
41     const char kGetStateJS[] =
42         "window.domAutomationController.send("
43         "window.document.visibilityState);";
44     EXPECT_TRUE(content::ExecuteScriptAndExtractString(
45         contents, kGetStateJS, &document_visibility_state));
46     if (i == visible_index) {
47       EXPECT_EQ("visible", document_visibility_state);
48     } else {
49       EXPECT_EQ("hidden", document_visibility_state);
50     }
51   }
52 }
53 
CreateTestTabs(Browser * browser)54 void CreateTestTabs(Browser* browser) {
55   GURL test_page(ui_test_utils::GetTestUrl(
56       base::FilePath(),
57       base::FilePath(FILE_PATH_LITERAL("tab-restore-visibility.html"))));
58   ui_test_utils::NavigateToURLWithDisposition(
59       browser, test_page, WindowOpenDisposition::NEW_FOREGROUND_TAB,
60       ui_test_utils::BROWSER_TEST_WAIT_FOR_LOAD_STOP);
61   ui_test_utils::NavigateToURLWithDisposition(
62       browser, test_page, WindowOpenDisposition::NEW_BACKGROUND_TAB,
63       ui_test_utils::BROWSER_TEST_WAIT_FOR_LOAD_STOP);
64 }
65 
IN_PROC_BROWSER_TEST_F(BrowserTabRestoreTest,RecentTabsMenuTabDisposition)66 IN_PROC_BROWSER_TEST_F(BrowserTabRestoreTest, RecentTabsMenuTabDisposition) {
67   // Create tabs.
68   CreateTestTabs(browser());
69   EXPECT_EQ(3, browser()->tab_strip_model()->count());
70 
71   // Create a new browser.
72   ui_test_utils::NavigateToURLWithDisposition(
73       browser(), GURL(url::kAboutBlankURL), WindowOpenDisposition::NEW_WINDOW,
74       ui_test_utils::BROWSER_TEST_WAIT_FOR_BROWSER);
75   BrowserList* active_browser_list = BrowserList::GetInstance();
76   EXPECT_EQ(2u, active_browser_list->size());
77 
78   // Close the first browser.
79   CloseBrowserSynchronously(browser());
80   EXPECT_EQ(1u, active_browser_list->size());
81 
82   // Restore tabs using the browser's recent tabs menu.
83   content::DOMMessageQueue queue;
84   Browser* browser = active_browser_list->get(0);
85   RecentTabsSubMenuModel menu(nullptr, browser);
86   menu.ExecuteCommand(RecentTabsSubMenuModel::GetFirstRecentTabsCommandId(), 0);
87   // There should be 3 restored tabs in the new browser. The active tab should
88   // be loading.
89   EXPECT_EQ(2u, active_browser_list->size());
90   Browser* restored_browser = active_browser_list->get(1);
91   EXPECT_EQ(3, restored_browser->tab_strip_model()->count());
92   EXPECT_TRUE(restored_browser->tab_strip_model()
93                   ->GetActiveWebContents()
94                   ->GetController()
95                   .GetPendingEntry());
96   AwaitTabsReady(&queue, 2);
97 
98   // For the two test tabs we've just received "READY" DOM message.
99   // But there won't be such message from the "about:blank" tab.
100   // And it is possible that TabLoader hasn't loaded it yet.
101   // Thus we should wait for "load stop" event before we will perform
102   // CheckVisbility on "about:blank".
103   {
104     content::WebContents* about_blank_contents =
105         restored_browser->tab_strip_model()->GetWebContentsAt(0);
106     EXPECT_EQ("about:blank", about_blank_contents->GetURL().spec());
107     if (about_blank_contents->IsLoading() ||
108         about_blank_contents->GetController().NeedsReload()) {
109       content::WindowedNotificationObserver load_stop_observer(
110           content::NOTIFICATION_LOAD_STOP,
111           content::Source<content::NavigationController>(
112               &about_blank_contents->GetController()));
113       load_stop_observer.Wait();
114     }
115   }
116 
117   // The middle tab only should have visible disposition.
118   CheckVisbility(restored_browser->tab_strip_model(), 1);
119 }
120 
121 // Expect a selected restored tab to start loading synchronously.
122 //
123 // Previously, on Mac, a selected restored tab only started loading when a
124 // native message indicated that the window was visible. On other platforms,
125 // it started loading synchronously. https://crbug.com/1022492
IN_PROC_BROWSER_TEST_F(BrowserTabRestoreTest,SelectedRestoredTabStartsLoading)126 IN_PROC_BROWSER_TEST_F(BrowserTabRestoreTest,
127                        SelectedRestoredTabStartsLoading) {
128   sessions::SerializedNavigationEntry navigation_entry;
129   navigation_entry.set_index(0);
130   navigation_entry.set_virtual_url(GURL(url::kAboutBlankURL));
131 
132   std::vector<sessions::SerializedNavigationEntry> navigations;
133   navigations.push_back(navigation_entry);
134 
135   content::WebContents* web_contents = chrome::AddRestoredTab(
136       browser(), navigations, /* tab_index=*/1, /* selected_navigation=*/0,
137       /* extension_app_id=*/std::string(), /* raw_group_id=*/base::nullopt,
138       /* select=*/true, /* pin=*/false, /* from_last_session=*/true,
139       /* last_active_time=*/base::TimeTicks::Now(),
140       /* storage_namespace=*/nullptr,
141       /* user_agent_override=*/sessions::SerializedUserAgentOverride(),
142       /* from_session_restore=*/true);
143 
144   EXPECT_TRUE(web_contents->GetController().GetPendingEntry());
145 }
146 
147 // Expect a *non* selected restored tab to *not* start loading synchronously.
IN_PROC_BROWSER_TEST_F(BrowserTabRestoreTest,NonSelectedRestoredTabDoesNotStartsLoading)148 IN_PROC_BROWSER_TEST_F(BrowserTabRestoreTest,
149                        NonSelectedRestoredTabDoesNotStartsLoading) {
150   sessions::SerializedNavigationEntry navigation_entry;
151   navigation_entry.set_index(0);
152   navigation_entry.set_virtual_url(GURL(url::kAboutBlankURL));
153 
154   std::vector<sessions::SerializedNavigationEntry> navigations;
155   navigations.push_back(navigation_entry);
156 
157   content::WebContents* web_contents = chrome::AddRestoredTab(
158       browser(), navigations, /* tab_index=*/1, /* selected_navigation=*/0,
159       /* extension_app_id=*/std::string(), /* raw_group_id=*/base::nullopt,
160       /* select=*/false, /* pin=*/false, /* from_last_session=*/true,
161       /* last_active_time=*/base::TimeTicks::Now(),
162       /* storage_namespace=*/nullptr,
163       /* user_agent_override=*/sessions::SerializedUserAgentOverride(),
164       /* from_session_restore=*/true);
165 
166   EXPECT_FALSE(web_contents->GetController().GetPendingEntry());
167 }
168 
IN_PROC_BROWSER_TEST_F(BrowserTabRestoreTest,DelegateRestoreTabDisposition)169 IN_PROC_BROWSER_TEST_F(BrowserTabRestoreTest, DelegateRestoreTabDisposition) {
170   // Create tabs.
171   CreateTestTabs(browser());
172   EXPECT_EQ(3, browser()->tab_strip_model()->count());
173 
174   // Create a new browser.
175   ui_test_utils::NavigateToURLWithDisposition(
176       browser(), GURL(url::kAboutBlankURL), WindowOpenDisposition::NEW_WINDOW,
177       ui_test_utils::BROWSER_TEST_WAIT_FOR_BROWSER);
178   BrowserList* active_browser_list = BrowserList::GetInstance();
179   EXPECT_EQ(2u, active_browser_list->size());
180 
181   // Close the first browser.
182   CloseBrowserSynchronously(browser());
183   EXPECT_EQ(1u, active_browser_list->size());
184 
185   // Check the browser has a delegated restore service.
186   Browser* browser = active_browser_list->get(0);
187   sessions::TabRestoreService* service =
188       TabRestoreServiceFactory::GetForProfile(browser->profile());
189   bool has_tab_restore_service = !!service;
190   ASSERT_TRUE(has_tab_restore_service);
191   sessions::LiveTabContext* context =
192       BrowserLiveTabContext::FindContextForWebContents(
193           browser->tab_strip_model()->GetActiveWebContents());
194   bool has_live_tab_context = !!context;
195   ASSERT_TRUE(has_live_tab_context);
196 
197   // Restore tabs using that delegated restore service.
198   content::DOMMessageQueue queue;
199   service->RestoreMostRecentEntry(context);
200   AwaitTabsReady(&queue, 2);
201 
202   // There should be 3 restored tabs in the new browser.
203   EXPECT_EQ(2u, active_browser_list->size());
204   browser = active_browser_list->get(1);
205   EXPECT_EQ(3, browser->tab_strip_model()->count());
206   // The same as in RecentTabsMenuTabDisposition test case.
207   // See there for the explanation.
208   {
209     content::WebContents* about_blank_contents =
210         browser->tab_strip_model()->GetWebContentsAt(0);
211     EXPECT_EQ("about:blank", about_blank_contents->GetURL().spec());
212     if (about_blank_contents->IsLoading() ||
213         about_blank_contents->GetController().NeedsReload()) {
214       content::WindowedNotificationObserver load_stop_observer(
215           content::NOTIFICATION_LOAD_STOP,
216           content::Source<content::NavigationController>(
217               &about_blank_contents->GetController()));
218       load_stop_observer.Wait();
219     }
220   }
221 
222   // The middle tab only should have visible disposition.
223   CheckVisbility(browser->tab_strip_model(), 1);
224 }
225