Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(147)

Side by Side Diff: components/ukm/ukm_service_unittest.cc

Issue 2883563002: Refactor UKM interface for mojo-ification (Closed)
Patch Set: Fix uma_session_stats.cc Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 <map>
8 #include <string> 8 #include <string>
9 #include <utility> 9 #include <utility>
10 10
11 #include "base/hash.h" 11 #include "base/hash.h"
12 #include "base/metrics/metrics_hashes.h" 12 #include "base/metrics/metrics_hashes.h"
13 #include "base/test/scoped_feature_list.h" 13 #include "base/test/scoped_feature_list.h"
14 #include "base/test/test_simple_task_runner.h" 14 #include "base/test/test_simple_task_runner.h"
15 #include "base/threading/thread_task_runner_handle.h" 15 #include "base/threading/thread_task_runner_handle.h"
16 #include "components/metrics/proto/ukm/report.pb.h" 16 #include "components/metrics/proto/ukm/report.pb.h"
17 #include "components/metrics/proto/ukm/source.pb.h" 17 #include "components/metrics/proto/ukm/source.pb.h"
18 #include "components/metrics/test_metrics_provider.h" 18 #include "components/metrics/test_metrics_provider.h"
19 #include "components/metrics/test_metrics_service_client.h" 19 #include "components/metrics/test_metrics_service_client.h"
20 #include "components/prefs/testing_pref_service.h" 20 #include "components/prefs/testing_pref_service.h"
21 #include "components/ukm/persisted_logs_metrics_impl.h" 21 #include "components/ukm/persisted_logs_metrics_impl.h"
22 #include "components/ukm/ukm_entry_builder.h" 22 #include "components/ukm/public/ukm_entry_builder.h"
23 #include "components/ukm/ukm_pref_names.h" 23 #include "components/ukm/ukm_pref_names.h"
24 #include "components/ukm/ukm_source.h" 24 #include "components/ukm/ukm_source.h"
25 #include "components/variations/variations_associated_data.h" 25 #include "components/variations/variations_associated_data.h"
26 #include "testing/gtest/include/gtest/gtest.h" 26 #include "testing/gtest/include/gtest/gtest.h"
27 #include "third_party/zlib/google/compression_utils.h" 27 #include "third_party/zlib/google/compression_utils.h"
28 28
29 namespace ukm { 29 namespace ukm {
30 30
31 // A small shim exposing UkmRecorder methods to tests.
32 class TestRecordingHelper {
33 public:
34 TestRecordingHelper(UkmRecorder* recorder) : recorder_(recorder) {}
35
36 void UpdateSourceURL(SourceId source_id, const GURL& url) {
37 recorder_->UpdateSourceURL(source_id, url);
38 };
39
40 std::unique_ptr<UkmEntryBuilder> GetEntryBuilder(SourceId source_id,
41 const char* event_name) {
42 return recorder_->GetEntryBuilder(source_id, event_name);
43 }
44
45 private:
46 UkmRecorder* recorder_;
47 };
48
31 namespace { 49 namespace {
32 50
33 // TODO(rkaplow): consider making this a generic testing class in 51 // TODO(rkaplow): consider making this a generic testing class in
34 // components/variations. 52 // components/variations.
35 class ScopedUkmFeatureParams { 53 class ScopedUkmFeatureParams {
36 public: 54 public:
37 ScopedUkmFeatureParams( 55 ScopedUkmFeatureParams(
38 base::FeatureList::OverrideState feature_state, 56 base::FeatureList::OverrideState feature_state,
39 const std::map<std::string, std::string>& variation_params) { 57 const std::map<std::string, std::string>& variation_params) {
40 static const char kTestFieldTrialName[] = "TestTrial"; 58 static const char kTestFieldTrialName[] = "TestTrial";
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after
140 service.EnableRecording(); 158 service.EnableRecording();
141 service.EnableReporting(); 159 service.EnableReporting();
142 EXPECT_TRUE(task_runner_->HasPendingTask()); 160 EXPECT_TRUE(task_runner_->HasPendingTask());
143 service.DisableReporting(); 161 service.DisableReporting();
144 task_runner_->RunPendingTasks(); 162 task_runner_->RunPendingTasks();
145 EXPECT_FALSE(task_runner_->HasPendingTask()); 163 EXPECT_FALSE(task_runner_->HasPendingTask());
146 } 164 }
147 165
148 TEST_F(UkmServiceTest, PersistAndPurge) { 166 TEST_F(UkmServiceTest, PersistAndPurge) {
149 UkmService service(&prefs_, &client_); 167 UkmService service(&prefs_, &client_);
168 TestRecordingHelper recorder(&service);
150 EXPECT_EQ(GetPersistedLogCount(), 0); 169 EXPECT_EQ(GetPersistedLogCount(), 0);
151 service.Initialize(); 170 service.Initialize();
152 task_runner_->RunUntilIdle(); 171 task_runner_->RunUntilIdle();
153 service.EnableRecording(); 172 service.EnableRecording();
154 service.EnableReporting(); 173 service.EnableReporting();
155 174
156 int32_t id = UkmService::GetNewSourceID(); 175 ukm::SourceId id = UkmRecorder::GetNewSourceID();
157 service.UpdateSourceURL(id, GURL("https://google.com/foobar")); 176 recorder.UpdateSourceURL(id, GURL("https://google.com/foobar"));
158 // Should init, generate a log, and start an upload for source. 177 // Should init, generate a log, and start an upload for source.
159 task_runner_->RunPendingTasks(); 178 task_runner_->RunPendingTasks();
160 EXPECT_TRUE(client_.uploader()->is_uploading()); 179 EXPECT_TRUE(client_.uploader()->is_uploading());
161 // Flushes the generated log to disk and generates a new entry. 180 // Flushes the generated log to disk and generates a new entry.
162 { 181 {
163 std::unique_ptr<UkmEntryBuilder> builder = 182 std::unique_ptr<UkmEntryBuilder> builder =
164 service.GetEntryBuilder(id, "PageLoad"); 183 recorder.GetEntryBuilder(id, "PageLoad");
165 builder->AddMetric("FirstContentfulPaint", 300); 184 builder->AddMetric("FirstContentfulPaint", 300);
166 } 185 }
167 service.Flush(); 186 service.Flush();
168 EXPECT_EQ(GetPersistedLogCount(), 2); 187 EXPECT_EQ(GetPersistedLogCount(), 2);
169 service.Purge(); 188 service.Purge();
170 EXPECT_EQ(GetPersistedLogCount(), 0); 189 EXPECT_EQ(GetPersistedLogCount(), 0);
171 } 190 }
172 191
173 TEST_F(UkmServiceTest, SourceSerialization) { 192 TEST_F(UkmServiceTest, SourceSerialization) {
174 UkmService service(&prefs_, &client_); 193 UkmService service(&prefs_, &client_);
194 TestRecordingHelper recorder(&service);
175 EXPECT_EQ(GetPersistedLogCount(), 0); 195 EXPECT_EQ(GetPersistedLogCount(), 0);
176 service.Initialize(); 196 service.Initialize();
177 task_runner_->RunUntilIdle(); 197 task_runner_->RunUntilIdle();
178 service.EnableRecording(); 198 service.EnableRecording();
179 service.EnableReporting(); 199 service.EnableReporting();
180 200
181 int32_t id = UkmService::GetNewSourceID(); 201 ukm::SourceId id = UkmRecorder::GetNewSourceID();
182 service.UpdateSourceURL(id, GURL("https://google.com/initial")); 202 recorder.UpdateSourceURL(id, GURL("https://google.com/initial"));
183 service.UpdateSourceURL(id, GURL("https://google.com/intermediate")); 203 recorder.UpdateSourceURL(id, GURL("https://google.com/intermediate"));
184 service.UpdateSourceURL(id, GURL("https://google.com/foobar")); 204 recorder.UpdateSourceURL(id, GURL("https://google.com/foobar"));
185 205
186 service.Flush(); 206 service.Flush();
187 EXPECT_EQ(GetPersistedLogCount(), 1); 207 EXPECT_EQ(GetPersistedLogCount(), 1);
188 208
189 Report proto_report = GetPersistedReport(); 209 Report proto_report = GetPersistedReport();
190 EXPECT_EQ(1, proto_report.sources_size()); 210 EXPECT_EQ(1, proto_report.sources_size());
191 EXPECT_FALSE(proto_report.has_session_id()); 211 EXPECT_FALSE(proto_report.has_session_id());
192 const Source& proto_source = proto_report.sources(0); 212 const Source& proto_source = proto_report.sources(0);
193 213
194 EXPECT_EQ(id, proto_source.id()); 214 EXPECT_EQ(id, proto_source.id());
195 EXPECT_EQ(GURL("https://google.com/foobar").spec(), proto_source.url()); 215 EXPECT_EQ(GURL("https://google.com/foobar").spec(), proto_source.url());
196 EXPECT_FALSE(proto_source.has_initial_url()); 216 EXPECT_FALSE(proto_source.has_initial_url());
197 } 217 }
198 218
199 TEST_F(UkmServiceTest, EntryBuilderAndSerialization) { 219 TEST_F(UkmServiceTest, EntryBuilderAndSerialization) {
200 UkmService service(&prefs_, &client_); 220 UkmService service(&prefs_, &client_);
221 TestRecordingHelper recorder(&service);
201 EXPECT_EQ(0, GetPersistedLogCount()); 222 EXPECT_EQ(0, GetPersistedLogCount());
202 service.Initialize(); 223 service.Initialize();
203 task_runner_->RunUntilIdle(); 224 task_runner_->RunUntilIdle();
204 service.EnableRecording(); 225 service.EnableRecording();
205 service.EnableReporting(); 226 service.EnableReporting();
206 227
207 int32_t id = UkmService::GetNewSourceID(); 228 ukm::SourceId id = UkmRecorder::GetNewSourceID();
208 service.UpdateSourceURL(id, GURL("https://google.com/foobar")); 229 recorder.UpdateSourceURL(id, GURL("https://google.com/foobar"));
209 { 230 {
210 std::unique_ptr<UkmEntryBuilder> foo_builder = 231 std::unique_ptr<UkmEntryBuilder> foo_builder =
211 service.GetEntryBuilder(id, "foo"); 232 service.GetEntryBuilder(id, "foo");
212 foo_builder->AddMetric("foo_start", 0); 233 foo_builder->AddMetric("foo_start", 0);
213 foo_builder->AddMetric("foo_end", 10); 234 foo_builder->AddMetric("foo_end", 10);
214 235
215 std::unique_ptr<UkmEntryBuilder> bar_builder = 236 std::unique_ptr<UkmEntryBuilder> bar_builder =
216 service.GetEntryBuilder(id, "bar"); 237 service.GetEntryBuilder(id, "bar");
217 bar_builder->AddMetric("bar_start", 5); 238 bar_builder->AddMetric("bar_start", 5);
218 bar_builder->AddMetric("bar_end", 15); 239 bar_builder->AddMetric("bar_end", 15);
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
254 EXPECT_EQ(base::HashMetricName("foo_start"), 275 EXPECT_EQ(base::HashMetricName("foo_start"),
255 proto_entry_foo_start.metric_hash()); 276 proto_entry_foo_start.metric_hash());
256 EXPECT_EQ(0, proto_entry_foo_start.value()); 277 EXPECT_EQ(0, proto_entry_foo_start.value());
257 const Entry::Metric proto_entry_foo_end = proto_entry_foo.metrics(1); 278 const Entry::Metric proto_entry_foo_end = proto_entry_foo.metrics(1);
258 EXPECT_EQ(base::HashMetricName("foo_end"), proto_entry_foo_end.metric_hash()); 279 EXPECT_EQ(base::HashMetricName("foo_end"), proto_entry_foo_end.metric_hash());
259 EXPECT_EQ(10, proto_entry_foo_end.value()); 280 EXPECT_EQ(10, proto_entry_foo_end.value());
260 } 281 }
261 282
262 TEST_F(UkmServiceTest, AddEntryWithEmptyMetrics) { 283 TEST_F(UkmServiceTest, AddEntryWithEmptyMetrics) {
263 UkmService service(&prefs_, &client_); 284 UkmService service(&prefs_, &client_);
285 TestRecordingHelper recorder(&service);
264 EXPECT_EQ(0, GetPersistedLogCount()); 286 EXPECT_EQ(0, GetPersistedLogCount());
265 service.Initialize(); 287 service.Initialize();
266 task_runner_->RunUntilIdle(); 288 task_runner_->RunUntilIdle();
267 service.EnableRecording(); 289 service.EnableRecording();
268 service.EnableReporting(); 290 service.EnableReporting();
269 291
270 int32_t id = UkmService::GetNewSourceID(); 292 ukm::SourceId id = UkmRecorder::GetNewSourceID();
271 service.UpdateSourceURL(id, GURL("https://google.com/foobar")); 293 recorder.UpdateSourceURL(id, GURL("https://google.com/foobar"));
272 294
273 { 295 {
274 std::unique_ptr<UkmEntryBuilder> builder = 296 std::unique_ptr<UkmEntryBuilder> builder =
275 service.GetEntryBuilder(id, "PageLoad"); 297 service.GetEntryBuilder(id, "PageLoad");
276 } 298 }
277 service.Flush(); 299 service.Flush();
278 EXPECT_EQ(1, GetPersistedLogCount()); 300 EXPECT_EQ(1, GetPersistedLogCount());
279 Report proto_report = GetPersistedReport(); 301 Report proto_report = GetPersistedReport();
280 EXPECT_EQ(1, proto_report.entries_size()); 302 EXPECT_EQ(1, proto_report.entries_size());
281 } 303 }
282 304
283 TEST_F(UkmServiceTest, MetricsProviderTest) { 305 TEST_F(UkmServiceTest, MetricsProviderTest) {
284 UkmService service(&prefs_, &client_); 306 UkmService service(&prefs_, &client_);
307 TestRecordingHelper recorder(&service);
285 308
286 metrics::TestMetricsProvider* provider = new metrics::TestMetricsProvider(); 309 metrics::TestMetricsProvider* provider = new metrics::TestMetricsProvider();
287 service.RegisterMetricsProvider( 310 service.RegisterMetricsProvider(
288 std::unique_ptr<metrics::MetricsProvider>(provider)); 311 std::unique_ptr<metrics::MetricsProvider>(provider));
289 312
290 service.Initialize(); 313 service.Initialize();
291 314
292 // Providers have not supplied system profile information yet. 315 // Providers have not supplied system profile information yet.
293 EXPECT_FALSE(provider->provide_system_profile_metrics_called()); 316 EXPECT_FALSE(provider->provide_system_profile_metrics_called());
294 317
295 task_runner_->RunUntilIdle(); 318 task_runner_->RunUntilIdle();
296 service.EnableRecording(); 319 service.EnableRecording();
297 service.EnableReporting(); 320 service.EnableReporting();
298 321
299 int32_t id = UkmService::GetNewSourceID(); 322 ukm::SourceId id = UkmRecorder::GetNewSourceID();
300 service.UpdateSourceURL(id, GURL("https://google.com/foobar")); 323 recorder.UpdateSourceURL(id, GURL("https://google.com/foobar"));
301 { 324 {
302 std::unique_ptr<UkmEntryBuilder> builder = 325 std::unique_ptr<UkmEntryBuilder> builder =
303 service.GetEntryBuilder(id, "PageLoad"); 326 service.GetEntryBuilder(id, "PageLoad");
304 builder->AddMetric("FirstContentfulPaint", 300); 327 builder->AddMetric("FirstContentfulPaint", 300);
305 } 328 }
306 service.Flush(); 329 service.Flush();
307 EXPECT_EQ(GetPersistedLogCount(), 1); 330 EXPECT_EQ(GetPersistedLogCount(), 1);
308 331
309 Report proto_report = GetPersistedReport(); 332 Report proto_report = GetPersistedReport();
310 EXPECT_EQ(1, proto_report.sources_size()); 333 EXPECT_EQ(1, proto_report.sources_size());
311 EXPECT_EQ(1, proto_report.entries_size()); 334 EXPECT_EQ(1, proto_report.entries_size());
312 335
313 // Providers have now supplied system profile information. 336 // Providers have now supplied system profile information.
314 EXPECT_TRUE(provider->provide_system_profile_metrics_called()); 337 EXPECT_TRUE(provider->provide_system_profile_metrics_called());
315 } 338 }
316 339
317 TEST_F(UkmServiceTest, LogsUploadedOnlyWhenHavingSourcesOrEntries) { 340 TEST_F(UkmServiceTest, LogsUploadedOnlyWhenHavingSourcesOrEntries) {
318 UkmService service(&prefs_, &client_); 341 UkmService service(&prefs_, &client_);
342 TestRecordingHelper recorder(&service);
319 EXPECT_EQ(GetPersistedLogCount(), 0); 343 EXPECT_EQ(GetPersistedLogCount(), 0);
320 service.Initialize(); 344 service.Initialize();
321 task_runner_->RunUntilIdle(); 345 task_runner_->RunUntilIdle();
322 service.EnableRecording(); 346 service.EnableRecording();
323 service.EnableReporting(); 347 service.EnableReporting();
324 348
325 EXPECT_TRUE(task_runner_->HasPendingTask()); 349 EXPECT_TRUE(task_runner_->HasPendingTask());
326 // Neither rotation or Flush should generate logs 350 // Neither rotation or Flush should generate logs
327 task_runner_->RunPendingTasks(); 351 task_runner_->RunPendingTasks();
328 service.Flush(); 352 service.Flush();
329 EXPECT_EQ(GetPersistedLogCount(), 0); 353 EXPECT_EQ(GetPersistedLogCount(), 0);
330 354
331 int32_t id = UkmService::GetNewSourceID(); 355 ukm::SourceId id = UkmRecorder::GetNewSourceID();
332 service.UpdateSourceURL(id, GURL("https://google.com/foobar")); 356 recorder.UpdateSourceURL(id, GURL("https://google.com/foobar"));
333 // Includes a Source, so will persist. 357 // Includes a Source, so will persist.
334 service.Flush(); 358 service.Flush();
335 EXPECT_EQ(GetPersistedLogCount(), 1); 359 EXPECT_EQ(GetPersistedLogCount(), 1);
336 360
337 { 361 {
338 std::unique_ptr<UkmEntryBuilder> builder = 362 std::unique_ptr<UkmEntryBuilder> builder =
339 service.GetEntryBuilder(id, "PageLoad"); 363 service.GetEntryBuilder(id, "PageLoad");
340 builder->AddMetric("FirstPaint", 300); 364 builder->AddMetric("FirstPaint", 300);
341 } 365 }
342 // Includes an Entry, so will persist. 366 // Includes an Entry, so will persist.
343 service.Flush(); 367 service.Flush();
344 EXPECT_EQ(GetPersistedLogCount(), 2); 368 EXPECT_EQ(GetPersistedLogCount(), 2);
345 369
346 service.UpdateSourceURL(id, GURL("https://google.com/foobar")); 370 recorder.UpdateSourceURL(id, GURL("https://google.com/foobar"));
347 { 371 {
348 std::unique_ptr<UkmEntryBuilder> builder = 372 std::unique_ptr<UkmEntryBuilder> builder =
349 service.GetEntryBuilder(id, "PageLoad"); 373 service.GetEntryBuilder(id, "PageLoad");
350 builder->AddMetric("FirstContentfulPaint", 300); 374 builder->AddMetric("FirstContentfulPaint", 300);
351 } 375 }
352 // Includes a Source and an Entry, so will persist. 376 // Includes a Source and an Entry, so will persist.
353 service.Flush(); 377 service.Flush();
354 EXPECT_EQ(GetPersistedLogCount(), 3); 378 EXPECT_EQ(GetPersistedLogCount(), 3);
355 379
356 // Current log has no Sources. 380 // Current log has no Sources.
357 service.Flush(); 381 service.Flush();
358 EXPECT_EQ(GetPersistedLogCount(), 3); 382 EXPECT_EQ(GetPersistedLogCount(), 3);
359 } 383 }
360 384
361 TEST_F(UkmServiceTest, GetNewSourceID) { 385 TEST_F(UkmServiceTest, GetNewSourceID) {
362 int32_t id1 = UkmService::GetNewSourceID(); 386 ukm::SourceId id1 = UkmRecorder::GetNewSourceID();
363 int32_t id2 = UkmService::GetNewSourceID(); 387 ukm::SourceId id2 = UkmRecorder::GetNewSourceID();
364 int32_t id3 = UkmService::GetNewSourceID(); 388 ukm::SourceId id3 = UkmRecorder::GetNewSourceID();
365 EXPECT_NE(id1, id2); 389 EXPECT_NE(id1, id2);
366 EXPECT_NE(id1, id3); 390 EXPECT_NE(id1, id3);
367 EXPECT_NE(id2, id3); 391 EXPECT_NE(id2, id3);
368 } 392 }
369 393
370 TEST_F(UkmServiceTest, RecordInitialUrl) { 394 TEST_F(UkmServiceTest, RecordInitialUrl) {
371 for (bool should_record_initial_url : {true, false}) { 395 for (bool should_record_initial_url : {true, false}) {
372 base::FieldTrialList field_trial_list(nullptr /* entropy_provider */); 396 base::FieldTrialList field_trial_list(nullptr /* entropy_provider */);
373 ScopedUkmFeatureParams params( 397 ScopedUkmFeatureParams params(
374 base::FeatureList::OVERRIDE_ENABLE_FEATURE, 398 base::FeatureList::OVERRIDE_ENABLE_FEATURE,
375 {{"RecordInitialUrl", should_record_initial_url ? "true" : "false"}}); 399 {{"RecordInitialUrl", should_record_initial_url ? "true" : "false"}});
376 400
377 ClearPrefs(); 401 ClearPrefs();
378 UkmService service(&prefs_, &client_); 402 UkmService service(&prefs_, &client_);
403 TestRecordingHelper recorder(&service);
379 EXPECT_EQ(GetPersistedLogCount(), 0); 404 EXPECT_EQ(GetPersistedLogCount(), 0);
380 service.Initialize(); 405 service.Initialize();
381 task_runner_->RunUntilIdle(); 406 task_runner_->RunUntilIdle();
382 service.EnableRecording(); 407 service.EnableRecording();
383 service.EnableReporting(); 408 service.EnableReporting();
384 409
385 int32_t id = UkmService::GetNewSourceID(); 410 ukm::SourceId id = UkmRecorder::GetNewSourceID();
386 service.UpdateSourceURL(id, GURL("https://google.com/initial")); 411 recorder.UpdateSourceURL(id, GURL("https://google.com/initial"));
387 service.UpdateSourceURL(id, GURL("https://google.com/intermediate")); 412 recorder.UpdateSourceURL(id, GURL("https://google.com/intermediate"));
388 service.UpdateSourceURL(id, GURL("https://google.com/foobar")); 413 recorder.UpdateSourceURL(id, GURL("https://google.com/foobar"));
389 414
390 service.Flush(); 415 service.Flush();
391 EXPECT_EQ(GetPersistedLogCount(), 1); 416 EXPECT_EQ(GetPersistedLogCount(), 1);
392 417
393 Report proto_report = GetPersistedReport(); 418 Report proto_report = GetPersistedReport();
394 EXPECT_EQ(1, proto_report.sources_size()); 419 EXPECT_EQ(1, proto_report.sources_size());
395 const Source& proto_source = proto_report.sources(0); 420 const Source& proto_source = proto_report.sources(0);
396 421
397 EXPECT_EQ(id, proto_source.id()); 422 EXPECT_EQ(id, proto_source.id());
398 EXPECT_EQ(GURL("https://google.com/foobar").spec(), proto_source.url()); 423 EXPECT_EQ(GURL("https://google.com/foobar").spec(), proto_source.url());
399 EXPECT_EQ(should_record_initial_url, proto_source.has_initial_url()); 424 EXPECT_EQ(should_record_initial_url, proto_source.has_initial_url());
400 if (should_record_initial_url) { 425 if (should_record_initial_url) {
401 EXPECT_EQ(GURL("https://google.com/initial").spec(), 426 EXPECT_EQ(GURL("https://google.com/initial").spec(),
402 proto_source.initial_url()); 427 proto_source.initial_url());
403 } 428 }
404 } 429 }
405 } 430 }
406 431
407 TEST_F(UkmServiceTest, RecordSessionId) { 432 TEST_F(UkmServiceTest, RecordSessionId) {
408 for (bool should_record_session_id : {true, false}) { 433 for (bool should_record_session_id : {true, false}) {
409 base::FieldTrialList field_trial_list(nullptr /* entropy_provider */); 434 base::FieldTrialList field_trial_list(nullptr /* entropy_provider */);
410 ScopedUkmFeatureParams params( 435 ScopedUkmFeatureParams params(
411 base::FeatureList::OVERRIDE_ENABLE_FEATURE, 436 base::FeatureList::OVERRIDE_ENABLE_FEATURE,
412 {{"RecordSessionId", should_record_session_id ? "true" : "false"}}); 437 {{"RecordSessionId", should_record_session_id ? "true" : "false"}});
413 438
414 ClearPrefs(); 439 ClearPrefs();
415 UkmService service(&prefs_, &client_); 440 UkmService service(&prefs_, &client_);
441 TestRecordingHelper recorder(&service);
416 EXPECT_EQ(0, GetPersistedLogCount()); 442 EXPECT_EQ(0, GetPersistedLogCount());
417 service.Initialize(); 443 service.Initialize();
418 task_runner_->RunUntilIdle(); 444 task_runner_->RunUntilIdle();
419 service.EnableRecording(); 445 service.EnableRecording();
420 service.EnableReporting(); 446 service.EnableReporting();
421 447
422 auto id = UkmService::GetNewSourceID(); 448 auto id = UkmRecorder::GetNewSourceID();
423 service.UpdateSourceURL(id, GURL("https://google.com/foobar")); 449 recorder.UpdateSourceURL(id, GURL("https://google.com/foobar"));
424 450
425 service.Flush(); 451 service.Flush();
426 EXPECT_EQ(1, GetPersistedLogCount()); 452 EXPECT_EQ(1, GetPersistedLogCount());
427 453
428 auto proto_report = GetPersistedReport(); 454 auto proto_report = GetPersistedReport();
429 EXPECT_EQ(should_record_session_id, proto_report.has_session_id()); 455 EXPECT_EQ(should_record_session_id, proto_report.has_session_id());
430 } 456 }
431 } 457 }
432 458
433 TEST_F(UkmServiceTest, SourceSize) { 459 TEST_F(UkmServiceTest, SourceSize) {
434 base::FieldTrialList field_trial_list(nullptr /* entropy_provider */); 460 base::FieldTrialList field_trial_list(nullptr /* entropy_provider */);
435 // Set a threshold of number of Sources via Feature Params. 461 // Set a threshold of number of Sources via Feature Params.
436 ScopedUkmFeatureParams params(base::FeatureList::OVERRIDE_ENABLE_FEATURE, 462 ScopedUkmFeatureParams params(base::FeatureList::OVERRIDE_ENABLE_FEATURE,
437 {{"MaxSources", "2"}}); 463 {{"MaxSources", "2"}});
438 464
439 ClearPrefs(); 465 ClearPrefs();
440 UkmService service(&prefs_, &client_); 466 UkmService service(&prefs_, &client_);
467 TestRecordingHelper recorder(&service);
441 EXPECT_EQ(0, GetPersistedLogCount()); 468 EXPECT_EQ(0, GetPersistedLogCount());
442 service.Initialize(); 469 service.Initialize();
443 task_runner_->RunUntilIdle(); 470 task_runner_->RunUntilIdle();
444 service.EnableRecording(); 471 service.EnableRecording();
445 service.EnableReporting(); 472 service.EnableReporting();
446 473
447 auto id = UkmService::GetNewSourceID(); 474 auto id = UkmRecorder::GetNewSourceID();
448 service.UpdateSourceURL(id, GURL("https://google.com/foobar1")); 475 recorder.UpdateSourceURL(id, GURL("https://google.com/foobar1"));
449 id = UkmService::GetNewSourceID(); 476 id = UkmRecorder::GetNewSourceID();
450 service.UpdateSourceURL(id, GURL("https://google.com/foobar2")); 477 recorder.UpdateSourceURL(id, GURL("https://google.com/foobar2"));
451 id = UkmService::GetNewSourceID(); 478 id = UkmRecorder::GetNewSourceID();
452 service.UpdateSourceURL(id, GURL("https://google.com/foobar3")); 479 recorder.UpdateSourceURL(id, GURL("https://google.com/foobar3"));
453 480
454 service.Flush(); 481 service.Flush();
455 EXPECT_EQ(1, GetPersistedLogCount()); 482 EXPECT_EQ(1, GetPersistedLogCount());
456 483
457 auto proto_report = GetPersistedReport(); 484 auto proto_report = GetPersistedReport();
458 // Note, 2 instead of 3 sources, since we overrode the max number of sources 485 // Note, 2 instead of 3 sources, since we overrode the max number of sources
459 // via Feature params. 486 // via Feature params.
460 EXPECT_EQ(2, proto_report.sources_size()); 487 EXPECT_EQ(2, proto_report.sources_size());
461 } 488 }
462 489
463 TEST_F(UkmServiceTest, PurgeMidUpload) { 490 TEST_F(UkmServiceTest, PurgeMidUpload) {
464 UkmService service(&prefs_, &client_); 491 UkmService service(&prefs_, &client_);
492 TestRecordingHelper recorder(&service);
465 EXPECT_EQ(GetPersistedLogCount(), 0); 493 EXPECT_EQ(GetPersistedLogCount(), 0);
466 service.Initialize(); 494 service.Initialize();
467 task_runner_->RunUntilIdle(); 495 task_runner_->RunUntilIdle();
468 service.EnableRecording(); 496 service.EnableRecording();
469 service.EnableReporting(); 497 service.EnableReporting();
470 auto id = UkmService::GetNewSourceID(); 498 auto id = UkmRecorder::GetNewSourceID();
471 service.UpdateSourceURL(id, GURL("https://google.com/foobar1")); 499 recorder.UpdateSourceURL(id, GURL("https://google.com/foobar1"));
472 // Should init, generate a log, and start an upload. 500 // Should init, generate a log, and start an upload.
473 task_runner_->RunPendingTasks(); 501 task_runner_->RunPendingTasks();
474 EXPECT_TRUE(client_.uploader()->is_uploading()); 502 EXPECT_TRUE(client_.uploader()->is_uploading());
475 // Purge should delete all logs, including the one being sent. 503 // Purge should delete all logs, including the one being sent.
476 service.Purge(); 504 service.Purge();
477 // Upload succeeds after logs was deleted. 505 // Upload succeeds after logs was deleted.
478 client_.uploader()->CompleteUpload(200); 506 client_.uploader()->CompleteUpload(200);
479 EXPECT_EQ(GetPersistedLogCount(), 0); 507 EXPECT_EQ(GetPersistedLogCount(), 0);
480 EXPECT_FALSE(client_.uploader()->is_uploading()); 508 EXPECT_FALSE(client_.uploader()->is_uploading());
481 } 509 }
482 510
483 TEST_F(UkmServiceTest, WhitelistEntryTest) { 511 TEST_F(UkmServiceTest, WhitelistEntryTest) {
484 base::FieldTrialList field_trial_list(nullptr /* entropy_provider */); 512 base::FieldTrialList field_trial_list(nullptr /* entropy_provider */);
485 // Testing two whitelisted Entries. 513 // Testing two whitelisted Entries.
486 ScopedUkmFeatureParams params(base::FeatureList::OVERRIDE_ENABLE_FEATURE, 514 ScopedUkmFeatureParams params(base::FeatureList::OVERRIDE_ENABLE_FEATURE,
487 {{"WhitelistEntries", "EntryA,EntryB"}}); 515 {{"WhitelistEntries", "EntryA,EntryB"}});
488 516
489 ClearPrefs(); 517 ClearPrefs();
490 UkmService service(&prefs_, &client_); 518 UkmService service(&prefs_, &client_);
519 TestRecordingHelper recorder(&service);
491 EXPECT_EQ(0, GetPersistedLogCount()); 520 EXPECT_EQ(0, GetPersistedLogCount());
492 service.Initialize(); 521 service.Initialize();
493 task_runner_->RunUntilIdle(); 522 task_runner_->RunUntilIdle();
494 service.EnableRecording(); 523 service.EnableRecording();
495 service.EnableReporting(); 524 service.EnableReporting();
496 525
497 auto id = UkmService::GetNewSourceID(); 526 auto id = UkmRecorder::GetNewSourceID();
498 service.UpdateSourceURL(id, GURL("https://google.com/foobar1")); 527 recorder.UpdateSourceURL(id, GURL("https://google.com/foobar1"));
499 528
500 { 529 {
501 std::unique_ptr<UkmEntryBuilder> builder = 530 std::unique_ptr<UkmEntryBuilder> builder =
502 service.GetEntryBuilder(id, "EntryA"); 531 service.GetEntryBuilder(id, "EntryA");
503 builder->AddMetric("MetricA", 300); 532 builder->AddMetric("MetricA", 300);
504 } 533 }
505 { 534 {
506 std::unique_ptr<UkmEntryBuilder> builder = 535 std::unique_ptr<UkmEntryBuilder> builder =
507 service.GetEntryBuilder(id, "EntryB"); 536 service.GetEntryBuilder(id, "EntryB");
508 builder->AddMetric("MetricB", 400); 537 builder->AddMetric("MetricB", 400);
(...skipping 17 matching lines...) Expand all
526 EXPECT_EQ(id, proto_entry_a.source_id()); 555 EXPECT_EQ(id, proto_entry_a.source_id());
527 EXPECT_EQ(base::HashMetricName("EntryA"), proto_entry_a.event_hash()); 556 EXPECT_EQ(base::HashMetricName("EntryA"), proto_entry_a.event_hash());
528 557
529 const Entry& proto_entry_b = proto_report.entries(1); 558 const Entry& proto_entry_b = proto_report.entries(1);
530 EXPECT_EQ(id, proto_entry_b.source_id()); 559 EXPECT_EQ(id, proto_entry_b.source_id());
531 EXPECT_EQ(base::HashMetricName("EntryB"), proto_entry_b.event_hash()); 560 EXPECT_EQ(base::HashMetricName("EntryB"), proto_entry_b.event_hash());
532 } 561 }
533 562
534 TEST_F(UkmServiceTest, SourceURLLength) { 563 TEST_F(UkmServiceTest, SourceURLLength) {
535 UkmService service(&prefs_, &client_); 564 UkmService service(&prefs_, &client_);
565 TestRecordingHelper recorder(&service);
536 EXPECT_EQ(0, GetPersistedLogCount()); 566 EXPECT_EQ(0, GetPersistedLogCount());
537 service.Initialize(); 567 service.Initialize();
538 task_runner_->RunUntilIdle(); 568 task_runner_->RunUntilIdle();
539 service.EnableRecording(); 569 service.EnableRecording();
540 service.EnableReporting(); 570 service.EnableReporting();
541 571
542 auto id = UkmService::GetNewSourceID(); 572 auto id = UkmRecorder::GetNewSourceID();
543 573
544 // This URL is too long to be recorded fully. 574 // This URL is too long to be recorded fully.
545 const std::string long_string = "https://" + std::string(10000, 'a'); 575 const std::string long_string = "https://" + std::string(10000, 'a');
546 service.UpdateSourceURL(id, GURL(long_string)); 576 recorder.UpdateSourceURL(id, GURL(long_string));
547 577
548 service.Flush(); 578 service.Flush();
549 EXPECT_EQ(1, GetPersistedLogCount()); 579 EXPECT_EQ(1, GetPersistedLogCount());
550 580
551 auto proto_report = GetPersistedReport(); 581 auto proto_report = GetPersistedReport();
552 ASSERT_EQ(1, proto_report.sources_size()); 582 ASSERT_EQ(1, proto_report.sources_size());
553 const Source& proto_source = proto_report.sources(0); 583 const Source& proto_source = proto_report.sources(0);
554 EXPECT_EQ("URLTooLong", proto_source.url()); 584 EXPECT_EQ("URLTooLong", proto_source.url());
555 } 585 }
556 586
557 } // namespace ukm 587 } // namespace ukm
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698