forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmedia_tray_unittest.cc
431 lines (347 loc) · 14.3 KB
/
media_tray_unittest.cc
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ash/system/media/media_tray.h"
#include "ash/public/cpp/media_notification_provider.h"
#include "ash/shelf/shelf.h"
#include "ash/shell.h"
#include "ash/system/status_area_widget.h"
#include "ash/system/status_area_widget_test_helper.h"
#include "ash/system/tray/tray_bubble_wrapper.h"
#include "ash/test/ash_test_base.h"
#include "base/test/scoped_feature_list.h"
#include "components/media_message_center/media_notification_view_impl.h"
#include "media/base/media_switches.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "ui/events/event.h"
using ::testing::_;
namespace ash {
namespace {
constexpr gfx::Size kMockTraySize = gfx::Size(48, 48);
class MockMediaNotificationProvider : public MediaNotificationProvider {
public:
MockMediaNotificationProvider() {
MediaNotificationProvider::Set(this);
ON_CALL(*this, GetMediaNotificationListView(_)).WillByDefault([](auto) {
return std::make_unique<views::View>();
});
}
~MockMediaNotificationProvider() override {
MediaNotificationProvider::Set(nullptr);
}
// Medianotificationprovider implementations.
MOCK_METHOD1(GetMediaNotificationListView, std::unique_ptr<views::View>(int));
MOCK_METHOD0(GetActiveMediaNotificationView, std::unique_ptr<views::View>());
MOCK_METHOD0(OnBubbleClosing, void());
void AddObserver(MediaNotificationProviderObserver* observer) override {}
void RemoveObserver(MediaNotificationProviderObserver* observer) override {}
bool HasActiveNotifications() override { return has_active_notifications_; }
bool HasFrozenNotifications() override { return has_frozen_notifications_; }
void SetColorTheme(
const media_message_center::NotificationTheme& color_theme) override {}
void SetHasActiveNotifications(bool has_active_notifications) {
has_active_notifications_ = has_active_notifications;
}
void SetHasFrozenNotifications(bool has_frozen_notifications) {
has_frozen_notifications_ = has_frozen_notifications;
}
private:
bool has_active_notifications_ = false;
bool has_frozen_notifications_ = false;
};
// Mock tray button used to test media tray bubble's anchor update.
class MockTrayBackgroundView : public ash::TrayBackgroundView {
public:
MockTrayBackgroundView(Shelf* shelf) : TrayBackgroundView(shelf) {
SetSize(kMockTraySize);
}
~MockTrayBackgroundView() override = default;
// TrayBackgroundview implementations
std::u16string GetAccessibleNameForTray() override { return u""; }
void HandleLocaleChange() override {}
void HideBubbleWithView(const TrayBubbleView* bubble_view) override {}
void ClickedOutsideBubble() override {}
};
} // namespace
class MediaTrayTest : public AshTestBase {
public:
MediaTrayTest() = default;
~MediaTrayTest() override = default;
void SetUp() override {
feature_list_.InitAndEnableFeature(media::kGlobalMediaControlsForChromeOS);
provider_ = std::make_unique<MockMediaNotificationProvider>();
AshTestBase::SetUp();
media_tray_ = status_area_widget()->media_tray();
ASSERT_TRUE(MediaTray::IsPinnedToShelf());
}
void TearDown() override {
provider_.reset();
mock_tray_.reset();
AshTestBase::TearDown();
}
// Insert mock tray to status area widget right before system tray (The last
// two tray buttons are always system tray and overview button tray).
void InsertMockTray() {
mock_tray_ =
std::make_unique<MockTrayBackgroundView>(status_area_widget()->shelf());
status_area_widget()->tray_buttons_.insert(
status_area_widget()->tray_buttons_.end() - 2, mock_tray_.get());
}
void SimulateNotificationListChanged() {
media_tray_->OnNotificationListChanged();
}
void SimulateTapOnMediaTray() {
ui::GestureEvent tap(0, 0, 0, base::TimeTicks(),
ui::GestureEventDetails(ui::ET_GESTURE_TAP));
media_tray_->PerformAction(tap);
}
void SimulateTapOnPinButton() {
ASSERT_TRUE(media_tray_->pin_button_for_testing());
ui::test::EventGenerator* generator = GetEventGenerator();
generator->MoveMouseTo(media_tray_->pin_button_for_testing()
->GetBoundsInScreen()
.CenterPoint());
generator->ClickLeftButton();
}
void SimulateMockTrayVisibilityChanged(bool visible) {
mock_tray_->SetVisible(visible);
media_tray_->AnchorUpdated();
}
TrayBubbleWrapper* GetBubbleWrapper() {
return media_tray_->tray_bubble_wrapper_for_testing();
}
gfx::Rect GetBubbleBounds() {
return GetBubbleWrapper()->GetBubbleView()->GetBoundsInScreen();
}
StatusAreaWidget* status_area_widget() {
return StatusAreaWidgetTestHelper::GetStatusAreaWidget();
}
MockMediaNotificationProvider* provider() { return provider_.get(); }
MediaTray* media_tray() { return media_tray_; }
views::View* empty_state_view() { return media_tray_->empty_state_view_; }
private:
std::unique_ptr<MockMediaNotificationProvider> provider_;
MediaTray* media_tray_;
std::unique_ptr<MockTrayBackgroundView> mock_tray_;
base::test::ScopedFeatureList feature_list_;
};
TEST_F(MediaTrayTest, MediaTrayVisibilityTest) {
// Media tray should be invisible initially.
ASSERT_TRUE(media_tray());
EXPECT_FALSE(media_tray()->GetVisible());
// Media tray should be visible when there is active notification.
provider()->SetHasActiveNotifications(true);
SimulateNotificationListChanged();
EXPECT_TRUE(media_tray()->GetVisible());
// Media tray should hide itself when no media is playing.
provider()->SetHasActiveNotifications(false);
SimulateNotificationListChanged();
EXPECT_FALSE(media_tray()->GetVisible());
// Media tray should be visible when there is frozen notification.
provider()->SetHasFrozenNotifications(true);
SimulateNotificationListChanged();
EXPECT_TRUE(media_tray()->GetVisible());
// Media tray should be hidden when screen is locked.
GetSessionControllerClient()->LockScreen();
GetSessionControllerClient()->FlushForTest();
EXPECT_FALSE(media_tray()->GetVisible());
// Media tray should be visible again when we unlock the screen.
GetSessionControllerClient()->UnlockScreen();
EXPECT_TRUE(media_tray()->GetVisible());
// Media tray should not be visible if global media controls is
// not pinned to shelf.
MediaTray::SetPinnedToShelf(false);
EXPECT_FALSE(media_tray()->GetVisible());
}
TEST_F(MediaTrayTest, ShowAndHideBubbleTest) {
// Media tray should be visible when there is active notification.
provider()->SetHasActiveNotifications(true);
SimulateNotificationListChanged();
EXPECT_TRUE(media_tray()->GetVisible());
// Bubble should not exist initially, and media tray should not
// be active.
EXPECT_EQ(GetBubbleWrapper(), nullptr);
EXPECT_FALSE(media_tray()->is_active());
// Tap the media tray should show the bubble, and media tray should
// be active. GetMediaNotificationlistview also should be called for
// getting active notifications.
EXPECT_CALL(*provider(), GetMediaNotificationListView(_));
SimulateTapOnMediaTray();
EXPECT_NE(GetBubbleWrapper(), nullptr);
EXPECT_TRUE(media_tray()->is_active());
// Tap again should close the bubble and MediaNotificationProvider should
// be notified.
EXPECT_CALL(*provider(), OnBubbleClosing());
SimulateTapOnMediaTray();
EXPECT_EQ(GetBubbleWrapper(), nullptr);
EXPECT_FALSE(media_tray()->is_active());
}
TEST_F(MediaTrayTest, ShowEmptyStateWhenNoActiveNotification) {
// Media tray should be visible when there is active notification.
provider()->SetHasActiveNotifications(true);
SimulateNotificationListChanged();
EXPECT_TRUE(media_tray()->GetVisible());
// Bubble should not exist initially, and media tray should not
// be active.
EXPECT_EQ(GetBubbleWrapper(), nullptr);
EXPECT_FALSE(media_tray()->is_active());
// Tap and show bubble.
SimulateTapOnMediaTray();
EXPECT_NE(GetBubbleWrapper(), nullptr);
EXPECT_TRUE(media_tray()->is_active());
// We should display empty state if no media is playing.
provider()->SetHasActiveNotifications(false);
SimulateNotificationListChanged();
EXPECT_NE(GetBubbleWrapper(), nullptr);
EXPECT_FALSE(media_tray()->GetVisible());
EXPECT_NE(empty_state_view(), nullptr);
EXPECT_TRUE(empty_state_view()->GetVisible());
// Empty state should be hidden if a new media starts playing.
provider()->SetHasActiveNotifications(true);
SimulateNotificationListChanged();
EXPECT_FALSE(empty_state_view()->GetVisible());
}
TEST_F(MediaTrayTest, PinButtonTest) {
// Media tray should be invisible initially.
ASSERT_TRUE(media_tray());
EXPECT_FALSE(media_tray()->GetVisible());
// Open global media controls dialog.
provider()->SetHasActiveNotifications(true);
SimulateNotificationListChanged();
EXPECT_TRUE(media_tray()->GetVisible());
SimulateTapOnMediaTray();
EXPECT_NE(GetBubbleWrapper(), nullptr);
// Tapping the pin button while the media controls dialog is opened
// should have the media tray hidden.
SimulateTapOnPinButton();
EXPECT_NE(GetBubbleWrapper(), nullptr);
EXPECT_FALSE(media_tray()->GetVisible());
EXPECT_FALSE(MediaTray::IsPinnedToShelf());
// Tap pin button again should bring back media tray.
SimulateTapOnPinButton();
EXPECT_TRUE(media_tray()->GetVisible());
EXPECT_TRUE(MediaTray::IsPinnedToShelf());
}
TEST_F(MediaTrayTest, PinToShelfDefaultBehavior) {
// Media controls should be pinned on shelf by default on a
// 10 inch display.
UpdateDisplay("800x530");
EXPECT_FALSE(MediaTray::IsPinnedToShelf());
// Media controls should be pinned on shelf by default on a
// display larger than 10 inches.
UpdateDisplay("800x600");
EXPECT_TRUE(MediaTray::IsPinnedToShelf());
}
TEST_F(MediaTrayTest, BubbleGetsFocusWhenOpenWithKeyboard) {
media_tray()->ShowBubble();
// Generate a tab key press.
ui::test::EventGenerator generator(Shell::GetPrimaryRootWindow());
generator.PressKey(ui::KeyboardCode::VKEY_TAB, ui::EventFlags::EF_NONE);
EXPECT_TRUE(GetBubbleWrapper()->GetBubbleWidget()->IsActive());
}
TEST_F(MediaTrayTest, DialogAnchor) {
InsertMockTray();
// Simulate active notification and tap media tray to show dialog.
provider()->SetHasActiveNotifications(true);
SimulateNotificationListChanged();
EXPECT_TRUE(media_tray()->GetVisible());
SimulateTapOnMediaTray();
EXPECT_NE(GetBubbleWrapper(), nullptr);
EXPECT_TRUE(status_area_widget()->shelf()->IsHorizontalAlignment());
gfx::Rect initial_bounds = GetBubbleBounds();
// Simulate mock tray becoming visible, bubble should shift left.
SimulateMockTrayVisibilityChanged(true);
EXPECT_EQ(initial_bounds - gfx::Vector2d(kMockTraySize.width(), 0),
GetBubbleBounds());
// Simulate mock tray disappearing, bubble should shift back to the
// original position.
SimulateMockTrayVisibilityChanged(false);
EXPECT_EQ(initial_bounds, GetBubbleBounds());
// Simulate tapping pin button to hide media tray, bubble position
// should not change.
SimulateTapOnPinButton();
EXPECT_FALSE(media_tray()->GetVisible());
EXPECT_EQ(initial_bounds, GetBubbleBounds());
// Simlate mock tray appearing and disappearing while the media tray
// is hidden. Bubble should shift accordingly.
SimulateMockTrayVisibilityChanged(true);
EXPECT_EQ(initial_bounds - gfx::Vector2d(kMockTraySize.width(), 0),
GetBubbleBounds());
SimulateMockTrayVisibilityChanged(false);
EXPECT_EQ(initial_bounds, GetBubbleBounds());
// Tap pin button and bring back media tray, bubble position should
// stay the same.
SimulateTapOnPinButton();
EXPECT_TRUE(media_tray()->GetVisible());
EXPECT_EQ(initial_bounds, GetBubbleBounds());
// Hide bubble, change shelf alignment to left (vertical), and open
// bubble again.
SimulateTapOnMediaTray();
status_area_widget()->shelf()->SetAlignment(ShelfAlignment::kLeft);
SimulateTapOnMediaTray();
// Get new bounds.
initial_bounds = GetBubbleBounds();
// Simulate mock tray appears and disappears while the shelf alignment is
// vertical. The bubble should shift vertically.
SimulateMockTrayVisibilityChanged(true);
EXPECT_EQ(initial_bounds - gfx::Vector2d(0, kMockTraySize.height()),
GetBubbleBounds());
SimulateMockTrayVisibilityChanged(false);
EXPECT_EQ(initial_bounds, GetBubbleBounds());
// Hide bubble, change shelf alignment back to bottom and switch ui
// direction to RTL.
SimulateTapOnMediaTray();
status_area_widget()->shelf()->SetAlignment(ShelfAlignment::kBottom);
base::i18n::SetRTLForTesting(true);
status_area_widget()->UpdateLayout(false);
SimulateTapOnMediaTray();
// Get new bounds.
initial_bounds = GetBubbleBounds();
// Simulate tray appears and triggers while ui direction is RTL,
// bubble should shift to the right.
SimulateMockTrayVisibilityChanged(true);
EXPECT_EQ(initial_bounds + gfx::Vector2d(kMockTraySize.width(), 0),
GetBubbleBounds());
SimulateMockTrayVisibilityChanged(false);
EXPECT_EQ(initial_bounds, GetBubbleBounds());
}
class MediaTrayPinnedParamTest : public AshTestBase {
public:
MediaTrayPinnedParamTest() = default;
~MediaTrayPinnedParamTest() override = default;
void SetUp() override {
auto& pin_param = media::kCrosGlobalMediaControlsPinParam;
feature_list_.InitAndEnableFeatureWithParameters(
media::kGlobalMediaControlsForChromeOS,
{{pin_param.name,
pin_param.GetName(media::kCrosGlobalMediaControlsPinOptions::kPin)}});
AshTestBase::SetUp();
}
private:
base::test::ScopedFeatureList feature_list_;
};
TEST_F(MediaTrayPinnedParamTest, PinParamTest) {
UpdateDisplay("100x100");
EXPECT_TRUE(MediaTray::IsPinnedToShelf());
}
class MediaTrayNotPinnedParamTest : public AshTestBase {
public:
MediaTrayNotPinnedParamTest() = default;
~MediaTrayNotPinnedParamTest() override = default;
void SetUp() override {
auto& pin_param = media::kCrosGlobalMediaControlsPinParam;
feature_list_.InitAndEnableFeatureWithParameters(
media::kGlobalMediaControlsForChromeOS,
{{pin_param.name,
pin_param.GetName(
media::kCrosGlobalMediaControlsPinOptions::kNotPin)}});
AshTestBase::SetUp();
}
private:
base::test::ScopedFeatureList feature_list_;
};
TEST_F(MediaTrayNotPinnedParamTest, PinParamTest) {
UpdateDisplay("2560x1440");
EXPECT_FALSE(MediaTray::IsPinnedToShelf());
}
} // namespace ash