Chromium Code Reviews| OLD | NEW | 
|---|---|
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 "chrome/browser/predictors/resource_prefetch_predictor.h" | 6 #include "chrome/browser/predictors/resource_prefetch_predictor.h" | 
| 7 #include "chrome/browser/predictors/resource_prefetch_predictor_factory.h" | 7 #include "chrome/browser/predictors/resource_prefetch_predictor_factory.h" | 
| 8 #include "chrome/browser/predictors/resource_prefetch_predictor_test_util.h" | 8 #include "chrome/browser/predictors/resource_prefetch_predictor_test_util.h" | 
| 9 #include "chrome/browser/profiles/profile.h" | 9 #include "chrome/browser/profiles/profile.h" | 
| 10 #include "chrome/browser/ui/browser.h" | 10 #include "chrome/browser/ui/browser.h" | 
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 54 void OnPredictorInitialized() override { run_loop_.Quit(); } | 54 void OnPredictorInitialized() override { run_loop_.Quit(); } | 
| 55 | 55 | 
| 56 void Wait() { run_loop_.Run(); } | 56 void Wait() { run_loop_.Run(); } | 
| 57 | 57 | 
| 58 private: | 58 private: | 
| 59 base::RunLoop run_loop_; | 59 base::RunLoop run_loop_; | 
| 60 | 60 | 
| 61 DISALLOW_COPY_AND_ASSIGN(InitializationObserver); | 61 DISALLOW_COPY_AND_ASSIGN(InitializationObserver); | 
| 62 }; | 62 }; | 
| 63 | 63 | 
| 64 using PageRequestSummary = ResourcePrefetchPredictor::PageRequestSummary; | |
| 65 using URLRequestSummary = ResourcePrefetchPredictor::URLRequestSummary; | |
| 66 | |
| 67 std::vector<URLRequestSummary> GetUniqueSubresources( | |
| 
 
pasko
2016/11/28 15:41:28
nit: anonymous namespace for everything between kI
 
alexilin
2016/11/29 10:32:00
Done.
 
 | |
| 68 const PageRequestSummary& summary) { | |
| 69 std::vector<URLRequestSummary> subresources(summary.subresource_requests); | |
| 70 std::stable_sort(subresources.begin(), subresources.end(), | |
| 71 [](const URLRequestSummary& x, const URLRequestSummary& y) { | |
| 72 return x.resource_url < y.resource_url; | |
| 73 }); | |
| 74 subresources.erase( | |
| 75 std::unique(subresources.begin(), subresources.end(), | |
| 76 [](const URLRequestSummary& x, const URLRequestSummary& y) { | |
| 77 return x.resource_url == y.resource_url; | |
| 78 }), | |
| 79 subresources.end()); | |
| 80 return subresources; | |
| 81 } | |
| 82 | |
| 64 // Helper class to track and allow waiting for ResourcePrefetchPredictor events. | 83 // Helper class to track and allow waiting for ResourcePrefetchPredictor events. | 
| 65 // These events are also used to verify that ResourcePrefetchPredictor works as | 84 // These events are also used to verify that ResourcePrefetchPredictor works as | 
| 66 // expected. | 85 // expected. | 
| 67 class ResourcePrefetchPredictorTestObserver : public TestObserver { | 86 class ResourcePrefetchPredictorTestObserver : public TestObserver { | 
| 68 public: | 87 public: | 
| 69 using PageRequestSummary = ResourcePrefetchPredictor::PageRequestSummary; | |
| 70 | 88 | 
| 71 explicit ResourcePrefetchPredictorTestObserver( | 89 explicit ResourcePrefetchPredictorTestObserver( | 
| 72 ResourcePrefetchPredictor* predictor, | 90 ResourcePrefetchPredictor* predictor, | 
| 73 const size_t expected_url_visit_count, | 91 const size_t expected_url_visit_count, | 
| 74 const PageRequestSummary& expected_summary) | 92 const PageRequestSummary& expected_summary) | 
| 75 : TestObserver(predictor), | 93 : TestObserver(predictor), | 
| 76 url_visit_count_(expected_url_visit_count), | 94 url_visit_count_(expected_url_visit_count), | 
| 77 summary_(expected_summary) {} | 95 summary_(expected_summary) {} | 
| 78 | 96 | 
| 79 // TestObserver: | 97 // TestObserver: | 
| 80 void OnNavigationLearned(size_t url_visit_count, | 98 void OnNavigationLearned(size_t url_visit_count, | 
| 81 const PageRequestSummary& summary) override { | 99 const PageRequestSummary& summary) override { | 
| 82 EXPECT_EQ(url_visit_count, url_visit_count_); | 100 EXPECT_EQ(url_visit_count, url_visit_count_); | 
| 83 EXPECT_EQ(summary.main_frame_url, summary_.main_frame_url); | 101 EXPECT_EQ(summary.main_frame_url, summary_.main_frame_url); | 
| 84 EXPECT_EQ(summary.initial_url, summary_.initial_url); | 102 EXPECT_EQ(summary.initial_url, summary_.initial_url); | 
| 85 EXPECT_THAT( | 103 // Duplicate resources can be observed in a single navigation but | 
| 86 summary.subresource_requests, | 104 // ResourcePrefetchPredictor only cares about the first occurrence of each. | 
| 87 testing::UnorderedElementsAreArray(summary_.subresource_requests)); | 105 vector<URLRequestSummary> subresources = GetUniqueSubresources(summary); | 
| 106 EXPECT_THAT(subresources, testing::UnorderedElementsAreArray( | |
| 107 summary_.subresource_requests)); | |
| 88 run_loop_.Quit(); | 108 run_loop_.Quit(); | 
| 89 } | 109 } | 
| 90 | 110 | 
| 91 void Wait() { run_loop_.Run(); } | 111 void Wait() { run_loop_.Run(); } | 
| 92 | 112 | 
| 93 private: | 113 private: | 
| 94 base::RunLoop run_loop_; | 114 base::RunLoop run_loop_; | 
| 95 size_t url_visit_count_; | 115 size_t url_visit_count_; | 
| 96 PageRequestSummary summary_; | 116 PageRequestSummary summary_; | 
| 97 | 117 | 
| 98 DISALLOW_COPY_AND_ASSIGN(ResourcePrefetchPredictorTestObserver); | 118 DISALLOW_COPY_AND_ASSIGN(ResourcePrefetchPredictorTestObserver); | 
| 99 }; | 119 }; | 
| 100 | 120 | 
| 101 class ResourcePrefetchPredictorBrowserTest : public InProcessBrowserTest { | 121 class ResourcePrefetchPredictorBrowserTest : public InProcessBrowserTest { | 
| 102 protected: | 122 protected: | 
| 103 using URLRequestSummary = ResourcePrefetchPredictor::URLRequestSummary; | |
| 104 | 123 | 
| 105 void SetUpCommandLine(base::CommandLine* command_line) override { | 124 void SetUpCommandLine(base::CommandLine* command_line) override { | 
| 106 command_line->AppendSwitchASCII( | 125 command_line->AppendSwitchASCII( | 
| 107 switches::kSpeculativeResourcePrefetching, | 126 switches::kSpeculativeResourcePrefetching, | 
| 108 switches::kSpeculativeResourcePrefetchingEnabled); | 127 switches::kSpeculativeResourcePrefetchingEnabled); | 
| 109 } | 128 } | 
| 110 | 129 | 
| 111 void SetUpOnMainThread() override { | 130 void SetUpOnMainThread() override { | 
| 112 embedded_test_server()->RegisterRequestHandler(base::Bind( | 131 embedded_test_server()->RegisterRequestHandler(base::Bind( | 
| 113 &ResourcePrefetchPredictorBrowserTest::HandleRedirectRequest, | 132 &ResourcePrefetchPredictorBrowserTest::HandleRedirectRequest, | 
| (...skipping 226 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 340 AddResource(https_server()->GetURL(kStylePath), | 359 AddResource(https_server()->GetURL(kStylePath), | 
| 341 content::RESOURCE_TYPE_STYLESHEET, net::HIGHEST); | 360 content::RESOURCE_TYPE_STYLESHEET, net::HIGHEST); | 
| 342 AddResource(https_server()->GetURL(kScriptPath), | 361 AddResource(https_server()->GetURL(kScriptPath), | 
| 343 content::RESOURCE_TYPE_SCRIPT, net::MEDIUM); | 362 content::RESOURCE_TYPE_SCRIPT, net::MEDIUM); | 
| 344 AddResource(https_server()->GetURL(kFontPath), | 363 AddResource(https_server()->GetURL(kFontPath), | 
| 345 content::RESOURCE_TYPE_FONT_RESOURCE, net::HIGHEST); | 364 content::RESOURCE_TYPE_FONT_RESOURCE, net::HIGHEST); | 
| 346 NavigateToURLAndCheckSubresources(GetURL(kRedirectPath)); | 365 NavigateToURLAndCheckSubresources(GetURL(kRedirectPath)); | 
| 347 } | 366 } | 
| 348 | 367 | 
| 349 } // namespace predictors | 368 } // namespace predictors | 
| OLD | NEW |