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 "components/previews/core/previews_io_data.h" | 5 #include "components/previews/core/previews_io_data.h" |
6 | 6 |
| 7 #include <map> |
7 #include <memory> | 8 #include <memory> |
| 9 #include <string> |
8 | 10 |
| 11 #include "base/bind.h" |
| 12 #include "base/bind_helpers.h" |
| 13 #include "base/callback.h" |
9 #include "base/memory/ptr_util.h" | 14 #include "base/memory/ptr_util.h" |
10 #include "base/memory/ref_counted.h" | 15 #include "base/memory/ref_counted.h" |
11 #include "base/message_loop/message_loop.h" | 16 #include "base/message_loop/message_loop.h" |
| 17 #include "base/metrics/field_trial.h" |
12 #include "base/run_loop.h" | 18 #include "base/run_loop.h" |
13 #include "base/single_thread_task_runner.h" | 19 #include "base/single_thread_task_runner.h" |
| 20 #include "base/test/histogram_tester.h" |
| 21 #include "base/threading/thread_task_runner_handle.h" |
| 22 #include "base/time/time.h" |
| 23 #include "components/previews/core/previews_black_list.h" |
| 24 #include "components/previews/core/previews_black_list_item.h" |
| 25 #include "components/previews/core/previews_opt_out_store.h" |
14 #include "components/previews/core/previews_ui_service.h" | 26 #include "components/previews/core/previews_ui_service.h" |
| 27 #include "components/variations/variations_associated_data.h" |
| 28 #include "net/nqe/effective_connection_type.h" |
| 29 #include "net/nqe/network_quality_estimator_test_util.h" |
| 30 #include "net/url_request/url_request.h" |
| 31 #include "net/url_request/url_request_test_util.h" |
15 #include "testing/gtest/include/gtest/gtest.h" | 32 #include "testing/gtest/include/gtest/gtest.h" |
| 33 #include "url/gurl.h" |
16 | 34 |
17 namespace previews { | 35 namespace previews { |
18 | 36 |
19 namespace { | 37 namespace { |
20 | 38 |
21 class TestPreviewsIOData : public PreviewsIOData { | 39 class TestPreviewsIOData : public PreviewsIOData { |
22 public: | 40 public: |
23 TestPreviewsIOData( | 41 TestPreviewsIOData( |
24 const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner, | 42 const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner, |
25 const scoped_refptr<base::SingleThreadTaskRunner>& ui_task_runner) | 43 const scoped_refptr<base::SingleThreadTaskRunner>& ui_task_runner) |
26 : PreviewsIOData(io_task_runner, ui_task_runner), initialized_(false) {} | 44 : PreviewsIOData(io_task_runner, ui_task_runner), initialized_(false) {} |
27 ~TestPreviewsIOData() override {} | 45 ~TestPreviewsIOData() override {} |
28 | 46 |
29 // Whether Initialize was called. | 47 // Whether Initialize was called. |
30 bool initialized() { return initialized_; } | 48 bool initialized() { return initialized_; } |
31 | 49 |
32 private: | 50 private: |
33 // Set |initialized_| to true and use base class functionality. | 51 // Set |initialized_| to true and use base class functionality. |
34 void InitializeOnIOThread( | 52 void InitializeOnIOThread( |
35 std::unique_ptr<PreviewsOptOutStore> previews_opt_out_store) override { | 53 std::unique_ptr<PreviewsOptOutStore> previews_opt_out_store) override { |
36 initialized_ = true; | 54 initialized_ = true; |
37 PreviewsIOData::InitializeOnIOThread(std::move(previews_opt_out_store)); | 55 PreviewsIOData::InitializeOnIOThread(std::move(previews_opt_out_store)); |
38 } | 56 } |
39 | 57 |
40 // Whether Initialize was called. | 58 // Whether Initialize was called. |
41 bool initialized_; | 59 bool initialized_; |
42 }; | 60 }; |
43 | 61 |
| 62 void RunLoadCallback( |
| 63 LoadBlackListCallback callback, |
| 64 std::unique_ptr<BlackListItemMap> black_list_item_map, |
| 65 std::unique_ptr<PreviewsBlackListItem> host_indifferent_black_list_item) { |
| 66 callback.Run(std::move(black_list_item_map), |
| 67 std::move(host_indifferent_black_list_item)); |
| 68 } |
| 69 |
| 70 class TestPreviewsOptOutStore : public PreviewsOptOutStore { |
| 71 public: |
| 72 TestPreviewsOptOutStore() {} |
| 73 ~TestPreviewsOptOutStore() override {} |
| 74 |
| 75 private: |
| 76 // PreviewsOptOutStore implementation: |
| 77 void AddPreviewNavigation(bool opt_out, |
| 78 const std::string& host_name, |
| 79 PreviewsType type, |
| 80 base::Time now) override {} |
| 81 |
| 82 void LoadBlackList(LoadBlackListCallback callback) override { |
| 83 std::unique_ptr<BlackListItemMap> black_list_item_map( |
| 84 new BlackListItemMap()); |
| 85 std::unique_ptr<PreviewsBlackListItem> host_indifferent_black_list_item = |
| 86 PreviewsBlackList::CreateHostIndifferentBlackListItem(); |
| 87 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 88 FROM_HERE, base::Bind(&RunLoadCallback, callback, |
| 89 base::Passed(&black_list_item_map), |
| 90 base::Passed(&host_indifferent_black_list_item))); |
| 91 } |
| 92 |
| 93 void ClearBlackList(base::Time begin_time, base::Time end_time) override {} |
| 94 }; |
| 95 |
44 class PreviewsIODataTest : public testing::Test { | 96 class PreviewsIODataTest : public testing::Test { |
45 public: | 97 public: |
46 PreviewsIODataTest() {} | 98 PreviewsIODataTest() {} |
47 | 99 |
48 ~PreviewsIODataTest() override {} | 100 ~PreviewsIODataTest() override {} |
49 | 101 |
50 void set_io_data(std::unique_ptr<TestPreviewsIOData> io_data) { | 102 void set_io_data(std::unique_ptr<TestPreviewsIOData> io_data) { |
51 io_data_ = std::move(io_data); | 103 io_data_ = std::move(io_data); |
52 } | 104 } |
53 | 105 |
54 TestPreviewsIOData* io_data() { return io_data_.get(); } | 106 TestPreviewsIOData* io_data() { return io_data_.get(); } |
55 | 107 |
56 void set_ui_service(std::unique_ptr<PreviewsUIService> ui_service) { | 108 void set_ui_service(std::unique_ptr<PreviewsUIService> ui_service) { |
57 ui_service_ = std::move(ui_service); | 109 ui_service_ = std::move(ui_service); |
58 } | 110 } |
59 | 111 |
60 protected: | 112 protected: |
61 // Run this test on a single thread. | 113 // Run this test on a single thread. |
62 base::MessageLoopForIO loop_; | 114 base::MessageLoopForIO loop_; |
63 | 115 |
64 private: | 116 private: |
65 std::unique_ptr<TestPreviewsIOData> io_data_; | 117 std::unique_ptr<TestPreviewsIOData> io_data_; |
66 std::unique_ptr<PreviewsUIService> ui_service_; | 118 std::unique_ptr<PreviewsUIService> ui_service_; |
67 }; | 119 }; |
68 | 120 |
69 } // namespace | 121 } // namespace |
70 | 122 |
71 TEST_F(PreviewsIODataTest, TestInitialization) { | 123 TEST_F(PreviewsIODataTest, TestInitialization) { |
72 set_io_data(base::WrapUnique( | 124 set_io_data(base::MakeUnique<TestPreviewsIOData>(loop_.task_runner(), |
73 new TestPreviewsIOData(loop_.task_runner(), loop_.task_runner()))); | 125 loop_.task_runner())); |
74 set_ui_service(base::WrapUnique( | 126 set_ui_service(base::MakeUnique<PreviewsUIService>( |
75 new PreviewsUIService(io_data(), loop_.task_runner(), nullptr))); | 127 io_data(), loop_.task_runner(), nullptr)); |
76 base::RunLoop().RunUntilIdle(); | 128 base::RunLoop().RunUntilIdle(); |
77 // After the outstanding posted tasks have run, |io_data_| should be fully | 129 // After the outstanding posted tasks have run, |io_data_| should be fully |
78 // initialized. | 130 // initialized. |
79 EXPECT_TRUE(io_data()->initialized()); | 131 EXPECT_TRUE(io_data()->initialized()); |
80 } | 132 } |
81 | 133 |
| 134 TEST_F(PreviewsIODataTest, TestShouldAllowPreview) { |
| 135 // Test most reasons to disallow the blacklist. |
| 136 // Excluded values are USER_RECENTLY_OPTED_OUT, USER_BLACKLISTED, |
| 137 // HOST_BLACKLISTED. These are internal to the blacklist. |
| 138 net::TestURLRequestContext context; |
| 139 GURL test_host("http://www.test_host.com"); |
| 140 std::unique_ptr<net::URLRequest> request = |
| 141 context.CreateRequest(test_host, net::DEFAULT_PRIORITY, nullptr); |
| 142 set_io_data(base::MakeUnique<TestPreviewsIOData>(loop_.task_runner(), |
| 143 loop_.task_runner())); |
| 144 base::HistogramTester histogram_tester; |
| 145 |
| 146 // If not in the field trial, don't log anything, and return false. |
| 147 EXPECT_FALSE(io_data()->ShouldAllowPreview(*request, PreviewsType::OFFLINE)); |
| 148 histogram_tester.ExpectTotalCount("Previews.EligibilityReason.Offline", 0); |
| 149 |
| 150 // Enable Offline previews field trial. |
| 151 base::FieldTrialList field_trial_list(nullptr); |
| 152 std::map<std::string, std::string> params; |
| 153 params["show_offline_pages"] = "true"; |
| 154 variations::AssociateVariationParams("ClientSidePreviews", "Enabled", params); |
| 155 base::FieldTrialList::CreateFieldTrial("ClientSidePreviews", "Enabled"); |
| 156 |
| 157 // The blacklist is not created yet. |
| 158 EXPECT_FALSE(io_data()->ShouldAllowPreview(*request, PreviewsType::OFFLINE)); |
| 159 histogram_tester.ExpectUniqueSample( |
| 160 "Previews.EligibilityReason.Offline", |
| 161 static_cast<int>(PreviewsEligibilityReason::BLACKLIST_UNAVAILABLE), 1); |
| 162 |
| 163 set_ui_service(base::WrapUnique( |
| 164 new PreviewsUIService(io_data(), loop_.task_runner(), |
| 165 base::MakeUnique<TestPreviewsOptOutStore>()))); |
| 166 |
| 167 base::RunLoop().RunUntilIdle(); |
| 168 |
| 169 // Return one of the failing statuses from the blacklist; cause the blacklist |
| 170 // to not be loaded by clearing the blacklist. |
| 171 base::Time now = base::Time::Now(); |
| 172 io_data()->ClearBlackList(now, now); |
| 173 |
| 174 EXPECT_FALSE(io_data()->ShouldAllowPreview(*request, PreviewsType::OFFLINE)); |
| 175 histogram_tester.ExpectBucketCount( |
| 176 "Previews.EligibilityReason.Offline", |
| 177 static_cast<int>(PreviewsEligibilityReason::BLACKLIST_DATA_NOT_LOADED), |
| 178 1); |
| 179 |
| 180 // Reload the blacklist. The blacklist should allow all navigations now. |
| 181 base::RunLoop().RunUntilIdle(); |
| 182 |
| 183 // NQE should be null on the context. |
| 184 EXPECT_FALSE(io_data()->ShouldAllowPreview(*request, PreviewsType::OFFLINE)); |
| 185 histogram_tester.ExpectBucketCount( |
| 186 "Previews.EligibilityReason.Offline", |
| 187 static_cast<int>(PreviewsEligibilityReason::NETWORK_QUALITY_UNAVAILABLE), |
| 188 1); |
| 189 |
| 190 net::TestNetworkQualityEstimator network_quality_estimator(params); |
| 191 context.set_network_quality_estimator(&network_quality_estimator); |
| 192 |
| 193 // Set NQE type to unknown. |
| 194 network_quality_estimator.set_effective_connection_type( |
| 195 net::EFFECTIVE_CONNECTION_TYPE_UNKNOWN); |
| 196 |
| 197 EXPECT_FALSE(io_data()->ShouldAllowPreview(*request, PreviewsType::OFFLINE)); |
| 198 histogram_tester.ExpectBucketCount( |
| 199 "Previews.EligibilityReason.Offline", |
| 200 static_cast<int>(PreviewsEligibilityReason::NETWORK_QUALITY_UNAVAILABLE), |
| 201 2); |
| 202 |
| 203 // Set NQE type to fast enough. |
| 204 network_quality_estimator.set_effective_connection_type( |
| 205 net::EFFECTIVE_CONNECTION_TYPE_2G); |
| 206 EXPECT_FALSE(io_data()->ShouldAllowPreview(*request, PreviewsType::OFFLINE)); |
| 207 histogram_tester.ExpectBucketCount( |
| 208 "Previews.EligibilityReason.Offline", |
| 209 static_cast<int>(PreviewsEligibilityReason::NETWORK_NOT_SLOW), 1); |
| 210 |
| 211 // Set NQE type to slow. |
| 212 network_quality_estimator.set_effective_connection_type( |
| 213 net::EFFECTIVE_CONNECTION_TYPE_SLOW_2G); |
| 214 |
| 215 EXPECT_TRUE(io_data()->ShouldAllowPreview(*request, PreviewsType::OFFLINE)); |
| 216 histogram_tester.ExpectBucketCount( |
| 217 "Previews.EligibilityReason.Offline", |
| 218 static_cast<int>(PreviewsEligibilityReason::ALLOWED), 1); |
| 219 |
| 220 histogram_tester.ExpectTotalCount("Previews.EligibilityReason.Offline", 6); |
| 221 |
| 222 variations::testing::ClearAllVariationParams(); |
| 223 } |
| 224 |
82 } // namespace previews | 225 } // namespace previews |
OLD | NEW |