File: browser_tabrestore_browsertest.cc

package info (click to toggle)
chromium 138.0.7204.157-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,864 kB
  • sloc: cpp: 34,936,859; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,967; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (223 lines) | stat: -rw-r--r-- 9,316 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/ui/browser_tabrestore.h"

#include <map>
#include <string>

#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/browser_live_tab_context.h"
#include "chrome/browser/ui/tabs/recent_tabs_sub_menu_model.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chrome/test/base/ui_test_utils.h"
#include "components/sessions/core/tab_restore_service.h"
#include "content/public/browser/navigation_controller.h"
#include "content/public/browser/web_contents.h"
#include "content/public/common/url_constants.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_utils.h"
#include "content/public/test/test_utils.h"

typedef InProcessBrowserTest BrowserTabRestoreTest;

void AwaitTabsReady(content::DOMMessageQueue* message_queue, int tabs) {
  for (int i = 0; i < tabs; ++i) {
    std::string message;
    EXPECT_TRUE(message_queue->WaitForMessage(&message));
    EXPECT_EQ("\"READY\"", message);
  }
}

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);
    const char kGetStateJS[] = "window.document.visibilityState;";
    std::string document_visibility_state =
        content::EvalJs(contents, kGetStateJS).ExtractString();
    if (i == visible_index) {
      EXPECT_EQ("visible", document_visibility_state);
    } else {
      EXPECT_EQ("hidden", document_visibility_state);
    }
  }
}

void CreateTestTabs(Browser* browser) {
  GURL test_page(ui_test_utils::GetTestUrl(
      base::FilePath(),
      base::FilePath(FILE_PATH_LITERAL("tab-restore-visibility.html"))));
  ui_test_utils::NavigateToURLWithDisposition(
      browser, test_page, WindowOpenDisposition::NEW_FOREGROUND_TAB,
      ui_test_utils::BROWSER_TEST_WAIT_FOR_LOAD_STOP);
  ui_test_utils::NavigateToURLWithDisposition(
      browser, test_page, WindowOpenDisposition::NEW_BACKGROUND_TAB,
      ui_test_utils::BROWSER_TEST_WAIT_FOR_LOAD_STOP);
}

IN_PROC_BROWSER_TEST_F(BrowserTabRestoreTest, RecentTabsMenuTabDisposition) {
  // Create tabs.
  CreateTestTabs(browser());
  EXPECT_EQ(3, browser()->tab_strip_model()->count());

  // Create a new browser.
  ui_test_utils::NavigateToURLWithDisposition(
      browser(), GURL(url::kAboutBlankURL), WindowOpenDisposition::NEW_WINDOW,
      ui_test_utils::BROWSER_TEST_WAIT_FOR_BROWSER);
  BrowserList* active_browser_list = BrowserList::GetInstance();
  EXPECT_EQ(2u, active_browser_list->size());

  // Close the first browser.
  const int active_tab_index = browser()->tab_strip_model()->active_index();
  CloseBrowserSynchronously(browser());
  EXPECT_EQ(1u, active_browser_list->size());

  // Restore tabs using the browser's recent tabs menu.
  content::DOMMessageQueue queue;
  Browser* browser = active_browser_list->get(0);
  RecentTabsSubMenuModel menu(nullptr, browser);
  menu.ExecuteCommand(menu.GetFirstRecentTabsCommandId(), 0);
  // There should be 3 restored tabs in the new browser. The active tab should
  // be loading.
  EXPECT_EQ(2u, active_browser_list->size());
  Browser* restored_browser = active_browser_list->get(1);
  EXPECT_EQ(3, restored_browser->tab_strip_model()->count());
  EXPECT_TRUE(restored_browser->tab_strip_model()
                  ->GetActiveWebContents()
                  ->GetController()
                  .GetPendingEntry());
  AwaitTabsReady(&queue, 2);

  // For the two test tabs we've just received "READY" DOM message.
  // But there won't be such message from the "about:blank" tab.
  // And it is possible that TabLoader hasn't loaded it yet.
  // Thus we should wait for "load stop" event before we will perform
  // CheckVisbility on "about:blank".
  {
    content::WebContents* about_blank_contents =
        restored_browser->tab_strip_model()->GetWebContentsAt(0);
    EXPECT_EQ("about:blank", about_blank_contents->GetURL().spec());
    if (about_blank_contents->IsLoading() ||
        about_blank_contents->GetController().NeedsReload()) {
      content::LoadStopObserver load_stop_observer(about_blank_contents);
      load_stop_observer.Wait();
    }
  }

  // Previously active tab should have visible disposition.
  CheckVisbility(restored_browser->tab_strip_model(), active_tab_index);
}

