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 "chrome/browser/lifetime/keep_alive_types.h" | |
| 15 #include "chrome/browser/lifetime/scoped_keep_alive.h" | |
| 14 #include "chrome/browser/page_load_metrics/metrics_web_contents_observer.h" | 16 #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" | 17 #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" | 18 #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" | 19 #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" | 20 #include "chrome/browser/page_load_metrics/observers/no_state_prefetch_page_load _metrics_observer.h" |
| 21 #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" | 22 #include "chrome/browser/page_load_metrics/page_load_tracker.h" |
| 23 #include "chrome/browser/prefs/session_startup_pref.h" | |
| 20 #include "chrome/browser/prerender/prerender_histograms.h" | 24 #include "chrome/browser/prerender/prerender_histograms.h" |
| 21 #include "chrome/browser/prerender/prerender_origin.h" | 25 #include "chrome/browser/prerender/prerender_origin.h" |
| 22 #include "chrome/browser/profiles/profile.h" | 26 #include "chrome/browser/profiles/profile.h" |
| 23 #include "chrome/browser/ui/browser.h" | 27 #include "chrome/browser/ui/browser.h" |
| 28 #include "chrome/browser/ui/browser_commands.h" | |
| 24 #include "chrome/browser/ui/browser_navigator_params.h" | 29 #include "chrome/browser/ui/browser_navigator_params.h" |
| 25 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 30 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 26 #include "chrome/common/chrome_features.h" | 31 #include "chrome/common/chrome_features.h" |
| 27 #include "chrome/common/pref_names.h" | 32 #include "chrome/common/pref_names.h" |
| 28 #include "chrome/common/url_constants.h" | 33 #include "chrome/common/url_constants.h" |
| 29 #include "chrome/test/base/in_process_browser_test.h" | 34 #include "chrome/test/base/in_process_browser_test.h" |
| 30 #include "chrome/test/base/ui_test_utils.h" | 35 #include "chrome/test/base/ui_test_utils.h" |
| 31 #include "components/prefs/pref_service.h" | 36 #include "components/prefs/pref_service.h" |
| 32 #include "content/public/browser/browser_thread.h" | 37 #include "content/public/browser/browser_thread.h" |
| 33 #include "content/public/browser/render_process_host.h" | 38 #include "content/public/browser/render_process_host.h" |
| 34 #include "content/public/browser/render_view_host.h" | 39 #include "content/public/browser/render_view_host.h" |
| 35 #include "content/public/common/content_features.h" | 40 #include "content/public/common/content_features.h" |
| 36 #include "content/public/common/content_switches.h" | 41 #include "content/public/common/content_switches.h" |
| 42 #include "content/public/common/referrer.h" | |
| 37 #include "content/public/test/browser_test_utils.h" | 43 #include "content/public/test/browser_test_utils.h" |
| 38 #include "content/public/test/download_test_observer.h" | 44 #include "content/public/test/download_test_observer.h" |
| 39 #include "net/base/net_errors.h" | 45 #include "net/base/net_errors.h" |
| 40 #include "net/dns/mock_host_resolver.h" | 46 #include "net/dns/mock_host_resolver.h" |
| 41 #include "net/http/failing_http_transaction_factory.h" | 47 #include "net/http/failing_http_transaction_factory.h" |
| 42 #include "net/http/http_cache.h" | 48 #include "net/http/http_cache.h" |
| 43 #include "net/test/embedded_test_server/embedded_test_server.h" | 49 #include "net/test/embedded_test_server/embedded_test_server.h" |
| 44 #include "net/test/url_request/url_request_failed_job.h" | 50 #include "net/test/url_request/url_request_failed_job.h" |
| 45 #include "net/url_request/url_request_context.h" | 51 #include "net/url_request/url_request_context.h" |
| 46 #include "net/url_request/url_request_context_getter.h" | 52 #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( | 1048 browser(), embedded_test_server()->GetURL( |
| 1043 "/page_load_metrics/download_anchor_click.html")); | 1049 "/page_load_metrics/download_anchor_click.html")); |
| 1044 downloads_observer.WaitForFinished(); | 1050 downloads_observer.WaitForFinished(); |
| 1045 } | 1051 } |
| 1046 base::ThreadRestrictions::SetIOAllowed(prev_io_allowed); | 1052 base::ThreadRestrictions::SetIOAllowed(prev_io_allowed); |
| 1047 | 1053 |
| 1048 NavigateToUntrackedUrl(); | 1054 NavigateToUntrackedUrl(); |
| 1049 | 1055 |
| 1050 histogram_tester_.ExpectUniqueSample(internal::kHistogramTotalBytes, 0, 1); | 1056 histogram_tester_.ExpectUniqueSample(internal::kHistogramTotalBytes, 0, 1); |
| 1051 } | 1057 } |
| 1058 | |
| 1059 class SessionRestorePageLoadMetricsBrowserTest | |
|
Bryan McQuade
2017/06/30 20:04:38
thanks for all these tests! i just want to make su
ducbui
2017/06/30 22:03:51
Done.
| |
| 1060 : public PageLoadMetricsBrowserTest { | |
| 1061 public: | |
| 1062 SessionRestorePageLoadMetricsBrowserTest() {} | |
| 1063 | |
| 1064 void SetUpOnMainThread() override { | |
| 1065 PageLoadMetricsBrowserTest::SetUpOnMainThread(); | |
| 1066 SessionStartupPref pref(SessionStartupPref::LAST); | |
| 1067 SessionStartupPref::SetStartupPref(browser()->profile(), pref); | |
| 1068 ASSERT_TRUE(embedded_test_server()->Start()); | |
| 1069 } | |
| 1070 | |
| 1071 Browser* QuitBrowserAndRestore(Browser* browser) { | |
| 1072 // Close the browser. | |
| 1073 std::unique_ptr<ScopedKeepAlive> keep_alive(new ScopedKeepAlive( | |
| 1074 KeepAliveOrigin::SESSION_RESTORE, KeepAliveRestartOption::DISABLED)); | |
| 1075 CloseBrowserSynchronously(browser); | |
| 1076 | |
| 1077 // Create a new window, which should trigger session restore. | |
| 1078 chrome::NewEmptyWindow(browser->profile()); | |
| 1079 ui_test_utils::BrowserAddedObserver window_observer; | |
| 1080 return window_observer.WaitForSingleNewBrowser(); | |
| 1081 } | |
| 1082 | |
| 1083 std::unique_ptr<PageLoadMetricsWaiter> CreatePageLoadMetricsWaiter( | |
| 1084 content::WebContents* web_contents) const { | |
| 1085 return base::MakeUnique<PageLoadMetricsWaiter>(web_contents); | |
| 1086 } | |
| 1087 | |
| 1088 void WaitForFirstMeaningfulPaintOfActiveTab(Browser* browser) const { | |
| 1089 auto waiter = CreatePageLoadMetricsWaiter( | |
| 1090 browser->tab_strip_model()->GetActiveWebContents()); | |
| 1091 waiter->AddPageExpectation(TimingField::FIRST_MEANINGFUL_PAINT); | |
| 1092 waiter->Wait(); | |
| 1093 } | |
| 1094 | |
| 1095 void WaitForTabsToLoad(Browser* browser) { | |
| 1096 for (int i = 0; i < browser->tab_strip_model()->count(); ++i) { | |
| 1097 content::WebContents* contents = | |
| 1098 browser->tab_strip_model()->GetWebContentsAt(i); | |
| 1099 contents->GetController().LoadIfNecessary(); | |
|
Bryan McQuade
2017/06/30 20:04:38
i notice we call this here, but we don't end up in
ducbui
2017/06/30 22:03:51
WaitForTabsToLoad() is used only in the MultipleTa
| |
| 1100 content::WaitForLoadStop(contents); | |
| 1101 } | |
| 1102 } | |
| 1103 | |
| 1104 GURL GetTestURL() const { | |
| 1105 return embedded_test_server()->GetURL( | |
| 1106 "/page_load_metrics/page_with_css.html"); | |
| 1107 } | |
| 1108 | |
| 1109 GURL GetTestURL2() const { | |
| 1110 return embedded_test_server()->GetURL( | |
| 1111 "/page_load_metrics/main_frame_with_iframe.html"); | |
| 1112 } | |
| 1113 | |
| 1114 void ExpectFirstPaintMetricsTotalCount(int expected_total_count) const { | |
| 1115 histogram_tester_.ExpectTotalCount( | |
| 1116 internal::kHistogramSessionRestoreForegroundTabFirstPaint, | |
| 1117 expected_total_count); | |
| 1118 histogram_tester_.ExpectTotalCount( | |
| 1119 internal::kHistogramSessionRestoreForegroundTabFirstContentfulPaint, | |
| 1120 expected_total_count); | |
| 1121 histogram_tester_.ExpectTotalCount( | |
| 1122 internal::kHistogramSessionRestoreForegroundTabFirstMeaningfulPaint, | |
| 1123 expected_total_count); | |
| 1124 } | |
| 1125 | |
| 1126 private: | |
| 1127 DISALLOW_COPY_AND_ASSIGN(SessionRestorePageLoadMetricsBrowserTest); | |
| 1128 }; | |
| 1129 | |
| 1130 IN_PROC_BROWSER_TEST_F(SessionRestorePageLoadMetricsBrowserTest, | |
| 1131 NoSessionRestore) { | |
| 1132 ui_test_utils::NavigateToURL(browser(), GetTestURL()); | |
| 1133 // No metrics recorded because navigation is outside of session restore. | |
| 1134 ExpectFirstPaintMetricsTotalCount(0); | |
| 1135 } | |
| 1136 | |
| 1137 IN_PROC_BROWSER_TEST_F(SessionRestorePageLoadMetricsBrowserTest, | |
| 1138 SingleTabSessionRestore) { | |
| 1139 ui_test_utils::NavigateToURL(browser(), GetTestURL()); | |
| 1140 Browser* new_browser = QuitBrowserAndRestore(browser()); | |
| 1141 WaitForFirstMeaningfulPaintOfActiveTab(new_browser); | |
| 1142 ExpectFirstPaintMetricsTotalCount(1); | |
| 1143 } | |
| 1144 | |
| 1145 IN_PROC_BROWSER_TEST_F(SessionRestorePageLoadMetricsBrowserTest, | |
| 1146 MultipleTabsSessionRestore) { | |
| 1147 ui_test_utils::NavigateToURL(browser(), GetTestURL()); | |
| 1148 ui_test_utils::NavigateToURLWithDisposition( | |
| 1149 browser(), GetTestURL(), WindowOpenDisposition::NEW_BACKGROUND_TAB, | |
| 1150 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION); | |
| 1151 Browser* new_browser = QuitBrowserAndRestore(browser()); | |
| 1152 | |
| 1153 TabStripModel* tab_strip = new_browser->tab_strip_model(); | |
| 1154 ASSERT_TRUE(tab_strip); | |
| 1155 ASSERT_EQ(2, tab_strip->count()); | |
| 1156 | |
| 1157 // Wait for first paints on the initial foreground tab and all tabs loaded. | |
| 1158 WaitForFirstMeaningfulPaintOfActiveTab(new_browser); | |
| 1159 WaitForTabsToLoad(new_browser); | |
| 1160 | |
| 1161 // Only metrics of the initial foreground tab are recorded. | |
| 1162 ExpectFirstPaintMetricsTotalCount(1); | |
| 1163 } | |
| 1164 | |
| 1165 IN_PROC_BROWSER_TEST_F(SessionRestorePageLoadMetricsBrowserTest, | |
| 1166 LoadingInForegroundTabInSessionRestore) { | |
| 1167 ui_test_utils::NavigateToURL(browser(), GetTestURL()); | |
| 1168 ui_test_utils::NavigateToURLWithDisposition( | |
| 1169 browser(), GetTestURL2(), WindowOpenDisposition::NEW_BACKGROUND_TAB, | |
| 1170 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION); | |
| 1171 Browser* new_browser = QuitBrowserAndRestore(browser()); | |
| 1172 | |
| 1173 // Load a new page in the foreground tab before it finishes loading. | |
| 1174 content::WebContents* active_web_contents = | |
| 1175 new_browser->tab_strip_model()->GetActiveWebContents(); | |
| 1176 auto waiter = CreatePageLoadMetricsWaiter(active_web_contents); | |
| 1177 waiter->AddPageExpectation(TimingField::FIRST_MEANINGFUL_PAINT); | |
| 1178 active_web_contents->GetController().LoadURL( | |
| 1179 GetTestURL2(), content::Referrer(), ui::PAGE_TRANSITION_TYPED, | |
| 1180 std::string()); | |
| 1181 waiter->Wait(); | |
| 1182 | |
| 1183 // We also count the new page load in the initial foreground tab. | |
| 1184 ExpectFirstPaintMetricsTotalCount(1); | |
|
Bryan McQuade
2017/06/30 20:04:38
interesting, is this the desired behavior? if i st
ducbui
2017/06/30 22:03:51
Done. I agree that it would better to filter out t
| |
| 1185 } | |
| 1186 | |
| 1187 IN_PROC_BROWSER_TEST_F(SessionRestorePageLoadMetricsBrowserTest, | |
| 1188 LoadingAfterSessionRestore) { | |
| 1189 ui_test_utils::NavigateToURL(browser(), GetTestURL()); | |
| 1190 Browser* new_browser = QuitBrowserAndRestore(browser()); | |
| 1191 | |
| 1192 // Wait for session restore to finish (i.e., the end of the only tab). | |
| 1193 WaitForFirstMeaningfulPaintOfActiveTab(new_browser); | |
| 1194 ExpectFirstPaintMetricsTotalCount(1); | |
| 1195 | |
| 1196 // Load a new page after session restore. | |
| 1197 auto waiter = CreatePageLoadMetricsWaiter( | |
| 1198 new_browser->tab_strip_model()->GetActiveWebContents()); | |
| 1199 waiter->AddPageExpectation(TimingField::FIRST_MEANINGFUL_PAINT); | |
| 1200 ui_test_utils::NavigateToURL(new_browser, GetTestURL2()); | |
| 1201 waiter->Wait(); | |
| 1202 | |
| 1203 // No more metrics because the navigation is after session restore. | |
| 1204 ExpectFirstPaintMetricsTotalCount(1); | |
| 1205 } | |
| 1206 | |
| 1207 IN_PROC_BROWSER_TEST_F(SessionRestorePageLoadMetricsBrowserTest, | |
| 1208 InitialForegroundTabChanged) { | |
| 1209 ui_test_utils::NavigateToURL(browser(), GetTestURL()); | |
| 1210 ui_test_utils::NavigateToURLWithDisposition( | |
| 1211 browser(), GetTestURL2(), WindowOpenDisposition::NEW_BACKGROUND_TAB, | |
| 1212 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION); | |
| 1213 Browser* new_browser = QuitBrowserAndRestore(browser()); | |
| 1214 | |
| 1215 // Change the foreground tab before the first meaningful paint. | |
| 1216 TabStripModel* tab_strip = new_browser->tab_strip_model(); | |
| 1217 ASSERT_TRUE(tab_strip); | |
| 1218 ASSERT_EQ(2, tab_strip->count()); | |
| 1219 ASSERT_EQ(0, tab_strip->active_index()); | |
| 1220 tab_strip->ActivateTabAt(1, true); | |
| 1221 | |
| 1222 // Wait for the first meaningful paint of the current foreground tab. | |
| 1223 WaitForFirstMeaningfulPaintOfActiveTab(new_browser); | |
| 1224 ExpectFirstPaintMetricsTotalCount(0); | |
| 1225 } | |
| 1226 | |
| 1227 IN_PROC_BROWSER_TEST_F(SessionRestorePageLoadMetricsBrowserTest, | |
| 1228 MultipleSessionRestores) { | |
| 1229 ui_test_utils::NavigateToURL(browser(), GetTestURL()); | |
| 1230 | |
| 1231 Browser* current_browser = browser(); | |
| 1232 const int num_session_restores = 3; | |
| 1233 for (int i = 1; i <= num_session_restores; ++i) { | |
| 1234 current_browser = QuitBrowserAndRestore(current_browser); | |
| 1235 WaitForFirstMeaningfulPaintOfActiveTab(current_browser); | |
| 1236 ExpectFirstPaintMetricsTotalCount(i); | |
| 1237 } | |
| 1238 } | |
| OLD | NEW |