| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/network_time/network_time_tracker.h" | 5 #include "components/network_time/network_time_tracker.h" |
| 6 | 6 |
| 7 #include <map> | 7 #include <map> |
| 8 #include <memory> | 8 #include <memory> |
| 9 #include <string> | 9 #include <string> |
| 10 #include <utility> | 10 #include <utility> |
| 11 | 11 |
| 12 #include "base/compiler_specific.h" | 12 #include "base/compiler_specific.h" |
| 13 #include "base/feature_list.h" | 13 #include "base/feature_list.h" |
| 14 #include "base/metrics/field_trial.h" | 14 #include "base/metrics/field_trial.h" |
| 15 #include "base/strings/string_number_conversions.h" | 15 #include "base/strings/string_number_conversions.h" |
| 16 #include "base/strings/stringprintf.h" | 16 #include "base/strings/stringprintf.h" |
| 17 #include "base/test/histogram_tester.h" | 17 #include "base/test/histogram_tester.h" |
| 18 #include "base/test/mock_entropy_provider.h" | 18 #include "base/test/mock_entropy_provider.h" |
| 19 #include "base/test/scoped_feature_list.h" |
| 19 #include "base/test/simple_test_clock.h" | 20 #include "base/test/simple_test_clock.h" |
| 20 #include "base/test/simple_test_tick_clock.h" | 21 #include "base/test/simple_test_tick_clock.h" |
| 21 #include "components/client_update_protocol/ecdsa.h" | 22 #include "components/client_update_protocol/ecdsa.h" |
| 22 #include "components/network_time/network_time_pref_names.h" | 23 #include "components/network_time/network_time_pref_names.h" |
| 23 #include "components/prefs/testing_pref_service.h" | 24 #include "components/prefs/testing_pref_service.h" |
| 24 #include "components/variations/variations_associated_data.h" | 25 #include "components/variations/variations_associated_data.h" |
| 25 #include "net/http/http_response_headers.h" | 26 #include "net/http/http_response_headers.h" |
| 26 #include "net/test/embedded_test_server/embedded_test_server.h" | 27 #include "net/test/embedded_test_server/embedded_test_server.h" |
| 27 #include "net/test/embedded_test_server/http_response.h" | 28 #include "net/test/embedded_test_server/http_response.h" |
| 28 #include "net/url_request/url_fetcher.h" | 29 #include "net/url_request/url_fetcher.h" |
| (...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 174 | 175 |
| 175 protected: | 176 protected: |
| 176 void SetNetworkQueriesWithVariationsService(bool enable, | 177 void SetNetworkQueriesWithVariationsService(bool enable, |
| 177 float query_probability) { | 178 float query_probability) { |
| 178 const std::string kTrialName = "Trial"; | 179 const std::string kTrialName = "Trial"; |
| 179 const std::string kGroupName = "group"; | 180 const std::string kGroupName = "group"; |
| 180 const base::Feature kFeature{"NetworkTimeServiceQuerying", | 181 const base::Feature kFeature{"NetworkTimeServiceQuerying", |
| 181 base::FEATURE_DISABLED_BY_DEFAULT}; | 182 base::FEATURE_DISABLED_BY_DEFAULT}; |
| 182 | 183 |
| 183 // Clear all the things. | 184 // Clear all the things. |
| 184 base::FeatureList::ClearInstanceForTesting(); | |
| 185 variations::testing::ClearAllVariationParams(); | 185 variations::testing::ClearAllVariationParams(); |
| 186 | 186 |
| 187 std::map<std::string, std::string> params; | 187 std::map<std::string, std::string> params; |
| 188 params["RandomQueryProbability"] = base::DoubleToString(query_probability); | 188 params["RandomQueryProbability"] = base::DoubleToString(query_probability); |
| 189 params["CheckTimeIntervalSeconds"] = base::Int64ToString(360); | 189 params["CheckTimeIntervalSeconds"] = base::Int64ToString(360); |
| 190 | 190 |
| 191 // There are 3 things here: a FieldTrial, a FieldTrialList, and a | 191 // There are 3 things here: a FieldTrial, a FieldTrialList, and a |
| 192 // FeatureList. Don't get confused! The FieldTrial is reference-counted, | 192 // FeatureList. Don't get confused! The FieldTrial is reference-counted, |
| 193 // and a reference is held by the FieldTrialList. The FieldTrialList and | 193 // and a reference is held by the FieldTrialList. The FieldTrialList and |
| 194 // FeatureList are both singletons. The authorized way to reset the former | 194 // FeatureList are both singletons. The authorized way to reset the former |
| 195 // for testing is to destruct it (above). The latter, by contrast, is reset | 195 // for testing is to destruct it (above). The latter, by contrast, should |
| 196 // with |ClearInstanceForTesting|, above. If this comment was useful to you | 196 // should already start in a clean state and can be manipulated via the |
| 197 // ScopedFeatureList helper class. If this comment was useful to you |
| 197 // please send me a postcard. | 198 // please send me a postcard. |
| 198 | 199 |
| 199 field_trial_list_.reset(); // Averts a CHECK fail in constructor below. | 200 field_trial_list_.reset(); // Averts a CHECK fail in constructor below. |
| 200 field_trial_list_.reset( | 201 field_trial_list_.reset( |
| 201 new base::FieldTrialList(new base::MockEntropyProvider())); | 202 new base::FieldTrialList(new base::MockEntropyProvider())); |
| 202 // refcounted, and reference held by field_trial_list_. | 203 // refcounted, and reference held by field_trial_list_. |
| 203 base::FieldTrial* trial = base::FieldTrialList::FactoryGetFieldTrial( | 204 base::FieldTrial* trial = base::FieldTrialList::FactoryGetFieldTrial( |
| 204 kTrialName, 100, kGroupName, 1971, 1, 1, | 205 kTrialName, 100, kGroupName, 1971, 1, 1, |
| 205 base::FieldTrial::SESSION_RANDOMIZED, | 206 base::FieldTrial::SESSION_RANDOMIZED, |
| 206 nullptr /* default_group_number */); | 207 nullptr /* default_group_number */); |
| 207 ASSERT_TRUE( | 208 ASSERT_TRUE( |
| 208 variations::AssociateVariationParams(kTrialName, kGroupName, params)); | 209 variations::AssociateVariationParams(kTrialName, kGroupName, params)); |
| 209 | 210 |
| 210 std::unique_ptr<base::FeatureList> feature_list(new base::FeatureList); | 211 std::unique_ptr<base::FeatureList> feature_list(new base::FeatureList); |
| 211 feature_list->RegisterFieldTrialOverride( | 212 feature_list->RegisterFieldTrialOverride( |
| 212 kFeature.name, enable ? base::FeatureList::OVERRIDE_ENABLE_FEATURE | 213 kFeature.name, enable ? base::FeatureList::OVERRIDE_ENABLE_FEATURE |
| 213 : base::FeatureList::OVERRIDE_DISABLE_FEATURE, | 214 : base::FeatureList::OVERRIDE_DISABLE_FEATURE, |
| 214 trial); | 215 trial); |
| 215 base::FeatureList::SetInstance(std::move(feature_list)); | 216 scoped_feature_list_.reset(new base::test::ScopedFeatureList); |
| 217 scoped_feature_list_->InitWithFeatureList(std::move(feature_list)); |
| 216 } | 218 } |
| 217 | 219 |
| 218 base::Thread io_thread_; | 220 base::Thread io_thread_; |
| 219 base::MessageLoop message_loop_; | 221 base::MessageLoop message_loop_; |
| 220 base::TimeDelta resolution_; | 222 base::TimeDelta resolution_; |
| 221 base::TimeDelta latency_; | 223 base::TimeDelta latency_; |
| 222 base::TimeDelta adjustment_; | 224 base::TimeDelta adjustment_; |
| 223 base::SimpleTestClock* clock_; | 225 base::SimpleTestClock* clock_; |
| 224 base::SimpleTestTickClock* tick_clock_; | 226 base::SimpleTestTickClock* tick_clock_; |
| 225 TestingPrefServiceSimple pref_service_; | 227 TestingPrefServiceSimple pref_service_; |
| 226 std::unique_ptr<base::FieldTrialList> field_trial_list_; | 228 std::unique_ptr<base::FieldTrialList> field_trial_list_; |
| 227 std::unique_ptr<NetworkTimeTracker> tracker_; | 229 std::unique_ptr<NetworkTimeTracker> tracker_; |
| 228 std::unique_ptr<net::EmbeddedTestServer> test_server_; | 230 std::unique_ptr<net::EmbeddedTestServer> test_server_; |
| 231 std::unique_ptr<base::test::ScopedFeatureList> scoped_feature_list_; |
| 229 }; | 232 }; |
| 230 | 233 |
| 231 TEST_F(NetworkTimeTrackerTest, Uninitialized) { | 234 TEST_F(NetworkTimeTrackerTest, Uninitialized) { |
| 232 base::Time network_time; | 235 base::Time network_time; |
| 233 base::TimeDelta uncertainty; | 236 base::TimeDelta uncertainty; |
| 234 EXPECT_FALSE(tracker_->GetNetworkTime(&network_time, &uncertainty)); | 237 EXPECT_FALSE(tracker_->GetNetworkTime(&network_time, &uncertainty)); |
| 235 } | 238 } |
| 236 | 239 |
| 237 TEST_F(NetworkTimeTrackerTest, LongPostingDelay) { | 240 TEST_F(NetworkTimeTrackerTest, LongPostingDelay) { |
| 238 // The request arrives at the server, which records the time. Advance the | 241 // The request arrives at the server, which records the time. Advance the |
| (...skipping 413 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 652 tracker_->WaitForFetchForTesting(123123123); | 655 tracker_->WaitForFetchForTesting(123123123); |
| 653 EXPECT_TRUE(tracker_->GetNetworkTime(&out_network_time, nullptr)); | 656 EXPECT_TRUE(tracker_->GetNetworkTime(&out_network_time, nullptr)); |
| 654 | 657 |
| 655 histograms.ExpectTotalCount(kFetchAttemptHistogram, 2); | 658 histograms.ExpectTotalCount(kFetchAttemptHistogram, 2); |
| 656 histograms.ExpectTotalCount(kFetchFailedHistogram, 1); | 659 histograms.ExpectTotalCount(kFetchFailedHistogram, 1); |
| 657 histograms.ExpectTotalCount(kFetchValidHistogram, 1); | 660 histograms.ExpectTotalCount(kFetchValidHistogram, 1); |
| 658 histograms.ExpectBucketCount(kFetchValidHistogram, true, 1); | 661 histograms.ExpectBucketCount(kFetchValidHistogram, true, 1); |
| 659 } | 662 } |
| 660 | 663 |
| 661 } // namespace network_time | 664 } // namespace network_time |
| OLD | NEW |