// Expect a selected restored tab to start loading synchronously.
//
// Previously, on Mac, a selected restored tab only started loading when a
// native message indicated that the window was visible. On other platforms,
// it started loading synchronously. https://crbug.com/1022492
IN_PROC_BROWSER_TEST_F(BrowserTabRestoreTest,
                       SelectedRestoredTabStartsLoading) {
  sessions::SerializedNavigationEntry navigation_entry;
  navigation_entry.set_index(0);
  navigation_entry.set_virtual_url(GURL(url::kAboutBlankURL));

  std::vector<sessions::SerializedNavigationEntry> navigations;
  navigations.push_back(navigation_entry);

  content::WebContents* web_contents = chrome::AddRestoredTab(
      browser(), navigations, /* tab_index=*/1, /* selected_navigation=*/0,
      /* extension_app_id=*/std::string(), /* group=*/std::nullopt,
      /* select=*/true, /* pin=*/false,
      /* last_active_time_ticks=*/base::TimeTicks::Now(),
      /* last_active_time=*/base::Time::Now(),
      /* storage_namespace=*/nullptr,
      /* user_agent_override=*/sessions::SerializedUserAgentOverride(),
      /* extra_data*/ std::map<std::string, std::string>(),
      /* from_session_restore=*/true,
      /* is_active_browser=*/true);

  EXPECT_TRUE(web_contents->GetController().GetPendingEntry());
}

// Expect a *non* selected restored tab to *not* start loading synchronously.
IN_PROC_BROWSER_TEST_F(BrowserTabRestoreTest,
                       NonSelectedRestoredTabDoesNotStartsLoading) {
  sessions::SerializedNavigationEntry navigation_entry;
  navigation_entry.set_index(0);
  navigation_entry.set_virtual_url(GURL(url::kAboutBlankURL));

  std::vector<sessions::SerializedNavigationEntry> navigations;
  navigations.push_back(navigation_entry);

  content::WebContents* web_contents = chrome::AddRestoredTab(
      browser(), navigations, /* tab_index=*/1, /* selected_navigation=*/0,
      /* extension_app_id=*/std::string(), /* group=*/std::nullopt,
      /* select=*/false, /* pin=*/false,
      /* last_active_time_ticks=*/base::TimeTicks::Now(),
      /* last_active_time=*/base::Time::Now(),
      /* storage_namespace=*/nullptr,
      /* user_agent_override=*/sessions::SerializedUserAgentOverride(),
      /* extra_data*/ std::map<std::string, std::string>(),
      /* from_session_restore=*/true,
      /* is_active_browser=*/true);

  EXPECT_FALSE(web_contents->GetController().GetPendingEntry());
}

IN_PROC_BROWSER_TEST_F(BrowserTabRestoreTest, DelegateRestoreTabDisposition) {
  // Create tabs.
  CreateTestTabs(browser());
  EXPECT_EQ(3, browser()->tab_strip_model()->count());

  // Create a new browser.
  ui_test_utils::NavigateToURLWithDisposition(
      browser(), GURL(url::kAboutBlankURL), WindowOpenDisposition::NEW_WINDOW,
      ui_test_utils::BROWSER_TEST_WAIT_FOR_BROWSER);
  BrowserList* active_browser_list = BrowserList::GetInstance();
  EXPECT_EQ(2u, active_browser_list->size());

  // Close the first browser.
  const int active_tab_index = browser()->tab_strip_model()->active_index();
  CloseBrowserSynchronously(browser());
  EXPECT_EQ(1u, active_browser_list->size());

  // Check the browser has a delegated restore service.
  Browser* browser = active_browser_list->get(0);
  sessions::TabRestoreService* service =
      TabRestoreServiceFactory::GetForProfile(browser->profile());
  bool has_tab_restore_service = !!service;
  ASSERT_TRUE(has_tab_restore_service);
  sessions::LiveTabContext* context =
      BrowserLiveTabContext::FindContextForWebContents(
          browser->tab_strip_model()->GetActiveWebContents());
  bool has_live_tab_context = !!context;
  ASSERT_TRUE(has_live_tab_context);

  // Restore tabs using that delegated restore service.
  content::DOMMessageQueue queue;
  service->RestoreMostRecentEntry(context);
  AwaitTabsReady(&queue, 2);

  // There should be 3 restored tabs in the new browser.
  EXPECT_EQ(2u, active_browser_list->size());
  browser = active_browser_list->get(1);
  EXPECT_EQ(3, browser->tab_strip_model()->count());
  // The same as in RecentTabsMenuTabDisposition test case.
  // See there for the explanation.
  {
    content::WebContents* about_blank_contents =
        browser->tab_strip_model()->GetWebContentsAt(0);
    EXPECT_EQ("about:blank", about_blank_contents->GetURL().spec());
    if (about_blank_contents->IsLoading() ||
        about_blank_contents->GetController().NeedsReload()) {
      content::LoadStopObserver load_stop_observer(about_blank_contents);
      load_stop_observer.Wait();
    }
  }

  // Previously active tab should have visible disposition.
  CheckVisbility(browser->tab_strip_model(), active_tab_index);
}