1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
content / browser / interest_group / test_interest_group_observer.cc [blame]
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "content/browser/interest_group/test_interest_group_observer.h"
#include <memory>
#include <string>
#include <tuple>
#include <vector>
#include "base/run_loop.h"
#include "base/strings/string_number_conversions.h"
#include "base/time/time.h"
#include "content/browser/interest_group/interest_group_manager_impl.h"
#include "content/common/content_export.h"
#include "testing/gmock/include/gmock/gmock-matchers.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "url/origin.h"
namespace content {
TestInterestGroupObserver::Entry::Entry(
std::string devtools_auction_id,
InterestGroupManagerImpl::InterestGroupObserver::AccessType access_type,
url::Origin owner_origin,
std::string ig_name,
std::optional<double> bid,
std::optional<std::string> bid_currency,
std::optional<url::Origin> component_seller_origin)
: devtools_auction_id(std::move(devtools_auction_id)),
access_type(access_type),
owner_origin(std::move(owner_origin)),
ig_name(std::move(ig_name)),
bid(std::move(bid)),
bid_currency(std::move(bid_currency)),
component_seller_origin(std::move(component_seller_origin)) {}
TestInterestGroupObserver::Entry::Entry(const Entry&) = default;
TestInterestGroupObserver::Entry::~Entry() = default;
TestInterestGroupObserver::Entry& TestInterestGroupObserver::Entry::operator=(
const Entry&) = default;
bool TestInterestGroupObserver::Entry::operator==(const Entry&) const = default;
TestInterestGroupObserver::TestInterestGroupObserver() = default;
TestInterestGroupObserver::~TestInterestGroupObserver() = default;
void TestInterestGroupObserver::OnInterestGroupAccessed(
base::optional_ref<const std::string> devtools_auction_id,
base::Time access_time,
InterestGroupManagerImpl::InterestGroupObserver::AccessType type,
const url::Origin& owner_origin,
const std::string& name,
base::optional_ref<const url::Origin> component_seller_origin,
std::optional<double> bid,
base::optional_ref<const std::string> bid_currency) {
// Hide the randomness of auction IDs for easier testing by renaming them to
// sequential integers (and "global" if not set)
std::string normalized_auction_id = "global";
if (devtools_auction_id.has_value()) {
auto it = seen_auction_ids_.find(*devtools_auction_id);
if (it != seen_auction_ids_.end()) {
normalized_auction_id = it->second;
} else {
normalized_auction_id =
base::NumberToString(seen_auction_ids_.size() + 1);
seen_auction_ids_[*devtools_auction_id] = normalized_auction_id;
}
}
accesses_.emplace_back(normalized_auction_id, type, owner_origin, name, bid,
bid_currency.CopyAsOptional(),
component_seller_origin.CopyAsOptional());
if (run_loop_ && accesses_.size() >= expected_.size()) {
run_loop_->Quit();
}
}
void TestInterestGroupObserver::WaitForAccesses(
const std::vector<Entry>& expected) {
DCHECK(!run_loop_);
if (accesses_.size() < expected.size()) {
run_loop_ = std::make_unique<base::RunLoop>();
expected_ = expected;
run_loop_->Run();
run_loop_.reset();
}
EXPECT_THAT(accesses_, ::testing::UnorderedElementsAreArray(expected));
// Clear accesses so can be reused.
accesses_.clear();
}
void TestInterestGroupObserver::WaitForAccessesInOrder(
const std::vector<Entry>& expected) {
DCHECK(!run_loop_);
if (accesses_.size() < expected.size()) {
run_loop_ = std::make_unique<base::RunLoop>();
expected_ = expected;
run_loop_->Run();
run_loop_.reset();
}
EXPECT_THAT(accesses_, ::testing::ElementsAreArray(expected));
// Clear accesses so can be reused.
accesses_.clear();
}
void PrintTo(const TestInterestGroupObserver::Entry& e, std::ostream* os) {
*os << "(";
*os << e.devtools_auction_id << ", ";
*os << e.access_type << ", ";
*os << e.owner_origin.Serialize() << ", ";
*os << e.ig_name << ", ";
if (e.bid.has_value()) {
*os << e.bid.value() << ", ";
} else {
*os << "(no bid), ";
}
if (e.bid_currency.has_value()) {
*os << e.bid_currency.value() << ", ";
} else {
*os << "(no bid_currency), ";
}
if (e.component_seller_origin.has_value()) {
*os << e.component_seller_origin->Serialize();
} else {
*os << "(no component_seller_origin)";
}
*os << ")";
}
} // namespace content