Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/predictors/loading_predictor.h" | |
| 6 | |
| 7 #include <memory> | |
| 8 #include <string> | |
| 9 | |
| 10 #include "base/run_loop.h" | |
| 11 #include "base/test/histogram_tester.h" | |
| 12 #include "chrome/browser/predictors/resource_prefetch_predictor_test_util.h" | |
| 13 #include "chrome/test/base/testing_profile.h" | |
| 14 #include "content/public/test/test_browser_thread_bundle.h" | |
| 15 #include "testing/gtest/include/gtest/gtest.h" | |
| 16 | |
| 17 namespace predictors { | |
| 18 | |
| 19 class LoadingPredictorTest : public testing::Test { | |
| 20 public: | |
| 21 LoadingPredictorTest(); | |
| 22 ~LoadingPredictorTest() override; | |
| 23 void SetUp() override; | |
| 24 void TearDown() override; | |
| 25 | |
| 26 protected: | |
| 27 std::unique_ptr<LoadingPredictor> predictor_; | |
| 28 content::TestBrowserThreadBundle thread_bundle_; | |
|
alexilin
2017/05/30 16:19:14
Nit: thread_bundle_ must be the first member.
http
Benoit L
2017/05/31 14:34:42
Thanks!
Done.
| |
| 29 std::unique_ptr<TestingProfile> profile_; | |
| 30 }; | |
| 31 | |
| 32 LoadingPredictorTest::LoadingPredictorTest() | |
| 33 : profile_(base::MakeUnique<TestingProfile>()) {} | |
| 34 | |
| 35 LoadingPredictorTest::~LoadingPredictorTest() { | |
| 36 profile_ = nullptr; | |
| 37 base::RunLoop().RunUntilIdle(); | |
| 38 } | |
| 39 | |
| 40 void LoadingPredictorTest::SetUp() { | |
| 41 LoadingPredictorConfig config; | |
| 42 PopulateTestConfig(&config); | |
| 43 predictor_ = base::MakeUnique<LoadingPredictor>(config, profile_.get()); | |
| 44 predictor_->StartInitialization(); | |
| 45 base::RunLoop loop; | |
|
alexilin
2017/05/30 16:19:14
tiny nit:
Why not base::RunLoop.RunUntilIdle() as
Benoit L
2017/05/31 14:34:42
Done.
| |
| 46 loop.RunUntilIdle(); // Runs the DB lookup. | |
| 47 } | |
| 48 | |
| 49 void LoadingPredictorTest::TearDown() { | |
| 50 predictor_ = nullptr; | |
| 51 profile_->DestroyHistoryService(); | |
| 52 } | |
| 53 | |
| 54 TEST_F(LoadingPredictorTest, TestPrefetchingDurationHistogram) { | |
| 55 base::HistogramTester histogram_tester; | |
| 56 | |
| 57 const GURL url = GURL("http://www.google.com/cats"); | |
| 58 const GURL url2 = GURL("http://www.notgoogle.com/dogs"); | |
| 59 | |
| 60 predictor_->PrepareForPageLoad(url, HintOrigin::EXTERNAL); | |
| 61 predictor_->CancelPageLoadHint(url); | |
| 62 histogram_tester.ExpectTotalCount( | |
| 63 internal::kResourcePrefetchPredictorPrefetchingDurationHistogram, 1); | |
| 64 | |
| 65 // Mismatched start / end. | |
| 66 predictor_->PrepareForPageLoad(url, HintOrigin::EXTERNAL); | |
| 67 predictor_->CancelPageLoadHint(url2); | |
| 68 // No increment. | |
| 69 histogram_tester.ExpectTotalCount( | |
| 70 internal::kResourcePrefetchPredictorPrefetchingDurationHistogram, 1); | |
| 71 | |
| 72 // Can track a navigation (url2) while one is still in progress (url). | |
| 73 predictor_->PrepareForPageLoad(url2, HintOrigin::EXTERNAL); | |
| 74 predictor_->CancelPageLoadHint(url2); | |
| 75 histogram_tester.ExpectTotalCount( | |
| 76 internal::kResourcePrefetchPredictorPrefetchingDurationHistogram, 2); | |
| 77 } | |
| 78 | |
| 79 TEST_F(LoadingPredictorTest, TestMainFrameResponseCancelHint) { | |
|
alexilin
2017/05/30 16:19:14
nit:
s/Cancel/Cancels/
Benoit L
2017/05/31 14:34:42
Done.
| |
| 80 const GURL url = GURL("http://www.google.com/cats"); | |
| 81 | |
| 82 predictor_->PrepareForPageLoad(url, HintOrigin::EXTERNAL); | |
| 83 EXPECT_EQ(1UL, predictor_->active_hints_.size()); | |
| 84 | |
| 85 auto summary = CreateURLRequestSummary(12, url.spec()); | |
| 86 predictor_->OnMainFrameResponse(summary); | |
| 87 EXPECT_TRUE(predictor_->active_hints_.empty()); | |
| 88 } | |
| 89 | |
| 90 TEST_F(LoadingPredictorTest, TestMainFrameRequestCancelStaleNavigations) { | |
|
alexilin
2017/05/30 16:19:14
ditto
Benoit L
2017/05/31 14:34:42
Done.
| |
| 91 const std::string url = "http://www.google.com/cats"; | |
| 92 const std::string url2 = "http://www.notgoogle.com/dogs"; | |
| 93 const int tab_id = 12; | |
| 94 const auto& active_navigations = predictor_->active_navigations_; | |
| 95 const auto& active_hints = predictor_->active_hints_; | |
| 96 | |
| 97 auto summary = CreateURLRequestSummary(tab_id, url); | |
| 98 auto navigation_id = CreateNavigationID(tab_id, url); | |
| 99 | |
| 100 predictor_->OnMainFrameRequest(summary); | |
| 101 EXPECT_NE(active_navigations.find(navigation_id), active_navigations.end()); | |
| 102 EXPECT_NE(active_hints.find(GURL(url)), active_hints.end()); | |
| 103 | |
| 104 summary = CreateURLRequestSummary(tab_id, url2); | |
| 105 predictor_->OnMainFrameRequest(summary); | |
| 106 EXPECT_EQ(active_navigations.find(navigation_id), active_navigations.end()); | |
| 107 EXPECT_EQ(active_hints.find(GURL(url)), active_hints.end()); | |
| 108 | |
| 109 auto navigation_id2 = CreateNavigationID(tab_id, url2); | |
| 110 EXPECT_NE(active_navigations.find(navigation_id2), active_navigations.end()); | |
| 111 } | |
| 112 | |
| 113 TEST_F(LoadingPredictorTest, TestMainFrameResponseClearsNavigation) { | |
| 114 const std::string url = "http://www.google.com/cats"; | |
| 115 const std::string redirected = "http://www.google.com/dogs"; | |
| 116 const int tab_id = 12; | |
| 117 const auto& active_navigations = predictor_->active_navigations_; | |
| 118 const auto& active_hints = predictor_->active_hints_; | |
| 119 | |
| 120 auto summary = CreateURLRequestSummary(tab_id, url); | |
| 121 auto navigation_id = CreateNavigationID(tab_id, url); | |
| 122 | |
| 123 predictor_->OnMainFrameRequest(summary); | |
| 124 EXPECT_NE(active_navigations.find(navigation_id), active_navigations.end()); | |
| 125 EXPECT_FALSE(active_hints.empty()); | |
| 126 | |
| 127 predictor_->OnMainFrameResponse(summary); | |
| 128 EXPECT_TRUE(active_navigations.empty()); | |
| 129 EXPECT_TRUE(active_hints.empty()); | |
| 130 | |
| 131 // With redirects. | |
| 132 predictor_->OnMainFrameRequest(summary); | |
| 133 EXPECT_NE(active_navigations.find(navigation_id), active_navigations.end()); | |
| 134 EXPECT_FALSE(active_hints.empty()); | |
| 135 | |
| 136 summary.redirect_url = GURL(redirected); | |
| 137 predictor_->OnMainFrameRedirect(summary); | |
| 138 EXPECT_FALSE(active_navigations.empty()); | |
| 139 EXPECT_FALSE(active_hints.empty()); | |
| 140 | |
| 141 summary.navigation_id.main_frame_url = GURL(redirected); | |
| 142 predictor_->OnMainFrameResponse(summary); | |
| 143 EXPECT_TRUE(active_navigations.empty()); | |
| 144 EXPECT_TRUE(active_hints.empty()); | |
| 145 } | |
| 146 | |
| 147 } // namespace predictors | |
|
alexilin
2017/05/30 16:19:14
Another useful usecase:
OnMainFrameRequest doesn't
Benoit L
2017/05/31 14:34:42
Done.
| |
| OLD | NEW |