| 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 // TODO(rkaplow): consider making this a generic testing class in |
| 34 // components/variations. |
| 35 class ScopedUkmFeatureParams { |
| 36 public: |
| 37 ScopedUkmFeatureParams( |
| 38 base::FeatureList::OverrideState feature_state, |
| 39 const std::map<std::string, std::string>& variation_params) { |
| 40 static const char kTestFieldTrialName[] = "TestTrial"; |
| 41 static const char kTestExperimentGroupName[] = "TestGroup"; |
| 42 |
| 43 variations::testing::ClearAllVariationParams(); |
| 44 |
| 45 EXPECT_TRUE(variations::AssociateVariationParams( |
| 46 kTestFieldTrialName, kTestExperimentGroupName, variation_params)); |
| 47 |
| 48 base::FieldTrial* field_trial = base::FieldTrialList::CreateFieldTrial( |
| 49 kTestFieldTrialName, kTestExperimentGroupName); |
| 50 |
| 51 std::unique_ptr<base::FeatureList> feature_list(new base::FeatureList); |
| 52 feature_list->RegisterFieldTrialOverride(kUkmFeature.name, feature_state, |
| 53 field_trial); |
| 54 |
| 55 // Since we are adding a scoped feature list after browser start, copy over |
| 56 // the existing feature list to prevent inconsistency. |
| 57 base::FeatureList* existing_feature_list = base::FeatureList::GetInstance(); |
| 58 if (existing_feature_list) { |
| 59 std::string enabled_features; |
| 60 std::string disabled_features; |
| 61 base::FeatureList::GetInstance()->GetFeatureOverrides(&enabled_features, |
| 62 &disabled_features); |
| 63 feature_list->InitializeFromCommandLine(enabled_features, |
| 64 disabled_features); |
| 65 } |
| 66 |
| 67 scoped_feature_list_.InitWithFeatureList(std::move(feature_list)); |
| 68 } |
| 69 |
| 70 ~ScopedUkmFeatureParams() { variations::testing::ClearAllVariationParams(); } |
| 71 |
| 72 private: |
| 73 base::test::ScopedFeatureList scoped_feature_list_; |
| 74 |
| 75 DISALLOW_COPY_AND_ASSIGN(ScopedUkmFeatureParams); |
| 76 }; |
| 77 |
| 30 class UkmServiceTest : public testing::Test { | 78 class UkmServiceTest : public testing::Test { |
| 31 public: | 79 public: |
| 32 UkmServiceTest() | 80 UkmServiceTest() |
| 33 : task_runner_(new base::TestSimpleTaskRunner), | 81 : task_runner_(new base::TestSimpleTaskRunner), |
| 34 task_runner_handle_(task_runner_) { | 82 task_runner_handle_(task_runner_) { |
| 35 UkmService::RegisterPrefs(prefs_.registry()); | 83 UkmService::RegisterPrefs(prefs_.registry()); |
| 84 ClearPrefs(); |
| 85 } |
| 86 |
| 87 void ClearPrefs() { |
| 36 prefs_.ClearPref(prefs::kUkmClientId); | 88 prefs_.ClearPref(prefs::kUkmClientId); |
| 89 prefs_.ClearPref(prefs::kUkmSessionId); |
| 37 prefs_.ClearPref(prefs::kUkmPersistedLogs); | 90 prefs_.ClearPref(prefs::kUkmPersistedLogs); |
| 38 } | 91 } |
| 39 | 92 |
| 40 int GetPersistedLogCount() { | 93 int GetPersistedLogCount() { |
| 41 const base::ListValue* list_value = | 94 const base::ListValue* list_value = |
| 42 prefs_.GetList(prefs::kUkmPersistedLogs); | 95 prefs_.GetList(prefs::kUkmPersistedLogs); |
| 43 return list_value->GetSize(); | 96 return list_value->GetSize(); |
| 44 } | 97 } |
| 45 | 98 |
| 46 Report GetPersistedReport() { | 99 Report GetPersistedReport() { |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 119 | 172 |
| 120 TEST_F(UkmServiceTest, SourceSerialization) { | 173 TEST_F(UkmServiceTest, SourceSerialization) { |
| 121 UkmService service(&prefs_, &client_); | 174 UkmService service(&prefs_, &client_); |
| 122 EXPECT_EQ(GetPersistedLogCount(), 0); | 175 EXPECT_EQ(GetPersistedLogCount(), 0); |
| 123 service.Initialize(); | 176 service.Initialize(); |
| 124 task_runner_->RunUntilIdle(); | 177 task_runner_->RunUntilIdle(); |
| 125 service.EnableRecording(); | 178 service.EnableRecording(); |
| 126 service.EnableReporting(); | 179 service.EnableReporting(); |
| 127 | 180 |
| 128 int32_t id = UkmService::GetNewSourceID(); | 181 int32_t id = UkmService::GetNewSourceID(); |
| 182 service.UpdateSourceURL(id, GURL("https://google.com/initial")); |
| 183 service.UpdateSourceURL(id, GURL("https://google.com/intermediate")); |
| 129 service.UpdateSourceURL(id, GURL("https://google.com/foobar")); | 184 service.UpdateSourceURL(id, GURL("https://google.com/foobar")); |
| 130 | 185 |
| 131 service.Flush(); | 186 service.Flush(); |
| 132 EXPECT_EQ(GetPersistedLogCount(), 1); | 187 EXPECT_EQ(GetPersistedLogCount(), 1); |
| 133 | 188 |
| 134 Report proto_report = GetPersistedReport(); | 189 Report proto_report = GetPersistedReport(); |
| 135 EXPECT_EQ(1, proto_report.sources_size()); | 190 EXPECT_EQ(1, proto_report.sources_size()); |
| 191 EXPECT_FALSE(proto_report.has_session_id()); |
| 136 const Source& proto_source = proto_report.sources(0); | 192 const Source& proto_source = proto_report.sources(0); |
| 137 | 193 |
| 138 EXPECT_EQ(id, proto_source.id()); | 194 EXPECT_EQ(id, proto_source.id()); |
| 139 EXPECT_EQ(GURL("https://google.com/foobar").spec(), proto_source.url()); | 195 EXPECT_EQ(GURL("https://google.com/foobar").spec(), proto_source.url()); |
| 196 EXPECT_FALSE(proto_source.has_initial_url()); |
| 140 } | 197 } |
| 141 | 198 |
| 142 TEST_F(UkmServiceTest, EntryBuilderAndSerialization) { | 199 TEST_F(UkmServiceTest, EntryBuilderAndSerialization) { |
| 143 UkmService service(&prefs_, &client_); | 200 UkmService service(&prefs_, &client_); |
| 144 EXPECT_EQ(0, GetPersistedLogCount()); | 201 EXPECT_EQ(0, GetPersistedLogCount()); |
| 145 service.Initialize(); | 202 service.Initialize(); |
| 146 task_runner_->RunUntilIdle(); | 203 task_runner_->RunUntilIdle(); |
| 147 service.EnableRecording(); | 204 service.EnableRecording(); |
| 148 service.EnableReporting(); | 205 service.EnableReporting(); |
| 149 | 206 |
| (...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 313 | 370 |
| 314 TEST_F(UkmServiceTest, GetNewSourceID) { | 371 TEST_F(UkmServiceTest, GetNewSourceID) { |
| 315 int32_t id1 = UkmService::GetNewSourceID(); | 372 int32_t id1 = UkmService::GetNewSourceID(); |
| 316 int32_t id2 = UkmService::GetNewSourceID(); | 373 int32_t id2 = UkmService::GetNewSourceID(); |
| 317 int32_t id3 = UkmService::GetNewSourceID(); | 374 int32_t id3 = UkmService::GetNewSourceID(); |
| 318 EXPECT_NE(id1, id2); | 375 EXPECT_NE(id1, id2); |
| 319 EXPECT_NE(id1, id3); | 376 EXPECT_NE(id1, id3); |
| 320 EXPECT_NE(id2, id3); | 377 EXPECT_NE(id2, id3); |
| 321 } | 378 } |
| 322 | 379 |
| 380 TEST_F(UkmServiceTest, RecordInitialUrl) { |
| 381 for (bool should_record_initial_url : {true, false}) { |
| 382 base::FieldTrialList field_trial_list(nullptr /* entropy_provider */); |
| 383 ScopedUkmFeatureParams params( |
| 384 base::FeatureList::OVERRIDE_ENABLE_FEATURE, |
| 385 {{"RecordInitialUrl", should_record_initial_url ? "true" : "false"}}); |
| 386 |
| 387 ClearPrefs(); |
| 388 UkmService service(&prefs_, &client_); |
| 389 EXPECT_EQ(GetPersistedLogCount(), 0); |
| 390 service.Initialize(); |
| 391 task_runner_->RunUntilIdle(); |
| 392 service.EnableRecording(); |
| 393 service.EnableReporting(); |
| 394 |
| 395 int32_t id = UkmService::GetNewSourceID(); |
| 396 service.UpdateSourceURL(id, GURL("https://google.com/initial")); |
| 397 service.UpdateSourceURL(id, GURL("https://google.com/intermediate")); |
| 398 service.UpdateSourceURL(id, GURL("https://google.com/foobar")); |
| 399 |
| 400 service.Flush(); |
| 401 EXPECT_EQ(GetPersistedLogCount(), 1); |
| 402 |
| 403 Report proto_report = GetPersistedReport(); |
| 404 EXPECT_EQ(1, proto_report.sources_size()); |
| 405 const Source& proto_source = proto_report.sources(0); |
| 406 |
| 407 EXPECT_EQ(id, proto_source.id()); |
| 408 EXPECT_EQ(GURL("https://google.com/foobar").spec(), proto_source.url()); |
| 409 EXPECT_EQ(should_record_initial_url, proto_source.has_initial_url()); |
| 410 if (should_record_initial_url) { |
| 411 EXPECT_EQ(GURL("https://google.com/initial").spec(), |
| 412 proto_source.initial_url()); |
| 413 } |
| 414 } |
| 415 } |
| 416 |
| 417 TEST_F(UkmServiceTest, RecordSessionId) { |
| 418 for (bool should_record_session_id : {true, false}) { |
| 419 base::FieldTrialList field_trial_list(nullptr /* entropy_provider */); |
| 420 ScopedUkmFeatureParams params( |
| 421 base::FeatureList::OVERRIDE_ENABLE_FEATURE, |
| 422 {{"RecordSessionId", should_record_session_id ? "true" : "false"}}); |
| 423 |
| 424 ClearPrefs(); |
| 425 UkmService service(&prefs_, &client_); |
| 426 EXPECT_EQ(GetPersistedLogCount(), 0); |
| 427 service.Initialize(); |
| 428 task_runner_->RunUntilIdle(); |
| 429 service.EnableRecording(); |
| 430 service.EnableReporting(); |
| 431 |
| 432 int32_t id = UkmService::GetNewSourceID(); |
| 433 service.UpdateSourceURL(id, GURL("https://google.com/foobar")); |
| 434 |
| 435 service.Flush(); |
| 436 EXPECT_EQ(GetPersistedLogCount(), 1); |
| 437 |
| 438 Report proto_report = GetPersistedReport(); |
| 439 EXPECT_EQ(should_record_session_id, proto_report.has_session_id()); |
| 440 } |
| 441 } |
| 442 |
| 323 } // namespace ukm | 443 } // namespace ukm |
| OLD | NEW |