ukm_test_helper.cc 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. // Copyright 2020 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "components/ukm/ukm_test_helper.h"
  5. #include <algorithm>
  6. #include <string>
  7. #include "base/containers/contains.h"
  8. #include "base/feature_list.h"
  9. #include "base/run_loop.h"
  10. #include "components/metrics/log_decoder.h"
  11. #include "components/metrics/unsent_log_store.h"
  12. namespace ukm {
  13. UkmTestHelper::UkmTestHelper(UkmService* ukm_service)
  14. : ukm_service_(ukm_service) {}
  15. bool UkmTestHelper::IsExtensionRecordingEnabled() const {
  16. return ukm_service_ ? ukm_service_->extensions_enabled_ : false;
  17. }
  18. bool UkmTestHelper::IsRecordingEnabled() const {
  19. return ukm_service_ ? ukm_service_->recording_enabled_ : false;
  20. }
  21. bool UkmTestHelper::IsReportUserNoisedUserBirthYearAndGenderEnabled() {
  22. return base::FeatureList::IsEnabled(
  23. ukm::UkmService::kReportUserNoisedUserBirthYearAndGender);
  24. }
  25. uint64_t UkmTestHelper::GetClientId() {
  26. return ukm_service_->client_id_;
  27. }
  28. std::unique_ptr<Report> UkmTestHelper::GetUkmReport() {
  29. if (!HasUnsentLogs())
  30. return nullptr;
  31. metrics::UnsentLogStore* log_store =
  32. ukm_service_->reporting_service_.ukm_log_store();
  33. if (log_store->has_staged_log()) {
  34. // For testing purposes, we examine the content of a staged log without
  35. // ever sending the log, so discard any previously staged log.
  36. log_store->DiscardStagedLog();
  37. }
  38. log_store->StageNextLog();
  39. if (!log_store->has_staged_log())
  40. return nullptr;
  41. std::unique_ptr<ukm::Report> report = std::make_unique<ukm::Report>();
  42. if (!metrics::DecodeLogDataToProto(log_store->staged_log(), report.get()))
  43. return nullptr;
  44. return report;
  45. }
  46. UkmSource* UkmTestHelper::GetSource(SourceId source_id) {
  47. if (!ukm_service_)
  48. return nullptr;
  49. auto it = ukm_service_->sources().find(source_id);
  50. return it == ukm_service_->sources().end() ? nullptr : it->second.get();
  51. }
  52. bool UkmTestHelper::HasSource(SourceId source_id) {
  53. return ukm_service_ && base::Contains(ukm_service_->sources(), source_id);
  54. }
  55. bool UkmTestHelper::IsSourceObsolete(SourceId source_id) {
  56. return ukm_service_ &&
  57. base::Contains(ukm_service_->recordings_.obsolete_source_ids,
  58. source_id);
  59. }
  60. void UkmTestHelper::RecordSourceForTesting(SourceId source_id) {
  61. if (ukm_service_)
  62. ukm_service_->UpdateSourceURL(source_id, GURL("http://example.com"));
  63. }
  64. void UkmTestHelper::BuildAndStoreLog() {
  65. // Wait for initialization to complete before flushing.
  66. base::RunLoop run_loop;
  67. ukm_service_->SetInitializationCompleteCallbackForTesting(
  68. run_loop.QuitClosure());
  69. run_loop.Run();
  70. ukm_service_->Flush();
  71. }
  72. bool UkmTestHelper::HasUnsentLogs() {
  73. return ukm_service_ &&
  74. ukm_service_->reporting_service_.ukm_log_store()->has_unsent_logs();
  75. }
  76. } // namespace ukm