File: capture_mode_game_dashboard_unittests.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (1316 lines) | stat: -rw-r--r-- 55,780 bytes parent folder | download | duplicates (6)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
// 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 "ash/capture_mode/capture_mode_bar_view.h"
#include "ash/capture_mode/capture_mode_camera_preview_view.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_session_focus_cycler.h"
#include "ash/capture_mode/capture_mode_session_test_api.h"
#include "ash/capture_mode/capture_mode_settings_test_api.h"
#include "ash/capture_mode/capture_mode_settings_view.h"
#include "ash/capture_mode/capture_mode_test_util.h"
#include "ash/capture_mode/capture_mode_types.h"
#include "ash/capture_mode/test_capture_mode_delegate.h"
#include "ash/constants/ash_features.h"
#include "ash/constants/ash_pref_names.h"
#include "ash/display/window_tree_host_manager.h"
#include "ash/game_dashboard/game_dashboard_context_test_api.h"
#include "ash/game_dashboard/game_dashboard_controller.h"
#include "ash/public/cpp/capture_mode/capture_mode_test_api.h"
#include "ash/public/cpp/window_properties.h"
#include "ash/screen_util.h"
#include "ash/session/session_controller_impl.h"
#include "ash/shell.h"
#include "ash/strings/grit/ash_strings.h"
#include "ash/style/pill_button.h"
#include "ash/system/unified/feature_tile.h"
#include "ash/test/ash_test_base.h"
#include "ash/test/ash_test_util.h"
#include "ash/wm/desks/desks_test_util.h"
#include "ash/wm/tablet_mode/tablet_mode_controller_test_api.h"
#include "base/system/sys_info.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/test/scoped_feature_list.h"
#include "chromeos/ui/base/window_properties.h"
#include "extensions/common/constants.h"
#include "ui/base/cursor/mojom/cursor_type.mojom-shared.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/wm/core/window_util.h"

namespace message_center {

bool operator==(const ButtonInfo& lhs, const ButtonInfo& rhs) {
  return std::tie(lhs.title, lhs.icon, lhs.placeholder, lhs.type) ==
         std::tie(rhs.title, rhs.icon, rhs.placeholder, rhs.type);
}

}  // namespace message_center

namespace ash {

namespace {

using ::ui::mojom::CursorType;

}  // namespace

using ButtonInfo = message_center::ButtonInfo;

class GameDashboardCaptureModeTest : public AshTestBase {
 public:
  GameDashboardCaptureModeTest() {
    scoped_feature_list_.InitWithFeatures(
        /*enabled_features=*/{features::
                                  kFeatureManagementGameDashboardRecordGame},
        /*disabled_features=*/{});
  }
  GameDashboardCaptureModeTest(const GameDashboardCaptureModeTest&) = delete;
  GameDashboardCaptureModeTest& operator=(const GameDashboardCaptureModeTest&) =
      delete;
  ~GameDashboardCaptureModeTest() override = default;

  aura::Window* game_window() const { return game_window_.get(); }
  void CloseGameWindow() { game_window_.reset(); }

  // AshTestBase:
  void SetUp() override {
    AshTestBase::SetUp();

    // Disable the Game Dashboard welcome dialog for all game windows.
    PrefService* active_user_prefs =
        Shell::Get()->session_controller()->GetActivePrefService();
    ASSERT_TRUE(active_user_prefs);
    active_user_prefs->SetBoolean(prefs::kGameDashboardShowWelcomeDialog,
                                  false);

    game_window_ = CreateAppWindow(gfx::Rect(0, 100, 300, 200));
    game_window_->SetProperty(kAppIDKey,
                              std::string(extension_misc::kGeForceNowAppId));
  }

  void TearDown() override {
    game_window_.reset();
    AshTestBase::TearDown();
  }

  CaptureModeController* StartGameCaptureModeSession() {
    auto* controller = CaptureModeController::Get();
    controller->StartForGameDashboard(game_window_.get());
    CHECK(controller->IsActive());
    return controller;
  }

  // Verifies that the game capture bar is inside the selected game window. And
  // centered above the fixed distance `kCaptureBarBottomPadding` from the
  // bottom of the window.
  void VerifyCaptureBarPosition() {
    views::Widget* bar_widget = GetCaptureModeBarWidget();
    CHECK(bar_widget);
    const gfx::Rect window_bounds = game_window()->GetBoundsInScreen();
    const gfx::Rect bar_bounds = bar_widget->GetWindowBoundsInScreen();
    EXPECT_TRUE(window_bounds.Contains(bar_bounds));
    EXPECT_EQ(bar_bounds.CenterPoint().x(), window_bounds.CenterPoint().x());
    EXPECT_EQ(bar_bounds.bottom() + capture_mode::kGameCaptureBarBottomPadding,
              window_bounds.bottom());
  }

 private:
  base::test::ScopedFeatureList scoped_feature_list_;

