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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
content / browser / renderer_host / visible_time_request_trigger_unittest.cc [blame]
// Copyright 2021 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/renderer_host/visible_time_request_trigger.h"
#include <tuple>
#include <utility>
#include "base/time/time.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/public/mojom/widget/record_content_to_visible_time_request.mojom.h"
namespace content {
namespace {
class VisibleTimeRequestTriggerTest : public testing::Test {
protected:
using RecordContentToVisibleTimeRequest =
blink::mojom::RecordContentToVisibleTimeRequest;
using RecordContentToVisibleTimeRequestPtr =
blink::mojom::RecordContentToVisibleTimeRequestPtr;
// Converts a base::TimeDelta into a base::TimeTicks value suitable for
// storing in the `event_start_time` field of a
// RecordContentToVisibleTimeRequest.
static base::TimeTicks StartTimeFromDelta(base::TimeDelta delta) {
return base::TimeTicks() + delta;
}
// Returns a request with the given `start_time`, which is given as a delta
// from 0 since callers don't have an easy way to create base::TimeTicks
// directly.
static RecordContentToVisibleTimeRequestPtr CreateRequestPtr(
base::TimeDelta start_time,
bool destination_is_loaded = false,
bool show_reason_tab_switching = false,
bool show_reason_bfcache_restore = false,
bool show_reason_unfolding = false) {
return RecordContentToVisibleTimeRequest::New(
StartTimeFromDelta(start_time), destination_is_loaded,
show_reason_tab_switching, show_reason_bfcache_restore,
show_reason_unfolding);
}
// Expects that all fields of `request` and `expected` match.
static void ExpectEqualRequests(
RecordContentToVisibleTimeRequestPtr request,
const RecordContentToVisibleTimeRequest& expected) {
ASSERT_TRUE(request);
EXPECT_EQ(request->event_start_time, expected.event_start_time);
EXPECT_EQ(request->destination_is_loaded, expected.destination_is_loaded);
EXPECT_EQ(request->show_reason_tab_switching,
expected.show_reason_tab_switching);
EXPECT_EQ(request->show_reason_bfcache_restore,
expected.show_reason_bfcache_restore);
}
};
TEST_F(VisibleTimeRequestTriggerTest, MergeEmpty) {
EXPECT_TRUE(
VisibleTimeRequestTrigger::ConsumeAndMergeRequests(nullptr, nullptr)
.is_null());
ExpectEqualRequests(VisibleTimeRequestTrigger::ConsumeAndMergeRequests(
nullptr, RecordContentToVisibleTimeRequest::New()),
{});
ExpectEqualRequests(VisibleTimeRequestTrigger::ConsumeAndMergeRequests(
RecordContentToVisibleTimeRequest::New(), nullptr),
{});
ExpectEqualRequests(VisibleTimeRequestTrigger::ConsumeAndMergeRequests(
RecordContentToVisibleTimeRequest::New(),
RecordContentToVisibleTimeRequest::New()),
{});
}
TEST_F(VisibleTimeRequestTriggerTest, MergeStartTimes) {
// Overflowing can make a clock wrap around to negatives.
constexpr auto kNegative = base::TimeDelta::Min();
constexpr auto kZero = base::TimeDelta();
auto expect_merged_start_time = [](base::TimeDelta left_time,
base::TimeDelta right_time,
base::TimeDelta expected_time) {
SCOPED_TRACE(::testing::Message() << "Merging requests with start times "
<< left_time << " and " << right_time);
RecordContentToVisibleTimeRequestPtr merged_request =
VisibleTimeRequestTrigger::ConsumeAndMergeRequests(
CreateRequestPtr(left_time), CreateRequestPtr(right_time));
ASSERT_TRUE(merged_request);
EXPECT_EQ(merged_request->event_start_time,
StartTimeFromDelta(expected_time));
};
// Comparisons with negative times.
expect_merged_start_time(kNegative, kNegative, kNegative);
expect_merged_start_time(kNegative, kZero, kNegative);
expect_merged_start_time(kNegative, base::Seconds(10), kNegative);
expect_merged_start_time(kNegative, base::TimeDelta::Max(), kNegative);
// Comparisons with time 0.
expect_merged_start_time(kZero, kNegative, kNegative);
expect_merged_start_time(kZero, kZero, kZero);
expect_merged_start_time(kZero, base::Seconds(10), kZero);
expect_merged_start_time(kZero, base::TimeDelta::Max(), kZero);
// Comparisons between a mid-range time and boundary conditions.
expect_merged_start_time(base::Seconds(10), kNegative, kNegative);
expect_merged_start_time(base::Seconds(10), kZero, kZero);
expect_merged_start_time(base::Seconds(10), base::TimeDelta::Max(),
base::Seconds(10));
// Comparisons between two mid-range times.
expect_merged_start_time(base::Seconds(10), base::Seconds(10),
base::Seconds(10));
expect_merged_start_time(base::Seconds(10), base::Seconds(100),
base::Seconds(10));
expect_merged_start_time(base::Seconds(100), base::Seconds(10),
base::Seconds(10));
// Comparisons with the max time.
expect_merged_start_time(base::TimeDelta::Max(), kNegative, kNegative);
expect_merged_start_time(base::TimeDelta::Max(), kZero, kZero);
expect_merged_start_time(base::TimeDelta::Max(), base::Seconds(10),
base::Seconds(10));
expect_merged_start_time(base::TimeDelta::Max(), base::TimeDelta::Max(),
base::TimeDelta::Max());
}
TEST_F(VisibleTimeRequestTriggerTest, MergeRequests) {
// Iterate over all possible combinations of request parameters. Tuple
// contains `destination_is_loaded`, `show_reason_tab_switching`,
// `show_reason_bfcache_restore`.
using ParamTuple = std::tuple<bool, bool, bool>;
constexpr ParamTuple kRequestParams[] = {
// show_reason_tab_switching = true
ParamTuple(false, true, false),
ParamTuple(true, true, false),
ParamTuple(false, true, true),
ParamTuple(true, true, true),
// show_reason_tab_switching = false
ParamTuple(false, false, false),
ParamTuple(false, false, true),
};
auto request_with_params = [](const ParamTuple& params) {
return CreateRequestPtr(base::TimeDelta(), std::get<0>(params),
std::get<1>(params), std::get<2>(params));
};
auto request_with_union_of_params = [](const ParamTuple& params1,
const ParamTuple& params2) {
const bool destination_is_loaded =
std::get<0>(params1) || std::get<0>(params2);
const bool show_reason_tab_switching =
std::get<1>(params1) || std::get<1>(params2);
const bool show_reason_bfcache_restore =
std::get<2>(params1) || std::get<2>(params2);
return CreateRequestPtr(base::TimeDelta(), destination_is_loaded,
show_reason_tab_switching,
show_reason_bfcache_restore);
};
for (const ParamTuple& params : kRequestParams) {
SCOPED_TRACE(::testing::Message()
<< "With params " << std::get<0>(params) << ","
<< std::get<1>(params) << "," << std::get<2>(params));
{
// Check that these fields are set in the result if they're set in either
// or both of the requests.
const auto expected = request_with_params(params);
ExpectEqualRequests(VisibleTimeRequestTrigger::ConsumeAndMergeRequests(
RecordContentToVisibleTimeRequest::New(),
request_with_params(params)),
*expected);
ExpectEqualRequests(VisibleTimeRequestTrigger::ConsumeAndMergeRequests(
request_with_params(params),
RecordContentToVisibleTimeRequest::New()),
*expected);
ExpectEqualRequests(
VisibleTimeRequestTrigger::ConsumeAndMergeRequests(
request_with_params(params), request_with_params(params)),
*expected);
}
// Check that when these fields are combined with another set of fields,
// all fields are set in the result.
for (const ParamTuple& params2 : kRequestParams) {
SCOPED_TRACE(::testing::Message()
<< "Combining with params " << std::get<0>(params2) << ","
<< std::get<1>(params2) << "," << std::get<2>(params2));
const auto expected = request_with_union_of_params(params, params2);
ExpectEqualRequests(
VisibleTimeRequestTrigger::ConsumeAndMergeRequests(
request_with_params(params), request_with_params(params2)),
*expected);
ExpectEqualRequests(
VisibleTimeRequestTrigger::ConsumeAndMergeRequests(
request_with_params(params2), request_with_params(params)),
*expected);
}
}
}
TEST_F(VisibleTimeRequestTriggerTest, UpdateAndTakeRequest) {
VisibleTimeRequestTrigger trigger;
EXPECT_TRUE(trigger.TakeRequest().is_null());
// Calling Update then Take should clear the stored request.
{
const auto expected =
CreateRequestPtr(base::Seconds(1), /*destination_is_loaded=*/false,
/*show_reason_tab_switching=*/true);
trigger.UpdateRequest(StartTimeFromDelta(base::Seconds(1)),
/*destination_is_loaded=*/false,
/*show_reason_tab_switching=*/true,
/*show_reason_bfcache_restore=*/false);
ExpectEqualRequests(trigger.TakeRequest(), *expected);
EXPECT_TRUE(trigger.TakeRequest().is_null());
}
// Calling Update twice should merge the requests.
{
const auto expected =
CreateRequestPtr(base::Seconds(2), /*destination_is_loaded=*/true,
/*show_reason_tab_switching=*/true,
/*show_reason_bfcache_restore=*/true);
trigger.UpdateRequest(StartTimeFromDelta(base::Seconds(2)),
/*destination_is_loaded=*/true,
/*show_reason_tab_switching=*/true,
/*show_reason_bfcache_restore=*/false);
trigger.UpdateRequest(StartTimeFromDelta(base::Seconds(3)),
/*destination_is_loaded=*/false,
/*show_reason_tab_switching=*/false,
/*show_reason_bfcache_restore=*/true);
ExpectEqualRequests(trigger.TakeRequest(), *expected);
EXPECT_TRUE(trigger.TakeRequest().is_null());
}
}
} // namespace
} // namespace content