| OLD | NEW |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 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 | 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 "chrome/browser/predictors/loading_predictor.h" | 5 #include "chrome/browser/predictors/loading_predictor.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 #include <set> | 8 #include <set> |
| 9 #include <string> | 9 #include <string> |
| 10 #include <utility> | 10 #include <utility> |
| 11 #include <vector> | 11 #include <vector> |
| 12 | 12 |
| 13 #include "base/run_loop.h" | 13 #include "base/run_loop.h" |
| 14 #include "base/test/histogram_tester.h" | 14 #include "base/test/histogram_tester.h" |
| 15 #include "chrome/browser/predictors/resource_prefetch_predictor_test_util.h" | 15 #include "chrome/browser/predictors/resource_prefetch_predictor_test_util.h" |
| 16 #include "chrome/test/base/testing_profile.h" | 16 #include "chrome/test/base/testing_profile.h" |
| 17 #include "content/public/test/test_browser_thread_bundle.h" | 17 #include "content/public/test/test_browser_thread_bundle.h" |
| 18 #include "testing/gmock/include/gmock/gmock.h" | 18 #include "testing/gmock/include/gmock/gmock.h" |
| 19 #include "testing/gtest/include/gtest/gtest.h" | 19 #include "testing/gtest/include/gtest/gtest.h" |
| 20 | 20 |
| 21 using testing::_; |
| 22 using testing::Return; |
| 23 using testing::StrictMock; |
| 24 |
| 21 namespace predictors { | 25 namespace predictors { |
| 22 | 26 |
| 23 namespace { | 27 namespace { |
| 24 // First two are prefetchable, last one is not (see SetUp()). | 28 // First two are prefetchable, last one is not (see SetUp()). |
| 25 const char kUrl[] = "http://www.google.com/cats"; | 29 const char kUrl[] = "http://www.google.com/cats"; |
| 26 const char kUrl2[] = "http://www.google.com/dogs"; | 30 const char kUrl2[] = "http://www.google.com/dogs"; |
| 27 const char kUrl3[] = "https://unknown.website/catsanddogs"; | 31 const char kUrl3[] = "https://unknown.website/catsanddogs"; |
| 28 } | 32 } |
| 29 | 33 |
| 30 class LoadingPredictorTest : public testing::Test { | 34 class LoadingPredictorTest : public testing::Test { |
| (...skipping 14 matching lines...) Expand all Loading... |
| 45 | 49 |
| 46 LoadingPredictorTest::~LoadingPredictorTest() { | 50 LoadingPredictorTest::~LoadingPredictorTest() { |
| 47 profile_ = nullptr; | 51 profile_ = nullptr; |
| 48 base::RunLoop().RunUntilIdle(); | 52 base::RunLoop().RunUntilIdle(); |
| 49 } | 53 } |
| 50 | 54 |
| 51 void LoadingPredictorTest::SetUp() { | 55 void LoadingPredictorTest::SetUp() { |
| 52 LoadingPredictorConfig config; | 56 LoadingPredictorConfig config; |
| 53 PopulateTestConfig(&config); | 57 PopulateTestConfig(&config); |
| 54 predictor_ = base::MakeUnique<LoadingPredictor>(config, profile_.get()); | 58 predictor_ = base::MakeUnique<LoadingPredictor>(config, profile_.get()); |
| 55 auto mock = | 59 |
| 56 base::MakeUnique<MockResourcePrefetchPredictor>(config, profile_.get()); | 60 auto mock = base::MakeUnique<StrictMock<MockResourcePrefetchPredictor>>( |
| 57 mock->AddPrefetchableUrl(GURL(kUrl)); | 61 config, profile_.get()); |
| 58 mock->AddPrefetchableUrl(GURL(kUrl2)); | 62 EXPECT_CALL(*mock, StartInitialization()); |
| 63 EXPECT_CALL(*mock, GetPrefetchData(GURL(kUrl), _)) |
| 64 .WillRepeatedly(Return(true)); |
| 65 EXPECT_CALL(*mock, GetPrefetchData(GURL(kUrl2), _)) |
| 66 .WillRepeatedly(Return(true)); |
| 67 EXPECT_CALL(*mock, GetPrefetchData(GURL(kUrl3), _)) |
| 68 .WillRepeatedly(Return(false)); |
| 69 |
| 59 predictor_->set_mock_resource_prefetch_predictor(std::move(mock)); | 70 predictor_->set_mock_resource_prefetch_predictor(std::move(mock)); |
| 60 predictor_->StartInitialization(); | 71 predictor_->StartInitialization(); |
| 61 base::RunLoop().RunUntilIdle(); | 72 base::RunLoop().RunUntilIdle(); |
| 62 } | 73 } |
| 63 | 74 |
| 64 void LoadingPredictorTest::TearDown() { | 75 void LoadingPredictorTest::TearDown() { |
| 65 predictor_ = nullptr; | 76 predictor_ = nullptr; |
| 66 profile_->DestroyHistoryService(); | 77 profile_->DestroyHistoryService(); |
| 67 } | 78 } |
| 68 | 79 |
| 69 TEST_F(LoadingPredictorTest, TestPrefetchingDurationHistogram) { | 80 TEST_F(LoadingPredictorTest, TestPrefetchingDurationHistogram) { |
| 70 base::HistogramTester histogram_tester; | 81 base::HistogramTester histogram_tester; |
| 71 | |
| 72 const GURL url = GURL(kUrl); | 82 const GURL url = GURL(kUrl); |
| 73 const GURL url2 = GURL(kUrl2); | 83 const GURL url2 = GURL(kUrl2); |
| 74 const GURL url3 = GURL(kUrl3); | 84 const GURL url3 = GURL(kUrl3); |
| 85 |
| 75 predictor_->PrepareForPageLoad(url, HintOrigin::EXTERNAL); | 86 predictor_->PrepareForPageLoad(url, HintOrigin::EXTERNAL); |
| 76 predictor_->CancelPageLoadHint(url); | 87 predictor_->CancelPageLoadHint(url); |
| 77 histogram_tester.ExpectTotalCount( | 88 histogram_tester.ExpectTotalCount( |
| 78 internal::kResourcePrefetchPredictorPrefetchingDurationHistogram, 1); | 89 internal::kResourcePrefetchPredictorPrefetchingDurationHistogram, 1); |
| 79 | 90 |
| 80 // Mismatched start / end. | 91 // Mismatched start / end. |
| 81 predictor_->PrepareForPageLoad(url, HintOrigin::EXTERNAL); | 92 predictor_->PrepareForPageLoad(url, HintOrigin::EXTERNAL); |
| 82 predictor_->CancelPageLoadHint(url2); | 93 predictor_->CancelPageLoadHint(url2); |
| 83 // No increment. | 94 // No increment. |
| 84 histogram_tester.ExpectTotalCount( | 95 histogram_tester.ExpectTotalCount( |
| (...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 190 EXPECT_EQ(start_time, it->second); | 201 EXPECT_EQ(start_time, it->second); |
| 191 } | 202 } |
| 192 | 203 |
| 193 TEST_F(LoadingPredictorTest, TestDontTrackNonPrefetchableUrls) { | 204 TEST_F(LoadingPredictorTest, TestDontTrackNonPrefetchableUrls) { |
| 194 const GURL url3 = GURL(kUrl3); | 205 const GURL url3 = GURL(kUrl3); |
| 195 predictor_->PrepareForPageLoad(url3, HintOrigin::EXTERNAL); | 206 predictor_->PrepareForPageLoad(url3, HintOrigin::EXTERNAL); |
| 196 EXPECT_TRUE(predictor_->active_hints_.empty()); | 207 EXPECT_TRUE(predictor_->active_hints_.empty()); |
| 197 } | 208 } |
| 198 | 209 |
| 199 } // namespace predictors | 210 } // namespace predictors |
| OLD | NEW |