  std::unique_ptr<aura::Window> game_window_;
};

TEST_F(GameDashboardCaptureModeTest, GameDashboardBehavior) {
  CaptureModeController* controller = StartGameCaptureModeSession();
  BaseCaptureModeSession* session = controller->capture_mode_session();
  CaptureModeBehavior* active_behavior = session->active_behavior();
  ASSERT_TRUE(active_behavior);

  EXPECT_FALSE(active_behavior->ShouldImageCaptureTypeBeAllowed());
  EXPECT_TRUE(active_behavior->ShouldVideoCaptureTypeBeAllowed());
  EXPECT_FALSE(active_behavior->ShouldFulscreenCaptureSourceBeAllowed());
  EXPECT_FALSE(active_behavior->ShouldRegionCaptureSourceBeAllowed());
  EXPECT_TRUE(active_behavior->ShouldWindowCaptureSourceBeAllowed());
  EXPECT_TRUE(
      active_behavior->SupportsAudioRecordingMode(AudioRecordingMode::kOff));
  EXPECT_TRUE(active_behavior->SupportsAudioRecordingMode(
      AudioRecordingMode::kSystemAndMicrophone));
  EXPECT_TRUE(active_behavior->ShouldCameraSelectionSettingsBeIncluded());
  EXPECT_FALSE(active_behavior->ShouldDemoToolsSettingsBeIncluded());
  EXPECT_TRUE(active_behavior->ShouldSaveToSettingsBeIncluded());
  EXPECT_FALSE(active_behavior->ShouldGifBeSupported());
  EXPECT_TRUE(active_behavior->ShouldShowPreviewNotification());
  EXPECT_FALSE(active_behavior->ShouldSkipVideoRecordingCountDown());
  EXPECT_FALSE(active_behavior->ShouldCreateAnnotationsOverlayController());
  EXPECT_FALSE(active_behavior->ShouldShowUserNudge());
  EXPECT_TRUE(active_behavior->ShouldAutoSelectFirstCamera());
}

// Tests that a fullscreen screenshot can be taken via the keyboard shortcut
// while a Game-Dashboard-initiated session is active without ending the
// session.
TEST_F(GameDashboardCaptureModeTest, FullscreenScreenshotKeyCombo) {
  StartGameCaptureModeSession();
  PressAndReleaseKey(ui::VKEY_MEDIA_LAUNCH_APP1, ui::EF_CONTROL_DOWN);
  WaitForCaptureFileToBeSaved();
  VerifyActiveBehavior(BehaviorType::kGameDashboard);
}

// Tests that if the user presses the shortcut to switch to default capture
// mode, it is ignored.
TEST_F(GameDashboardCaptureModeTest, SwitchToDefaultCaptureMode) {
  StartGameCaptureModeSession();
  VerifyActiveBehavior(BehaviorType::kGameDashboard);
  PressAndReleaseKey(ui::VKEY_MEDIA_LAUNCH_APP1,
                     ui::EF_SHIFT_DOWN | ui::EF_CONTROL_DOWN);
  VerifyActiveBehavior(BehaviorType::kGameDashboard);
}

// Tests that when starting the capture mode session from game dashboard, the
// window is pre-selected and won't be altered on mouse hover during the
// session. On the destroying of the pre-selected window, the selected window
// will be reset.
TEST_F(GameDashboardCaptureModeTest, StartForGameDashboardTest) {
  UpdateDisplay("1000x700");
  std::unique_ptr<aura::Window> other_window(
      CreateAppWindow(gfx::Rect(0, 300, 500, 300)));
  CaptureModeController* controller = StartGameCaptureModeSession();
  BaseCaptureModeSession* capture_mode_session =
      controller->capture_mode_session();
  ASSERT_TRUE(capture_mode_session);
  EXPECT_EQ(capture_mode_session->GetSelectedWindow(), game_window());

  // The selected window will not change when mouse hovers on `other_window`.
  auto* event_generator = GetEventGenerator();
  event_generator->MoveMouseToCenterOf(other_window.get());
  EXPECT_EQ(capture_mode_session->GetSelectedWindow(), game_window());

  CloseGameWindow();
  EXPECT_FALSE(controller->IsActive());
}

TEST_F(GameDashboardCaptureModeTest, CaptureBar) {
  CaptureModeController* controller = StartGameCaptureModeSession();

  views::Widget* bar_widget = GetCaptureModeBarWidget();
  ASSERT_TRUE(bar_widget);

  auto* start_recording_button = GetStartRecordingButton();
  // Checks that the game capture bar only includes the start recording button,
  // settings button and close button.
  EXPECT_TRUE(start_recording_button);
  EXPECT_FALSE(GetImageToggleButton());
  EXPECT_FALSE(GetVideoToggleButton());
  EXPECT_FALSE(GetFullscreenToggleButton());
  EXPECT_FALSE(GetRegionToggleButton());
  EXPECT_FALSE(GetWindowToggleButton());
  EXPECT_TRUE(GetSettingsButton());
  EXPECT_TRUE(GetCloseButton());

  BaseCaptureModeSession* session = controller->capture_mode_session();
  EXPECT_EQ(game_window(), session->GetSelectedWindow());
  // Clicking the start recording button should start the video recording.
  ClickOnView(start_recording_button, GetEventGenerator());
  WaitForRecordingToStart();
  EXPECT_TRUE(controller->is_recording_in_progress());
}

TEST_F(GameDashboardCaptureModeTest, CaptureBarPosition) {
  StartGameCaptureModeSession();
  views::Widget* bar_widget = GetCaptureModeBarWidget();
  ASSERT_TRUE(bar_widget);

  VerifyCaptureBarPosition();

  // Switching to the tablet mode, the game capture bar should still be inside
  // the window. And centered above the constant distance from the bottom of the
  // window.
  SwitchToTabletMode();
  VerifyCaptureBarPosition();

  // Switching back to the clamshell mode, the game capture bar should be
  // positioned back to the constant distance from the bottom center of the
  // window.
  LeaveTabletMode();
  VerifyCaptureBarPosition();
}

TEST_F(GameDashboardCaptureModeTest, CaptureBarPositionOnDisplayRotation) {
  StartGameCaptureModeSession();
  views::Widget* bar_widget = GetCaptureModeBarWidget();
  ASSERT_TRUE(bar_widget);

  VerifyCaptureBarPosition();

  auto* display_manager = Shell::Get()->display_manager();
  const int64_t display_id = WindowTreeHostManager::GetPrimaryDisplayId();

  // Verifies that the capture bar is still at the bottom center position inside
  // the selected window after display rotation.
  for (const auto rotation :
       {display::Display::ROTATE_90, display::Display::ROTATE_180,
        display::Display::ROTATE_270}) {
    display_manager->SetDisplayRotation(display_id, rotation,
                                        display::Display::RotationSource::USER);
    VerifyCaptureBarPosition();
  }
}

// Tests that the game dashboard-initiated capture mode session shows the
// notification view with 'Share to YouTube' button and 'delete' buttons.
TEST_F(GameDashboardCaptureModeTest, NotificationView) {
  CaptureModeController* controller = StartGameCaptureModeSession();
  BaseCaptureModeSession* session = controller->capture_mode_session();
  CaptureModeBehavior* active_behavior = session->active_behavior();
  ASSERT_TRUE(active_behavior);
  StartVideoRecordingImmediately();
  CaptureModeTestApi().FlushRecordingServiceForTesting();

  auto* test_delegate =
      static_cast<TestCaptureModeDelegate*>(controller->delegate_for_testing());

  // Request and wait for a video frame so that the recording service can use it
  // to create a video thumbnail.
  test_delegate->RequestAndWaitForVideoFrame();
  SkBitmap service_thumbnail =
      gfx::Image(test_delegate->GetVideoThumbnail()).AsBitmap();
  EXPECT_FALSE(service_thumbnail.drawsNothing());

  controller->EndVideoRecording(EndRecordingReason::kStopRecordingButton);
  EXPECT_FALSE(controller->is_recording_in_progress());
  EXPECT_FALSE(controller->can_start_new_recording());
  CaptureNotificationWaiter().Wait();
  EXPECT_TRUE(controller->can_start_new_recording());

  const message_center::Notification* notification = GetPreviewNotification();
  EXPECT_TRUE(notification);
  EXPECT_FALSE(notification->image().IsEmpty());

  std::vector<ButtonInfo> expected_buttons_info = {
      ButtonInfo(
          l10n_util::GetStringUTF16(IDS_ASH_SCREEN_CAPTURE_SHARE_TO_YOUTUBE)),
      ButtonInfo(
          l10n_util::GetStringUTF16(IDS_ASH_SCREEN_CAPTURE_BUTTON_DELETE))};
  auto actual_buttons_info =
      active_behavior->GetNotificationButtonsInfo(/*for_video=*/true);
  EXPECT_EQ(actual_buttons_info.size(), 2u);
  EXPECT_TRUE(actual_buttons_info == expected_buttons_info);

  const int share_to_youtube_button = 0;
  ClickOnNotification(share_to_youtube_button);
  EXPECT_FALSE(GetPreviewNotification());
}

// Tests that the camera preview widget shows up when starting the game
// dashboard initiated capture mode session for the first time.
TEST_F(GameDashboardCaptureModeTest, CameraPreviewWidgetTest) {
  AddDefaultCamera();
  auto* camera_controller = CaptureModeController::Get()->camera_controller();
  EXPECT_FALSE(camera_controller->selected_camera().is_valid());

  auto* controller = StartGameCaptureModeSession();
  EXPECT_TRUE(camera_controller->selected_camera().is_valid());
  EXPECT_TRUE(camera_controller->should_show_preview());
  GetEventGenerator()->MoveMouseToCenterOf(game_window());
  EXPECT_TRUE(camera_controller->camera_preview_widget());

  controller->StartVideoRecordingImmediatelyForTesting();
  EXPECT_TRUE(camera_controller->should_show_preview());
  EXPECT_TRUE(camera_controller->camera_preview_widget());

  controller->EndVideoRecording(EndRecordingReason::kStopRecordingButton);
  EXPECT_FALSE(camera_controller->should_show_preview());
  EXPECT_FALSE(camera_controller->camera_preview_widget());
}

TEST_F(GameDashboardCaptureModeTest, FocusNavigationOfCaptureBar) {
  UpdateDisplay("1200x1100");
  AddDefaultCamera();
  auto* camera_controller = CaptureModeController::Get()->camera_controller();
  auto* controller = StartGameCaptureModeSession();
  ASSERT_TRUE(GetCaptureModeBarWidget());
  EXPECT_EQ(controller->capture_mode_session()->GetSelectedWindow(),
            game_window());
  // Make the selected window large enough to hold collapsible camera preview.
  game_window()->SetBounds({0, 0, 800, 700});

  CaptureModeSessionTestApi test_api(controller->capture_mode_session());
  using FocusGroup = CaptureModeSessionFocusCycler::FocusGroup;
  auto* event_generator = GetEventGenerator();

  // First tab should focus on the start recording button.
  auto* start_recording_button = GetStartRecordingButton();
  SendKey(ui::VKEY_TAB, event_generator);
  EXPECT_EQ(FocusGroup::kStartRecordingButton, test_api.GetCurrentFocusGroup());
  EXPECT_EQ(0u, test_api.GetCurrentFocusIndex());
  EXPECT_TRUE(CaptureModeSessionFocusCycler::HighlightHelper::Get(
                  start_recording_button)
                  ->has_focus());

  // Tab again should advance the focus to the camera preview.
  auto* camera_preview_view = camera_controller->camera_preview_view();
  SendKey(ui::VKEY_TAB, event_generator);
  EXPECT_EQ(FocusGroup::kCameraPreview, test_api.GetCurrentFocusGroup());
  EXPECT_EQ(0u, test_api.GetCurrentFocusIndex());
  EXPECT_TRUE(camera_preview_view->has_focus());

  // Tab again should advance the focus to the resize button inside the camera
  // preview.
  auto* resize_button = camera_preview_view->resize_button();
  SendKey(ui::VKEY_TAB, event_generator);
  EXPECT_EQ(FocusGroup::kCameraPreview, test_api.GetCurrentFocusGroup());
  EXPECT_EQ(1u, test_api.GetCurrentFocusIndex());
  EXPECT_TRUE(resize_button->has_focus());

  // Tab again should advance the focus to the settings button.
  auto* settings_button = GetSettingsButton();
  SendKey(ui::VKEY_TAB, event_generator);
  EXPECT_EQ(FocusGroup::kSettingsClose, test_api.GetCurrentFocusGroup());
  EXPECT_EQ(0u, test_api.GetCurrentFocusIndex());
  EXPECT_TRUE(
      CaptureModeSessionFocusCycler::HighlightHelper::Get(settings_button)
          ->has_focus());

  // Tab again should advance the focus to the close button.
  auto* close_button = GetCloseButton();
  SendKey(ui::VKEY_TAB, event_generator);
  EXPECT_EQ(FocusGroup::kSettingsClose, test_api.GetCurrentFocusGroup());
  EXPECT_EQ(1u, test_api.GetCurrentFocusIndex());
  EXPECT_TRUE(CaptureModeSessionFocusCycler::HighlightHelper::Get(close_button)
                  ->has_focus());

  // Shift tab should advance the focus from the close button to the settings
  // button.
  SendKey(ui::VKEY_TAB, event_generator, ui::EF_SHIFT_DOWN);
  EXPECT_EQ(FocusGroup::kSettingsClose, test_api.GetCurrentFocusGroup());
  EXPECT_EQ(0u, test_api.GetCurrentFocusIndex());
  EXPECT_TRUE(
      CaptureModeSessionFocusCycler::HighlightHelper::Get(settings_button)
          ->has_focus());

  // Shift tab again should advance the focus from the settings button to the
  // resize button inside the camera preview.
  SendKey(ui::VKEY_TAB, event_generator, ui::EF_SHIFT_DOWN);
  EXPECT_EQ(FocusGroup::kCameraPreview, test_api.GetCurrentFocusGroup());
  EXPECT_EQ(1u, test_api.GetCurrentFocusIndex());
  EXPECT_TRUE(resize_button->has_focus());

  // Shift tab again should advance the focus from the resize button to the
  // camera preview.
  SendKey(ui::VKEY_TAB, event_generator, ui::EF_SHIFT_DOWN);
  EXPECT_EQ(FocusGroup::kCameraPreview, test_api.GetCurrentFocusGroup());
  EXPECT_EQ(0u, test_api.GetCurrentFocusIndex());
  EXPECT_TRUE(camera_preview_view->has_focus());

  // Shift tab again should advance the focus from the camera preview to the
  // start recording button.
  SendKey(ui::VKEY_TAB, event_generator, ui::EF_SHIFT_DOWN);
  EXPECT_EQ(FocusGroup::kStartRecordingButton, test_api.GetCurrentFocusGroup());
  EXPECT_EQ(0u, test_api.GetCurrentFocusIndex());
  EXPECT_TRUE(CaptureModeSessionFocusCycler::HighlightHelper::Get(
                  start_recording_button)
                  ->has_focus());
}

TEST_F(GameDashboardCaptureModeTest, GameCaptureModeSessionConfigs) {
  // Verify capture mode session configs for the game dashboard initiated
  // capture session.
  auto* controller = StartGameCaptureModeSession();
  EXPECT_EQ(controller->type(), CaptureModeType::kVideo);
  EXPECT_EQ(controller->source(), CaptureModeSource::kWindow);
  EXPECT_EQ(controller->recording_type(), RecordingType::kWebM);
  EXPECT_EQ(controller->audio_recording_mode(),
            AudioRecordingMode::kSystemAndMicrophone);
  EXPECT_EQ(controller->enable_demo_tools(), false);

  // Update the audio recording mode and demo tools configs and stop the
  // session.
  controller->SetAudioRecordingMode(AudioRecordingMode::kSystem);
  controller->EnableDemoTools(true);
  controller->Stop();

  // Start another game dashboard initiated capture mode session and verify
  // that the audio recording mode and demo tools settings are restored from
  // previous session.
  StartGameCaptureModeSession();
  EXPECT_EQ(controller->type(), CaptureModeType::kVideo);
  EXPECT_EQ(controller->source(), CaptureModeSource::kWindow);
  EXPECT_EQ(controller->recording_type(), RecordingType::kWebM);
  EXPECT_EQ(controller->audio_recording_mode(), AudioRecordingMode::kSystem);
  EXPECT_TRUE(controller->enable_demo_tools());
  controller->Stop();

  // Verify that the session configs from the game dashboard initiated capture
  // mode session will not be carried over to the default capture mode session.
  StartCaptureSession(CaptureModeSource::kFullscreen, CaptureModeType::kImage);
  EXPECT_EQ(controller->type(), CaptureModeType::kImage);
  EXPECT_EQ(controller->source(), CaptureModeSource::kFullscreen);
  EXPECT_EQ(controller->recording_type(), RecordingType::kWebM);
  EXPECT_EQ(controller->audio_recording_mode(), AudioRecordingMode::kOff);
  EXPECT_FALSE(controller->enable_demo_tools());
  controller->Stop();
}

TEST_F(GameDashboardCaptureModeTest, MultiDisplay) {
  UpdateDisplay("800x700,801+0-900x800");
  const auto& displays = display_manager()->active_display_list();
  ASSERT_EQ(2u, displays.size());
  EXPECT_EQ(displays[0].size(), gfx::Size(800, 700));
  EXPECT_EQ(displays[1].size(), gfx::Size(900, 800));

  display::Screen* screen = display::Screen::GetScreen();
  auto* controller = StartGameCaptureModeSession();
  BaseCaptureModeSession* capture_mode_session =
      controller->capture_mode_session();
  auto* event_generator = GetEventGenerator();
  EXPECT_EQ(displays[0].id(),
            screen->GetDisplayNearestWindow(game_window()).id());
  EXPECT_EQ(Shell::GetAllRootWindows()[0],
            capture_mode_session->current_root());
  VerifyCaptureBarPosition();
  // The current root window should not change if moving the cursor to a
  // different display as the game window.
  event_generator->MoveMouseTo(displays[1].bounds().CenterPoint());
  EXPECT_EQ(Shell::GetAllRootWindows()[0],
            capture_mode_session->current_root());

  // Using the shortcut ALT+SEARCH+M to move the window to another display.
  PressAndReleaseKey(ui::VKEY_M, ui::EF_COMMAND_DOWN | ui::EF_ALT_DOWN);
  // Verifies that the capture bar and the current root window of the capture
  // mode session are updated correctly after moving the game window to another
  // display.
  EXPECT_EQ(displays[1].id(),
            screen->GetDisplayNearestWindow(game_window()).id());
  EXPECT_EQ(Shell::GetAllRootWindows()[1],
            capture_mode_session->current_root());
  VerifyCaptureBarPosition();
  // The current root window should not change if moving the cursor to a
  // different display as the game window.
  event_generator->MoveMouseTo(displays[0].bounds().CenterPoint());
  EXPECT_EQ(Shell::GetAllRootWindows()[1],
            capture_mode_session->current_root());

  // Using the shortcut ALT+SEARCH+M to move the window back to the previous
  // display.
  PressAndReleaseKey(ui::VKEY_M, ui::EF_COMMAND_DOWN | ui::EF_ALT_DOWN);
  // Verifies the capture bar and the current root window after moving the game
  // window back to the previous display.
  EXPECT_EQ(displays[0].id(),
            screen->GetDisplayNearestWindow(game_window()).id());
  EXPECT_EQ(Shell::GetAllRootWindows()[0],
            capture_mode_session->current_root());
  VerifyCaptureBarPosition();
}

TEST_F(GameDashboardCaptureModeTest, NoLabelInTabletMode) {
  auto* controller = StartGameCaptureModeSession();

  BaseCaptureModeSession* session = controller->capture_mode_session();
  ASSERT_EQ(game_window(), session->GetSelectedWindow());

  SwitchToTabletMode();

  views::Label* label_internal_view =
      CaptureModeSessionTestApi(session).GetCaptureLabelInternalView();
  EXPECT_FALSE(label_internal_view->GetVisible());
}

TEST_F(GameDashboardCaptureModeTest, SettingsMenuHeightFullAboveBar) {
  UpdateDisplay("1000x500");
  game_window()->SetBounds(gfx::Rect(0, 50, 500, 450));
  StartGameCaptureModeSession();
  BaseCaptureModeSession* session =
      CaptureModeController::Get()->capture_mode_session();
  ASSERT_TRUE(session);

  // Open the settings menu.
  ClickOnView(GetSettingsButton(), GetEventGenerator());
  CaptureModeSessionTestApi test_api(session);
  ASSERT_TRUE(test_api.GetCaptureModeSettingsWidget());

  const gfx::Rect settings_bounds =
      test_api.GetCaptureModeSettingsWidget()->GetWindowBoundsInScreen();

  // There is space for the settings menu to take up its entire preferred size
  // above the bar view.
  EXPECT_GE(settings_bounds.y(),
            capture_mode::kMinDistanceFromSettingsToScreen);
  EXPECT_EQ(settings_bounds.height(),
            test_api.GetCaptureModeSettingsView()->GetPreferredSize().height());
  EXPECT_EQ(
      settings_bounds.size(),
      CaptureModeSettingsTestApi().GetSettingsView()->GetVisibleRect().size());
}

TEST_F(GameDashboardCaptureModeTest, SettingsMenuHeightFullBelowBar) {
  UpdateDisplay("1000x500");
  game_window()->SetBounds(gfx::Rect(0, 0, 500, 100));
  StartGameCaptureModeSession();
  BaseCaptureModeSession* session =
      CaptureModeController::Get()->capture_mode_session();
  ASSERT_TRUE(session);

  // Open the settings menu.
  ClickOnView(GetSettingsButton(), GetEventGenerator());
  CaptureModeSessionTestApi test_api(session);
  ASSERT_TRUE(test_api.GetCaptureModeSettingsWidget());

  const gfx::Rect settings_bounds =
      test_api.GetCaptureModeSettingsWidget()->GetWindowBoundsInScreen();

  // There is space for the settings menu to take up its entire preferred size
  // below the bar view, but not above it.
  const auto bar_bounds = test_api.GetCaptureModeBarView()->GetBoundsInScreen();
  EXPECT_EQ(settings_bounds.y(),
            bar_bounds.bottom() +
                capture_mode::kSpaceBetweenCaptureBarAndSettingsMenu);
  EXPECT_GE(settings_bounds.y(),
            capture_mode::kMinDistanceFromSettingsToScreen);
  EXPECT_EQ(settings_bounds.height(),
            test_api.GetCaptureModeSettingsView()->GetPreferredSize().height());
  EXPECT_EQ(
      settings_bounds.size(),
      CaptureModeSettingsTestApi().GetSettingsView()->GetVisibleRect().size());
}

TEST_F(GameDashboardCaptureModeTest, SettingsMenuHeightConstrainedAboveBar) {
  UpdateDisplay("1000x250");
  game_window()->SetBounds(gfx::Rect(0, 50, 500, 200));
  StartGameCaptureModeSession();
  BaseCaptureModeSession* session =
      CaptureModeController::Get()->capture_mode_session();
  ASSERT_TRUE(session);

  // Open the settings menu.
  ClickOnView(GetSettingsButton(), GetEventGenerator());
  CaptureModeSessionTestApi test_api(session);
  ASSERT_TRUE(test_api.GetCaptureModeSettingsWidget());

  const gfx::Rect settings_bounds =
      test_api.GetCaptureModeSettingsWidget()->GetWindowBoundsInScreen();

  // The menu height has been constrained, but has not reached its minimum size.
  EXPECT_EQ(settings_bounds.y(),
            capture_mode::kMinDistanceFromSettingsToScreen);
  EXPECT_LT(settings_bounds.height(),
            test_api.GetCaptureModeSettingsView()->GetPreferredSize().height());
  EXPECT_GT(settings_bounds.height(), capture_mode::kSettingsMenuMinHeight);
  EXPECT_EQ(
      settings_bounds.size(),
      CaptureModeSettingsTestApi().GetSettingsView()->GetVisibleRect().size());
}

TEST_F(GameDashboardCaptureModeTest, SettingsMenuHeightConstrainedBelowBar) {
  UpdateDisplay("1000x500");
  game_window()->SetBounds(gfx::Rect(0, 0, 500, 200));
  StartGameCaptureModeSession();
  BaseCaptureModeSession* session =
      CaptureModeController::Get()->capture_mode_session();
  ASSERT_TRUE(session);

  // Open the settings menu.
  ClickOnView(GetSettingsButton(), GetEventGenerator());
  CaptureModeSessionTestApi test_api(session);
  ASSERT_TRUE(test_api.GetCaptureModeSettingsWidget());

  const gfx::Rect settings_bounds =
      test_api.GetCaptureModeSettingsWidget()->GetWindowBoundsInScreen();

  // The menu height has been constrained, but has not reached its minimum size.
  const auto bar_bounds = test_api.GetCaptureModeBarView()->GetBoundsInScreen();
  EXPECT_EQ(settings_bounds.y(),
            bar_bounds.bottom() +
                capture_mode::kSpaceBetweenCaptureBarAndSettingsMenu);
  EXPECT_LT(settings_bounds.height(),
            test_api.GetCaptureModeSettingsView()->GetPreferredSize().height());
  EXPECT_GT(settings_bounds.height(), capture_mode::kSettingsMenuMinHeight);
  EXPECT_EQ(
      settings_bounds.size(),
      CaptureModeSettingsTestApi().GetSettingsView()->GetVisibleRect().size());
}

TEST_F(GameDashboardCaptureModeTest, SettingsMenuHeightMinimumAboveBar) {
  UpdateDisplay("1000x100");
  game_window()->SetBounds(gfx::Rect(0, 50, 500, 50));
  StartGameCaptureModeSession();
  BaseCaptureModeSession* session =
      CaptureModeController::Get()->capture_mode_session();
  ASSERT_TRUE(session);

  // Open the settings menu.
  ClickOnView(GetSettingsButton(), GetEventGenerator());
  CaptureModeSessionTestApi test_api(session);
  ASSERT_TRUE(test_api.GetCaptureModeSettingsWidget());

  const gfx::Rect settings_bounds =
      test_api.GetCaptureModeSettingsWidget()->GetWindowBoundsInScreen();

  // The menu is at its minimum size and height above the bar view.
  EXPECT_EQ(settings_bounds.y(),
            capture_mode::kMinDistanceFromSettingsToScreen);
  EXPECT_EQ(settings_bounds.height(), capture_mode::kSettingsMenuMinHeight);
  EXPECT_EQ(
      settings_bounds.size(),
      CaptureModeSettingsTestApi().GetSettingsView()->GetVisibleRect().size());
}

TEST_F(GameDashboardCaptureModeTest, StopOnWindowSentToDifferentDesk) {
  NewDesk();
  auto* controller = StartGameCaptureModeSession();

  // Send the window to a different desk using the accelerator.
  PressAndReleaseKey(ui::VKEY_OEM_6, ui::EF_COMMAND_DOWN | ui::EF_SHIFT_DOWN);
  EXPECT_FALSE(controller->IsActive());
}

TEST_F(GameDashboardCaptureModeTest, StopOnActiveDeskChanged) {
  NewDesk();
  auto* controller = StartGameCaptureModeSession();

  // Switch to the new desk using the accelerator.
  DeskSwitchAnimationWaiter waiter;
  PressAndReleaseKey(ui::VKEY_OEM_6, ui::EF_COMMAND_DOWN);
  waiter.Wait();
  EXPECT_FALSE(controller->IsActive());
}

TEST_F(GameDashboardCaptureModeTest, SettingsMenuHeightMinimumBelowBar) {
  UpdateDisplay("1000x100");
  game_window()->SetBounds(gfx::Rect(0, 0, 500, 50));
  StartGameCaptureModeSession();
  BaseCaptureModeSession* session =
      CaptureModeController::Get()->capture_mode_session();
  ASSERT_TRUE(session);

  // Open the settings menu.
  ClickOnView(GetSettingsButton(), GetEventGenerator());
  CaptureModeSessionTestApi test_api(session);
  ASSERT_TRUE(test_api.GetCaptureModeSettingsWidget());

  const gfx::Rect settings_bounds =
      test_api.GetCaptureModeSettingsWidget()->GetWindowBoundsInScreen();

  // The menu is at its minimum size and height below the bar view.
  const auto bar_bounds = test_api.GetCaptureModeBarView()->GetBoundsInScreen();
  EXPECT_EQ(settings_bounds.y(),
            bar_bounds.bottom() +
                capture_mode::kSpaceBetweenCaptureBarAndSettingsMenu);
  EXPECT_EQ(settings_bounds.height(), capture_mode::kSettingsMenuMinHeight);
  EXPECT_EQ(
      settings_bounds.size(),
      CaptureModeSettingsTestApi().GetSettingsView()->GetVisibleRect().size());
}

TEST_F(GameDashboardCaptureModeTest, GameCaptureModeRecordInstantlyTest) {
  AddDefaultCamera();

  // Start a game dashboard initiated capture mode session and check the initial
  // configs for game dashboard initiated capture mode.
  auto* controller = StartGameCaptureModeSession();
  EXPECT_FALSE(controller->enable_demo_tools());
  EXPECT_EQ(controller->audio_recording_mode(),
            AudioRecordingMode::kSystemAndMicrophone);

  auto* camera_controller = controller->camera_controller();
  ASSERT_TRUE(camera_controller->camera_preview_widget());
  const CameraId camera_id(kDefaultCameraModelId, 1);
  EXPECT_EQ(camera_id, camera_controller->selected_camera());

  // Update the audio recording mode and demo tools configs for the
  // game-dashboard initiated capture mode.
  controller->SetAudioRecordingMode(AudioRecordingMode::kOff);
  controller->EnableDemoTools(true);
  controller->Stop();

  // Start the recording with the game dashboard instant recording API.
  controller->StartRecordingInstantlyForGameDashboard(game_window());

  // Verify that recording starts immediately.
  EXPECT_TRUE(controller->is_recording_in_progress());

  // Verify that the game dashboard capture mode configs are not overwritten.
  CaptureModeBehavior* behavior =
      CaptureModeTestApi().GetBehavior(BehaviorType::kGameDashboard);
  const auto capture_mode_configs = behavior->capture_mode_configs();
  EXPECT_EQ(capture_mode_configs.audio_recording_mode,
            AudioRecordingMode::kOff);
  EXPECT_TRUE(capture_mode_configs.demo_tools_enabled);

  // Verify that the configs in `CaptureModeController` are restored.
  EXPECT_EQ(controller->audio_recording_mode(), AudioRecordingMode::kOff);
  EXPECT_FALSE(controller->enable_demo_tools());

  // Verify that selfie camera is visible and is parented correctly to the game
  // window.
  const auto* camera_preview_widget =
      camera_controller->camera_preview_widget();
  ASSERT_TRUE(camera_preview_widget);
  EXPECT_EQ(camera_preview_widget->GetNativeWindow()->parent(), game_window());
}

TEST_F(GameDashboardCaptureModeTest, UserTurnsOffCamera) {
  AddDefaultCamera();

  // By default, the first available camera should be auto selected.
  auto* controller = StartGameCaptureModeSession();
  auto* camera_controller = controller->camera_controller();
  ASSERT_TRUE(camera_controller->camera_preview_widget());
  const CameraId camera_id(kDefaultCameraModelId, 1);
  EXPECT_EQ(camera_id, camera_controller->selected_camera());

  // Now, open the settings menu, and select the "camera off" option.
  LeftClickOn(GetSettingsButton());
  CaptureModeSettingsTestApi test_api;
  CaptureModeMenuGroup* camera_menu_group = test_api.GetCameraMenuGroup();
  ASSERT_TRUE(camera_menu_group);
  EXPECT_TRUE(camera_menu_group->GetVisible());
  auto* off_option = test_api.GetCameraOption(kCameraOff);
  EXPECT_TRUE(off_option);
  LeftClickOn(off_option);

  // No camera should be selected, and the preview widget should be closed.
  EXPECT_FALSE(camera_controller->selected_camera().is_valid());
  EXPECT_FALSE(camera_controller->camera_preview_widget());

  // Close the session and start a new one. No camera should be auto selected.
  controller->Stop();
  StartGameCaptureModeSession();
  EXPECT_FALSE(camera_controller->selected_camera().is_valid());
  EXPECT_FALSE(camera_controller->camera_preview_widget());
  controller->Stop();

  // Start a new default session and select a camera, then end the session.
  StartCaptureSession(CaptureModeSource::kFullscreen, CaptureModeType::kVideo);
  camera_controller->SetSelectedCamera(camera_id);
  EXPECT_TRUE(camera_controller->camera_preview_widget());
  controller->Stop();

  // When we start a new Game Dashboard session next, the camera selected from
  // the previous default session will remain selected.
  StartGameCaptureModeSession();
  EXPECT_EQ(camera_id, camera_controller->selected_camera());
  EXPECT_TRUE(camera_controller->camera_preview_widget());
  controller->Stop();
}

TEST_F(GameDashboardCaptureModeTest, StartWithNoCamera) {
  // Initially there's no camera connected, so the Game Dashboard auto camera
  // selection won't work.
  auto* controller = StartGameCaptureModeSession();
  auto* camera_controller = controller->camera_controller();
  EXPECT_FALSE(camera_controller->selected_camera().is_valid());
  EXPECT_FALSE(camera_controller->camera_preview_widget());

  // Now stop the current session, connect a camera, and start a new Game
  // Dashboard session. This new session should be able to auto-select the
  // camera.
  controller->Stop();
  AddDefaultCamera();
  StartGameCaptureModeSession();
  EXPECT_TRUE(camera_controller->camera_preview_widget());
  const CameraId camera_id(kDefaultCameraModelId, 1);
  EXPECT_EQ(camera_id, camera_controller->selected_camera());
  controller->Stop();

  // The next Game Dashboard session should still launch with a camera.
  StartGameCaptureModeSession();
  EXPECT_TRUE(camera_controller->camera_preview_widget());
  EXPECT_EQ(camera_id, camera_controller->selected_camera());
}

TEST_F(GameDashboardCaptureModeTest, CameraAutoSelectionDisabledOnChange) {
  const std::string device_id_1 = "/dev/video0";
  const std::string display_name_1 = "Integrated Webcam";

  const std::string device_id_2 = "/dev/video1";
  const std::string display_name_2 = "Integrated Webcam 1";

  AddFakeCamera(device_id_1, display_name_1, display_name_1);
  AddFakeCamera(device_id_2, display_name_2, display_name_2);

  // The first Game Dashboard session, the first camera will be auto selected.
  auto* controller = StartGameCaptureModeSession();
  auto* camera_controller = controller->camera_controller();
  EXPECT_TRUE(camera_controller->camera_preview_widget());
  const CameraId camera_id1(display_name_1, 1);
  EXPECT_EQ(camera_id1, camera_controller->selected_camera());

  // Now, simulate a change by the user to select a different camera while the
  // session is still running.
  const CameraId camera_id2(display_name_2, 1);
  camera_controller->SetSelectedCamera(camera_id2);
  EXPECT_EQ(camera_id2, camera_controller->selected_camera());

  // Stop the session and expect that the camera remains selected.
  controller->Stop();
  EXPECT_EQ(camera_id2, camera_controller->selected_camera());
}

TEST_F(GameDashboardCaptureModeTest, NoDimmingOfGameDashboardWidgets) {
  auto* controller = CaptureModeController::Get();
  controller->StartRecordingInstantlyForGameDashboard(game_window());
  EXPECT_TRUE(controller->is_recording_in_progress());
  auto* recording_watcher = controller->video_recording_watcher_for_testing();
  ASSERT_EQ(game_window(), recording_watcher->window_being_recorded());
  EXPECT_FALSE(recording_watcher->IsWindowDimmedForTesting(game_window()));

  // The window that hosts the game dashboard button should not be dimmed.
  GameDashboardContextTestApi context_test_api{
      GameDashboardController::Get()->GetGameDashboardContext(game_window()),
      GetEventGenerator()};
  auto* game_dashboard_button_widget =
      context_test_api.GetGameDashboardButtonWidget();
  ASSERT_TRUE(game_dashboard_button_widget);
  EXPECT_FALSE(recording_watcher->IsWindowDimmedForTesting(
      game_dashboard_button_widget->GetNativeWindow()));

  // Open the game dashboard menu, and expect that the window hosting the menu
  // is not dimmed either.
  context_test_api.OpenTheMainMenu();
  auto* game_dashboard_menu_widget = context_test_api.GetMainMenuWidget();
  ASSERT_TRUE(game_dashboard_menu_widget);
  EXPECT_FALSE(recording_watcher->IsWindowDimmedForTesting(
      game_dashboard_menu_widget->GetNativeWindow()));

  // Finally, the toolbar widget should also not be dimmed.
  context_test_api.OpenTheToolbar();
  auto* game_dashboard_toolbar_widget = context_test_api.GetToolbarWidget();
  ASSERT_TRUE(game_dashboard_toolbar_widget);
  EXPECT_FALSE(recording_watcher->IsWindowDimmedForTesting(
      game_dashboard_toolbar_widget->GetNativeWindow()));
}

TEST_F(GameDashboardCaptureModeTest, AvoidToolbarAndCameraPreviewIntersection) {
  UpdateDisplay("1200x1100");
  AddDefaultCamera();

  // Make the game window large enough to show a camera preview.
  game_window()->SetBounds({50, 50, 1100, 1000});

  auto* controller = CaptureModeController::Get();
  controller->StartRecordingInstantlyForGameDashboard(game_window());

  auto* camera_controller = controller->camera_controller();
  ASSERT_TRUE(camera_controller->should_show_preview());

  // The window that hosts the game dashboard button should not be dimmed.
  GameDashboardContextTestApi context_test_api{
      GameDashboardController::Get()->GetGameDashboardContext(game_window()),
      GetEventGenerator()};

  // Open the game dashboard toolbar.
  context_test_api.OpenTheMainMenu();
  context_test_api.OpenTheToolbar();
  context_test_api.CloseTheMainMenu();
  auto* game_dashboard_toolbar_widget = context_test_api.GetToolbarWidget();
  ASSERT_TRUE(game_dashboard_toolbar_widget);

  const auto* camera_preview_widget =
      camera_controller->camera_preview_widget();
  ASSERT_TRUE(camera_preview_widget);

  auto* preview_window = camera_preview_widget->GetNativeWindow();
  auto* toolbar_window = game_dashboard_toolbar_widget->GetNativeWindow();

  // Verify that the toolbar and camera preview do not overlap initially.
  EXPECT_FALSE(preview_window->GetBoundsInScreen().Intersects(
      toolbar_window->GetBoundsInScreen()));

  // Drag the camera preview widget and drop it on the toolbar, it should find a
  // different place to exist.
  auto* event_generator = GetEventGenerator();
  event_generator->MoveMouseTo(
      preview_window->GetBoundsInScreen().CenterPoint());
  event_generator->DragMouseTo(
      toolbar_window->GetBoundsInScreen().CenterPoint());
  EXPECT_FALSE(preview_window->GetBoundsInScreen().Intersects(
      toolbar_window->GetBoundsInScreen()));

  // Now drag the toolbar and drop it on top of the camera preview. The camera
  // preview should move out of the way.
  auto preview_bounds_before_dragging_toolbar =
      preview_window->GetBoundsInScreen();
  event_generator->MoveMouseTo(
      toolbar_window->GetBoundsInScreen().CenterPoint());
  event_generator->DragMouseTo(
      preview_window->GetBoundsInScreen().CenterPoint());
  EXPECT_FALSE(preview_window->GetBoundsInScreen().Intersects(
      toolbar_window->GetBoundsInScreen()));
  EXPECT_NE(preview_window->GetBoundsInScreen(),
            preview_bounds_before_dragging_toolbar);
}

TEST_F(GameDashboardCaptureModeTest, CursorAndClickBehaviorWhenAnchored) {
  // Create second window on screen that underlaps `game_window_` slightly.
  std::unique_ptr<aura::Window> window(
      CreateTestWindow(gfx::Rect(50, 150, 100, 100)));

  // The game window should be the top most active window.
  wm::ActivateWindow(game_window());
  auto* controller = StartGameCaptureModeSession();

  // Hover over empty space where there is no window.
  auto* event_generator = GetEventGenerator();
  event_generator->MoveMouseTo(gfx::Point(0, 0));
  auto* cursor_manager = Shell::Get()->cursor_manager();
  EXPECT_EQ(CursorType::kPointer, cursor_manager->GetCursor().type());
  // Clicking should not start a recording.
  event_generator->ClickLeftButton();
  EXPECT_TRUE(controller->IsActive());
  EXPECT_FALSE(controller->is_recording_in_progress());

  // Hover over the non-game window.
  event_generator->MoveMouseTo(gfx::Point(125, 225));
  EXPECT_EQ(CursorType::kPointer, cursor_manager->GetCursor().type());
  event_generator->ClickLeftButton();
  // Clicking should not start a recording.
  EXPECT_TRUE(controller->IsActive());
  EXPECT_FALSE(controller->is_recording_in_progress());

  // Hover over the anchored window where it overlaps with the non-game window.
  event_generator->MoveMouseTo(gfx::Point(75, 175));
  EXPECT_EQ(CursorType::kCustom, cursor_manager->GetCursor().type());
  EXPECT_TRUE(
      CaptureModeSessionTestApi().IsUsingCustomCursor(CaptureModeType::kVideo));
  // Start recording by clicking on the anchored window.
  event_generator->ClickLeftButton();
  WaitForRecordingToStart();
  EXPECT_FALSE(controller->IsActive());
  EXPECT_TRUE(controller->is_recording_in_progress());
}

// -----------------------------------------------------------------------------
// GameDashboardCaptureModeHistogramTest:
// Test fixture to verify game dashboard initiated screen capture histograms
// depending on the test param (true for tablet mode, false for clamshell mode).

class GameDashboardCaptureModeHistogramTest
    : public GameDashboardCaptureModeTest,
      public ::testing::WithParamInterface<bool> {
 public:
  GameDashboardCaptureModeHistogramTest() = default;
  ~GameDashboardCaptureModeHistogramTest() override = default;

  // GameDashboardCaptureModeTest:
  void SetUp() override {
    GameDashboardCaptureModeTest::SetUp();
    if (GetParam()) {
      SwitchToTabletMode();
    }
  }

 protected:
  base::HistogramTester histogram_tester_;
};

TEST_P(GameDashboardCaptureModeHistogramTest,
       GameCaptureConfigurationHistogram) {
  constexpr char kCaptureConfigurationBase[] = "CaptureConfiguration";
  CaptureModeTestApi test_api;
  const std::string histogram_name =
      BuildHistogramName(kCaptureConfigurationBase,
                         test_api.GetBehavior(BehaviorType::kGameDashboard),
                         /*append_ui_mode_suffix=*/true);
  histogram_tester_.ExpectBucketCount(
      histogram_name, CaptureModeConfiguration::kWindowRecording, 0);

  auto* controller = StartGameCaptureModeSession();
  StartVideoRecordingImmediately();
  EXPECT_TRUE(controller->is_recording_in_progress());
  test_api.StopVideoRecording();
  WaitForCaptureFileToBeSaved();
  EXPECT_FALSE(controller->is_recording_in_progress());
  histogram_tester_.ExpectBucketCount(
      histogram_name, CaptureModeConfiguration::kWindowRecording, 1);
}

TEST_P(GameDashboardCaptureModeHistogramTest,
       GameCaptureConfigurationHistogramForInstantScreenshot) {
  constexpr char kCaptureConfigurationBase[] = "CaptureConfiguration";
  CaptureModeTestApi test_api;
  const std::string histogram_name =
      BuildHistogramName(kCaptureConfigurationBase,
                         test_api.GetBehavior(BehaviorType::kGameDashboard),
                         /*append_ui_mode_suffix=*/true);
  histogram_tester_.ExpectBucketCount(
      histogram_name, CaptureModeConfiguration::kWindowScreenshot, 0);

  CaptureModeController::Get()->CaptureScreenshotOfGivenWindow(game_window());
  histogram_tester_.ExpectBucketCount(
      histogram_name, CaptureModeConfiguration::kWindowScreenshot, 1);
}

TEST_P(GameDashboardCaptureModeHistogramTest,
       GameScreenRecordingLengthHistogram) {
  constexpr char kRecordLenthHistogramBase[] = "ScreenRecordingLength";

  auto* controller = StartGameCaptureModeSession();
  StartVideoRecordingImmediately();
  EXPECT_TRUE(controller->is_recording_in_progress());
  WaitForSeconds(/*seconds=*/1);

  CaptureModeTestApi test_api;
  test_api.StopVideoRecording();
  EXPECT_FALSE(controller->is_recording_in_progress());
  WaitForCaptureFileToBeSaved();

  histogram_tester_.ExpectUniqueSample(
      BuildHistogramName(kRecordLenthHistogramBase,
                         test_api.GetBehavior(BehaviorType::kGameDashboard),
                         /*append_ui_mode_suffix=*/true),
      /*sample=*/1, /*expected_bucket_count=*/1);
}

TEST_P(GameDashboardCaptureModeHistogramTest,
       GameScreenRecordingFileSizeHistogram) {
  constexpr char kHistogramNameBase[] = "ScreenRecordingFileSize";

  CaptureModeTestApi test_api;
  const auto histogram_name = BuildHistogramName(
      kHistogramNameBase, test_api.GetBehavior(BehaviorType::kGameDashboard),
      /*append_ui_mode_suffix=*/true);
  histogram_tester_.ExpectTotalCount(histogram_name, /*expected_count=*/0);

  StartGameCaptureModeSession();
  StartVideoRecordingImmediately();
  test_api.StopVideoRecording();
  WaitForCaptureFileToBeSaved();
  histogram_tester_.ExpectTotalCount(histogram_name,
                                     /*expected_count=*/1);
}

TEST_P(GameDashboardCaptureModeHistogramTest, GameSaveToLocationHistogram) {
  constexpr char kHistogramNameBase[] = "SaveLocation";

  CaptureModeTestApi test_api;
  const auto histogram_name = BuildHistogramName(
      kHistogramNameBase, test_api.GetBehavior(BehaviorType::kGameDashboard),
      /*append_ui_mode_suffix=*/true);

  auto* test_delegate = CaptureModeController::Get()->delegate_for_testing();

  // Initialize four different save-to locations for screen capture that
  // includes default downloads folder, local customized folder, root drive and
  // a specific folder on drive.
  const auto downloads_folder = test_delegate->GetUserDefaultDownloadsFolder();
  const base::FilePath custom_folder =
      CreateCustomFolderInUserDownloadsPath("test");
  base::FilePath mount_point_path;
  test_delegate->GetDriveFsMountPointPath(&mount_point_path);
  const auto root_drive_folder = mount_point_path.Append("root");
  const base::FilePath non_root_drive_folder = CreateFolderOnDriveFS("test");

  struct {
    base::FilePath set_save_file_folder;
    CaptureModeSaveToLocation save_location;
  } kTestCases[] = {
      {downloads_folder, CaptureModeSaveToLocation::kDefault},
      {custom_folder, CaptureModeSaveToLocation::kCustomizedFolder},
      {root_drive_folder, CaptureModeSaveToLocation::kDrive},
      {non_root_drive_folder, CaptureModeSaveToLocation::kDriveFolder},
  };

  for (auto test_case : kTestCases) {
    histogram_tester_.ExpectBucketCount(histogram_name, test_case.save_location,
                                        0);
    auto* controller = StartGameCaptureModeSession();
    controller->SetCustomCaptureFolder(test_case.set_save_file_folder);
    StartVideoRecordingImmediately();
    test_api.StopVideoRecording();
    auto file_saved_path = WaitForCaptureFileToBeSaved();
    histogram_tester_.ExpectBucketCount(histogram_name, test_case.save_location,
                                        1);
  }
}

TEST_P(GameDashboardCaptureModeHistogramTest,
       GameRecordingStartsWithCameraHistogram) {
  UpdateDisplay("1000x700");
  constexpr char kHistogramNameBase[] = "RecordingStartsWithCamera";
  AddDefaultCamera();

  for (const auto camera_on : {true, false}) {
    CaptureModeTestApi test_api;
    const std::string histogram_name = BuildHistogramName(
        kHistogramNameBase, test_api.GetBehavior(BehaviorType::kGameDashboard),
        /*append_ui_mode_suffix=*/true);
    histogram_tester_.ExpectBucketCount(histogram_name, camera_on, 0);

    auto* controller = StartGameCaptureModeSession();
    EXPECT_EQ(controller->type(), CaptureModeType::kVideo);
    auto* camera_controller = controller->camera_controller();
    if (!camera_on) {
      camera_controller->SetSelectedCamera(CameraId());
    }
    test_api.PerformCapture();
    WaitForRecordingToStart();
    EXPECT_TRUE(controller->is_recording_in_progress());

    test_api.StopVideoRecording();
    EXPECT_FALSE(controller->is_recording_in_progress());
    WaitForCaptureFileToBeSaved();
    histogram_tester_.ExpectBucketCount(histogram_name, camera_on, 1);
  }
}

TEST_P(GameDashboardCaptureModeHistogramTest,
       GameDemoToolsEnabledOnRecordingHistogram) {
  constexpr char kHistogramNameBase[] = "DemoToolsEnabledOnRecordingStart";
  CaptureModeTestApi test_api;
  for (const auto enable_demo_tools : {false, true}) {
    const auto histogram_name = BuildHistogramName(
        kHistogramNameBase, test_api.GetBehavior(BehaviorType::kGameDashboard),
        /*append_ui_mode_suffix=*/true);
    histogram_tester_.ExpectBucketCount(histogram_name, enable_demo_tools, 0);
    auto* controller = StartGameCaptureModeSession();
    controller->EnableDemoTools(enable_demo_tools);
    StartVideoRecordingImmediately();
    test_api.StopVideoRecording();
    WaitForCaptureFileToBeSaved();
    histogram_tester_.ExpectBucketCount(histogram_name, enable_demo_tools, 1);
  }
}

TEST_P(GameDashboardCaptureModeHistogramTest, GameAudioRecordingModeHistogram) {
  constexpr char kHistogramNameBase[] = "AudioRecordingMode";
  CaptureModeTestApi test_api;
  for (const auto audio_mode :
       {AudioRecordingMode::kOff, AudioRecordingMode::kMicrophone,
        AudioRecordingMode::kSystem,
        AudioRecordingMode::kSystemAndMicrophone}) {
    const auto histogram_name = BuildHistogramName(
        kHistogramNameBase, test_api.GetBehavior(BehaviorType::kGameDashboard),
        /*append_ui_mode_suffix=*/true);
    histogram_tester_.ExpectBucketCount(histogram_name, audio_mode, 0);
    auto* controller = StartGameCaptureModeSession();
    controller->SetAudioRecordingMode(audio_mode);
    controller->Stop();
    histogram_tester_.ExpectBucketCount(histogram_name, audio_mode, 1);
  }
}

TEST_P(GameDashboardCaptureModeHistogramTest,
       GameDashboardEndRecordingReasonHistogram) {
  constexpr char kHistogramNameBase[] = "EndRecordingReason";

  CaptureModeTestApi test_api;

  const std::string histogram_name = BuildHistogramName(
      kHistogramNameBase, test_api.GetBehavior(BehaviorType::kDefault),
      /*append_ui_mode_suffix=*/true);

  // Testing the game dashboard stop recording button enum.
  histogram_tester_.ExpectBucketCount(
      histogram_name,
      /*sample=*/EndRecordingReason::kGameDashboardStopRecordingButton,
      /*expected_count=*/0);
  StartGameCaptureModeSession();
  StartVideoRecordingImmediately();
  CaptureModeController::Get()->EndVideoRecording(
      EndRecordingReason::kGameDashboardStopRecordingButton);
  WaitForCaptureFileToBeSaved();
  histogram_tester_.ExpectBucketCount(
      histogram_name,
      /*sample=*/EndRecordingReason::kGameDashboardStopRecordingButton,
      /*expected_count=*/1);

  // Testing the game toolbar stop recording button enum.
  histogram_tester_.ExpectBucketCount(
      histogram_name,
      /*sample=*/EndRecordingReason::kGameToolbarStopRecordingButton,
      /*expected_count=*/0);
  StartGameCaptureModeSession();
  StartVideoRecordingImmediately();
  CaptureModeController::Get()->EndVideoRecording(
      EndRecordingReason::kGameToolbarStopRecordingButton);
  WaitForCaptureFileToBeSaved();
  histogram_tester_.ExpectBucketCount(
      histogram_name,
      /*sample=*/EndRecordingReason::kGameToolbarStopRecordingButton,
      /*expected_count=*/1);

  // Testing stop recording by the tablet mode enum.
  // Game dashboard is not available on the tablet mode.
  if (GetParam()) {
    return;
  }

  histogram_tester_.ExpectBucketCount(
      histogram_name,
      /*sample=*/EndRecordingReason::kGameDashboardTabletMode,
      /*expected_count=*/0);
  auto game_dashboard_test_api = std::make_unique<GameDashboardContextTestApi>(
      GameDashboardController::Get()->GetGameDashboardContext(game_window()),
      GetEventGenerator());

  game_dashboard_test_api->OpenTheMainMenu();
  LeftClickOn(game_dashboard_test_api->GetMainMenuRecordGameTile());
  // Clicking on the record game tile closes the main menu, and asynchronously
  // starts the capture session. Run until idle to ensure that the posted task
  // runs synchronously and completes before proceeding.
  base::RunLoop().RunUntilIdle();
  LeftClickOn(GetStartRecordingButton());
  WaitForRecordingToStart();
  EXPECT_TRUE(CaptureModeController::Get()->is_recording_in_progress());
  TabletModeControllerTestApi().EnterTabletMode();
  WaitForCaptureFileToBeSaved();
  // The histogram name becomes
  // "ash.CaptureModeController.EndRecordingReason.TabletMode";
  histogram_tester_.ExpectBucketCount(
      BuildHistogramName(kHistogramNameBase,
                         test_api.GetBehavior(BehaviorType::kDefault),
                         /*append_ui_mode_suffix=*/true),
      /*sample=*/EndRecordingReason::kGameDashboardTabletMode,
      /*expected_count=*/1);
}

TEST_P(GameDashboardCaptureModeHistogramTest,
       CaptureScreenshotOfGivenWindowMetric) {
  constexpr char kHistogramNameBase[] = "SaveLocation";
  const base::FilePath custom_folder =
      CreateCustomFolderInUserDownloadsPath("test");
  const auto histogram_name = BuildHistogramName(
      kHistogramNameBase,
      CaptureModeTestApi().GetBehavior(BehaviorType::kGameDashboard),
      /*append_ui_mode_suffix=*/true);

  histogram_tester_.ExpectBucketCount(
      histogram_name, CaptureModeSaveToLocation::kCustomizedFolder, 0);
  CaptureModeController* controller = CaptureModeController::Get();
  controller->SetCustomCaptureFolder(custom_folder);
  controller->CaptureScreenshotOfGivenWindow(game_window());
  const auto file_saved_path = WaitForCaptureFileToBeSaved();
  histogram_tester_.ExpectBucketCount(
      histogram_name, CaptureModeSaveToLocation::kCustomizedFolder, 1);
}

// Tests that the entry point metrics are recorded correctly for game dashboard
// initiated capture mode both with and without a capture mode session.
TEST_P(GameDashboardCaptureModeHistogramTest, EntryPointTest) {
  constexpr char kCaptureConfigurationBase[] = "EntryPoint";
  CaptureModeTestApi test_api;
  const std::string histogram_name =
      BuildHistogramName(kCaptureConfigurationBase, nullptr,
                         /*append_ui_mode_suffix=*/true);
  histogram_tester_.ExpectBucketCount(histogram_name,
                                      CaptureModeEntryType::kGameDashboard, 0);

  auto* controller = StartGameCaptureModeSession();
  controller->Stop();
  histogram_tester_.ExpectBucketCount(histogram_name,
                                      CaptureModeEntryType::kGameDashboard, 1);

  controller->StartRecordingInstantlyForGameDashboard(game_window());
  histogram_tester_.ExpectBucketCount(histogram_name,
                                      CaptureModeEntryType::kGameDashboard, 2);
}

INSTANTIATE_TEST_SUITE_P(All,
                         GameDashboardCaptureModeHistogramTest,
                         ::testing::Bool());

}  // namespace ash