forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcapture_mode_unittests.cc
3526 lines (3002 loc) · 148 KB
/
capture_mode_unittests.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
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// 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 <memory>
#include "ash/app_list/app_list_controller_impl.h"
#include "ash/capture_mode/capture_mode_bar_view.h"
#include "ash/capture_mode/capture_mode_button.h"
#include "ash/capture_mode/capture_mode_constants.h"
#include "ash/capture_mode/capture_mode_controller.h"
#include "ash/capture_mode/capture_mode_metrics.h"
#include "ash/capture_mode/capture_mode_session.h"
#include "ash/capture_mode/capture_mode_settings_entry_view.h"
#include "ash/capture_mode/capture_mode_settings_view.h"
#include "ash/capture_mode/capture_mode_source_view.h"
#include "ash/capture_mode/capture_mode_toggle_button.h"
#include "ash/capture_mode/capture_mode_type_view.h"
#include "ash/capture_mode/capture_mode_types.h"
#include "ash/capture_mode/capture_mode_util.h"
#include "ash/capture_mode/stop_recording_button_tray.h"
#include "ash/capture_mode/test_capture_mode_delegate.h"
#include "ash/capture_mode/video_recording_watcher.h"
#include "ash/display/cursor_window_controller.h"
#include "ash/display/output_protection_delegate.h"
#include "ash/display/screen_orientation_controller_test_api.h"
#include "ash/display/window_tree_host_manager.h"
#include "ash/magnifier/docked_magnifier_controller_impl.h"
#include "ash/magnifier/magnifier_glass.h"
#include "ash/public/cpp/ash_features.h"
#include "ash/public/cpp/capture_mode_test_api.h"
#include "ash/root_window_controller.h"
#include "ash/shell.h"
#include "ash/system/status_area_widget.h"
#include "ash/test/ash_test_base.h"
#include "ash/wm/desks/desk.h"
#include "ash/wm/desks/desks_controller.h"
#include "ash/wm/desks/desks_test_util.h"
#include "ash/wm/tablet_mode/tablet_mode_controller.h"
#include "ash/wm/tablet_mode/tablet_mode_controller_test_api.h"
#include "ash/wm/window_state.h"
#include "ash/wm/window_util.h"
#include "base/callback_helpers.h"
#include "base/run_loop.h"
#include "base/scoped_observation.h"
#include "base/test/bind.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/test/scoped_feature_list.h"
#include "chromeos/dbus/power/fake_power_manager_client.h"
#include "chromeos/dbus/power_manager/suspend.pb.h"
#include "components/account_id/account_id.h"
#include "mojo/public/cpp/bindings/pending_receiver.h"
#include "mojo/public/cpp/bindings/pending_remote.h"
#include "mojo/public/cpp/bindings/receiver.h"
#include "services/viz/privileged/mojom/compositing/frame_sink_video_capture.mojom.h"
#include "ui/aura/client/capture_client.h"
#include "ui/aura/client/capture_client_observer.h"
#include "ui/aura/window_tracker.h"
#include "ui/compositor/compositor.h"
#include "ui/compositor/scoped_animation_duration_scale_mode.h"
#include "ui/display/types/display_constants.h"
#include "ui/events/keycodes/keyboard_codes_posix.h"
#include "ui/events/test/event_generator.h"
#include "ui/gfx/geometry/insets.h"
#include "ui/gfx/geometry/rect.h"
#include "ui/gfx/geometry/vector2d.h"
#include "ui/gfx/image/image_unittest_util.h"
#include "ui/message_center/message_center.h"
#include "ui/message_center/message_center_observer.h"
#include "ui/message_center/public/cpp/notification.h"
#include "ui/message_center/public/cpp/notification_delegate.h"
#include "ui/views/accessibility/view_accessibility.h"
#include "ui/views/view.h"
#include "ui/views/widget/widget_observer.h"
#include "ui/wm/core/window_util.h"
namespace ash {
namespace {
constexpr char kEndRecordingReasonInClamshellHistogramName[] =
"Ash.CaptureModeController.EndRecordingReason.ClamshellMode";
constexpr char kScreenCaptureNotificationId[] = "capture_mode_notification";
// Returns true if the software-composited cursor is enabled.
bool IsCursorCompositingEnabled() {
return Shell::Get()
->window_tree_host_manager()
->cursor_window_controller()
->is_cursor_compositing_enabled();
}
void ClickOnView(const views::View* view,
ui::test::EventGenerator* event_generator) {
DCHECK(view);
DCHECK(event_generator);
const gfx::Point view_center = view->GetBoundsInScreen().CenterPoint();
event_generator->MoveMouseTo(view_center);
event_generator->ClickLeftButton();
}
// Sends a press release key combo |count| times.
void SendKey(ui::KeyboardCode key_code,
ui::test::EventGenerator* event_generator,
bool shift_down = false,
int count = 1) {
const int flags = shift_down ? ui::EF_SHIFT_DOWN : 0;
for (int i = 0; i < count; ++i) {
event_generator->PressKey(key_code, flags);
event_generator->ReleaseKey(key_code, flags);
}
}
const message_center::Notification* GetPreviewNotification() {
const message_center::NotificationList::Notifications notifications =
message_center::MessageCenter::Get()->GetVisibleNotifications();
for (const auto* notification : notifications) {
if (notification->id() == kScreenCaptureNotificationId)
return notification;
}
return nullptr;
}
void ClickNotification(base::Optional<int> button_index) {
const message_center::Notification* notification = GetPreviewNotification();
DCHECK(notification);
notification->delegate()->Click(button_index, base::nullopt);
}
// Moves the mouse and updates the cursor's display manually to imitate what a
// real mouse move event does in shell.
// TODO(crbug.com/990589): Unit tests should be able to simulate mouse input
// without having to call |CursorManager::SetDisplay|.
void MoveMouseToAndUpdateCursorDisplay(
const gfx::Point& point,
ui::test::EventGenerator* event_generator) {
Shell::Get()->cursor_manager()->SetDisplay(
display::Screen::GetScreen()->GetDisplayNearestPoint(point));
event_generator->MoveMouseTo(point);
}
bool IsLayerStackedRightBelow(ui::Layer* layer, ui::Layer* sibling) {
DCHECK_EQ(layer->parent(), sibling->parent());
const auto& children = layer->parent()->children();
const int sibling_index =
std::find(children.begin(), children.end(), sibling) - children.begin();
return sibling_index > 0 && children[sibling_index - 1] == layer;
}
// Defines a capture client observer, that sets the input capture to the window
// given to the constructor, and destroys it once capture is lost.
class TestCaptureClientObserver : public aura::client::CaptureClientObserver {
public:
explicit TestCaptureClientObserver(std::unique_ptr<aura::Window> window)
: window_(std::move(window)) {
DCHECK(window_);
auto* capture_client =
aura::client::GetCaptureClient(window_->GetRootWindow());
capture_client->SetCapture(window_.get());
capture_client->AddObserver(this);
}
~TestCaptureClientObserver() override { StopObserving(); }
// aura::client::CaptureClientObserver:
void OnCaptureChanged(aura::Window* lost_capture,
aura::Window* gained_capture) override {
if (lost_capture != window_.get())
return;
StopObserving();
window_.reset();
}
private:
void StopObserving() {
if (!window_)
return;
auto* capture_client =
aura::client::GetCaptureClient(window_->GetRootWindow());
capture_client->RemoveObserver(this);
}
std::unique_ptr<aura::Window> window_;
};
} // namespace
// Wrapper for CaptureModeSession that exposes internal state to test functions.
class CaptureModeSessionTestApi {
public:
explicit CaptureModeSessionTestApi(CaptureModeSession* session)
: session_(session) {}
CaptureModeSessionTestApi(const CaptureModeSessionTestApi&) = delete;
CaptureModeSessionTestApi& operator=(const CaptureModeSessionTestApi&) =
delete;
~CaptureModeSessionTestApi() = default;
CaptureModeBarView* capture_mode_bar_view() const {
return session_->capture_mode_bar_view_;
}
CaptureModeSettingsView* capture_mode_settings_view() const {
return session_->capture_mode_settings_view_;
}
views::Widget* capture_mode_settings_widget() const {
return session_->capture_mode_settings_widget_.get();
}
views::Widget* capture_label_widget() const {
return session_->capture_label_widget_.get();
}
views::Widget* dimensions_label_widget() const {
return session_->dimensions_label_widget_.get();
}
const MagnifierGlass& magnifier_glass() const {
return session_->magnifier_glass_;
}
bool IsUsingCustomCursor(CaptureModeType type) const {
return session_->IsUsingCustomCursor(type);
}
CaptureModeSessionFocusCycler::FocusGroup GetCurrentFocusGroup() const {
return session_->focus_cycler_->current_focus_group_;
}
size_t GetCurrentFocusIndex() const {
return session_->focus_cycler_->focus_index_;
}
bool HasFocus() const { return session_->focus_cycler_->HasFocus(); }
private:
const CaptureModeSession* const session_;
};
class CaptureModeTest : public AshTestBase {
public:
CaptureModeTest() = default;
explicit CaptureModeTest(base::test::TaskEnvironment::TimeSource time)
: AshTestBase(time) {}
CaptureModeTest(const CaptureModeTest&) = delete;
CaptureModeTest& operator=(const CaptureModeTest&) = delete;
~CaptureModeTest() override = default;
// AshTestBase:
void SetUp() override {
scoped_feature_list_.InitAndEnableFeature(features::kCaptureMode);
AshTestBase::SetUp();
}
CaptureModeBarView* GetCaptureModeBarView() const {
auto* session = CaptureModeController::Get()->capture_mode_session();
DCHECK(session);
return CaptureModeSessionTestApi(session).capture_mode_bar_view();
}
views::Widget* GetCaptureModeBarWidget() const {
auto* session = CaptureModeController::Get()->capture_mode_session();
DCHECK(session);
return session->capture_mode_bar_widget();
}
views::Widget* GetCaptureModeLabelWidget() const {
auto* session = CaptureModeController::Get()->capture_mode_session();
DCHECK(session);
return CaptureModeSessionTestApi(session).capture_label_widget();
}
CaptureModeSettingsView* GetCaptureModeSettingsView() const {
auto* session = CaptureModeController::Get()->capture_mode_session();
DCHECK(session);
return CaptureModeSessionTestApi(session).capture_mode_settings_view();
}
views::Widget* GetCaptureModeSettingsWidget() const {
auto* session = CaptureModeController::Get()->capture_mode_session();
DCHECK(session);
return CaptureModeSessionTestApi(session).capture_mode_settings_widget();
}
CaptureModeToggleButton* GetImageToggleButton() const {
auto* controller = CaptureModeController::Get();
DCHECK(controller->IsActive());
return GetCaptureModeBarView()->capture_type_view()->image_toggle_button();
}
CaptureModeToggleButton* GetVideoToggleButton() const {
auto* controller = CaptureModeController::Get();
DCHECK(controller->IsActive());
return GetCaptureModeBarView()->capture_type_view()->video_toggle_button();
}
CaptureModeToggleButton* GetFullscreenToggleButton() const {
auto* controller = CaptureModeController::Get();
DCHECK(controller->IsActive());
return GetCaptureModeBarView()
->capture_source_view()
->fullscreen_toggle_button();
}
CaptureModeToggleButton* GetRegionToggleButton() const {
auto* controller = CaptureModeController::Get();
DCHECK(controller->IsActive());
return GetCaptureModeBarView()
->capture_source_view()
->region_toggle_button();
}
CaptureModeToggleButton* GetWindowToggleButton() const {
auto* controller = CaptureModeController::Get();
DCHECK(controller->IsActive());
return GetCaptureModeBarView()
->capture_source_view()
->window_toggle_button();
}
CaptureModeToggleButton* GetSettingsButton() const {
auto* controller = CaptureModeController::Get();
DCHECK(controller->IsActive());
return GetCaptureModeBarView()->settings_button();
}
CaptureModeButton* GetCloseButton() const {
auto* controller = CaptureModeController::Get();
DCHECK(controller->IsActive());
return GetCaptureModeBarView()->close_button();
}
views::ToggleButton* GetMicrophoneToggle() const {
auto* controller = CaptureModeController::Get();
DCHECK(controller->IsActive());
DCHECK(GetCaptureModeSettingsView());
return GetCaptureModeSettingsView()
->microphone_view()
->toggle_button_view();
}
aura::Window* GetDimensionsLabelWindow() const {
auto* controller = CaptureModeController::Get();
DCHECK(controller->IsActive());
auto* widget = CaptureModeSessionTestApi(controller->capture_mode_session())
.dimensions_label_widget();
return widget ? widget->GetNativeWindow() : nullptr;
}
base::Optional<gfx::Point> GetMagnifierGlassCenterPoint() const {
auto* controller = CaptureModeController::Get();
DCHECK(controller->IsActive());
auto& magnifier =
CaptureModeSessionTestApi(controller->capture_mode_session())
.magnifier_glass();
if (magnifier.host_widget_for_testing()) {
return magnifier.host_widget_for_testing()
->GetWindowBoundsInScreen()
.CenterPoint();
}
return base::nullopt;
}
CaptureModeController* StartCaptureSession(CaptureModeSource source,
CaptureModeType type) {
auto* controller = CaptureModeController::Get();
controller->SetSource(source);
controller->SetType(type);
controller->Start(CaptureModeEntryType::kQuickSettings);
DCHECK(controller->IsActive());
return controller;
}
// Start Capture Mode with source region and type image.
CaptureModeController* StartImageRegionCapture() {
return StartCaptureSession(CaptureModeSource::kRegion,
CaptureModeType::kImage);
}
CaptureModeController* StartSessionAndRecordWindow(aura::Window* window) {
auto* controller = StartCaptureSession(CaptureModeSource::kWindow,
CaptureModeType::kVideo);
GetEventGenerator()->MoveMouseToCenterOf(window);
controller->StartVideoRecordingImmediatelyForTesting();
EXPECT_TRUE(controller->is_recording_in_progress());
return controller;
}
// Select a region by pressing and dragging the mouse.
void SelectRegion(const gfx::Rect& region, bool release_mouse = true) {
auto* controller = CaptureModeController::Get();
ASSERT_TRUE(controller->IsActive());
ASSERT_EQ(CaptureModeSource::kRegion, controller->source());
auto* event_generator = GetEventGenerator();
event_generator->set_current_screen_location(region.origin());
event_generator->PressLeftButton();
event_generator->MoveMouseTo(region.bottom_right());
if (release_mouse)
event_generator->ReleaseLeftButton();
EXPECT_EQ(region, controller->user_capture_region());
}
void WaitForCountDownToFinish() {
auto* controller = CaptureModeController::Get();
DCHECK(controller->IsActive());
DCHECK_EQ(controller->type(), CaptureModeType::kVideo);
while (!controller->is_recording_in_progress()) {
base::RunLoop run_loop;
base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
FROM_HERE, run_loop.QuitClosure(),
base::TimeDelta::FromMilliseconds(100));
run_loop.Run();
}
}
void RemoveSecondaryDisplay() {
const int64_t primary_id = WindowTreeHostManager::GetPrimaryDisplayId();
display::ManagedDisplayInfo primary_info =
display_manager()->GetDisplayInfo(primary_id);
std::vector<display::ManagedDisplayInfo> display_info_list;
display_info_list.push_back(primary_info);
display_manager()->OnNativeDisplaysChanged(display_info_list);
// Spin the run loop so that we get a signal that the associated root window
// of the removed display is destroyed.
base::RunLoop().RunUntilIdle();
}
void SwitchToUser2() {
auto* session_controller = GetSessionControllerClient();
constexpr char kUserEmail[] = "user2@capture_mode";
session_controller->AddUserSession(kUserEmail);
session_controller->SwitchActiveUser(AccountId::FromUserEmail(kUserEmail));
}
void WaitForSeconds(int seconds) {
base::RunLoop loop;
base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
FROM_HERE, base::BindLambdaForTesting([&]() { loop.Quit(); }),
base::TimeDelta::FromSeconds(seconds));
loop.Run();
}
void WaitForCaptureFileToBeSaved() {
base::RunLoop run_loop;
CaptureModeTestApi().SetOnCaptureFileSavedCallback(
base::BindLambdaForTesting(
[&run_loop](const base::FilePath& path) { run_loop.Quit(); }));
run_loop.Run();
}
private:
base::test::ScopedFeatureList scoped_feature_list_;
};
class CaptureSessionWidgetObserver : public views::WidgetObserver {
public:
explicit CaptureSessionWidgetObserver(views::Widget* widget) {
DCHECK(widget);
observer_.Observe(widget);
}
CaptureSessionWidgetObserver(const CaptureSessionWidgetObserver&) = delete;
CaptureSessionWidgetObserver& operator=(const CaptureSessionWidgetObserver&) =
delete;
~CaptureSessionWidgetObserver() override = default;
bool GetWidgetDestroyed() const { return !observer_.IsObserving(); }
// views::WidgetObserver
void OnWidgetClosing(views::Widget* widget) override {
DCHECK(observer_.IsObservingSource(widget));
observer_.Reset();
}
private:
base::ScopedObservation<views::Widget, views::WidgetObserver> observer_{this};
};
class CaptureNotificationWaiter : public message_center::MessageCenterObserver {
public:
CaptureNotificationWaiter() {
message_center::MessageCenter::Get()->AddObserver(this);
}
~CaptureNotificationWaiter() override {
message_center::MessageCenter::Get()->RemoveObserver(this);
}
void Wait() { run_loop_.Run(); }
// message_center::MessageCenterObserver:
void OnNotificationAdded(const std::string& notification_id) override {
if (notification_id == kScreenCaptureNotificationId)
run_loop_.Quit();
}
private:
base::RunLoop run_loop_;
};
TEST_F(CaptureModeTest, StartStop) {
auto* controller = CaptureModeController::Get();
controller->Start(CaptureModeEntryType::kQuickSettings);
EXPECT_TRUE(controller->IsActive());
// Calling start again is a no-op.
controller->Start(CaptureModeEntryType::kQuickSettings);
EXPECT_TRUE(controller->IsActive());
// Closing the session should close the native window of capture mode bar
// immediately.
auto* bar_window = GetCaptureModeBarWidget()->GetNativeWindow();
aura::WindowTracker tracker({bar_window});
controller->Stop();
EXPECT_TRUE(tracker.windows().empty());
EXPECT_FALSE(controller->IsActive());
}
// Regression test for https://crbug.com/1172425.
TEST_F(CaptureModeTest, NoCrashOnClearingCapture) {
TestCaptureClientObserver observer(CreateTestWindow(gfx::Rect(200, 200)));
auto* controller = StartImageRegionCapture();
EXPECT_TRUE(controller->IsActive());
}
TEST_F(CaptureModeTest, CheckWidgetClosed) {
auto* controller = CaptureModeController::Get();
controller->Start(CaptureModeEntryType::kQuickSettings);
EXPECT_TRUE(controller->IsActive());
EXPECT_TRUE(GetCaptureModeBarWidget());
CaptureSessionWidgetObserver observer(GetCaptureModeBarWidget());
EXPECT_FALSE(observer.GetWidgetDestroyed());
controller->Stop();
EXPECT_FALSE(controller->IsActive());
EXPECT_FALSE(controller->capture_mode_session());
// The Widget should have been destroyed by now.
EXPECT_TRUE(observer.GetWidgetDestroyed());
}
TEST_F(CaptureModeTest, StartWithMostRecentTypeAndSource) {
auto* controller = CaptureModeController::Get();
controller->SetSource(CaptureModeSource::kFullscreen);
controller->SetType(CaptureModeType::kVideo);
controller->Start(CaptureModeEntryType::kQuickSettings);
EXPECT_TRUE(controller->IsActive());
EXPECT_FALSE(GetImageToggleButton()->GetToggled());
EXPECT_TRUE(GetVideoToggleButton()->GetToggled());
EXPECT_TRUE(GetFullscreenToggleButton()->GetToggled());
EXPECT_FALSE(GetRegionToggleButton()->GetToggled());
EXPECT_FALSE(GetWindowToggleButton()->GetToggled());
ClickOnView(GetCloseButton(), GetEventGenerator());
EXPECT_FALSE(controller->IsActive());
}
TEST_F(CaptureModeTest, ChangeTypeAndSourceFromUI) {
auto* controller = CaptureModeController::Get();
controller->Start(CaptureModeEntryType::kQuickSettings);
EXPECT_TRUE(controller->IsActive());
EXPECT_TRUE(GetImageToggleButton()->GetToggled());
EXPECT_FALSE(GetVideoToggleButton()->GetToggled());
auto* event_generator = GetEventGenerator();
ClickOnView(GetVideoToggleButton(), event_generator);
EXPECT_FALSE(GetImageToggleButton()->GetToggled());
EXPECT_TRUE(GetVideoToggleButton()->GetToggled());
EXPECT_EQ(controller->type(), CaptureModeType::kVideo);
ClickOnView(GetWindowToggleButton(), event_generator);
EXPECT_FALSE(GetFullscreenToggleButton()->GetToggled());
EXPECT_FALSE(GetRegionToggleButton()->GetToggled());
EXPECT_TRUE(GetWindowToggleButton()->GetToggled());
EXPECT_EQ(controller->source(), CaptureModeSource::kWindow);
ClickOnView(GetFullscreenToggleButton(), event_generator);
EXPECT_TRUE(GetFullscreenToggleButton()->GetToggled());
EXPECT_FALSE(GetRegionToggleButton()->GetToggled());
EXPECT_FALSE(GetWindowToggleButton()->GetToggled());
EXPECT_EQ(controller->source(), CaptureModeSource::kFullscreen);
}
TEST_F(CaptureModeTest, VideoRecordingUiBehavior) {
// Start Capture Mode in a fullscreen video recording mode.
CaptureModeController* controller = StartCaptureSession(
CaptureModeSource::kFullscreen, CaptureModeType::kVideo);
EXPECT_TRUE(controller->IsActive());
EXPECT_FALSE(controller->is_recording_in_progress());
EXPECT_FALSE(IsCursorCompositingEnabled());
// Hit Enter to begin recording.
auto* event_generator = GetEventGenerator();
SendKey(ui::VKEY_RETURN, event_generator);
EXPECT_EQ(ui::mojom::CursorType::kPointer,
Shell::Get()->cursor_manager()->GetCursor().type());
WaitForCountDownToFinish();
EXPECT_FALSE(controller->IsActive());
EXPECT_TRUE(controller->is_recording_in_progress());
// The composited cursor should remain disabled now that we're using the
// cursor overlay on the capturer. The stop-recording button should show up in
// the status area widget.
EXPECT_FALSE(IsCursorCompositingEnabled());
auto* stop_recording_button = Shell::GetPrimaryRootWindowController()
->GetStatusAreaWidget()
->stop_recording_button_tray();
EXPECT_TRUE(stop_recording_button->visible_preferred());
// End recording via the stop-recording button. Expect that it's now hidden.
base::HistogramTester histogram_tester;
ClickOnView(stop_recording_button, event_generator);
EXPECT_FALSE(stop_recording_button->visible_preferred());
EXPECT_FALSE(controller->is_recording_in_progress());
EXPECT_FALSE(IsCursorCompositingEnabled());
histogram_tester.ExpectBucketCount(
kEndRecordingReasonInClamshellHistogramName,
EndRecordingReason::kStopRecordingButton, 1);
}
// Tests the behavior of repositioning a region with capture mode.
TEST_F(CaptureModeTest, CaptureRegionRepositionBehavior) {
// Use a set display size as we will be choosing points in this test.
UpdateDisplay("800x800");
auto* controller = StartImageRegionCapture();
// The first time selecting a region, the region is a default rect.
EXPECT_EQ(gfx::Rect(), controller->user_capture_region());
// Press down and drag to select a region.
SelectRegion(gfx::Rect(100, 100, 600, 600));
// Click somewhere in the center on the region and drag. The whole region
// should move. Note that the point cannot be in the capture button bounds,
// which is located in the center of the region.
auto* event_generator = GetEventGenerator();
event_generator->set_current_screen_location(gfx::Point(200, 200));
event_generator->DragMouseBy(-50, -50);
EXPECT_EQ(gfx::Rect(50, 50, 600, 600), controller->user_capture_region());
// Try to drag the region offscreen. The region should be bound by the display
// size.
event_generator->set_current_screen_location(gfx::Point(100, 100));
event_generator->DragMouseBy(-150, -150);
EXPECT_EQ(gfx::Rect(600, 600), controller->user_capture_region());
}
// Tests the behavior of resizing a region with capture mode using the corner
// drag affordances.
TEST_F(CaptureModeTest, CaptureRegionCornerResizeBehavior) {
// Use a set display size as we will be choosing points in this test.
UpdateDisplay("800x800");
auto* controller = StartImageRegionCapture();
// Create the initial region.
const gfx::Rect target_region(gfx::Rect(200, 200, 400, 400));
SelectRegion(target_region);
// For each corner point try dragging to several points and verify that the
// capture region is as expected.
struct {
std::string trace;
gfx::Point drag_point;
// The point that stays the same while dragging. It is the opposite vertex
// to |drag_point| on |target_region|.
gfx::Point anchor_point;
} kDragCornerCases[] = {
{"origin", target_region.origin(), target_region.bottom_right()},
{"top_right", target_region.top_right(), target_region.bottom_left()},
{"bottom_right", target_region.bottom_right(), target_region.origin()},
{"bottom_left", target_region.bottom_left(), target_region.top_right()},
};
// The test corner points are one in each corner outside |target_region| and
// one point inside |target_region|.
auto drag_test_points = {gfx::Point(100, 100), gfx::Point(700, 100),
gfx::Point(700, 700), gfx::Point(100, 700),
gfx::Point(400, 400)};
auto* event_generator = GetEventGenerator();
for (auto test_case : kDragCornerCases) {
SCOPED_TRACE(test_case.trace);
event_generator->set_current_screen_location(test_case.drag_point);
event_generator->PressLeftButton();
// At each drag test point, the region rect should be the rect created by
// the given |corner_point| and the drag test point. That is, the width
// should match the x distance between the two points, the height should
// match the y distance between the two points and that both points are
// contained in the region.
for (auto drag_test_point : drag_test_points) {
event_generator->MoveMouseTo(drag_test_point);
gfx::Rect region = controller->user_capture_region();
const gfx::Vector2d distance = test_case.anchor_point - drag_test_point;
EXPECT_EQ(std::abs(distance.x()), region.width());
EXPECT_EQ(std::abs(distance.y()), region.height());
// gfx::Rect::Contains returns the point (x+width, y+height) as false, so
// make the region one unit bigger to account for this.
region.Inset(gfx::Insets(-1));
EXPECT_TRUE(region.Contains(drag_test_point));
EXPECT_TRUE(region.Contains(test_case.anchor_point));
}
// Make sure the region is reset for the next iteration.
event_generator->MoveMouseTo(test_case.drag_point);
event_generator->ReleaseLeftButton();
ASSERT_EQ(target_region, controller->user_capture_region());
}
}
// Tests the behavior of resizing a region with capture mode using the edge drag
// affordances.
TEST_F(CaptureModeTest, CaptureRegionEdgeResizeBehavior) {
// Use a set display size as we will be choosing points in this test.
UpdateDisplay("800x800");
auto* controller = StartImageRegionCapture();
// Create the initial region.
const gfx::Rect target_region(gfx::Rect(200, 200, 200, 200));
SelectRegion(target_region);
// For each edge point try dragging to several points and verify that the
// capture region is as expected.
struct {
std::string trace;
gfx::Point drag_point;
// True if horizontal direction (left, right). Height stays the same while
// dragging if true, width stays the same while dragging if false.
bool horizontal;
// The edge that stays the same while dragging. It is the opposite edge to
// |drag_point|. For example, if |drag_point| is the left center of
// |target_region|, then |anchor_edge| is the right edge.
int anchor_edge;
} kDragEdgeCases[] = {
{"left", target_region.left_center(), true, target_region.right()},
{"top", target_region.top_center(), false, target_region.bottom()},
{"right", target_region.right_center(), true, target_region.x()},
{"bottom", target_region.bottom_center(), false, target_region.y()},
};
// Drag to a couple of points that change both x and y. In all these cases,
// only the width or height should change.
auto drag_test_points = {gfx::Point(150, 150), gfx::Point(350, 350),
gfx::Point(450, 450)};
auto* event_generator = GetEventGenerator();
for (auto test_case : kDragEdgeCases) {
SCOPED_TRACE(test_case.trace);
event_generator->set_current_screen_location(test_case.drag_point);
event_generator->PressLeftButton();
for (auto drag_test_point : drag_test_points) {
event_generator->MoveMouseTo(drag_test_point);
const gfx::Rect region = controller->user_capture_region();
// One of width/height will always be the same as |target_region|'s
// initial width/height, depending on the edge affordance. The other
// dimension will be the distance from |drag_test_point| to the anchor
// edge.
const int variable_length = std::abs(
(test_case.horizontal ? drag_test_point.x() : drag_test_point.y()) -
test_case.anchor_edge);
const int expected_width =
test_case.horizontal ? variable_length : target_region.width();
const int expected_height =
test_case.horizontal ? target_region.height() : variable_length;
EXPECT_EQ(expected_width, region.width());
EXPECT_EQ(expected_height, region.height());
}
// Make sure the region is reset for the next iteration.
event_generator->MoveMouseTo(test_case.drag_point);
event_generator->ReleaseLeftButton();
ASSERT_EQ(target_region, controller->user_capture_region());
}
}
// Tests that the capture region persists after exiting and reentering capture
// mode.
TEST_F(CaptureModeTest, CaptureRegionPersistsAfterExit) {
auto* controller = StartImageRegionCapture();
const gfx::Rect region(100, 100, 200, 200);
SelectRegion(region);
controller->Stop();
controller->Start(CaptureModeEntryType::kQuickSettings);
EXPECT_EQ(region, controller->user_capture_region());
}
// Tests that the capture region resets when clicking outside the current
// capture regions bounds.
TEST_F(CaptureModeTest, CaptureRegionResetsOnClickOutside) {
auto* controller = StartImageRegionCapture();
SelectRegion(gfx::Rect(100, 100, 200, 200));
// Click on an area outside of the current capture region. The capture region
// should reset to default rect.
auto* event_generator = GetEventGenerator();
event_generator->set_current_screen_location(gfx::Point(400, 400));
event_generator->ClickLeftButton();
EXPECT_EQ(gfx::Rect(), controller->user_capture_region());
}
// Tests that buttons on the capture mode bar still work when a region is
// "covering" them.
TEST_F(CaptureModeTest, CaptureRegionCoversCaptureModeBar) {
UpdateDisplay("800x800");
auto* controller = StartImageRegionCapture();
// Select a region such that the capture mode bar is covered.
SelectRegion(gfx::Rect(5, 5, 795, 795));
EXPECT_TRUE(controller->user_capture_region().Contains(
GetCaptureModeBarView()->GetBoundsInScreen()));
// Click on the fullscreen toggle button to verify that we enter fullscreen
// capture mode. Then click on the region toggle button to verify that we
// reenter region capture mode and that the region is still covering the
// capture mode bar.
auto* event_generator = GetEventGenerator();
ClickOnView(GetFullscreenToggleButton(), event_generator);
EXPECT_EQ(CaptureModeSource::kFullscreen, controller->source());
ClickOnView(GetRegionToggleButton(), GetEventGenerator());
ASSERT_EQ(CaptureModeSource::kRegion, controller->source());
ASSERT_TRUE(controller->user_capture_region().Contains(
GetCaptureModeBarView()->GetBoundsInScreen()));
ClickOnView(GetCloseButton(), event_generator);
EXPECT_FALSE(controller->IsActive());
}
// Tests that the magnifying glass appears while fine tuning the capture region,
// and that the cursor is hidden if the magnifying glass is present.
TEST_F(CaptureModeTest, CaptureRegionMagnifierWhenFineTuning) {
const gfx::Vector2d kDragDelta(50, 50);
UpdateDisplay("800x800");
// Start Capture Mode in a region in image mode.
StartImageRegionCapture();
// Press down and drag to select a region. The magnifier should not be
// visible yet.
gfx::Rect capture_region{200, 200, 400, 400};
SelectRegion(capture_region);
EXPECT_EQ(base::nullopt, GetMagnifierGlassCenterPoint());
auto check_magnifier_shows_properly = [this](const gfx::Point& origin,
const gfx::Point& destination,
bool should_show_magnifier) {
// If |should_show_magnifier|, check that the magnifying glass is centered
// on the mouse after press and during drag, and that the cursor is hidden.
// If not |should_show_magnifier|, check that the magnifying glass never
// shows. Should always be not visible when mouse button is released.
auto* event_generator = GetEventGenerator();
base::Optional<gfx::Point> expected_origin =
should_show_magnifier ? base::make_optional(origin) : base::nullopt;
base::Optional<gfx::Point> expected_destination =
should_show_magnifier ? base::make_optional(destination)
: base::nullopt;
auto* cursor_manager = Shell::Get()->cursor_manager();
EXPECT_TRUE(cursor_manager->IsCursorVisible());
// Move cursor to |origin| and click.
event_generator->set_current_screen_location(origin);
event_generator->PressLeftButton();
EXPECT_EQ(expected_origin, GetMagnifierGlassCenterPoint());
EXPECT_NE(should_show_magnifier, cursor_manager->IsCursorVisible());
// Drag to |destination| while holding left button.
event_generator->MoveMouseTo(destination);
EXPECT_EQ(expected_destination, GetMagnifierGlassCenterPoint());
EXPECT_NE(should_show_magnifier, cursor_manager->IsCursorVisible());
// Drag back to |origin| while still holding left button.
event_generator->MoveMouseTo(origin);
EXPECT_EQ(expected_origin, GetMagnifierGlassCenterPoint());
EXPECT_NE(should_show_magnifier, cursor_manager->IsCursorVisible());
// Release left button.
event_generator->ReleaseLeftButton();
EXPECT_EQ(base::nullopt, GetMagnifierGlassCenterPoint());
EXPECT_TRUE(cursor_manager->IsCursorVisible());
};
// Drag the capture region from within the existing selected region. The
// magnifier should not be visible at any point.
check_magnifier_shows_properly(gfx::Point(400, 250), gfx::Point(500, 350),
/*should_show_magnifier=*/false);
// Check that each corner fine tune position shows the magnifier when
// dragging.
struct {
std::string trace;
FineTunePosition position;
} kFineTunePositions[] = {{"top_left", FineTunePosition::kTopLeft},
{"top_right", FineTunePosition::kTopRight},
{"bottom_right", FineTunePosition::kBottomRight},
{"bottom_left", FineTunePosition::kBottomLeft}};
for (const auto& fine_tune_position : kFineTunePositions) {
SCOPED_TRACE(fine_tune_position.trace);
const gfx::Point drag_affordance_location =
capture_mode_util::GetLocationForFineTunePosition(
capture_region, fine_tune_position.position);
check_magnifier_shows_properly(drag_affordance_location,
drag_affordance_location + kDragDelta,
/*should_show_magnifier=*/true);
}
}
// Tests that the dimensions label properly renders for capture regions.
TEST_F(CaptureModeTest, CaptureRegionDimensionsLabelLocation) {
UpdateDisplay("800x800");
// Start Capture Mode in a region in image mode.
StartImageRegionCapture();
// Press down and don't move the mouse. Label shouldn't display for empty
// capture regions.
auto* generator = GetEventGenerator();
generator->set_current_screen_location(gfx::Point(0, 0));
generator->PressLeftButton();
auto* controller = CaptureModeController::Get();
EXPECT_TRUE(controller->IsActive());
EXPECT_TRUE(controller->user_capture_region().IsEmpty());
EXPECT_EQ(nullptr, GetDimensionsLabelWindow());
generator->ReleaseLeftButton();
// Press down and drag to select a large region. Verify that the dimensions
// label is centered and that the label is below the capture region.
gfx::Rect capture_region{100, 100, 600, 200};
SelectRegion(capture_region, /*release_mouse=*/false);
EXPECT_EQ(capture_region.CenterPoint().x(),
GetDimensionsLabelWindow()->bounds().CenterPoint().x());
EXPECT_EQ(capture_region.bottom() +
CaptureModeSession::kSizeLabelYDistanceFromRegionDp,
GetDimensionsLabelWindow()->bounds().y());
generator->ReleaseLeftButton();
EXPECT_EQ(nullptr, GetDimensionsLabelWindow());
// Create a new capture region close to the left side of the screen such that
// if the label was centered it would extend out of the screen.
// The x value of the label should be the left edge of the screen (0).
capture_region.SetRect(2, 100, 2, 100);
SelectRegion(capture_region, /*release_mouse=*/false);
EXPECT_EQ(0, GetDimensionsLabelWindow()->bounds().x());
generator->ReleaseLeftButton();
EXPECT_EQ(nullptr, GetDimensionsLabelWindow());
// Create a new capture region close to the right side of the screen such that
// if the label was centered it would extend out of the screen.
// The right (x + width) of the label should be the right edge of the screen
// (800).
capture_region.SetRect(796, 100, 2, 100);
SelectRegion(capture_region, /*release_mouse=*/false);
EXPECT_EQ(800, GetDimensionsLabelWindow()->bounds().right());
generator->ReleaseLeftButton();
EXPECT_EQ(nullptr, GetDimensionsLabelWindow());
// Create a new capture region close to the bottom side of the screen.
// The label should now appear inside the capture region, just above the
// bottom edge. It should be above the bottom of the screen as well.
capture_region.SetRect(100, 700, 600, 790);
SelectRegion(capture_region, /*release_mouse=*/false);
EXPECT_EQ(800 - CaptureModeSession::kSizeLabelYDistanceFromRegionDp,
GetDimensionsLabelWindow()->bounds().bottom());
generator->ReleaseLeftButton();
EXPECT_EQ(nullptr, GetDimensionsLabelWindow());
}
TEST_F(CaptureModeTest, CaptureRegionCaptureButtonLocation) {
UpdateDisplay("800x800");
auto* controller = StartImageRegionCapture();
// Select a large region. Verify that the capture button widget is centered.
SelectRegion(gfx::Rect(100, 100, 600, 600));
views::Widget* capture_button_widget = GetCaptureModeLabelWidget();
ASSERT_TRUE(capture_button_widget);
aura::Window* capture_button_window =
capture_button_widget->GetNativeWindow();
EXPECT_EQ(gfx::Point(400, 400),
capture_button_window->bounds().CenterPoint());
// Drag the bottom corner so that the region is too small to fit the capture
// button. Verify that the button is aligned horizontally and placed below the
// region.
auto* event_generator = GetEventGenerator();
event_generator->DragMouseTo(gfx::Point(120, 120));
EXPECT_EQ(gfx::Rect(100, 100, 20, 20), controller->user_capture_region());
EXPECT_EQ(110, capture_button_window->bounds().CenterPoint().x());
const int distance_from_region =
CaptureModeSession::kCaptureButtonDistanceFromRegionDp;
EXPECT_EQ(120 + distance_from_region, capture_button_window->bounds().y());
// Click inside the region to drag the entire region to the bottom of the
// screen. Verify that the button is aligned horizontally and placed above the
// region.