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

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

Powered by Google App Engine
This is Rietveld 408576698