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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
media / gpu / android / surface_chooser_helper_unittest.cc [blame]
// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "media/gpu/android/surface_chooser_helper.h"
#include <stdint.h>
#include <memory>
#include "base/functional/bind.h"
#include "base/memory/raw_ptr.h"
#include "base/test/simple_test_tick_clock.h"
#include "media/gpu/android/mock_android_video_surface_chooser.h"
#include "media/gpu/android/mock_promotion_hint_aggregator.h"
#include "testing/gtest/include/gtest/gtest.h"
using testing::_;
using testing::AtLeast;
namespace media {
// Unit tests for PromotionHintAggregatorImplTest
class SurfaceChooserHelperTest : public testing::Test {
public:
~SurfaceChooserHelperTest() override {}
void SetUp() override {
// Create a default helper.
ReplaceHelper(false, false);
}
void TearDown() override {}
void ReplaceHelper(bool is_overlay_required,
bool promote_secure_only,
bool always_use_texture_owner = false) {
// Advance the clock so that time 0 isn't recent.
tick_clock_.Advance(base::Seconds(10000));
std::unique_ptr<MockAndroidVideoSurfaceChooser> chooser =
std::make_unique<MockAndroidVideoSurfaceChooser>();
chooser_ = chooser.get();
std::unique_ptr<MockPromotionHintAggregator> aggregator =
std::make_unique<MockPromotionHintAggregator>();
aggregator_ = aggregator.get();
helper_ = std::make_unique<SurfaceChooserHelper>(
std::move(chooser), is_overlay_required, promote_secure_only,
always_use_texture_owner, std::move(aggregator), &tick_clock_);
}
// Convenience function.
void UpdateChooserState() {
EXPECT_CALL(*chooser_, MockUpdateState());
helper_->UpdateChooserState(std::optional<AndroidOverlayFactoryCB>());
}
base::SimpleTestTickClock tick_clock_;
raw_ptr<MockPromotionHintAggregator> aggregator_ = nullptr;
raw_ptr<MockAndroidVideoSurfaceChooser> chooser_ = nullptr;
std::unique_ptr<SurfaceChooserHelper> helper_;
};
TEST_F(SurfaceChooserHelperTest, SetIsFullscreen) {
// Entering fullscreen should expect relayout.
helper_->SetIsFullscreen(true);
UpdateChooserState();
ASSERT_TRUE(chooser_->current_state_.is_fullscreen);
ASSERT_TRUE(chooser_->current_state_.is_expecting_relayout);
// Exiting fullscreen should not reset the expecting layout flag.
helper_->SetIsFullscreen(false);
UpdateChooserState();
ASSERT_FALSE(chooser_->current_state_.is_fullscreen);
// We don't really care if it sets expecting_relayout, clears it, or not.
}
TEST_F(SurfaceChooserHelperTest, SetVideoRotation) {
// VideoRotation should be forwarded to the chooser.
helper_->SetVideoRotation(VIDEO_ROTATION_90);
UpdateChooserState();
ASSERT_EQ(chooser_->current_state_.video_rotation, VIDEO_ROTATION_90);
}
TEST_F(SurfaceChooserHelperTest, SetIsPersistentVideo) {
helper_->SetIsPersistentVideo(true);
UpdateChooserState();
ASSERT_TRUE(chooser_->current_state_.is_persistent_video);
helper_->SetIsPersistentVideo(false);
UpdateChooserState();
ASSERT_FALSE(chooser_->current_state_.is_persistent_video);
}
TEST_F(SurfaceChooserHelperTest, SetIsOverlayRequired) {
// The default helper was created without |is_required|, so verify that.
UpdateChooserState();
ASSERT_FALSE(chooser_->current_state_.is_required);
ReplaceHelper(true, false);
UpdateChooserState();
ASSERT_TRUE(chooser_->current_state_.is_required);
}
TEST_F(SurfaceChooserHelperTest, SetInsecureSurface) {
helper_->SetSecureSurfaceMode(
SurfaceChooserHelper::SecureSurfaceMode::kInsecure);
UpdateChooserState();
ASSERT_FALSE(chooser_->current_state_.is_secure);
ASSERT_FALSE(chooser_->current_state_.is_required);
}
TEST_F(SurfaceChooserHelperTest, SetRequestedSecureSurface) {
helper_->SetSecureSurfaceMode(
SurfaceChooserHelper::SecureSurfaceMode::kRequested);
UpdateChooserState();
ASSERT_TRUE(chooser_->current_state_.is_secure);
ASSERT_FALSE(chooser_->current_state_.is_required);
}
TEST_F(SurfaceChooserHelperTest, SetRequiredSecureSurface) {
helper_->SetSecureSurfaceMode(
SurfaceChooserHelper::SecureSurfaceMode::kRequired);
UpdateChooserState();
ASSERT_TRUE(chooser_->current_state_.is_secure);
ASSERT_TRUE(chooser_->current_state_.is_required);
// Also check that removing kRequired puts |is_required| back, since that has
// special processing for "always required".
helper_->SetSecureSurfaceMode(
SurfaceChooserHelper::SecureSurfaceMode::kInsecure);
UpdateChooserState();
ASSERT_FALSE(chooser_->current_state_.is_required);
}
TEST_F(SurfaceChooserHelperTest, StillRequiredAfterClearingSecure) {
// Verify that setting then clearing kRequired doesn't make |is_required|
// false if overlays were required during construction.
ReplaceHelper(true, false);
helper_->SetSecureSurfaceMode(
SurfaceChooserHelper::SecureSurfaceMode::kRequired);
UpdateChooserState();
ASSERT_TRUE(chooser_->current_state_.is_required);
helper_->SetSecureSurfaceMode(
SurfaceChooserHelper::SecureSurfaceMode::kInsecure);
UpdateChooserState();
// Should still be true.
ASSERT_TRUE(chooser_->current_state_.is_required);
}
TEST_F(SurfaceChooserHelperTest, SetPromoteSecureOnly) {
UpdateChooserState();
ASSERT_FALSE(chooser_->current_state_.promote_secure_only);
ReplaceHelper(false, true);
UpdateChooserState();
ASSERT_TRUE(chooser_->current_state_.promote_secure_only);
}
TEST_F(SurfaceChooserHelperTest, SetAlwaysUseTextureOwner) {
UpdateChooserState();
ASSERT_FALSE(chooser_->current_state_.always_use_texture_owner);
ReplaceHelper(false, true, true);
UpdateChooserState();
ASSERT_TRUE(chooser_->current_state_.always_use_texture_owner);
}
TEST_F(SurfaceChooserHelperTest, PromotionHintsForwardsHint) {
// Make sure that NotifyPromotionHint relays the hint to the aggregator.
PromotionHintAggregator::Hint hint(gfx::Rect(1, 2, 3, 4), false);
EXPECT_CALL(*aggregator_, NotifyPromotionHint(hint));
helper_->NotifyPromotionHintAndUpdateChooser(hint, false);
}
TEST_F(SurfaceChooserHelperTest, PromotionHintsRelayPosition) {
// Make sure that the overlay position is sent to the chooser.
gfx::Rect rect(0, 1, 2, 3);
// Send an unpromotable hint and verify that the state reflects it. We set
// it to be promotable so that it notifies the chooser.
helper_->NotifyPromotionHintAndUpdateChooser(
PromotionHintAggregator::Hint(rect, true), false);
ASSERT_EQ(chooser_->current_state_.initial_position, rect);
}
TEST_F(SurfaceChooserHelperTest, PromotionHintsRelayPromotable) {
// Make sure that the promotability state is forwarded to the chooser.
EXPECT_CALL(*chooser_, MockUpdateState()).Times(AtLeast(1));
PromotionHintAggregator::Hint hint(gfx::Rect(), false);
// Send a hint while the aggregator says it's unpromotable, and verify that
// the state reflects it.
helper_->NotifyPromotionHintAndUpdateChooser(hint, false);
ASSERT_FALSE(chooser_->current_state_.is_compositor_promotable);
// Send a promotable hint and check the state. Note that the hint has nothing
// to do with it; it's the aggregator's state.
aggregator_->SetIsSafeToPromote(true);
helper_->NotifyPromotionHintAndUpdateChooser(hint, false);
ASSERT_TRUE(chooser_->current_state_.is_compositor_promotable);
}
TEST_F(SurfaceChooserHelperTest, PromotionHintsClearRelayoutFlag) {
// Set fullscreen to enable relayout.
helper_->SetIsFullscreen(true);
UpdateChooserState();
ASSERT_TRUE(chooser_->current_state_.is_expecting_relayout);
// Send a bunch of hints.
EXPECT_CALL(*chooser_, MockUpdateState()).Times(AtLeast(1));
for (int i = 0; i < 15; i++) {
PromotionHintAggregator::Hint hint(gfx::Rect(), false);
helper_->NotifyPromotionHintAndUpdateChooser(hint, false);
}
// It should no longer be expecting fs.
ASSERT_FALSE(chooser_->current_state_.is_expecting_relayout);
}
TEST_F(SurfaceChooserHelperTest, PromotionHintsUpdateChooserStatePeriodically) {
// Verify that, if enough time passes, we'll get chooser updates if we want
// and overlay but don't have one.
PromotionHintAggregator::Hint hint(gfx::Rect(), false);
// Sending the first hint should update the chooser, since we're becoming
// safe to promote.
aggregator_->SetIsSafeToPromote(true);
EXPECT_CALL(*chooser_, MockUpdateState()).Times(1);
helper_->NotifyPromotionHintAndUpdateChooser(hint, false);
// Sending an additional hint should not, whether or not we're using an
// overlay currently.
EXPECT_CALL(*chooser_, MockUpdateState()).Times(0);
helper_->NotifyPromotionHintAndUpdateChooser(hint, true);
EXPECT_CALL(*chooser_, MockUpdateState()).Times(0);
helper_->NotifyPromotionHintAndUpdateChooser(hint, false);
// Advancing the time and using an overlay should not send a hint.
tick_clock_.Advance(base::Seconds(10));
EXPECT_CALL(*chooser_, MockUpdateState()).Times(0);
helper_->NotifyPromotionHintAndUpdateChooser(hint, true);
// If we're not using an overlay, then it should update the chooser to see
// if it's willing to try for one now.
EXPECT_CALL(*chooser_, MockUpdateState()).Times(1);
helper_->NotifyPromotionHintAndUpdateChooser(hint, false);
}
TEST_F(SurfaceChooserHelperTest, FrameInformationIsCorrectForL1) {
// Verify L1 cases.
helper_->SetSecureSurfaceMode(
SurfaceChooserHelper::SecureSurfaceMode::kRequired);
ASSERT_EQ(SurfaceChooserHelper::FrameInformation::OVERLAY_L1,
helper_->ComputeFrameInformation(true));
// We don't check the "not using overlay" case; it's unclear what we should be
// doing in this case anyway.
}
TEST_F(SurfaceChooserHelperTest, FrameInformationIsCorrectForL3) {
// Verify L3 cases.
helper_->SetSecureSurfaceMode(
SurfaceChooserHelper::SecureSurfaceMode::kRequested);
ASSERT_EQ(SurfaceChooserHelper::FrameInformation::OVERLAY_L3,
helper_->ComputeFrameInformation(true));
ASSERT_EQ(SurfaceChooserHelper::FrameInformation::NON_OVERLAY_L3,
helper_->ComputeFrameInformation(false));
}
TEST_F(SurfaceChooserHelperTest, FrameInformationIsCorrectForInsecure) {
// Verify insecure cases.
helper_->SetSecureSurfaceMode(
SurfaceChooserHelper::SecureSurfaceMode::kInsecure);
// Not using an overlay should be NON_OVERLAY_INSECURE
ASSERT_EQ(SurfaceChooserHelper::FrameInformation::NON_OVERLAY_INSECURE,
helper_->ComputeFrameInformation(false));
// Fullscreen state should affect the result, so that we can tell the
// difference between player-element-fs and div-fs (custom controls).
helper_->SetIsFullscreen(true);
ASSERT_EQ(SurfaceChooserHelper::FrameInformation::
OVERLAY_INSECURE_PLAYER_ELEMENT_FULLSCREEN,
helper_->ComputeFrameInformation(true));
helper_->SetIsFullscreen(false);
ASSERT_EQ(SurfaceChooserHelper::FrameInformation::
OVERLAY_INSECURE_NON_PLAYER_ELEMENT_FULLSCREEN,
helper_->ComputeFrameInformation(true));
}
} // namespace media