Chromium Code Reviews| 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 "components/ukm/ukm_service.h" | 5 #include "components/ukm/ukm_service.h" |
| 6 | 6 |
| 7 #include <map> | |
| 7 #include <string> | 8 #include <string> |
| 8 #include <utility> | 9 #include <utility> |
| 9 | 10 |
| 10 #include "base/hash.h" | 11 #include "base/hash.h" |
| 11 #include "base/metrics/metrics_hashes.h" | 12 #include "base/metrics/metrics_hashes.h" |
| 13 #include "base/test/scoped_feature_list.h" | |
| 12 #include "base/test/test_simple_task_runner.h" | 14 #include "base/test/test_simple_task_runner.h" |
| 13 #include "base/threading/thread_task_runner_handle.h" | 15 #include "base/threading/thread_task_runner_handle.h" |
| 14 #include "components/metrics/proto/ukm/report.pb.h" | 16 #include "components/metrics/proto/ukm/report.pb.h" |
| 15 #include "components/metrics/proto/ukm/source.pb.h" | 17 #include "components/metrics/proto/ukm/source.pb.h" |
| 16 #include "components/metrics/test_metrics_provider.h" | 18 #include "components/metrics/test_metrics_provider.h" |
| 17 #include "components/metrics/test_metrics_service_client.h" | 19 #include "components/metrics/test_metrics_service_client.h" |
| 18 #include "components/prefs/testing_pref_service.h" | 20 #include "components/prefs/testing_pref_service.h" |
| 19 #include "components/ukm/persisted_logs_metrics_impl.h" | 21 #include "components/ukm/persisted_logs_metrics_impl.h" |
| 20 #include "components/ukm/ukm_entry_builder.h" | 22 #include "components/ukm/ukm_entry_builder.h" |
| 21 #include "components/ukm/ukm_pref_names.h" | 23 #include "components/ukm/ukm_pref_names.h" |
| 22 #include "components/ukm/ukm_source.h" | 24 #include "components/ukm/ukm_source.h" |
| 25 #include "components/variations/variations_associated_data.h" | |
| 23 #include "testing/gtest/include/gtest/gtest.h" | 26 #include "testing/gtest/include/gtest/gtest.h" |
| 24 #include "third_party/zlib/google/compression_utils.h" | 27 #include "third_party/zlib/google/compression_utils.h" |
| 25 | 28 |
| 26 namespace ukm { | 29 namespace ukm { |
| 27 | 30 |
| 28 namespace { | 31 namespace { |
| 29 | 32 |
| 33 class ScopedUkmFeatureParams { | |
|
Bryan McQuade
2017/03/02 18:21:08
just to give credit, this class is based on a simi
rkaplow
2017/03/02 18:46:54
This isn't really UKM specific either. If this has
Bryan McQuade
2017/03/02 22:18:32
Sure, I added a TODO for you.
| |
| 34 public: | |
| 35 explicit ScopedUkmFeatureParams( | |
| 36 const std::map<std::string, std::string>& variation_params) { | |
| 37 static const char kTestFieldTrialName[] = "TestTrial"; | |
| 38 static const char kTestExperimentGroupName[] = "TestGroup"; | |
| 39 | |
| 40 variations::testing::ClearAllVariationParams(); | |
| 41 | |
| 42 EXPECT_TRUE(variations::AssociateVariationParams( | |
| 43 kTestFieldTrialName, kTestExperimentGroupName, variation_params)); | |
| 44 | |
| 45 base::FieldTrial* field_trial = base::FieldTrialList::CreateFieldTrial( | |
| 46 kTestFieldTrialName, kTestExperimentGroupName); | |
| 47 | |
| 48 std::unique_ptr<base::FeatureList> feature_list(new base::FeatureList); | |
| 49 feature_list->RegisterFieldTrialOverride( | |
| 50 kUkmFeature.name, base::FeatureList::OVERRIDE_ENABLE_FEATURE, | |
| 51 field_trial); | |
| 52 | |
| 53 // Since we are adding a scoped feature list after browser start, copy over | |
| 54 // the existing feature list to prevent inconsistency. | |
| 55 base::FeatureList* existing_feature_list = base::FeatureList::GetInstance(); | |
| 56 if (existing_feature_list) { | |
| 57 std::string enabled_features; | |
| 58 std::string disabled_features; | |
| 59 base::FeatureList::GetInstance()->GetFeatureOverrides(&enabled_features, | |
| 60 &disabled_features); | |
| 61 feature_list->InitializeFromCommandLine(enabled_features, | |
| 62 disabled_features); | |
| 63 } | |
| 64 | |
| 65 scoped_feature_list_.InitWithFeatureList(std::move(feature_list)); | |
| 66 } | |
| 67 | |
| 68 ~ScopedUkmFeatureParams() { variations::testing::ClearAllVariationParams(); } | |
| 69 | |
| 70 private: | |
| 71 base::test::ScopedFeatureList scoped_feature_list_; | |
| 72 }; | |
| 73 | |
| 30 class UkmServiceTest : public testing::Test { | 74 class UkmServiceTest : public testing::Test { |
| 31 public: | 75 public: |
| 32 UkmServiceTest() | 76 UkmServiceTest() |
| 33 : task_runner_(new base::TestSimpleTaskRunner), | 77 : task_runner_(new base::TestSimpleTaskRunner), |
| 34 task_runner_handle_(task_runner_) { | 78 task_runner_handle_(task_runner_) { |
| 35 UkmService::RegisterPrefs(prefs_.registry()); | 79 UkmService::RegisterPrefs(prefs_.registry()); |
| 80 ClearPrefs(); | |
| 81 } | |
| 82 | |
| 83 void ClearPrefs() { | |
| 36 prefs_.ClearPref(prefs::kUkmClientId); | 84 prefs_.ClearPref(prefs::kUkmClientId); |
| 85 prefs_.ClearPref(prefs::kUkmSessionId); | |
| 37 prefs_.ClearPref(prefs::kUkmPersistedLogs); | 86 prefs_.ClearPref(prefs::kUkmPersistedLogs); |
| 38 } | 87 } |
| 39 | 88 |
| 40 int GetPersistedLogCount() { | 89 int GetPersistedLogCount() { |
| 41 const base::ListValue* list_value = | 90 const base::ListValue* list_value = |
| 42 prefs_.GetList(prefs::kUkmPersistedLogs); | 91 prefs_.GetList(prefs::kUkmPersistedLogs); |
| 43 return list_value->GetSize(); | 92 return list_value->GetSize(); |
| 44 } | 93 } |
| 45 | 94 |
| 46 Report GetPersistedReport() { | 95 Report GetPersistedReport() { |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 119 | 168 |
| 120 TEST_F(UkmServiceTest, SourceSerialization) { | 169 TEST_F(UkmServiceTest, SourceSerialization) { |
| 121 UkmService service(&prefs_, &client_); | 170 UkmService service(&prefs_, &client_); |
| 122 EXPECT_EQ(GetPersistedLogCount(), 0); | 171 EXPECT_EQ(GetPersistedLogCount(), 0); |
| 123 service.Initialize(); | 172 service.Initialize(); |
| 124 task_runner_->RunUntilIdle(); | 173 task_runner_->RunUntilIdle(); |
| 125 service.EnableRecording(); | 174 service.EnableRecording(); |
| 126 service.EnableReporting(); | 175 service.EnableReporting(); |
| 127 | 176 |
| 128 int32_t id = UkmService::GetNewSourceID(); | 177 int32_t id = UkmService::GetNewSourceID(); |
| 178 service.UpdateSourceURL(id, GURL("https://google.com/initial")); | |
| 179 service.UpdateSourceURL(id, GURL("https://google.com/intermediate")); | |
| 129 service.UpdateSourceURL(id, GURL("https://google.com/foobar")); | 180 service.UpdateSourceURL(id, GURL("https://google.com/foobar")); |
| 130 | 181 |
| 131 service.Flush(); | 182 service.Flush(); |
| 132 EXPECT_EQ(GetPersistedLogCount(), 1); | 183 EXPECT_EQ(GetPersistedLogCount(), 1); |
| 133 | 184 |
| 134 Report proto_report = GetPersistedReport(); | 185 Report proto_report = GetPersistedReport(); |
| 135 EXPECT_EQ(1, proto_report.sources_size()); | 186 EXPECT_EQ(1, proto_report.sources_size()); |
| 187 EXPECT_FALSE(proto_report.has_session_id()); | |
| 136 const Source& proto_source = proto_report.sources(0); | 188 const Source& proto_source = proto_report.sources(0); |
| 137 | 189 |
| 138 EXPECT_EQ(id, proto_source.id()); | 190 EXPECT_EQ(id, proto_source.id()); |
| 139 EXPECT_EQ(GURL("https://google.com/foobar").spec(), proto_source.url()); | 191 EXPECT_EQ(GURL("https://google.com/foobar").spec(), proto_source.url()); |
| 192 EXPECT_FALSE(proto_source.has_initial_url()); | |
| 140 } | 193 } |
| 141 | 194 |
| 142 TEST_F(UkmServiceTest, EntryBuilderAndSerialization) { | 195 TEST_F(UkmServiceTest, EntryBuilderAndSerialization) { |
| 143 UkmService service(&prefs_, &client_); | 196 UkmService service(&prefs_, &client_); |
| 144 EXPECT_EQ(0, GetPersistedLogCount()); | 197 EXPECT_EQ(0, GetPersistedLogCount()); |
| 145 service.Initialize(); | 198 service.Initialize(); |
| 146 task_runner_->RunUntilIdle(); | 199 task_runner_->RunUntilIdle(); |
| 147 service.EnableRecording(); | 200 service.EnableRecording(); |
| 148 service.EnableReporting(); | 201 service.EnableReporting(); |
| 149 | 202 |
| (...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 313 | 366 |
| 314 TEST_F(UkmServiceTest, GetNewSourceID) { | 367 TEST_F(UkmServiceTest, GetNewSourceID) { |
| 315 int32_t id1 = UkmService::GetNewSourceID(); | 368 int32_t id1 = UkmService::GetNewSourceID(); |
| 316 int32_t id2 = UkmService::GetNewSourceID(); | 369 int32_t id2 = UkmService::GetNewSourceID(); |
| 317 int32_t id3 = UkmService::GetNewSourceID(); | 370 int32_t id3 = UkmService::GetNewSourceID(); |
| 318 EXPECT_NE(id1, id2); | 371 EXPECT_NE(id1, id2); |
| 319 EXPECT_NE(id1, id3); | 372 EXPECT_NE(id1, id3); |
| 320 EXPECT_NE(id2, id3); | 373 EXPECT_NE(id2, id3); |
| 321 } | 374 } |
| 322 | 375 |
| 376 TEST_F(UkmServiceTest, RecordInitialUrl) { | |
| 377 for (bool should_record_initial_url : {true, false}) { | |
| 378 base::FieldTrialList field_trial_list(nullptr /* entropy_provider */); | |
| 379 ScopedUkmFeatureParams params( | |
| 380 {{"RecordInitialUrl", should_record_initial_url ? "true" : "false"}}); | |
| 381 | |
| 382 ClearPrefs(); | |
| 383 UkmService service(&prefs_, &client_); | |
| 384 EXPECT_EQ(GetPersistedLogCount(), 0); | |
| 385 service.Initialize(); | |
| 386 task_runner_->RunUntilIdle(); | |
| 387 service.EnableRecording(); | |
| 388 service.EnableReporting(); | |
| 389 | |
| 390 int32_t id = UkmService::GetNewSourceID(); | |
| 391 service.UpdateSourceURL(id, GURL("https://google.com/initial")); | |
| 392 service.UpdateSourceURL(id, GURL("https://google.com/intermediate")); | |
| 393 service.UpdateSourceURL(id, GURL("https://google.com/foobar")); | |
| 394 | |
| 395 service.Flush(); | |
| 396 EXPECT_EQ(GetPersistedLogCount(), 1); | |
| 397 | |
| 398 Report proto_report = GetPersistedReport(); | |
| 399 EXPECT_EQ(1, proto_report.sources_size()); | |
| 400 const Source& proto_source = proto_report.sources(0); | |
| 401 | |
| 402 EXPECT_EQ(id, proto_source.id()); | |
| 403 EXPECT_EQ(GURL("https://google.com/foobar").spec(), proto_source.url()); | |
| 404 EXPECT_EQ(should_record_initial_url, proto_source.has_initial_url()); | |
| 405 if (should_record_initial_url) { | |
| 406 EXPECT_EQ(GURL("https://google.com/initial").spec(), | |
| 407 proto_source.initial_url()); | |
| 408 } | |
| 409 } | |
| 410 } | |
| 411 | |
| 412 TEST_F(UkmServiceTest, RecordSessionId) { | |
| 413 for (bool should_record_session_id : {true, false}) { | |
| 414 base::FieldTrialList field_trial_list(nullptr /* entropy_provider */); | |
| 415 ScopedUkmFeatureParams params( | |
| 416 {{"RecordSessionId", should_record_session_id ? "true" : "false"}}); | |
| 417 | |
| 418 ClearPrefs(); | |
| 419 UkmService service(&prefs_, &client_); | |
| 420 EXPECT_EQ(GetPersistedLogCount(), 0); | |
| 421 service.Initialize(); | |
| 422 task_runner_->RunUntilIdle(); | |
| 423 service.EnableRecording(); | |
| 424 service.EnableReporting(); | |
| 425 | |
| 426 int32_t id = UkmService::GetNewSourceID(); | |
| 427 service.UpdateSourceURL(id, GURL("https://google.com/foobar")); | |
| 428 | |
| 429 service.Flush(); | |
| 430 EXPECT_EQ(GetPersistedLogCount(), 1); | |
| 431 | |
| 432 Report proto_report = GetPersistedReport(); | |
| 433 EXPECT_EQ(should_record_session_id, proto_report.has_session_id()); | |
| 434 } | |
| 435 } | |
| 436 | |
| 323 } // namespace ukm | 437 } // namespace ukm |
| OLD | NEW |