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