| 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> | |
| 8 #include <memory> | 7 #include <memory> |
| 9 #include <string> | |
| 10 | 8 |
| 11 #include "base/bind.h" | |
| 12 #include "base/bind_helpers.h" | |
| 13 #include "base/callback.h" | |
| 14 #include "base/memory/ptr_util.h" | 9 #include "base/memory/ptr_util.h" |
| 15 #include "base/memory/ref_counted.h" | 10 #include "base/memory/ref_counted.h" |
| 16 #include "base/message_loop/message_loop.h" | 11 #include "base/message_loop/message_loop.h" |
| 17 #include "base/metrics/field_trial.h" | |
| 18 #include "base/run_loop.h" | 12 #include "base/run_loop.h" |
| 19 #include "base/single_thread_task_runner.h" | 13 #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" | |
| 26 #include "components/previews/core/previews_ui_service.h" | 14 #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" | |
| 32 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
| 33 #include "url/gurl.h" | |
| 34 | 16 |
| 35 namespace previews { | 17 namespace previews { |
| 36 | 18 |
| 37 namespace { | 19 namespace { |
| 38 | 20 |
| 39 class TestPreviewsIOData : public PreviewsIOData { | 21 class TestPreviewsIOData : public PreviewsIOData { |
| 40 public: | 22 public: |
| 41 TestPreviewsIOData( | 23 TestPreviewsIOData( |
| 42 const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner, | 24 const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner, |
| 43 const scoped_refptr<base::SingleThreadTaskRunner>& ui_task_runner) | 25 const scoped_refptr<base::SingleThreadTaskRunner>& ui_task_runner) |
| 44 : PreviewsIOData(io_task_runner, ui_task_runner), initialized_(false) {} | 26 : PreviewsIOData(io_task_runner, ui_task_runner), initialized_(false) {} |
| 45 ~TestPreviewsIOData() override {} | 27 ~TestPreviewsIOData() override {} |
| 46 | 28 |
| 47 // Whether Initialize was called. | 29 // Whether Initialize was called. |
| 48 bool initialized() { return initialized_; } | 30 bool initialized() { return initialized_; } |
| 49 | 31 |
| 50 private: | 32 private: |
| 51 // Set |initialized_| to true and use base class functionality. | 33 // Set |initialized_| to true and use base class functionality. |
| 52 void InitializeOnIOThread( | 34 void InitializeOnIOThread( |
| 53 std::unique_ptr<PreviewsOptOutStore> previews_opt_out_store) override { | 35 std::unique_ptr<PreviewsOptOutStore> previews_opt_out_store) override { |
| 54 initialized_ = true; | 36 initialized_ = true; |
| 55 PreviewsIOData::InitializeOnIOThread(std::move(previews_opt_out_store)); | 37 PreviewsIOData::InitializeOnIOThread(std::move(previews_opt_out_store)); |
| 56 } | 38 } |
| 57 | 39 |
| 58 // Whether Initialize was called. | 40 // Whether Initialize was called. |
| 59 bool initialized_; | 41 bool initialized_; |
| 60 }; | 42 }; |
| 61 | 43 |
| 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 | |
| 96 class PreviewsIODataTest : public testing::Test { | 44 class PreviewsIODataTest : public testing::Test { |
| 97 public: | 45 public: |
| 98 PreviewsIODataTest() {} | 46 PreviewsIODataTest() {} |
| 99 | 47 |
| 100 ~PreviewsIODataTest() override {} | 48 ~PreviewsIODataTest() override {} |
| 101 | 49 |
| 102 void set_io_data(std::unique_ptr<TestPreviewsIOData> io_data) { | 50 void set_io_data(std::unique_ptr<TestPreviewsIOData> io_data) { |
| 103 io_data_ = std::move(io_data); | 51 io_data_ = std::move(io_data); |
| 104 } | 52 } |
| 105 | 53 |
| 106 TestPreviewsIOData* io_data() { return io_data_.get(); } | 54 TestPreviewsIOData* io_data() { return io_data_.get(); } |
| 107 | 55 |
| 108 void set_ui_service(std::unique_ptr<PreviewsUIService> ui_service) { | 56 void set_ui_service(std::unique_ptr<PreviewsUIService> ui_service) { |
| 109 ui_service_ = std::move(ui_service); | 57 ui_service_ = std::move(ui_service); |
| 110 } | 58 } |
| 111 | 59 |
| 112 protected: | 60 protected: |
| 113 // Run this test on a single thread. | 61 // Run this test on a single thread. |
| 114 base::MessageLoopForIO loop_; | 62 base::MessageLoopForIO loop_; |
| 115 | 63 |
| 116 private: | 64 private: |
| 117 std::unique_ptr<TestPreviewsIOData> io_data_; | 65 std::unique_ptr<TestPreviewsIOData> io_data_; |
| 118 std::unique_ptr<PreviewsUIService> ui_service_; | 66 std::unique_ptr<PreviewsUIService> ui_service_; |
| 119 }; | 67 }; |
| 120 | 68 |
| 121 } // namespace | 69 } // namespace |
| 122 | 70 |
| 123 TEST_F(PreviewsIODataTest, TestInitialization) { | 71 TEST_F(PreviewsIODataTest, TestInitialization) { |
| 124 set_io_data(base::MakeUnique<TestPreviewsIOData>(loop_.task_runner(), | 72 set_io_data(base::WrapUnique( |
| 125 loop_.task_runner())); | 73 new TestPreviewsIOData(loop_.task_runner(), loop_.task_runner()))); |
| 126 set_ui_service(base::MakeUnique<PreviewsUIService>( | 74 set_ui_service(base::WrapUnique( |
| 127 io_data(), loop_.task_runner(), nullptr)); | 75 new PreviewsUIService(io_data(), loop_.task_runner(), nullptr))); |
| 128 base::RunLoop().RunUntilIdle(); | 76 base::RunLoop().RunUntilIdle(); |
| 129 // After the outstanding posted tasks have run, |io_data_| should be fully | 77 // After the outstanding posted tasks have run, |io_data_| should be fully |
| 130 // initialized. | 78 // initialized. |
| 131 EXPECT_TRUE(io_data()->initialized()); | 79 EXPECT_TRUE(io_data()->initialized()); |
| 132 } | 80 } |
| 133 | 81 |
| 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. | |
| 171 io_data()->ClearBlackList(base::Time::Now(), base::Time::Now()); | |
| 172 | |
| 173 EXPECT_FALSE(io_data()->ShouldAllowPreview(*request, PreviewsType::OFFLINE)); | |
| 174 histogram_tester.ExpectBucketCount( | |
| 175 "Previews.EligibilityReason.Offline", | |
| 176 static_cast<int>(PreviewsEligibilityReason::BLACKLIST_DATA_NOT_LOADED), | |
| 177 1); | |
| 178 | |
| 179 // Reload the blacklist. The blacklist should allow all navigations now. | |
| 180 base::RunLoop().RunUntilIdle(); | |
| 181 | |
| 182 // NQE should be null on the context. | |
| 183 EXPECT_FALSE(io_data()->ShouldAllowPreview(*request, PreviewsType::OFFLINE)); | |
| 184 histogram_tester.ExpectBucketCount( | |
| 185 "Previews.EligibilityReason.Offline", | |
| 186 static_cast<int>(PreviewsEligibilityReason::NETWORK_QUALITY_UNAVAILABLE), | |
| 187 1); | |
| 188 | |
| 189 net::TestNetworkQualityEstimator network_quality_estimator(params); | |
| 190 context.set_network_quality_estimator(&network_quality_estimator); | |
| 191 | |
| 192 // Set NQE type to unknown. | |
| 193 network_quality_estimator.set_effective_connection_type( | |
| 194 net::EFFECTIVE_CONNECTION_TYPE_UNKNOWN); | |
| 195 | |
| 196 EXPECT_FALSE(io_data()->ShouldAllowPreview(*request, PreviewsType::OFFLINE)); | |
| 197 histogram_tester.ExpectBucketCount( | |
| 198 "Previews.EligibilityReason.Offline", | |
| 199 static_cast<int>(PreviewsEligibilityReason::NETWORK_QUALITY_UNAVAILABLE), | |
| 200 2); | |
| 201 | |
| 202 // Set NQE type to fast enough. | |
| 203 network_quality_estimator.set_effective_connection_type( | |
| 204 net::EFFECTIVE_CONNECTION_TYPE_2G); | |
| 205 EXPECT_FALSE(io_data()->ShouldAllowPreview(*request, PreviewsType::OFFLINE)); | |
| 206 histogram_tester.ExpectBucketCount( | |
| 207 "Previews.EligibilityReason.Offline", | |
| 208 static_cast<int>(PreviewsEligibilityReason::NETWORK_NOT_SLOW), 1); | |
| 209 | |
| 210 // Set NQE type to slow. | |
| 211 network_quality_estimator.set_effective_connection_type( | |
| 212 net::EFFECTIVE_CONNECTION_TYPE_SLOW_2G); | |
| 213 | |
| 214 EXPECT_TRUE(io_data()->ShouldAllowPreview(*request, PreviewsType::OFFLINE)); | |
| 215 histogram_tester.ExpectBucketCount( | |
| 216 "Previews.EligibilityReason.Offline", | |
| 217 static_cast<int>(PreviewsEligibilityReason::ALLOWED), 1); | |
| 218 | |
| 219 histogram_tester.ExpectTotalCount("Previews.EligibilityReason.Offline", 6); | |
| 220 | |
| 221 variations::testing::ClearAllVariationParams(); | |
| 222 } | |
| 223 | |
| 224 } // namespace previews | 82 } // namespace previews |
| OLD | NEW |