Chromium Code Reviews| 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 <map> |
| 8 #include <memory> | 8 #include <memory> |
| 9 #include <string> | 9 #include <string> |
| 10 | 10 |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 30 #include "net/nqe/network_quality_estimator_test_util.h" | 30 #include "net/nqe/network_quality_estimator_test_util.h" |
| 31 #include "net/url_request/url_request.h" | 31 #include "net/url_request/url_request.h" |
| 32 #include "net/url_request/url_request_test_util.h" | 32 #include "net/url_request/url_request_test_util.h" |
| 33 #include "testing/gtest/include/gtest/gtest.h" | 33 #include "testing/gtest/include/gtest/gtest.h" |
| 34 #include "url/gurl.h" | 34 #include "url/gurl.h" |
| 35 | 35 |
| 36 namespace previews { | 36 namespace previews { |
| 37 | 37 |
| 38 namespace { | 38 namespace { |
| 39 | 39 |
| 40 bool CheckOfflineFieldTrial(PreviewsType type) { | |
| 41 return previews::params::IsOfflinePreviewsEnabled(); | |
| 42 } | |
| 43 | |
| 40 class TestPreviewsIOData : public PreviewsIOData { | 44 class TestPreviewsIOData : public PreviewsIOData { |
| 41 public: | 45 public: |
| 42 TestPreviewsIOData( | 46 TestPreviewsIOData( |
| 43 const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner, | 47 const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner, |
| 44 const scoped_refptr<base::SingleThreadTaskRunner>& ui_task_runner) | 48 const scoped_refptr<base::SingleThreadTaskRunner>& ui_task_runner) |
| 45 : PreviewsIOData(io_task_runner, ui_task_runner), initialized_(false) {} | 49 : PreviewsIOData(io_task_runner, ui_task_runner), initialized_(false) {} |
| 46 ~TestPreviewsIOData() override {} | 50 ~TestPreviewsIOData() override {} |
| 47 | 51 |
| 48 // Whether Initialize was called. | 52 // Whether Initialize was called. |
| 49 bool initialized() { return initialized_; } | 53 bool initialized() { return initialized_; } |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 118 std::unique_ptr<TestPreviewsIOData> io_data_; | 122 std::unique_ptr<TestPreviewsIOData> io_data_; |
| 119 std::unique_ptr<PreviewsUIService> ui_service_; | 123 std::unique_ptr<PreviewsUIService> ui_service_; |
| 120 }; | 124 }; |
| 121 | 125 |
| 122 } // namespace | 126 } // namespace |
| 123 | 127 |
| 124 TEST_F(PreviewsIODataTest, TestInitialization) { | 128 TEST_F(PreviewsIODataTest, TestInitialization) { |
| 125 set_io_data(base::MakeUnique<TestPreviewsIOData>(loop_.task_runner(), | 129 set_io_data(base::MakeUnique<TestPreviewsIOData>(loop_.task_runner(), |
| 126 loop_.task_runner())); | 130 loop_.task_runner())); |
| 127 set_ui_service(base::MakeUnique<PreviewsUIService>( | 131 set_ui_service(base::MakeUnique<PreviewsUIService>( |
| 128 io_data(), loop_.task_runner(), nullptr)); | 132 io_data(), loop_.task_runner(), nullptr, PreviewsIsEnabledCallback())); |
| 129 base::RunLoop().RunUntilIdle(); | 133 base::RunLoop().RunUntilIdle(); |
| 130 // After the outstanding posted tasks have run, |io_data_| should be fully | 134 // After the outstanding posted tasks have run, |io_data_| should be fully |
| 131 // initialized. | 135 // initialized. |
| 132 EXPECT_TRUE(io_data()->initialized()); | 136 EXPECT_TRUE(io_data()->initialized()); |
| 133 } | 137 } |
| 134 | 138 |
| 135 TEST_F(PreviewsIODataTest, TestShouldAllowPreview) { | 139 TEST_F(PreviewsIODataTest, TestShouldAllowPreview) { |
| 136 // Test most reasons to disallow the blacklist. | 140 // Test most reasons to disallow the blacklist. |
| 137 // Excluded values are USER_RECENTLY_OPTED_OUT, USER_BLACKLISTED, | 141 // Excluded values are USER_RECENTLY_OPTED_OUT, USER_BLACKLISTED, |
| 138 // HOST_BLACKLISTED. These are internal to the blacklist. | 142 // HOST_BLACKLISTED. These are internal to the blacklist. |
| 139 net::TestURLRequestContext context; | 143 net::TestURLRequestContext context; |
| 140 GURL test_host("http://www.test_host.com"); | 144 GURL test_host("http://www.test_host.com"); |
| 141 std::unique_ptr<net::URLRequest> request = | 145 std::unique_ptr<net::URLRequest> request = |
| 142 context.CreateRequest(test_host, net::DEFAULT_PRIORITY, nullptr); | 146 context.CreateRequest(test_host, net::DEFAULT_PRIORITY, nullptr); |
| 143 set_io_data(base::MakeUnique<TestPreviewsIOData>(loop_.task_runner(), | 147 set_io_data(base::MakeUnique<TestPreviewsIOData>(loop_.task_runner(), |
| 144 loop_.task_runner())); | 148 loop_.task_runner())); |
| 145 base::HistogramTester histogram_tester; | 149 base::HistogramTester histogram_tester; |
| 146 | 150 |
| 147 // If not in the field trial, don't log anything, and return false. | |
| 148 EXPECT_FALSE(io_data()->ShouldAllowPreview(*request, PreviewsType::OFFLINE)); | |
| 149 histogram_tester.ExpectTotalCount("Previews.EligibilityReason.Offline", 0); | |
| 150 | |
| 151 // Enable Offline previews field trial. | |
| 152 base::FieldTrialList field_trial_list(nullptr); | |
| 153 std::map<std::string, std::string> params; | |
| 154 params["show_offline_pages"] = "true"; | |
| 155 variations::AssociateVariationParams("ClientSidePreviews", "Enabled", params); | |
| 156 base::FieldTrialList::CreateFieldTrial("ClientSidePreviews", "Enabled"); | |
| 157 | |
| 158 // The blacklist is not created yet. | 151 // The blacklist is not created yet. |
| 159 EXPECT_FALSE(io_data()->ShouldAllowPreview(*request, PreviewsType::OFFLINE)); | 152 EXPECT_FALSE(io_data()->ShouldAllowPreview(*request, PreviewsType::OFFLINE)); |
| 160 histogram_tester.ExpectUniqueSample( | 153 histogram_tester.ExpectUniqueSample( |
| 161 "Previews.EligibilityReason.Offline", | 154 "Previews.EligibilityReason.Offline", |
| 162 static_cast<int>(PreviewsEligibilityReason::BLACKLIST_UNAVAILABLE), 1); | 155 static_cast<int>(PreviewsEligibilityReason::BLACKLIST_UNAVAILABLE), 1); |
| 163 | 156 |
| 164 set_ui_service(base::WrapUnique( | 157 set_ui_service(base::MakeUnique<PreviewsUIService>( |
| 165 new PreviewsUIService(io_data(), loop_.task_runner(), | 158 io_data(), loop_.task_runner(), |
| 166 base::MakeUnique<TestPreviewsOptOutStore>()))); | 159 base::MakeUnique<TestPreviewsOptOutStore>(), |
| 160 base::Bind(&CheckOfflineFieldTrial))); | |
|
tbansal1
2017/03/10 23:06:23
why can we not pass previews::params::IsOfflinePre
RyanSturm
2017/03/13 21:12:47
Type mismatch (takes a PreviewsType parameter)
| |
| 161 | |
| 162 // The blacklist is not created yet. | |
| 163 EXPECT_FALSE(io_data()->ShouldAllowPreview(*request, PreviewsType::OFFLINE)); | |
| 164 histogram_tester.ExpectBucketCount( | |
| 165 "Previews.EligibilityReason.Offline", | |
| 166 static_cast<int>(PreviewsEligibilityReason::BLACKLIST_UNAVAILABLE), 2); | |
| 167 | 167 |
| 168 base::RunLoop().RunUntilIdle(); | 168 base::RunLoop().RunUntilIdle(); |
| 169 | 169 |
| 170 histogram_tester.ExpectTotalCount("Previews.EligibilityReason.Offline", 2); | |
| 171 // If not in the field trial, don't log anything, and return false. | |
| 172 EXPECT_FALSE(io_data()->ShouldAllowPreview(*request, PreviewsType::OFFLINE)); | |
| 173 histogram_tester.ExpectTotalCount("Previews.EligibilityReason.Offline", 2); | |
| 174 | |
| 175 // Enable Offline previews field trial. | |
| 176 base::FieldTrialList field_trial_list(nullptr); | |
| 177 std::map<std::string, std::string> params; | |
| 178 params["show_offline_pages"] = "true"; | |
| 179 | |
| 180 variations::AssociateVariationParams("ClientSidePreviews", "Enabled", params); | |
| 181 base::FieldTrialList::CreateFieldTrial("ClientSidePreviews", "Enabled"); | |
| 182 | |
| 170 // Return one of the failing statuses from the blacklist; cause the blacklist | 183 // Return one of the failing statuses from the blacklist; cause the blacklist |
| 171 // to not be loaded by clearing the blacklist. | 184 // to not be loaded by clearing the blacklist. |
| 172 base::Time now = base::Time::Now(); | 185 base::Time now = base::Time::Now(); |
| 173 io_data()->ClearBlackList(now, now); | 186 io_data()->ClearBlackList(now, now); |
| 174 | 187 |
| 175 EXPECT_FALSE(io_data()->ShouldAllowPreview(*request, PreviewsType::OFFLINE)); | 188 EXPECT_FALSE(io_data()->ShouldAllowPreview(*request, PreviewsType::OFFLINE)); |
| 176 histogram_tester.ExpectBucketCount( | 189 histogram_tester.ExpectBucketCount( |
| 177 "Previews.EligibilityReason.Offline", | 190 "Previews.EligibilityReason.Offline", |
| 178 static_cast<int>(PreviewsEligibilityReason::BLACKLIST_DATA_NOT_LOADED), | 191 static_cast<int>(PreviewsEligibilityReason::BLACKLIST_DATA_NOT_LOADED), |
| 179 1); | 192 1); |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 220 static_cast<int>( | 233 static_cast<int>( |
| 221 PreviewsEligibilityReason::RELOAD_DISALLOWED_FOR_OFFLINE), | 234 PreviewsEligibilityReason::RELOAD_DISALLOWED_FOR_OFFLINE), |
| 222 1); | 235 1); |
| 223 | 236 |
| 224 request->SetLoadFlags(0); | 237 request->SetLoadFlags(0); |
| 225 EXPECT_TRUE(io_data()->ShouldAllowPreview(*request, PreviewsType::OFFLINE)); | 238 EXPECT_TRUE(io_data()->ShouldAllowPreview(*request, PreviewsType::OFFLINE)); |
| 226 histogram_tester.ExpectBucketCount( | 239 histogram_tester.ExpectBucketCount( |
| 227 "Previews.EligibilityReason.Offline", | 240 "Previews.EligibilityReason.Offline", |
| 228 static_cast<int>(PreviewsEligibilityReason::ALLOWED), 1); | 241 static_cast<int>(PreviewsEligibilityReason::ALLOWED), 1); |
| 229 | 242 |
| 230 histogram_tester.ExpectTotalCount("Previews.EligibilityReason.Offline", 7); | 243 histogram_tester.ExpectTotalCount("Previews.EligibilityReason.Offline", 8); |
| 231 | 244 |
| 232 variations::testing::ClearAllVariationParams(); | 245 variations::testing::ClearAllVariationParams(); |
| 233 } | 246 } |
| 234 | 247 |
| 235 } // namespace previews | 248 } // namespace previews |
| OLD | NEW |