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

Side by Side Diff: chrome/browser/predictors/resource_prefetch_predictor_browsertest.cc

Issue 2529263003: predictors: Ignore repeating subresources while checking. (Closed)
Patch Set: Created 4 years 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
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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(
68 const PageRequestSummary& summary) {
69 std::vector<URLRequestSummary> subresources(
Benoit L 2016/11/28 14:58:04 nit: why not just a copy?
alexilin 2016/11/28 15:12:15 Done.
70 summary.subresource_requests.begin(), summary.subresource_requests.end());
71 std::sort(subresources.begin(), subresources.end(),
Benoit L 2016/11/28 14:58:04 Do you need stable_sort here?
alexilin 2016/11/28 15:12:15 Thanks! Indeed we need first occurrences of each s
72 [](const URLRequestSummary& x, const URLRequestSummary& y) {
73 return x.resource_url < y.resource_url;
74 });
75 subresources.erase(
76 std::unique(subresources.begin(), subresources.end(),
77 [](const URLRequestSummary& x, const URLRequestSummary& y) {
78 return x.resource_url == y.resource_url;
79 }),
80 subresources.end());
81 return subresources;
82 }
83
64 // Helper class to track and allow waiting for ResourcePrefetchPredictor events. 84 // Helper class to track and allow waiting for ResourcePrefetchPredictor events.
65 // These events are also used to verify that ResourcePrefetchPredictor works as 85 // These events are also used to verify that ResourcePrefetchPredictor works as
66 // expected. 86 // expected.
67 class ResourcePrefetchPredictorTestObserver : public TestObserver { 87 class ResourcePrefetchPredictorTestObserver : public TestObserver {
68 public: 88 public:
69 using PageRequestSummary = ResourcePrefetchPredictor::PageRequestSummary;
70 89
71 explicit ResourcePrefetchPredictorTestObserver( 90 explicit ResourcePrefetchPredictorTestObserver(
72 ResourcePrefetchPredictor* predictor, 91 ResourcePrefetchPredictor* predictor,
73 const size_t expected_url_visit_count, 92 const size_t expected_url_visit_count,
74 const PageRequestSummary& expected_summary) 93 const PageRequestSummary& expected_summary)
75 : TestObserver(predictor), 94 : TestObserver(predictor),
76 url_visit_count_(expected_url_visit_count), 95 url_visit_count_(expected_url_visit_count),
77 summary_(expected_summary) {} 96 summary_(expected_summary) {}
78 97
79 // TestObserver: 98 // TestObserver:
80 void OnNavigationLearned(size_t url_visit_count, 99 void OnNavigationLearned(size_t url_visit_count,
81 const PageRequestSummary& summary) override { 100 const PageRequestSummary& summary) override {
82 EXPECT_EQ(url_visit_count, url_visit_count_); 101 EXPECT_EQ(url_visit_count, url_visit_count_);
83 EXPECT_EQ(summary.main_frame_url, summary_.main_frame_url); 102 EXPECT_EQ(summary.main_frame_url, summary_.main_frame_url);
84 EXPECT_EQ(summary.initial_url, summary_.initial_url); 103 EXPECT_EQ(summary.initial_url, summary_.initial_url);
85 EXPECT_THAT( 104 EXPECT_THAT(
Benoit L 2016/11/28 14:58:04 Can you add a comment to explain that we have dupl
alexilin 2016/11/28 15:12:15 Done.
86 summary.subresource_requests, 105 GetUniqueSubresources(summary),
87 testing::UnorderedElementsAreArray(summary_.subresource_requests)); 106 testing::UnorderedElementsAreArray(summary_.subresource_requests));
88 run_loop_.Quit(); 107 run_loop_.Quit();
89 } 108 }
90 109
91 void Wait() { run_loop_.Run(); } 110 void Wait() { run_loop_.Run(); }
92 111
93 private: 112 private:
94 base::RunLoop run_loop_; 113 base::RunLoop run_loop_;
95 size_t url_visit_count_; 114 size_t url_visit_count_;
96 PageRequestSummary summary_; 115 PageRequestSummary summary_;
97 116
98 DISALLOW_COPY_AND_ASSIGN(ResourcePrefetchPredictorTestObserver); 117 DISALLOW_COPY_AND_ASSIGN(ResourcePrefetchPredictorTestObserver);
99 }; 118 };
100 119
101 class ResourcePrefetchPredictorBrowserTest : public InProcessBrowserTest { 120 class ResourcePrefetchPredictorBrowserTest : public InProcessBrowserTest {
102 protected: 121 protected:
103 using URLRequestSummary = ResourcePrefetchPredictor::URLRequestSummary;
104 122
105 void SetUpCommandLine(base::CommandLine* command_line) override { 123 void SetUpCommandLine(base::CommandLine* command_line) override {
106 command_line->AppendSwitchASCII( 124 command_line->AppendSwitchASCII(
107 switches::kSpeculativeResourcePrefetching, 125 switches::kSpeculativeResourcePrefetching,
108 switches::kSpeculativeResourcePrefetchingEnabled); 126 switches::kSpeculativeResourcePrefetchingEnabled);
109 } 127 }
110 128
111 void SetUpOnMainThread() override { 129 void SetUpOnMainThread() override {
112 embedded_test_server()->RegisterRequestHandler(base::Bind( 130 embedded_test_server()->RegisterRequestHandler(base::Bind(
113 &ResourcePrefetchPredictorBrowserTest::HandleRedirectRequest, 131 &ResourcePrefetchPredictorBrowserTest::HandleRedirectRequest,
(...skipping 226 matching lines...) Expand 10 before | Expand all | Expand 10 after
340 AddResource(https_server()->GetURL(kStylePath), 358 AddResource(https_server()->GetURL(kStylePath),
341 content::RESOURCE_TYPE_STYLESHEET, net::HIGHEST); 359 content::RESOURCE_TYPE_STYLESHEET, net::HIGHEST);
342 AddResource(https_server()->GetURL(kScriptPath), 360 AddResource(https_server()->GetURL(kScriptPath),
343 content::RESOURCE_TYPE_SCRIPT, net::MEDIUM); 361 content::RESOURCE_TYPE_SCRIPT, net::MEDIUM);
344 AddResource(https_server()->GetURL(kFontPath), 362 AddResource(https_server()->GetURL(kFontPath),
345 content::RESOURCE_TYPE_FONT_RESOURCE, net::HIGHEST); 363 content::RESOURCE_TYPE_FONT_RESOURCE, net::HIGHEST);
346 NavigateToURLAndCheckSubresources(GetURL(kRedirectPath)); 364 NavigateToURLAndCheckSubresources(GetURL(kRedirectPath));
347 } 365 }
348 366
349 } // namespace predictors 367 } // namespace predictors
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698