Fix emulator build error
[platform/framework/web/chromium-efl.git] / components / browsing_topics / test_util.h
1 // Copyright 2022 The Chromium Authors
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_BROWSING_TOPICS_TEST_UTIL_H_
6 #define COMPONENTS_BROWSING_TOPICS_TEST_UTIL_H_
7
8 #include "base/containers/queue.h"
9
10 #include "base/callback_list.h"
11 #include "base/memory/weak_ptr.h"
12 #include "base/time/time.h"
13 #include "components/browsing_topics/annotator.h"
14 #include "components/browsing_topics/browsing_topics_calculator.h"
15 #include "components/browsing_topics/browsing_topics_service.h"
16 #include "components/browsing_topics/mojom/browsing_topics_internals.mojom.h"
17 #include "testing/gmock/include/gmock/gmock.h"
18 #include "third_party/abseil-cpp/absl/types/optional.h"
19 #include "third_party/blink/public/mojom/browsing_topics/browsing_topics.mojom.h"
20
21 namespace ukm {
22 class TestAutoSetUkmRecorder;
23 }  // namespace ukm
24
25 namespace browsing_topics {
26
27 struct ApiResultUkmMetrics {
28   ApiResultUkmMetrics(absl::optional<ApiAccessResult> failure_reason,
29                       CandidateTopic topic0,
30                       CandidateTopic topic1,
31                       CandidateTopic topic2)
32       : failure_reason(std::move(failure_reason)),
33         topic0(std::move(topic0)),
34         topic1(std::move(topic1)),
35         topic2(std::move(topic2)) {}
36
37   absl::optional<ApiAccessResult> failure_reason;
38   CandidateTopic topic0;
39   CandidateTopic topic1;
40   CandidateTopic topic2;
41 };
42
43 // Parse the `BrowsingTopics_DocumentBrowsingTopicsApiResult2` metrics.
44 std::vector<ApiResultUkmMetrics> ReadApiResultUkmMetrics(
45     const ukm::TestAutoSetUkmRecorder& ukm_recorder);
46
47 // Returns whether the URL entry is eligible in topics calculation.
48 // Precondition: the history visits contain exactly one matching URL.
49 bool BrowsingTopicsEligibleForURLVisit(history::HistoryService* history_service,
50                                        const GURL& url);
51
52 // A tester class that allows mocking the generated random numbers, or directly
53 // returning a mock result with a delay.
54 class TesterBrowsingTopicsCalculator : public BrowsingTopicsCalculator {
55  public:
56   // Initialize a regular `BrowsingTopicsCalculator` with an additional
57   // `rand_uint64_queue` member for generating random numbers.
58   TesterBrowsingTopicsCalculator(
59       privacy_sandbox::PrivacySandboxSettings* privacy_sandbox_settings,
60       history::HistoryService* history_service,
61       content::BrowsingTopicsSiteDataManager* site_data_manager,
62       Annotator* annotator,
63       const base::circular_deque<EpochTopics>& epochs,
64       CalculateCompletedCallback callback,
65       base::queue<uint64_t> rand_uint64_queue);
66
67   // Initialize a mock `BrowsingTopicsCalculator` (with mock result and delay).
68   TesterBrowsingTopicsCalculator(
69       privacy_sandbox::PrivacySandboxSettings* privacy_sandbox_settings,
70       history::HistoryService* history_service,
71       content::BrowsingTopicsSiteDataManager* site_data_manager,
72       Annotator* annotator,
73       CalculateCompletedCallback callback,
74       EpochTopics mock_result,
75       base::TimeDelta mock_result_delay);
76
77   ~TesterBrowsingTopicsCalculator() override;
78
79   TesterBrowsingTopicsCalculator(const TesterBrowsingTopicsCalculator&) =
80       delete;
81   TesterBrowsingTopicsCalculator& operator=(
82       const TesterBrowsingTopicsCalculator&) = delete;
83   TesterBrowsingTopicsCalculator(TesterBrowsingTopicsCalculator&&) = delete;
84   TesterBrowsingTopicsCalculator& operator=(TesterBrowsingTopicsCalculator&&) =
85       delete;
86
87   // Pop and return the next number in `rand_uint64_queue_`. Precondition:
88   // `rand_uint64_queue_` is not empty.
89   uint64_t GenerateRandUint64() override;
90
91   // If `use_mock_result_` is true, post a task with `mock_result_delay_` to
92   // directly invoke the `finish_callback_` with `mock_result_`; otherwise, use
93   // the default handling for `CheckCanCalculate`.
94   void CheckCanCalculate() override;
95
96  private:
97   void MockDelayReached();
98
99   base::queue<uint64_t> rand_uint64_queue_;
100
101   bool use_mock_result_ = false;
102   EpochTopics mock_result_{base::Time()};
103   base::TimeDelta mock_result_delay_;
104   CalculateCompletedCallback finish_callback_;
105
106   base::WeakPtrFactory<TesterBrowsingTopicsCalculator> weak_ptr_factory_{this};
107 };
108
109 class MockBrowsingTopicsService : public BrowsingTopicsService {
110  public:
111   MockBrowsingTopicsService();
112   ~MockBrowsingTopicsService() override;
113
114   MOCK_METHOD(bool,
115               HandleTopicsWebApi,
116               (const url::Origin&,
117                content::RenderFrameHost*,
118                ApiCallerSource,
119                bool,
120                bool,
121                std::vector<blink::mojom::EpochTopicPtr>&),
122               (override));
123   MOCK_METHOD(int, NumVersionsInEpochs, (const url::Origin&), (const override));
124   MOCK_METHOD(void,
125               GetBrowsingTopicsStateForWebUi,
126               (bool, mojom::PageHandler::GetBrowsingTopicsStateCallback),
127               (override));
128   MOCK_METHOD(std::vector<privacy_sandbox::CanonicalTopic>,
129               GetTopTopicsForDisplay,
130               (),
131               (const override));
132   MOCK_METHOD(Annotator*, GetAnnotator, (), (override));
133   MOCK_METHOD(void,
134               ClearTopic,
135               (const privacy_sandbox::CanonicalTopic&),
136               (override));
137   MOCK_METHOD(void, ClearTopicsDataForOrigin, (const url::Origin&), (override));
138   MOCK_METHOD(void, ClearAllTopicsData, (), (override));
139 };
140
141 // An Annotator to use in tests, does not run a model nor use background tasks.
142 class TestAnnotator : public Annotator {
143  public:
144   TestAnnotator();
145   ~TestAnnotator() override;
146
147   // Used in calls to |BatchAnnotate|.
148   void UseAnnotations(
149       const std::map<std::string, std::set<int32_t>>& annotations);
150
151   // Used in calls to |GetBrowsingTopicsModelInfo|.
152   void UseModelInfo(
153       const absl::optional<optimization_guide::ModelInfo>& model_info);
154
155   // If setting to true when it had been false, all callbacks that have been
156   // passed to |NotifyWhenModelAvailable| will be ran.
157   void SetModelAvailable(bool is_available);
158
159   // Annotator:
160   void BatchAnnotate(BatchAnnotationCallback callback,
161                      const std::vector<std::string>& inputs) override;
162   void NotifyWhenModelAvailable(base::OnceClosure callback) override;
163   absl::optional<optimization_guide::ModelInfo> GetBrowsingTopicsModelInfo()
164       const override;
165
166  private:
167   std::map<std::string, std::set<int32_t>> annotations_;
168   absl::optional<optimization_guide::ModelInfo> model_info_;
169   bool model_available_ = true;
170   base::OnceClosureList model_available_callbacks_;
171 };
172
173 }  // namespace browsing_topics
174
175 #endif  // COMPONENTS_BROWSING_TOPICS_TEST_UTIL_H_