Chromium Code Reviews| OLD | NEW |
|---|---|
| 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" | |
| 15 #include "chrome/browser/lifetime/keep_alive_types.h" | |
| 16 #include "chrome/browser/lifetime/scoped_keep_alive.h" | |
| 14 #include "chrome/browser/page_load_metrics/metrics_web_contents_observer.h" | 17 #include "chrome/browser/page_load_metrics/metrics_web_contents_observer.h" |
| 15 #include "chrome/browser/page_load_metrics/observers/aborts_page_load_metrics_ob server.h" | 18 #include "chrome/browser/page_load_metrics/observers/aborts_page_load_metrics_ob server.h" |
| 16 #include "chrome/browser/page_load_metrics/observers/core_page_load_metrics_obse rver.h" | 19 #include "chrome/browser/page_load_metrics/observers/core_page_load_metrics_obse rver.h" |
| 17 #include "chrome/browser/page_load_metrics/observers/document_write_page_load_me trics_observer.h" | 20 #include "chrome/browser/page_load_metrics/observers/document_write_page_load_me trics_observer.h" |
| 18 #include "chrome/browser/page_load_metrics/observers/no_state_prefetch_page_load _metrics_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/session_restore_page_load_m etrics_observer.h" | |
| 19 #include "chrome/browser/page_load_metrics/page_load_tracker.h" | 23 #include "chrome/browser/page_load_metrics/page_load_tracker.h" |
| 24 #include "chrome/browser/prefs/session_startup_pref.h" | |
| 20 #include "chrome/browser/prerender/prerender_histograms.h" | 25 #include "chrome/browser/prerender/prerender_histograms.h" |
| 21 #include "chrome/browser/prerender/prerender_origin.h" | 26 #include "chrome/browser/prerender/prerender_origin.h" |
| 22 #include "chrome/browser/profiles/profile.h" | 27 #include "chrome/browser/profiles/profile.h" |
| 28 #include "chrome/browser/sessions/session_service_factory.h" | |
| 29 #include "chrome/browser/sessions/session_service_test_helper.h" | |
| 23 #include "chrome/browser/ui/browser.h" | 30 #include "chrome/browser/ui/browser.h" |
| 31 #include "chrome/browser/ui/browser_commands.h" | |
| 24 #include "chrome/browser/ui/browser_navigator_params.h" | 32 #include "chrome/browser/ui/browser_navigator_params.h" |
| 25 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 33 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 26 #include "chrome/common/chrome_features.h" | 34 #include "chrome/common/chrome_features.h" |
| 27 #include "chrome/common/pref_names.h" | 35 #include "chrome/common/pref_names.h" |
| 28 #include "chrome/common/url_constants.h" | 36 #include "chrome/common/url_constants.h" |
| 29 #include "chrome/test/base/in_process_browser_test.h" | 37 #include "chrome/test/base/in_process_browser_test.h" |
| 30 #include "chrome/test/base/ui_test_utils.h" | 38 #include "chrome/test/base/ui_test_utils.h" |
| 31 #include "components/prefs/pref_service.h" | 39 #include "components/prefs/pref_service.h" |
| 32 #include "content/public/browser/browser_thread.h" | 40 #include "content/public/browser/browser_thread.h" |
| 33 #include "content/public/browser/render_process_host.h" | 41 #include "content/public/browser/render_process_host.h" |
| 34 #include "content/public/browser/render_view_host.h" | 42 #include "content/public/browser/render_view_host.h" |
| 43 #include "content/public/common/browser_side_navigation_policy.h" | |
| 35 #include "content/public/common/content_features.h" | 44 #include "content/public/common/content_features.h" |
| 36 #include "content/public/common/content_switches.h" | 45 #include "content/public/common/content_switches.h" |
| 46 #include "content/public/common/referrer.h" | |
| 37 #include "content/public/test/browser_test_utils.h" | 47 #include "content/public/test/browser_test_utils.h" |
| 38 #include "content/public/test/download_test_observer.h" | 48 #include "content/public/test/download_test_observer.h" |
| 39 #include "net/base/net_errors.h" | 49 #include "net/base/net_errors.h" |
| 40 #include "net/dns/mock_host_resolver.h" | 50 #include "net/dns/mock_host_resolver.h" |
| 41 #include "net/http/failing_http_transaction_factory.h" | 51 #include "net/http/failing_http_transaction_factory.h" |
| 42 #include "net/http/http_cache.h" | 52 #include "net/http/http_cache.h" |
| 43 #include "net/test/embedded_test_server/embedded_test_server.h" | 53 #include "net/test/embedded_test_server/embedded_test_server.h" |
| 44 #include "net/test/url_request/url_request_failed_job.h" | 54 #include "net/test/url_request/url_request_failed_job.h" |
| 45 #include "net/url_request/url_request_context.h" | 55 #include "net/url_request/url_request_context.h" |
| 46 #include "net/url_request/url_request_context_getter.h" | 56 #include "net/url_request/url_request_context_getter.h" |
| (...skipping 995 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1042 browser(), embedded_test_server()->GetURL( | 1052 browser(), embedded_test_server()->GetURL( |
| 1043 "/page_load_metrics/download_anchor_click.html")); | 1053 "/page_load_metrics/download_anchor_click.html")); |
| 1044 downloads_observer.WaitForFinished(); | 1054 downloads_observer.WaitForFinished(); |
| 1045 } | 1055 } |
| 1046 base::ThreadRestrictions::SetIOAllowed(prev_io_allowed); | 1056 base::ThreadRestrictions::SetIOAllowed(prev_io_allowed); |
| 1047 | 1057 |
| 1048 NavigateToUntrackedUrl(); | 1058 NavigateToUntrackedUrl(); |
| 1049 | 1059 |
| 1050 histogram_tester_.ExpectUniqueSample(internal::kHistogramTotalBytes, 0, 1); | 1060 histogram_tester_.ExpectUniqueSample(internal::kHistogramTotalBytes, 0, 1); |
| 1051 } | 1061 } |
| 1062 | |
| 1063 class SessionRestorePageLoadMetricsBrowserTest | |
| 1064 : public PageLoadMetricsBrowserTest { | |
| 1065 public: | |
| 1066 SessionRestorePageLoadMetricsBrowserTest() {} | |
| 1067 | |
| 1068 void SetUpOnMainThread() override { | |
| 1069 PageLoadMetricsBrowserTest::SetUpOnMainThread(); | |
| 1070 SessionStartupPref::SetStartupPref( | |
| 1071 browser()->profile(), SessionStartupPref(SessionStartupPref::LAST)); | |
| 1072 ASSERT_TRUE(embedded_test_server()->Start()); | |
| 1073 #if defined(OS_CHROMEOS) | |
| 1074 SessionServiceTestHelper helper( | |
| 1075 SessionServiceFactory::GetForProfile(browser()->profile())); | |
| 1076 helper.SetForceBrowserNotAliveWithNoWindows(true); | |
| 1077 helper.ReleaseService(); | |
| 1078 #endif | |
| 1079 } | |
| 1080 | |
| 1081 Browser* QuitBrowserAndRestore(Browser* browser) { | |
| 1082 Profile* profile = browser->profile(); | |
| 1083 | |
| 1084 // Close the browser. | |
| 1085 std::unique_ptr<ScopedKeepAlive> keep_alive(new ScopedKeepAlive( | |
| 1086 KeepAliveOrigin::SESSION_RESTORE, KeepAliveRestartOption::DISABLED)); | |
| 1087 CloseBrowserSynchronously(browser); | |
| 1088 | |
| 1089 // Create a new window, which should trigger session restore. | |
| 1090 chrome::NewEmptyWindow(profile); | |
| 1091 ui_test_utils::BrowserAddedObserver window_observer; | |
| 1092 return window_observer.WaitForSingleNewBrowser(); | |
| 1093 } | |
| 1094 | |
| 1095 std::unique_ptr<PageLoadMetricsWaiter> CreatePageLoadMetricsWaiter( | |
| 1096 content::WebContents* web_contents) const { | |
| 1097 return base::MakeUnique<PageLoadMetricsWaiter>(web_contents); | |
| 1098 } | |
| 1099 | |
| 1100 void WaitForFirstMeaningfulPaintOfActiveTab(Browser* browser) const { | |
| 1101 auto waiter = CreatePageLoadMetricsWaiter( | |
| 1102 browser->tab_strip_model()->GetActiveWebContents()); | |
| 1103 waiter->AddPageExpectation(TimingField::FIRST_MEANINGFUL_PAINT); | |
| 1104 waiter->Wait(); | |
| 1105 } | |
| 1106 | |
| 1107 void WaitForTabsToLoad(Browser* browser) { | |
| 1108 for (int i = 0; i < browser->tab_strip_model()->count(); ++i) { | |
| 1109 content::WebContents* contents = | |
| 1110 browser->tab_strip_model()->GetWebContentsAt(i); | |
| 1111 contents->GetController().LoadIfNecessary(); | |
| 1112 content::WaitForLoadStop(contents); | |
| 1113 } | |
| 1114 } | |
| 1115 | |
| 1116 GURL GetTestURL() const { | |
| 1117 return embedded_test_server()->GetURL( | |
| 1118 "/page_load_metrics/page_with_css.html"); | |
| 1119 } | |
| 1120 | |
| 1121 GURL GetTestURL2() const { | |
| 1122 return embedded_test_server()->GetURL( | |
| 1123 "/page_load_metrics/main_frame_with_iframe.html"); | |
| 1124 } | |
| 1125 | |
| 1126 void ExpectFirstPaintMetricsTotalCount(int expected_total_count) const { | |
| 1127 // SessionRestorePageLoadMetricsObserver is disabled when browser-side | |
| 1128 // navigation is enabled | |
| 1129 if (content::IsBrowserSideNavigationEnabled()) | |
| 1130 expected_total_count = 0; | |
| 1131 | |
| 1132 histogram_tester_.ExpectTotalCount( | |
| 1133 internal::kHistogramSessionRestoreForegroundTabFirstPaint, | |
| 1134 expected_total_count); | |
| 1135 histogram_tester_.ExpectTotalCount( | |
| 1136 internal::kHistogramSessionRestoreForegroundTabFirstContentfulPaint, | |
| 1137 expected_total_count); | |
| 1138 histogram_tester_.ExpectTotalCount( | |
| 1139 internal::kHistogramSessionRestoreForegroundTabFirstMeaningfulPaint, | |
| 1140 expected_total_count); | |
| 1141 } | |
| 1142 | |
| 1143 private: | |
| 1144 DISALLOW_COPY_AND_ASSIGN(SessionRestorePageLoadMetricsBrowserTest); | |
| 1145 }; | |
| 1146 | |
| 1147 IN_PROC_BROWSER_TEST_F(SessionRestorePageLoadMetricsBrowserTest, | |
| 1148 NoSessionRestore) { | |
| 1149 ui_test_utils::NavigateToURL(browser(), GetTestURL()); | |
| 1150 // No metrics recorded because navigation is outside of session restore. | |
| 1151 ExpectFirstPaintMetricsTotalCount(0); | |
| 1152 } | |
| 1153 | |
| 1154 IN_PROC_BROWSER_TEST_F(SessionRestorePageLoadMetricsBrowserTest, | |
| 1155 SingleTabSessionRestore) { | |
| 1156 ui_test_utils::NavigateToURL(browser(), GetTestURL()); | |
| 1157 Browser* new_browser = QuitBrowserAndRestore(browser()); | |
| 1158 WaitForFirstMeaningfulPaintOfActiveTab(new_browser); | |
| 1159 ExpectFirstPaintMetricsTotalCount(1); | |
| 1160 } | |
| 1161 | |
| 1162 IN_PROC_BROWSER_TEST_F(SessionRestorePageLoadMetricsBrowserTest, | |
| 1163 MultipleTabsSessionRestore) { | |
| 1164 ui_test_utils::NavigateToURL(browser(), GetTestURL()); | |
| 1165 ui_test_utils::NavigateToURLWithDisposition( | |
| 1166 browser(), GetTestURL(), WindowOpenDisposition::NEW_BACKGROUND_TAB, | |
| 1167 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION); | |
| 1168 Browser* new_browser = QuitBrowserAndRestore(browser()); | |
| 1169 | |
| 1170 TabStripModel* tab_strip = new_browser->tab_strip_model(); | |
| 1171 ASSERT_TRUE(tab_strip); | |
| 1172 ASSERT_EQ(2, tab_strip->count()); | |
| 1173 | |
| 1174 // Wait for first paints on the initial foreground tab and all tabs loaded. | |
| 1175 WaitForFirstMeaningfulPaintOfActiveTab(new_browser); | |
| 1176 WaitForTabsToLoad(new_browser); | |
| 1177 | |
| 1178 // Only metrics of the initial foreground tab are recorded. | |
| 1179 ExpectFirstPaintMetricsTotalCount(1); | |
| 1180 } | |
| 1181 | |
| 1182 IN_PROC_BROWSER_TEST_F(SessionRestorePageLoadMetricsBrowserTest, | |
| 1183 LoadingInForegroundTabInSessionRestore) { | |
| 1184 ui_test_utils::NavigateToURL(browser(), GetTestURL()); | |
| 1185 ui_test_utils::NavigateToURLWithDisposition( | |
| 1186 browser(), GetTestURL2(), WindowOpenDisposition::NEW_BACKGROUND_TAB, | |
| 1187 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION); | |
| 1188 Browser* new_browser = QuitBrowserAndRestore(browser()); | |
| 1189 | |
| 1190 // Load a new page in the foreground tab before it finishes loading. | |
| 1191 chrome::NavigateParams params(new_browser, GetTestURL2(), | |
| 1192 ui::PAGE_TRANSITION_LINK); | |
| 1193 chrome::Navigate(¶ms); | |
| 1194 // When PlzNavigate enabled, the waiter does not see first paints | |
| 1195 if (!content::IsBrowserSideNavigationEnabled()) { | |
|
ducbui
2017/07/03 23:38:44
Somehow the PageLoadMetricsWaiter does not see the
Bryan McQuade
2017/07/03 23:42:24
Ah, I missed these browser-side navigation checks.
| |
| 1196 auto waiter = | |
| 1197 base::MakeUnique<PageLoadMetricsWaiter>(params.target_contents); | |
| 1198 waiter->AddPageExpectation(TimingField::FIRST_MEANINGFUL_PAINT); | |
| 1199 waiter->Wait(); | |
| 1200 } | |
| 1201 | |
| 1202 // Do not count the new page load in the initial foreground tab. | |
| 1203 ExpectFirstPaintMetricsTotalCount(0); | |
| 1204 } | |
| 1205 | |
| 1206 IN_PROC_BROWSER_TEST_F(SessionRestorePageLoadMetricsBrowserTest, | |
| 1207 LoadingAfterSessionRestore) { | |
| 1208 ui_test_utils::NavigateToURL(browser(), GetTestURL()); | |
| 1209 Browser* new_browser = QuitBrowserAndRestore(browser()); | |
| 1210 | |
| 1211 // Wait for session restore to finish (i.e., the end of the only tab). | |
| 1212 WaitForFirstMeaningfulPaintOfActiveTab(new_browser); | |
| 1213 ExpectFirstPaintMetricsTotalCount(1); | |
| 1214 | |
| 1215 // Load a new page after session restore. | |
| 1216 auto waiter = CreatePageLoadMetricsWaiter( | |
| 1217 new_browser->tab_strip_model()->GetActiveWebContents()); | |
| 1218 waiter->AddPageExpectation(TimingField::FIRST_MEANINGFUL_PAINT); | |
| 1219 ui_test_utils::NavigateToURL(new_browser, GetTestURL2()); | |
| 1220 waiter->Wait(); | |
| 1221 | |
| 1222 // No more metrics because the navigation is after session restore. | |
| 1223 ExpectFirstPaintMetricsTotalCount(1); | |
| 1224 } | |
| 1225 | |
| 1226 IN_PROC_BROWSER_TEST_F(SessionRestorePageLoadMetricsBrowserTest, | |
| 1227 InitialForegroundTabChanged) { | |
| 1228 ui_test_utils::NavigateToURL(browser(), GetTestURL()); | |
| 1229 ui_test_utils::NavigateToURLWithDisposition( | |
| 1230 browser(), GetTestURL2(), WindowOpenDisposition::NEW_BACKGROUND_TAB, | |
| 1231 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION); | |
| 1232 Browser* new_browser = QuitBrowserAndRestore(browser()); | |
| 1233 | |
| 1234 // Change the foreground tab before the first meaningful paint. | |
| 1235 TabStripModel* tab_strip = new_browser->tab_strip_model(); | |
| 1236 ASSERT_TRUE(tab_strip); | |
| 1237 ASSERT_EQ(2, tab_strip->count()); | |
| 1238 ASSERT_EQ(0, tab_strip->active_index()); | |
| 1239 tab_strip->ActivateTabAt(1, true); | |
| 1240 | |
| 1241 // Wait for the first meaningful paint of the current foreground tab. | |
| 1242 WaitForFirstMeaningfulPaintOfActiveTab(new_browser); | |
| 1243 ExpectFirstPaintMetricsTotalCount(0); | |
| 1244 } | |
| 1245 | |
| 1246 IN_PROC_BROWSER_TEST_F(SessionRestorePageLoadMetricsBrowserTest, | |
| 1247 MultipleSessionRestores) { | |
| 1248 ui_test_utils::NavigateToURL(browser(), GetTestURL()); | |
| 1249 | |
| 1250 Browser* current_browser = browser(); | |
| 1251 const int num_session_restores = 3; | |
| 1252 for (int i = 1; i <= num_session_restores; ++i) { | |
| 1253 current_browser = QuitBrowserAndRestore(current_browser); | |
| 1254 WaitForFirstMeaningfulPaintOfActiveTab(current_browser); | |
| 1255 ExpectFirstPaintMetricsTotalCount(i); | |
| 1256 } | |
| 1257 } | |
| OLD | NEW |