OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "content/browser/frame_host/navigation_controller_impl.h" | 5 #include "content/browser/frame_host/navigation_controller_impl.h" |
6 | 6 |
7 #include <stdint.h> | 7 #include <stdint.h> |
8 #include <utility> | 8 #include <utility> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
11 #include "base/command_line.h" | 11 #include "base/command_line.h" |
12 #include "base/macros.h" | 12 #include "base/macros.h" |
13 #include "base/strings/stringprintf.h" | 13 #include "base/strings/stringprintf.h" |
14 #include "base/strings/utf_string_conversions.h" | 14 #include "base/strings/utf_string_conversions.h" |
| 15 #include "base/test/histogram_tester.h" |
15 #include "content/browser/frame_host/frame_navigation_entry.h" | 16 #include "content/browser/frame_host/frame_navigation_entry.h" |
16 #include "content/browser/frame_host/frame_tree.h" | 17 #include "content/browser/frame_host/frame_tree.h" |
17 #include "content/browser/frame_host/navigation_entry_impl.h" | 18 #include "content/browser/frame_host/navigation_entry_impl.h" |
18 #include "content/browser/loader/resource_dispatcher_host_impl.h" | 19 #include "content/browser/loader/resource_dispatcher_host_impl.h" |
19 #include "content/browser/web_contents/web_contents_impl.h" | 20 #include "content/browser/web_contents/web_contents_impl.h" |
20 #include "content/common/frame_messages.h" | 21 #include "content/common/frame_messages.h" |
21 #include "content/common/page_state_serialization.h" | 22 #include "content/common/page_state_serialization.h" |
22 #include "content/common/site_isolation_policy.h" | 23 #include "content/common/site_isolation_policy.h" |
23 #include "content/public/browser/navigation_handle.h" | 24 #include "content/public/browser/navigation_handle.h" |
24 #include "content/public/browser/render_view_host.h" | 25 #include "content/public/browser/render_view_host.h" |
(...skipping 6202 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
6227 EXPECT_TRUE(NavigateToURL(shell(), url2)); | 6228 EXPECT_TRUE(NavigateToURL(shell(), url2)); |
6228 | 6229 |
6229 // What happens now is that attempting to unload the first page will trigger a | 6230 // What happens now is that attempting to unload the first page will trigger a |
6230 // JavaScript alert but allow navigation. The alert IPC will be suspended by | 6231 // JavaScript alert but allow navigation. The alert IPC will be suspended by |
6231 // the message filter. The commit of the second page will unblock the IPC. If | 6232 // the message filter. The commit of the second page will unblock the IPC. If |
6232 // the dialog IPC is allowed to spawn a dialog, the call by the WebContents to | 6233 // the dialog IPC is allowed to spawn a dialog, the call by the WebContents to |
6233 // its delegate to get the JavaScriptDialogManager will cause a CHECK and the | 6234 // its delegate to get the JavaScriptDialogManager will cause a CHECK and the |
6234 // test will fail. | 6235 // test will fail. |
6235 } | 6236 } |
6236 | 6237 |
| 6238 namespace { |
| 6239 |
| 6240 // Execute JavaScript without the user gesture flag set, and wait for the |
| 6241 // triggered load finished. |
| 6242 void ExecuteJavaScriptAndWaitForLoadStop(WebContents* web_contents, |
| 6243 const std::string script) { |
| 6244 // WaitForLoadStop() does not work to wait for loading that is triggered by |
| 6245 // JavaScript asynchronously. |
| 6246 TestNavigationObserver observer(web_contents); |
| 6247 |
| 6248 // ExecuteScript() sets a user gesture flag internally for testing, but we |
| 6249 // want to run JavaScript without the flag. Call ExecuteJavaScriptForTests |
| 6250 // directory. |
| 6251 static_cast<WebContentsImpl*>(web_contents) |
| 6252 ->GetMainFrame() |
| 6253 ->ExecuteJavaScriptForTests(base::UTF8ToUTF16(script)); |
| 6254 |
| 6255 observer.Wait(); |
| 6256 } |
| 6257 |
| 6258 } // namespace |
| 6259 |
| 6260 // Check if consecutive reloads can be correctly captured by metrics. |
| 6261 IN_PROC_BROWSER_TEST_F(NavigationControllerBrowserTest, |
| 6262 ConsecutiveReloadMetrics) { |
| 6263 base::HistogramTester histogram; |
| 6264 |
| 6265 const char kReloadToReloadMetricName[] = |
| 6266 "Navigation.Reload.ReloadToReloadDuration"; |
| 6267 const char kReloadMainResourceToReloadMetricName[] = |
| 6268 "Navigation.Reload.ReloadMainResourceToReloadDuration"; |
| 6269 |
| 6270 // Navigate to a page, and check if metrics are initialized correctly. |
| 6271 NavigateToURL(shell(), embedded_test_server()->GetURL( |
| 6272 "/navigation_controller/page_with_links.html")); |
| 6273 histogram.ExpectTotalCount(kReloadToReloadMetricName, 0); |
| 6274 histogram.ExpectTotalCount(kReloadMainResourceToReloadMetricName, 0); |
| 6275 |
| 6276 NavigationControllerImpl& controller = static_cast<NavigationControllerImpl&>( |
| 6277 shell()->web_contents()->GetController()); |
| 6278 |
| 6279 // ReloadToRefreshContent triggers a reload of ReloadType::MAIN_RESOURCE. The |
| 6280 // first reload should not be counted. |
| 6281 controller.ReloadToRefreshContent(false); |
| 6282 EXPECT_TRUE(WaitForLoadStop(shell()->web_contents())); |
| 6283 histogram.ExpectTotalCount(kReloadToReloadMetricName, 0); |
| 6284 histogram.ExpectTotalCount(kReloadMainResourceToReloadMetricName, 0); |
| 6285 |
| 6286 // ReloadBypassingCache triggers a reload of ReloadType::BYPASSING_CACHE. |
| 6287 // Both metrics should count the consecutive reloads. |
| 6288 controller.ReloadBypassingCache(false); |
| 6289 EXPECT_TRUE(WaitForLoadStop(shell()->web_contents())); |
| 6290 histogram.ExpectTotalCount(kReloadToReloadMetricName, 1); |
| 6291 histogram.ExpectTotalCount(kReloadMainResourceToReloadMetricName, 1); |
| 6292 |
| 6293 // Triggers another reload of ReloadType::BYPASSING_CACHE. |
| 6294 // ReloadMainResourceToReload should not be counted here. |
| 6295 controller.ReloadBypassingCache(false); |
| 6296 EXPECT_TRUE(WaitForLoadStop(shell()->web_contents())); |
| 6297 histogram.ExpectTotalCount(kReloadToReloadMetricName, 2); |
| 6298 histogram.ExpectTotalCount(kReloadMainResourceToReloadMetricName, 1); |
| 6299 |
| 6300 // A browser-initiated navigation should reset the reload tracking |
| 6301 // information. |
| 6302 NavigateToURL(shell(), embedded_test_server()->GetURL( |
| 6303 "/navigation_controller/simple_page_1.html")); |
| 6304 histogram.ExpectTotalCount(kReloadToReloadMetricName, 2); |
| 6305 histogram.ExpectTotalCount(kReloadMainResourceToReloadMetricName, 1); |
| 6306 |
| 6307 // Then, the next reload should be assumed as the first reload. Metrics |
| 6308 // should not be changed for the first reload. |
| 6309 controller.ReloadToRefreshContent(false); |
| 6310 EXPECT_TRUE(WaitForLoadStop(shell()->web_contents())); |
| 6311 histogram.ExpectTotalCount(kReloadToReloadMetricName, 2); |
| 6312 histogram.ExpectTotalCount(kReloadMainResourceToReloadMetricName, 1); |
| 6313 |
| 6314 // Another reload of ReloadType::MAIN_RESOURCE should be counted by both |
| 6315 // metrics again. |
| 6316 controller.ReloadToRefreshContent(false); |
| 6317 EXPECT_TRUE(WaitForLoadStop(shell()->web_contents())); |
| 6318 histogram.ExpectTotalCount(kReloadToReloadMetricName, 3); |
| 6319 histogram.ExpectTotalCount(kReloadMainResourceToReloadMetricName, 2); |
| 6320 |
| 6321 // A renderer-initiated navigations with no user gesture don't reset reload |
| 6322 // tracking information, and the following reload will be counted by metrics. |
| 6323 ExecuteJavaScriptAndWaitForLoadStop( |
| 6324 shell()->web_contents(), |
| 6325 "history.pushState({}, 'page 1', 'simple_page_1.html')"); |
| 6326 histogram.ExpectTotalCount(kReloadToReloadMetricName, 3); |
| 6327 histogram.ExpectTotalCount(kReloadMainResourceToReloadMetricName, 2); |
| 6328 ExecuteJavaScriptAndWaitForLoadStop(shell()->web_contents(), |
| 6329 "location.href='simple_page_2.html'"); |
| 6330 histogram.ExpectTotalCount(kReloadToReloadMetricName, 3); |
| 6331 histogram.ExpectTotalCount(kReloadMainResourceToReloadMetricName, 2); |
| 6332 |
| 6333 controller.ReloadToRefreshContent(false); |
| 6334 EXPECT_TRUE(WaitForLoadStop(shell()->web_contents())); |
| 6335 histogram.ExpectTotalCount(kReloadToReloadMetricName, 4); |
| 6336 histogram.ExpectTotalCount(kReloadMainResourceToReloadMetricName, 3); |
| 6337 |
| 6338 // Go back to the first page. Reload tracking information should be reset. |
| 6339 shell()->web_contents()->GetController().GoBack(); |
| 6340 EXPECT_TRUE(WaitForLoadStop(shell()->web_contents())); |
| 6341 histogram.ExpectTotalCount(kReloadToReloadMetricName, 4); |
| 6342 histogram.ExpectTotalCount(kReloadMainResourceToReloadMetricName, 3); |
| 6343 |
| 6344 controller.ReloadToRefreshContent(false); |
| 6345 EXPECT_TRUE(WaitForLoadStop(shell()->web_contents())); |
| 6346 histogram.ExpectTotalCount(kReloadToReloadMetricName, 4); |
| 6347 histogram.ExpectTotalCount(kReloadMainResourceToReloadMetricName, 3); |
| 6348 } |
| 6349 |
6237 } // namespace content | 6350 } // namespace content |
OLD | NEW |