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

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

Issue 2928033003: predictors: move ResourcePrefetcher handling to LoadingPredictor. (Closed)
Patch Set: Address comment. Created 3 years, 5 months 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
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 <algorithm> 5 #include <algorithm>
6 #include <cstddef> 6 #include <cstddef>
7 #include <set> 7 #include <set>
8 8
9 #include "base/command_line.h" 9 #include "base/command_line.h"
10 #include "base/memory/ptr_util.h" 10 #include "base/memory/ptr_util.h"
(...skipping 270 matching lines...) Expand 10 before | Expand all | Expand 10 after
281 bool match_navigation_id_; 281 bool match_navigation_id_;
282 bool match_before_first_contentful_paint_; 282 bool match_before_first_contentful_paint_;
283 std::set<NavigationID> current_navigation_ids_; 283 std::set<NavigationID> current_navigation_ids_;
284 284
285 DISALLOW_COPY_AND_ASSIGN(LearningObserver); 285 DISALLOW_COPY_AND_ASSIGN(LearningObserver);
286 }; 286 };
287 287
288 // Helper class to track and allow waiting for a single OnPrefetchingFinished 288 // Helper class to track and allow waiting for a single OnPrefetchingFinished
289 // event. Checks also that {Start,Stop}Prefetching are called with the right 289 // event. Checks also that {Start,Stop}Prefetching are called with the right
290 // argument. 290 // argument.
291 class PrefetchingObserver : public TestObserver { 291 class PrefetchingObserver : public TestLoadingObserver {
292 public: 292 public:
293 PrefetchingObserver(ResourcePrefetchPredictor* predictor, 293 PrefetchingObserver(LoadingPredictor* predictor,
294 const GURL& expected_main_frame_url, 294 const GURL& expected_main_frame_url)
295 bool is_learning_allowed) 295 : TestLoadingObserver(predictor),
296 : TestObserver(predictor), 296 main_frame_url_(expected_main_frame_url) {}
297 main_frame_url_(expected_main_frame_url),
298 is_learning_allowed_(is_learning_allowed) {}
299 297
300 // TestObserver: 298 // LoadingTestObserver:
301 void OnNavigationLearned(size_t url_visit_count,
302 const PageRequestSummary& summary) override {
303 if (!is_learning_allowed_)
304 ADD_FAILURE() << "Prefetching shouldn't activate learning";
305 }
306
307 void OnPrefetchingStarted(const GURL& main_frame_url) override { 299 void OnPrefetchingStarted(const GURL& main_frame_url) override {
308 EXPECT_EQ(main_frame_url_, main_frame_url); 300 EXPECT_EQ(main_frame_url_, main_frame_url);
309 } 301 }
310 302
311 void OnPrefetchingStopped(const GURL& main_frame_url) override { 303 void OnPrefetchingStopped(const GURL& main_frame_url) override {
312 EXPECT_EQ(main_frame_url_, main_frame_url); 304 EXPECT_EQ(main_frame_url_, main_frame_url);
313 } 305 }
314 306
315 void OnPrefetchingFinished(const GURL& main_frame_url) override { 307 void OnPrefetchingFinished(const GURL& main_frame_url) override {
316 EXPECT_EQ(main_frame_url_, main_frame_url); 308 EXPECT_EQ(main_frame_url_, main_frame_url);
317 run_loop_.Quit(); 309 run_loop_.Quit();
318 } 310 }
319 311
312 // TODO(alexilin): Consider checking that prefetching does not activate
313 // learning here.
314
320 void Wait() { run_loop_.Run(); } 315 void Wait() { run_loop_.Run(); }
321 316
322 private: 317 private:
323 base::RunLoop run_loop_; 318 base::RunLoop run_loop_;
324 GURL main_frame_url_; 319 GURL main_frame_url_;
325 bool is_learning_allowed_;
326 320
327 DISALLOW_COPY_AND_ASSIGN(PrefetchingObserver); 321 DISALLOW_COPY_AND_ASSIGN(PrefetchingObserver);
328 }; 322 };
329 323
330 class ResourcePrefetchPredictorBrowserTest : public InProcessBrowserTest { 324 class ResourcePrefetchPredictorBrowserTest : public InProcessBrowserTest {
331 protected: 325 protected:
332 using URLRequestSummary = ResourcePrefetchPredictor::URLRequestSummary; 326 using URLRequestSummary = ResourcePrefetchPredictor::URLRequestSummary;
333 327
334 void SetUpCommandLine(base::CommandLine* command_line) override { 328 void SetUpCommandLine(base::CommandLine* command_line) override {
335 command_line->AppendSwitchASCII("force-fieldtrials", "trial/group"); 329 command_line->AppendSwitchASCII("force-fieldtrials", "trial/group");
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after
439 433
440 for (auto& kv : resources_) { 434 for (auto& kv : resources_) {
441 if (kv.second.is_observable) 435 if (kv.second.is_observable)
442 kv.second.request.was_cached = true; 436 kv.second.request.was_cached = true;
443 } 437 }
444 for (const auto& nav : observer.current_navigation_ids()) 438 for (const auto& nav : observer.current_navigation_ids())
445 navigation_id_history_.insert(nav); 439 navigation_id_history_.insert(nav);
446 } 440 }
447 441
448 void NavigateToURLAndCheckPrefetching(const GURL& main_frame_url) { 442 void NavigateToURLAndCheckPrefetching(const GURL& main_frame_url) {
449 PrefetchingObserver observer(resource_prefetch_predictor_, main_frame_url, 443 PrefetchingObserver observer(predictor_, main_frame_url);
450 true);
451 ui_test_utils::NavigateToURL(browser(), main_frame_url); 444 ui_test_utils::NavigateToURL(browser(), main_frame_url);
452 observer.Wait(); 445 observer.Wait();
453 for (auto& kv : resources_) { 446 for (auto& kv : resources_) {
454 if (kv.second.is_observable) 447 if (kv.second.is_observable)
455 kv.second.request.was_cached = true; 448 kv.second.request.was_cached = true;
456 } 449 }
457 } 450 }
458 451
459 void PrefetchURL(const GURL& main_frame_url) { 452 void PrefetchURL(const GURL& main_frame_url) {
460 PrefetchingObserver observer(resource_prefetch_predictor_, main_frame_url, 453 PrefetchingObserver observer(predictor_, main_frame_url);
461 false);
462 predictor_->PrepareForPageLoad(main_frame_url, HintOrigin::EXTERNAL); 454 predictor_->PrepareForPageLoad(main_frame_url, HintOrigin::EXTERNAL);
463 observer.Wait(); 455 observer.Wait();
464 for (auto& kv : resources_) { 456 for (auto& kv : resources_) {
465 if (kv.second.is_observable) 457 if (kv.second.is_observable)
466 kv.second.request.was_cached = true; 458 kv.second.request.was_cached = true;
467 } 459 }
468 } 460 }
469 461
470 void TryToPrefetchURL(const GURL& main_frame_url) { 462 void TryToPrefetchURL(const GURL& main_frame_url) {
471 predictor_->PrepareForPageLoad(main_frame_url, HintOrigin::EXTERNAL); 463 predictor_->PrepareForPageLoad(main_frame_url, HintOrigin::EXTERNAL);
(...skipping 537 matching lines...) Expand 10 before | Expand all | Expand 10 after
1009 AddResourcesFromSubresourceHtml(); 1001 AddResourcesFromSubresourceHtml();
1010 1002
1011 NavigateToURLAndCheckSubresources(initial_url); 1003 NavigateToURLAndCheckSubresources(initial_url);
1012 ClearCache(); 1004 ClearCache();
1013 NavigateToURLAndCheckSubresources(initial_url); 1005 NavigateToURLAndCheckSubresources(initial_url);
1014 ClearCache(); 1006 ClearCache();
1015 NavigateToURLAndCheckPrefetching(initial_url); 1007 NavigateToURLAndCheckPrefetching(initial_url);
1016 } 1008 }
1017 1009
1018 } // namespace predictors 1010 } // namespace predictors
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698