test_ukm_recorder.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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
  3. // found in the LICENSE file.
  4. #ifndef COMPONENTS_UKM_TEST_UKM_RECORDER_H_
  5. #define COMPONENTS_UKM_TEST_UKM_RECORDER_H_
  6. #include <stddef.h>
  7. #include <map>
  8. #include <memory>
  9. #include <set>
  10. #include <utility>
  11. #include <vector>
  12. #include "base/compiler_specific.h"
  13. #include "base/memory/weak_ptr.h"
  14. #include "components/ukm/ukm_recorder_impl.h"
  15. #include "services/metrics/public/cpp/ukm_recorder.h"
  16. #include "services/metrics/public/mojom/ukm_interface.mojom.h"
  17. #include "url/gurl.h"
  18. namespace ukm {
  19. // Wraps an UkmRecorder with additional accessors used for testing.
  20. class TestUkmRecorder : public UkmRecorderImpl {
  21. public:
  22. using HumanReadableUkmMetrics = std::map<std::string, int64_t>;
  23. struct HumanReadableUkmEntry {
  24. HumanReadableUkmEntry();
  25. HumanReadableUkmEntry(ukm::SourceId source_id,
  26. HumanReadableUkmMetrics ukm_metrics);
  27. ~HumanReadableUkmEntry();
  28. HumanReadableUkmEntry(const HumanReadableUkmEntry&);
  29. bool operator==(const HumanReadableUkmEntry& other) const;
  30. ukm::SourceId source_id = kInvalidSourceId;
  31. HumanReadableUkmMetrics metrics;
  32. };
  33. TestUkmRecorder();
  34. TestUkmRecorder(const TestUkmRecorder&) = delete;
  35. TestUkmRecorder& operator=(const TestUkmRecorder&) = delete;
  36. ~TestUkmRecorder() override;
  37. bool ShouldRestrictToWhitelistedSourceIds() const override;
  38. void AddEntry(mojom::UkmEntryPtr entry) override;
  39. size_t sources_count() const { return sources().size(); }
  40. size_t entries_count() const { return entries().size(); }
  41. using UkmRecorderImpl::UpdateSourceURL;
  42. using UkmRecorderImpl::RecordOtherURL;
  43. // Gets all recorded UkmSource data.
  44. const std::map<ukm::SourceId, std::unique_ptr<UkmSource>>& GetSources()
  45. const {
  46. return sources();
  47. }
  48. // Gets UkmSource data for a single SourceId. Returns null if not found.
  49. const UkmSource* GetSourceForSourceId(ukm::SourceId source_id) const;
  50. // Gets DocumentCreatedEntry for a single SourceId. Returns null if not found.
  51. const ukm::mojom::UkmEntry* GetDocumentCreatedEntryForSourceId(
  52. ukm::SourceId source_id) const;
  53. // Sets a callback that will be called when recording an entry for entry name.
  54. void SetOnAddEntryCallback(base::StringPiece entry_name,
  55. base::RepeatingClosure on_add_entry);
  56. // Gets all of the entries recorded for entry name.
  57. std::vector<const mojom::UkmEntry*> GetEntriesByName(
  58. base::StringPiece entry_name) const;
  59. // Gets the data for all entries with given entry name, merged to one entry
  60. // for each source id. Intended for singular="true" metrics.
  61. std::map<ukm::SourceId, mojom::UkmEntryPtr> GetMergedEntriesByName(
  62. base::StringPiece entry_name) const;
  63. // Checks if an entry is associated with a url.
  64. void ExpectEntrySourceHasUrl(const mojom::UkmEntry* entry,
  65. const GURL& url) const;
  66. // Expects the value of a metric from an entry.
  67. static void ExpectEntryMetric(const mojom::UkmEntry* entry,
  68. base::StringPiece metric_name,
  69. int64_t expected_value);
  70. // Checks if an entry contains a specific metric.
  71. static bool EntryHasMetric(const mojom::UkmEntry* entry,
  72. base::StringPiece metric_name);
  73. // Gets the value of a metric from an entry. Returns nullptr if the metric is
  74. // not found.
  75. static const int64_t* GetEntryMetric(const mojom::UkmEntry* entry,
  76. base::StringPiece metric_name);
  77. // A test helper returning all metrics for all entries with a given name in a
  78. // human-readable form, allowing to write clearer test expectations.
  79. std::vector<HumanReadableUkmMetrics> GetMetrics(
  80. std::string entry_name,
  81. const std::vector<std::string>& metric_names) const;
  82. // A test helper returning all entries for a given name in a human-readable
  83. // form, allowing to write clearer test expectations.
  84. std::vector<HumanReadableUkmEntry> GetEntries(
  85. std::string entry_name,
  86. const std::vector<std::string>& metric_names) const;
  87. // A test helper returning all logged metrics with the given |metric_name| for
  88. // the entry with the given |entry_name|, filtered to remove any empty
  89. // HumanReadableUkmEntry results.
  90. std::vector<HumanReadableUkmMetrics> FilteredHumanReadableMetricForEntry(
  91. const std::string& entry_name,
  92. const std::string& metric_name) const;
  93. private:
  94. uint64_t entry_hash_to_wait_for_ = 0;
  95. base::RepeatingClosure on_add_entry_;
  96. };
  97. // Similar to a TestUkmRecorder, but also sets itself as the global UkmRecorder
  98. // on construction, and unsets itself on destruction.
  99. class TestAutoSetUkmRecorder : public TestUkmRecorder {
  100. public:
  101. TestAutoSetUkmRecorder();
  102. ~TestAutoSetUkmRecorder() override;
  103. private:
  104. base::WeakPtrFactory<TestAutoSetUkmRecorder> self_ptr_factory_{this};
  105. };
  106. } // namespace ukm
  107. #endif // COMPONENTS_UKM_TEST_UKM_RECORDER_H_