Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(112)

Side by Side Diff: chrome/browser/page_load_metrics/page_load_metrics_browsertest.cc

Issue 2930013005: [Tab Metrics] Measure FP, FCP and FMP for Foreground Tab during Session Restore (Closed)
Patch Set: Add RemoveFromWebContents() to RestoreOrigin. Created 3 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include <memory>
6
5 #include "base/command_line.h" 7 #include "base/command_line.h"
6 #include "base/files/scoped_temp_dir.h" 8 #include "base/files/scoped_temp_dir.h"
7 #include "base/logging.h" 9 #include "base/logging.h"
8 #include "base/macros.h" 10 #include "base/macros.h"
11 #include "base/memory/ptr_util.h"
9 #include "base/memory/weak_ptr.h" 12 #include "base/memory/weak_ptr.h"
10 #include "base/run_loop.h" 13 #include "base/run_loop.h"
11 #include "base/test/histogram_tester.h" 14 #include "base/test/histogram_tester.h"
12 #include "base/threading/thread_restrictions.h" 15 #include "base/threading/thread_restrictions.h"
13 #include "base/time/time.h" 16 #include "base/time/time.h"
14 #include "build/build_config.h" 17 #include "build/build_config.h"
18 #include "chrome/browser/browser_process.h"
15 #include "chrome/browser/lifetime/keep_alive_types.h" 19 #include "chrome/browser/lifetime/keep_alive_types.h"
16 #include "chrome/browser/lifetime/scoped_keep_alive.h" 20 #include "chrome/browser/lifetime/scoped_keep_alive.h"
17 #include "chrome/browser/page_load_metrics/metrics_web_contents_observer.h" 21 #include "chrome/browser/page_load_metrics/metrics_web_contents_observer.h"
18 #include "chrome/browser/page_load_metrics/observers/aborts_page_load_metrics_ob server.h" 22 #include "chrome/browser/page_load_metrics/observers/aborts_page_load_metrics_ob server.h"
19 #include "chrome/browser/page_load_metrics/observers/core_page_load_metrics_obse rver.h" 23 #include "chrome/browser/page_load_metrics/observers/core_page_load_metrics_obse rver.h"
20 #include "chrome/browser/page_load_metrics/observers/document_write_page_load_me trics_observer.h" 24 #include "chrome/browser/page_load_metrics/observers/document_write_page_load_me trics_observer.h"
21 #include "chrome/browser/page_load_metrics/observers/no_state_prefetch_page_load _metrics_observer.h" 25 #include "chrome/browser/page_load_metrics/observers/no_state_prefetch_page_load _metrics_observer.h"
26 #include "chrome/browser/page_load_metrics/observers/session_restore_page_load_m etrics_observer.h"
22 #include "chrome/browser/page_load_metrics/page_load_tracker.h" 27 #include "chrome/browser/page_load_metrics/page_load_tracker.h"
23 #include "chrome/browser/prefs/session_startup_pref.h" 28 #include "chrome/browser/prefs/session_startup_pref.h"
24 #include "chrome/browser/prerender/prerender_histograms.h" 29 #include "chrome/browser/prerender/prerender_histograms.h"
25 #include "chrome/browser/prerender/prerender_origin.h" 30 #include "chrome/browser/prerender/prerender_origin.h"
26 #include "chrome/browser/profiles/profile.h" 31 #include "chrome/browser/profiles/profile.h"
32 #include "chrome/browser/resource_coordinator/tab_manager.h"
27 #include "chrome/browser/sessions/session_service_factory.h" 33 #include "chrome/browser/sessions/session_service_factory.h"
28 #include "chrome/browser/sessions/session_service_test_helper.h" 34 #include "chrome/browser/sessions/session_service_test_helper.h"
35 #include "chrome/browser/sessions/tab_loader.h"
36 #include "chrome/browser/sessions/tab_restore_service_factory.h"
29 #include "chrome/browser/ui/browser.h" 37 #include "chrome/browser/ui/browser.h"
30 #include "chrome/browser/ui/browser_commands.h" 38 #include "chrome/browser/ui/browser_commands.h"
39 #include "chrome/browser/ui/browser_live_tab_context.h"
31 #include "chrome/browser/ui/browser_navigator_params.h" 40 #include "chrome/browser/ui/browser_navigator_params.h"
32 #include "chrome/browser/ui/tabs/tab_strip_model.h" 41 #include "chrome/browser/ui/tabs/tab_strip_model.h"
33 #include "chrome/common/chrome_features.h" 42 #include "chrome/common/chrome_features.h"
34 #include "chrome/common/pref_names.h" 43 #include "chrome/common/pref_names.h"
35 #include "chrome/common/url_constants.h" 44 #include "chrome/common/url_constants.h"
36 #include "chrome/test/base/in_process_browser_test.h" 45 #include "chrome/test/base/in_process_browser_test.h"
37 #include "chrome/test/base/ui_test_utils.h" 46 #include "chrome/test/base/ui_test_utils.h"
38 #include "components/prefs/pref_service.h" 47 #include "components/prefs/pref_service.h"
48 #include "components/sessions/core/tab_restore_service.h"
39 #include "content/public/browser/browser_thread.h" 49 #include "content/public/browser/browser_thread.h"
40 #include "content/public/browser/render_process_host.h" 50 #include "content/public/browser/render_process_host.h"
41 #include "content/public/browser/render_view_host.h" 51 #include "content/public/browser/render_view_host.h"
42 #include "content/public/common/content_features.h" 52 #include "content/public/common/content_features.h"
43 #include "content/public/common/content_switches.h" 53 #include "content/public/common/content_switches.h"
54 #include "content/public/common/referrer.h"
44 #include "content/public/test/browser_test_utils.h" 55 #include "content/public/test/browser_test_utils.h"
45 #include "content/public/test/download_test_observer.h" 56 #include "content/public/test/download_test_observer.h"
46 #include "net/base/net_errors.h" 57 #include "net/base/net_errors.h"
47 #include "net/dns/mock_host_resolver.h" 58 #include "net/dns/mock_host_resolver.h"
48 #include "net/http/failing_http_transaction_factory.h" 59 #include "net/http/failing_http_transaction_factory.h"
49 #include "net/http/http_cache.h" 60 #include "net/http/http_cache.h"
50 #include "net/test/embedded_test_server/embedded_test_server.h" 61 #include "net/test/embedded_test_server/embedded_test_server.h"
51 #include "net/test/url_request/url_request_failed_job.h" 62 #include "net/test/url_request/url_request_failed_job.h"
52 #include "net/url_request/url_request_context.h" 63 #include "net/url_request/url_request_context.h"
53 #include "net/url_request/url_request_context_getter.h" 64 #include "net/url_request/url_request_context_getter.h"
(...skipping 1049 matching lines...) Expand 10 before | Expand all | Expand 10 after
1103 std::unique_ptr<ScopedKeepAlive> keep_alive(new ScopedKeepAlive( 1114 std::unique_ptr<ScopedKeepAlive> keep_alive(new ScopedKeepAlive(
1104 KeepAliveOrigin::SESSION_RESTORE, KeepAliveRestartOption::DISABLED)); 1115 KeepAliveOrigin::SESSION_RESTORE, KeepAliveRestartOption::DISABLED));
1105 CloseBrowserSynchronously(browser); 1116 CloseBrowserSynchronously(browser);
1106 1117
1107 // Create a new window, which should trigger session restore. 1118 // Create a new window, which should trigger session restore.
1108 chrome::NewEmptyWindow(profile); 1119 chrome::NewEmptyWindow(profile);
1109 ui_test_utils::BrowserAddedObserver window_observer; 1120 ui_test_utils::BrowserAddedObserver window_observer;
1110 return window_observer.WaitForSingleNewBrowser(); 1121 return window_observer.WaitForSingleNewBrowser();
1111 } 1122 }
1112 1123
1124 std::unique_ptr<PageLoadMetricsWaiter> CreatePageLoadMetricsWaiter(
1125 content::WebContents* web_contents) const {
1126 return base::MakeUnique<PageLoadMetricsWaiter>(web_contents);
1127 }
1128
1129 void WaitForFirstMeaningfulPaintOfActiveTab(Browser* browser) const {
1130 auto waiter = CreatePageLoadMetricsWaiter(
1131 browser->tab_strip_model()->GetActiveWebContents());
1132 waiter->AddPageExpectation(TimingField::FIRST_MEANINGFUL_PAINT);
1133 waiter->Wait();
1134 }
1135
1113 void WaitForTabsToLoad(Browser* browser) { 1136 void WaitForTabsToLoad(Browser* browser) {
1114 for (int i = 0; i < browser->tab_strip_model()->count(); ++i) { 1137 for (int i = 0; i < browser->tab_strip_model()->count(); ++i) {
1115 content::WebContents* contents = 1138 content::WebContents* contents =
1116 browser->tab_strip_model()->GetWebContentsAt(i); 1139 browser->tab_strip_model()->GetWebContentsAt(i);
1117 contents->GetController().LoadIfNecessary(); 1140 contents->GetController().LoadIfNecessary();
1118 content::WaitForLoadStop(contents); 1141 content::WaitForLoadStop(contents);
1119 } 1142 }
1120 } 1143 }
1121 1144
1145 // The PageLoadMetricsWaiter can observe first meaningful paints on these test
1146 // pages while not on other simple pages such as /title1.html.
1122 GURL GetTestURL() const { 1147 GURL GetTestURL() const {
1123 return embedded_test_server()->GetURL("/title1.html"); 1148 return embedded_test_server()->GetURL(
1149 "/page_load_metrics/page_with_css.html");
1150 }
1151
1152 GURL GetTestURL2() const {
1153 return embedded_test_server()->GetURL(
1154 "/page_load_metrics/main_frame_with_iframe.html");
1155 }
1156
1157 void ExpectFirstPaintMetricsTotalCount(int expected_total_count) const {
1158 // SessionRestorePageLoadMetricsObserver is disabled when browser-side
1159 // navigation is enabled.
1160 histogram_tester_.ExpectTotalCount(
1161 internal::kHistogramSessionRestoreForegroundTabFirstPaint,
1162 expected_total_count);
1163 histogram_tester_.ExpectTotalCount(
1164 internal::kHistogramSessionRestoreForegroundTabFirstContentfulPaint,
1165 expected_total_count);
1166 histogram_tester_.ExpectTotalCount(
1167 internal::kHistogramSessionRestoreForegroundTabFirstMeaningfulPaint,
1168 expected_total_count);
1124 } 1169 }
1125 1170
1126 private: 1171 private:
1127 DISALLOW_COPY_AND_ASSIGN(SessionRestorePageLoadMetricsBrowserTest); 1172 DISALLOW_COPY_AND_ASSIGN(SessionRestorePageLoadMetricsBrowserTest);
1128 }; 1173 };
1129 1174
1130 IN_PROC_BROWSER_TEST_F(SessionRestorePageLoadMetricsBrowserTest, 1175 IN_PROC_BROWSER_TEST_F(SessionRestorePageLoadMetricsBrowserTest,
1131 InitialVisibilityOfSingleRestoredTab) { 1176 InitialVisibilityOfSingleRestoredTab) {
1132 ui_test_utils::NavigateToURL(browser(), GetTestURL()); 1177 ui_test_utils::NavigateToURL(browser(), GetTestURL());
1133 histogram_tester_.ExpectTotalCount( 1178 histogram_tester_.ExpectTotalCount(
(...skipping 28 matching lines...) Expand all
1162 ASSERT_TRUE(tab_strip); 1207 ASSERT_TRUE(tab_strip);
1163 ASSERT_EQ(2, tab_strip->count()); 1208 ASSERT_EQ(2, tab_strip->count());
1164 1209
1165 histogram_tester_.ExpectTotalCount( 1210 histogram_tester_.ExpectTotalCount(
1166 page_load_metrics::internal::kPageLoadStartedInForeground, 4); 1211 page_load_metrics::internal::kPageLoadStartedInForeground, 4);
1167 histogram_tester_.ExpectBucketCount( 1212 histogram_tester_.ExpectBucketCount(
1168 page_load_metrics::internal::kPageLoadStartedInForeground, true, 2); 1213 page_load_metrics::internal::kPageLoadStartedInForeground, true, 2);
1169 histogram_tester_.ExpectBucketCount( 1214 histogram_tester_.ExpectBucketCount(
1170 page_load_metrics::internal::kPageLoadStartedInForeground, false, 2); 1215 page_load_metrics::internal::kPageLoadStartedInForeground, false, 2);
1171 } 1216 }
1217
1218 IN_PROC_BROWSER_TEST_F(SessionRestorePageLoadMetricsBrowserTest,
1219 NoSessionRestore) {
1220 ui_test_utils::NavigateToURL(browser(), GetTestURL());
1221 // No metrics recorded because navigation is outside of session restore.
1222 ExpectFirstPaintMetricsTotalCount(0);
1223 }
1224
1225 IN_PROC_BROWSER_TEST_F(SessionRestorePageLoadMetricsBrowserTest,
1226 SingleTabSessionRestore) {
1227 ui_test_utils::NavigateToURL(browser(), GetTestURL());
1228 Browser* new_browser = QuitBrowserAndRestore(browser());
1229 WaitForFirstMeaningfulPaintOfActiveTab(new_browser);
1230 ExpectFirstPaintMetricsTotalCount(1);
1231 }
1232
1233 IN_PROC_BROWSER_TEST_F(SessionRestorePageLoadMetricsBrowserTest,
1234 MultipleTabsSessionRestore) {
1235 ui_test_utils::NavigateToURL(browser(), GetTestURL());
1236 ui_test_utils::NavigateToURLWithDisposition(
1237 browser(), GetTestURL(), WindowOpenDisposition::NEW_BACKGROUND_TAB,
1238 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
1239 Browser* new_browser = QuitBrowserAndRestore(browser());
1240
1241 TabStripModel* tab_strip = new_browser->tab_strip_model();
1242 ASSERT_TRUE(tab_strip);
1243 ASSERT_EQ(2, tab_strip->count());
1244
1245 // Wait for first paints on the initial foreground tab and all tabs loaded.
1246 WaitForFirstMeaningfulPaintOfActiveTab(new_browser);
1247 WaitForTabsToLoad(new_browser);
1248
1249 // Only metrics of the initial foreground tab are recorded.
1250 ExpectFirstPaintMetricsTotalCount(1);
1251 }
1252
1253 IN_PROC_BROWSER_TEST_F(SessionRestorePageLoadMetricsBrowserTest,
1254 LoadingInForegroundTabInSessionRestore) {
1255 ui_test_utils::NavigateToURL(browser(), GetTestURL());
1256 Browser* new_browser = QuitBrowserAndRestore(browser());
1257 // Ensure the session restore not stop in the middle.
1258 scoped_refptr<TabLoader> tab_loader_retainer = TabLoader::shared_tab_loader_;
1259
1260 // Need to stop before loading a new page.
1261 content::WebContents* active_web_contents =
1262 new_browser->tab_strip_model()->GetActiveWebContents();
1263 active_web_contents->Stop();
1264 ExpectFirstPaintMetricsTotalCount(0);
1265
1266 auto waiter = base::MakeUnique<PageLoadMetricsWaiter>(active_web_contents);
1267 waiter->AddPageExpectation(TimingField::FIRST_MEANINGFUL_PAINT);
1268 ASSERT_TRUE(
1269 g_browser_process->GetTabManager()->IsSessionRestoreLoadingTabs());
1270 active_web_contents->GetController().LoadURL(
1271 GetTestURL2(), content::Referrer(), ui::PAGE_TRANSITION_TYPED,
1272 std::string());
1273 waiter->Wait();
1274
1275 // Do not count the new page load in the initial foreground tab.
1276 ExpectFirstPaintMetricsTotalCount(0);
1277 }
1278
1279 IN_PROC_BROWSER_TEST_F(SessionRestorePageLoadMetricsBrowserTest,
1280 LoadingAfterSessionRestore) {
1281 ui_test_utils::NavigateToURL(browser(), GetTestURL());
1282 Browser* new_browser = QuitBrowserAndRestore(browser());
1283
1284 // Wait for session restore to finish (i.e., the end of the only tab).
1285 WaitForFirstMeaningfulPaintOfActiveTab(new_browser);
1286 ExpectFirstPaintMetricsTotalCount(1);
1287
1288 // Load a new page after session restore.
1289 auto waiter = CreatePageLoadMetricsWaiter(
1290 new_browser->tab_strip_model()->GetActiveWebContents());
1291 waiter->AddPageExpectation(TimingField::FIRST_MEANINGFUL_PAINT);
1292 ui_test_utils::NavigateToURL(new_browser, GetTestURL2());
1293 waiter->Wait();
1294
1295 // No more metrics because the navigation is after session restore.
1296 ExpectFirstPaintMetricsTotalCount(1);
1297 }
1298
1299 IN_PROC_BROWSER_TEST_F(SessionRestorePageLoadMetricsBrowserTest,
1300 InitialForegroundTabChanged) {
1301 ui_test_utils::NavigateToURL(browser(), GetTestURL());
1302 ui_test_utils::NavigateToURLWithDisposition(
1303 browser(), GetTestURL2(), WindowOpenDisposition::NEW_BACKGROUND_TAB,
1304 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
1305 Browser* new_browser = QuitBrowserAndRestore(browser());
1306
1307 // Change the foreground tab before the first meaningful paint.
1308 TabStripModel* tab_strip = new_browser->tab_strip_model();
1309 ASSERT_TRUE(tab_strip);
1310 ASSERT_EQ(2, tab_strip->count());
1311 ASSERT_EQ(0, tab_strip->active_index());
1312 tab_strip->ActivateTabAt(1, true);
1313
1314 // Wait for the first meaningful paint of the current foreground tab.
1315 WaitForFirstMeaningfulPaintOfActiveTab(new_browser);
1316 ExpectFirstPaintMetricsTotalCount(0);
1317 }
1318
1319 IN_PROC_BROWSER_TEST_F(SessionRestorePageLoadMetricsBrowserTest,
1320 MultipleSessionRestores) {
1321 ui_test_utils::NavigateToURL(browser(), GetTestURL());
1322
1323 Browser* current_browser = browser();
1324 const int num_session_restores = 3;
1325 for (int i = 1; i <= num_session_restores; ++i) {
1326 current_browser = QuitBrowserAndRestore(current_browser);
1327 WaitForFirstMeaningfulPaintOfActiveTab(current_browser);
1328 ExpectFirstPaintMetricsTotalCount(i);
1329 }
1330 }
1331
1332 IN_PROC_BROWSER_TEST_F(SessionRestorePageLoadMetricsBrowserTest,
1333 ReopenClosedTabInSessionRestore) {
1334 // Open then restore a browser with 2 tabs.
1335 ui_test_utils::NavigateToURL(browser(), GetTestURL());
1336 ui_test_utils::NavigateToURLWithDisposition(
1337 browser(), GetTestURL2(), WindowOpenDisposition::NEW_BACKGROUND_TAB,
1338 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
1339
1340 Browser* new_browser = QuitBrowserAndRestore(browser());
1341 auto new_browser_waiter = CreatePageLoadMetricsWaiter(
1342 new_browser->tab_strip_model()->GetActiveWebContents());
1343 new_browser_waiter->AddPageExpectation(TimingField::FIRST_MEANINGFUL_PAINT);
1344
1345 // Ensure the session restore not stop in the middle.
1346 scoped_refptr<TabLoader> tab_loader_retainer = TabLoader::shared_tab_loader_;
1347
1348 TabStripModel* tab_strip = new_browser->tab_strip_model();
1349 ASSERT_TRUE(tab_strip);
1350 ASSERT_EQ(2, tab_strip->count());
1351
1352 // Check the browser has a delegated restore service.
1353 sessions::TabRestoreService* service =
1354 TabRestoreServiceFactory::GetForProfile(new_browser->profile());
1355 bool has_tab_restore_service = !!service;
1356 ASSERT_TRUE(has_tab_restore_service);
1357 sessions::LiveTabContext* context =
1358 BrowserLiveTabContext::FindContextForWebContents(
1359 new_browser->tab_strip_model()->GetActiveWebContents());
1360 bool has_live_tab_context = !!context;
1361 ASSERT_TRUE(has_live_tab_context);
1362
1363 // Restore tabs from last session using that delegated restore service.
1364 service->RestoreMostRecentEntry(context);
1365
1366 // There should be 2 restored tabs in the new browser.
1367 BrowserList* active_browser_list = BrowserList::GetInstance();
1368 EXPECT_EQ(2u, active_browser_list->size());
1369 EXPECT_EQ(new_browser, active_browser_list->get(0));
1370 Browser* reopened_browser = active_browser_list->get(1);
1371 EXPECT_EQ(2, reopened_browser->tab_strip_model()->count());
1372
1373 // Only metrics of the initial foreground tab are recorded.
1374 WaitForFirstMeaningfulPaintOfActiveTab(reopened_browser);
1375 new_browser_waiter->Wait();
1376 ExpectFirstPaintMetricsTotalCount(1);
1377 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698