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.
5 #ifndef COMPONENTS_BROWSING_TOPICS_TEST_UTIL_H_
6 #define COMPONENTS_BROWSING_TOPICS_TEST_UTIL_H_
8 #include "base/containers/queue.h"
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"
22 class TestAutoSetUkmRecorder;
25 namespace browsing_topics {
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)) {}
37 absl::optional<ApiAccessResult> failure_reason;
38 CandidateTopic topic0;
39 CandidateTopic topic1;
40 CandidateTopic topic2;
43 // Parse the `BrowsingTopics_DocumentBrowsingTopicsApiResult2` metrics.
44 std::vector<ApiResultUkmMetrics> ReadApiResultUkmMetrics(
45 const ukm::TestAutoSetUkmRecorder& ukm_recorder);
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,
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 {
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,
63 const base::circular_deque<EpochTopics>& epochs,
64 CalculateCompletedCallback callback,
65 base::queue<uint64_t> rand_uint64_queue);
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,
73 CalculateCompletedCallback callback,
74 EpochTopics mock_result,
75 base::TimeDelta mock_result_delay);
77 ~TesterBrowsingTopicsCalculator() override;
79 TesterBrowsingTopicsCalculator(const TesterBrowsingTopicsCalculator&) =
81 TesterBrowsingTopicsCalculator& operator=(
82 const TesterBrowsingTopicsCalculator&) = delete;
83 TesterBrowsingTopicsCalculator(TesterBrowsingTopicsCalculator&&) = delete;
84 TesterBrowsingTopicsCalculator& operator=(TesterBrowsingTopicsCalculator&&) =
87 // Pop and return the next number in `rand_uint64_queue_`. Precondition:
88 // `rand_uint64_queue_` is not empty.
89 uint64_t GenerateRandUint64() override;
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;
97 void MockDelayReached();
99 base::queue<uint64_t> rand_uint64_queue_;
101 bool use_mock_result_ = false;
102 EpochTopics mock_result_{base::Time()};
103 base::TimeDelta mock_result_delay_;
104 CalculateCompletedCallback finish_callback_;
106 base::WeakPtrFactory<TesterBrowsingTopicsCalculator> weak_ptr_factory_{this};
109 class MockBrowsingTopicsService : public BrowsingTopicsService {
111 MockBrowsingTopicsService();
112 ~MockBrowsingTopicsService() override;
117 content::RenderFrameHost*,
121 std::vector<blink::mojom::EpochTopicPtr>&),
123 MOCK_METHOD(int, NumVersionsInEpochs, (const url::Origin&), (const override));
125 GetBrowsingTopicsStateForWebUi,
126 (bool, mojom::PageHandler::GetBrowsingTopicsStateCallback),
128 MOCK_METHOD(std::vector<privacy_sandbox::CanonicalTopic>,
129 GetTopTopicsForDisplay,
132 MOCK_METHOD(Annotator*, GetAnnotator, (), (override));
135 (const privacy_sandbox::CanonicalTopic&),
137 MOCK_METHOD(void, ClearTopicsDataForOrigin, (const url::Origin&), (override));
138 MOCK_METHOD(void, ClearAllTopicsData, (), (override));
141 // An Annotator to use in tests, does not run a model nor use background tasks.
142 class TestAnnotator : public Annotator {
145 ~TestAnnotator() override;
147 // Used in calls to |BatchAnnotate|.
149 const std::map<std::string, std::set<int32_t>>& annotations);
151 // Used in calls to |GetBrowsingTopicsModelInfo|.
153 const absl::optional<optimization_guide::ModelInfo>& model_info);
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);
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()
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_;
173 } // namespace browsing_topics
175 #endif // COMPONENTS_BROWSING_TOPICS_TEST_UTIL_H_