File: video_overlay_window_views_unittest.cc

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,811; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (1317 lines) | stat: -rw-r--r-- 54,978 bytes parent folder | download | duplicates (2)
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
1317
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/ui/views/overlay/video_overlay_window_views.h"

#include <memory>
#include <utility>

#include "base/containers/contains.h"
#include "base/memory/raw_ptr.h"
#include "base/test/mock_callback.h"
#include "base/test/scoped_feature_list.h"
#include "build/build_config.h"
#include "chrome/browser/picture_in_picture/auto_pip_setting_overlay_view.h"
#include "chrome/browser/picture_in_picture/picture_in_picture_occlusion_tracker.h"
#include "chrome/browser/picture_in_picture/picture_in_picture_window_manager.h"
#include "chrome/browser/ui/views/chrome_layout_provider.h"
#include "chrome/browser/ui/views/overlay/back_to_tab_button.h"
#include "chrome/browser/ui/views/overlay/close_image_button.h"
#include "chrome/browser/ui/views/overlay/hang_up_button.h"
#include "chrome/browser/ui/views/overlay/minimize_button.h"
#include "chrome/browser/ui/views/overlay/overlay_window_live_caption_dialog.h"
#include "chrome/browser/ui/views/overlay/playback_image_button.h"
#include "chrome/browser/ui/views/overlay/simple_overlay_window_image_button.h"
#include "chrome/browser/ui/views/overlay/toggle_camera_button.h"
#include "chrome/browser/ui/views/overlay/toggle_microphone_button.h"
#include "chrome/test/base/testing_profile.h"
#include "chrome/test/views/chrome_views_test_base.h"
#include "components/global_media_controls/public/views/media_progress_view.h"
#include "components/live_caption/pref_names.h"
#include "components/prefs/pref_service.h"
#include "components/prefs/testing_pref_service.h"
#include "components/soda/mock_soda_installer.h"
#include "components/vector_icons/vector_icons.h"
#include "content/public/browser/overlay_window.h"
#include "content/public/browser/video_picture_in_picture_window_controller.h"
#include "content/public/browser/web_contents.h"
#include "content/public/test/test_web_contents_factory.h"
#include "media/base/media_switches.h"
#include "services/media_session/public/cpp/media_position.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "ui/base/models/image_model.h"
#include "ui/compositor/layer.h"
#include "ui/display/test/test_screen.h"
#include "ui/events/base_event_utils.h"
#include "ui/events/event.h"
#include "ui/events/test/event_generator.h"
#include "ui/gfx/geometry/point.h"
#include "ui/gfx/geometry/vector2d.h"
#include "ui/views/controls/button/label_button.h"
#include "ui/views/controls/button/toggle_button.h"
#include "ui/views/controls/image_view.h"
#include "ui/views/controls/label.h"
#include "ui/views/test/button_test_api.h"
#include "ui/views/test/views_test_base.h"
#include "ui/views/widget/widget_utils.h"

namespace {

constexpr gfx::Size kMinWindowSize(200, 100);

// Reported minimum bubble size for the setting view.  The pip window should be
// larger than this when the setting view is shown.
constexpr gfx::Size kBubbleSize(300, 200);

// Size that's big enough to accommodate a `kBubbleSize`-sized bubble without
// being further adjusted upwards for margin.
constexpr gfx::Size kSizeBigEnoughForBubble(400, 300);

}  // namespace

using testing::_;
using ::testing::Return;

// Mock of AutoPipSettingOverlayView. Used for injection during tests.
class MockOverlayView : public AutoPipSettingOverlayView {
 public:
  explicit MockOverlayView(views::View* anchor_view)
      : AutoPipSettingOverlayView(base::DoNothing(),
                                  GURL{"https://example.com"},
                                  anchor_view,
                                  views::BubbleBorder::Arrow::FLOAT) {}
  MOCK_METHOD(void, ShowBubble, (gfx::NativeView parent), (override));

  void SetWantsEvent(bool wants_event) { wants_event_ = wants_event; }

  bool WantsEvent(const gfx::Point& point_in_screen) override {
    // Consume any event we're given.  The goal is to make sure we're given the
    // opportunity to take an event.
    return wants_event_;
  }

  gfx::Size GetBubbleSize() const override {
    // Return something that's bigger than the minimum.
    return kBubbleSize;
  }

 private:
  bool wants_event_ = false;
};

class TestVideoPictureInPictureWindowController
    : public content::VideoPictureInPictureWindowController {
 public:
  TestVideoPictureInPictureWindowController() = default;

  // PictureInPictureWindowController:
  void Show() override {}
  void FocusInitiator() override {}
  MOCK_METHOD(void, Close, (bool));
  MOCK_METHOD(void, CloseAndFocusInitiator, ());
  MOCK_METHOD(void, OnWindowDestroyed, (bool));
  content::VideoOverlayWindow* GetWindowForTesting() override {
    return nullptr;
  }
  void UpdateLayerBounds() override {}
  bool IsPlayerActive() override { return false; }
  void set_web_contents(content::WebContents* web_contents) {
    web_contents_ = web_contents;
  }
  content::WebContents* GetWebContents() override { return web_contents_; }
  content::WebContents* GetChildWebContents() override { return nullptr; }
  bool TogglePlayPause() override { return false; }
  void Play() override {}
  void Pause() override {}
  void SkipAd() override {}
  void NextTrack() override {}
  void PreviousTrack() override {}
  void NextSlide() override {}
  void PreviousSlide() override {}
  void ToggleMicrophone() override {}
  void ToggleCamera() override {}
  void HangUp() override {}
  MOCK_METHOD(void, SeekTo, (base::TimeDelta time));
  const gfx::Rect& GetSourceBounds() const override { return source_bounds_; }
  void GetMediaImage(
      const media_session::MediaImage& image,
      int minimum_size_px,
      int desired_size_px,
      content::MediaSession::GetMediaImageBitmapCallback callback) override {
    std::move(callback).Run(
        GetMediaImageImpl(image, minimum_size_px, desired_size_px));
  }
  MOCK_METHOD(SkBitmap,
              GetMediaImageImpl,
              (const media_session::MediaImage& image,
               int minimum_size_px,
               int desired_size_px));
  std::optional<gfx::Rect> GetWindowBounds() override { return std::nullopt; }
  std::optional<url::Origin> GetOrigin() override { return std::nullopt; }
  void SetOnWindowCreatedNotifyObserversCallback(base::OnceClosure) override {}

 private:
  raw_ptr<content::WebContents> web_contents_;
  gfx::Rect source_bounds_;
};

class VideoOverlayWindowViewsTest : public ChromeViewsTestBase {
 public:
  VideoOverlayWindowViewsTest() = default;
  // ChromeViewsTestBase:
  void SetUp() override {
    enabled_features_.push_back(media::kPictureInPictureOcclusionTracking);
    feature_list_.InitWithFeatures(enabled_features_, {});
    display::Screen::SetScreenInstance(&test_screen_);

    // Purposely skip ChromeViewsTestBase::SetUp() as that creates ash::Shell
    // on ChromeOS, which we don't want.
    ViewsTestBase::SetUp();
    test_views_delegate()->set_layout_provider(
        ChromeLayoutProvider::CreateLayoutProvider());
    mock_soda_installer_ = std::make_unique<speech::MockSodaInstaller>();
    mock_soda_installer_->NeverDownloadSodaForTesting();
    // web_contents_ needs to be created after the constructor, so that
    // |feature_list_| can be initialized before other threads check if a
    // feature is enabled.
    web_contents_ = web_contents_factory_.CreateWebContents(&profile_);
    pip_window_controller_.set_web_contents(web_contents_);

#if BUILDFLAG(IS_CHROMEOS)
    test_views_delegate()->set_context(GetContext());
#endif
    test_views_delegate()->set_use_desktop_native_widgets(true);

    // The default work area must be big enough to fit the minimum
    // VideoOverlayWindowViews size.
    SetDisplayWorkArea({0, 0, 1000, 1000});

    overlay_window_ = VideoOverlayWindowViews::Create(&pip_window_controller_);
    overlay_window_->set_overlay_view_cb_for_testing(
        base::BindRepeating(&VideoOverlayWindowViewsTest::GetOverlayViewImpl,
                            base::Unretained(this)));

    // On some platforms, OnNativeWidgetMove is invoked on creation.
    WaitForMove();
    overlay_window_->set_minimum_size_for_testing(kMinWindowSize);

    event_generator_ = std::make_unique<ui::test::EventGenerator>(
        views::GetRootWindow(overlay_window_.get()));
  }

  void TearDown() override {
    overlay_window_.reset();
    ViewsTestBase::TearDown();
    display::Screen::SetScreenInstance(nullptr);
  }

  void SetDisplayWorkArea(const gfx::Rect& work_area) {
    display::Display display = test_screen_.GetPrimaryDisplay();
    display.set_work_area(work_area);
    test_screen_.display_list().UpdateDisplay(display);
  }

  VideoOverlayWindowViews& overlay_window() { return *overlay_window_; }

  content::WebContents* web_contents() { return web_contents_; }

  TestVideoPictureInPictureWindowController& pip_window_controller() {
    return pip_window_controller_;
  }

  MockOverlayView* SetOverlayView() {
    std::unique_ptr<MockOverlayView> mock_overlay_view =
        std::make_unique<MockOverlayView>(
            overlay_window().window_background_view_for_testing());
    overlay_view_ = std::move(mock_overlay_view);
    return overlay_view_.get();
  }

  ui::test::EventGenerator* event_generator() { return event_generator_.get(); }

 protected:
  void WaitForMove() {
    task_environment()->FastForwardBy(
        VideoOverlayWindowViews::kControlHideDelayAfterMove +
        base::Milliseconds(1));
  }

  void WaitForLayout() { task_environment()->FastForwardBy(base::Seconds(1)); }

  void DestroyOverlayWindow() { overlay_window_.reset(); }

  void AddEnabledFeature(base::test::FeatureRef feature) {
    enabled_features_.push_back(feature);
  }

  TestingProfile& profile() { return profile_; }

 private:
  std::unique_ptr<AutoPipSettingOverlayView> GetOverlayViewImpl() {
    return std::move(overlay_view_);
  }

  std::unique_ptr<speech::MockSodaInstaller> mock_soda_installer_;
  TestingProfile profile_;
  content::TestWebContentsFactory web_contents_factory_;
  raw_ptr<content::WebContents> web_contents_;
  TestVideoPictureInPictureWindowController pip_window_controller_;

  display::test::TestScreen test_screen_;

  // Overlay view that we'll send to the window.  May be null.
  std::unique_ptr<MockOverlayView> overlay_view_;

  std::unique_ptr<ui::test::EventGenerator> event_generator_;

  std::unique_ptr<VideoOverlayWindowViews> overlay_window_;

  std::vector<base::test::FeatureRef> enabled_features_;

  base::test::ScopedFeatureList feature_list_;
};

TEST_F(VideoOverlayWindowViewsTest, InitialWindowSize_Square) {
  // Fit the window taking 1/5 (both dimensions) of the work area as the
  // starting size, and applying the size and aspect ratio constraints.
  overlay_window().UpdateNaturalSize({400, 400});
  EXPECT_EQ(gfx::Size(200, 200), overlay_window().GetBounds().size());
  EXPECT_EQ(gfx::Size(200, 200),
            overlay_window().video_layer_for_testing()->size());
}

TEST_F(VideoOverlayWindowViewsTest, InitialWindowSize_Horizontal) {
  // Fit the window taking 1/5 (both dimensions) of the work area as the
  // starting size, and applying the size and aspect ratio constraints.
  overlay_window().UpdateNaturalSize({400, 200});
  EXPECT_EQ(gfx::Size(400, 200), overlay_window().GetBounds().size());
  EXPECT_EQ(gfx::Size(400, 200),
            overlay_window().video_layer_for_testing()->size());
}

TEST_F(VideoOverlayWindowViewsTest, InitialWindowSize_Vertical) {
  // Fit the window taking 1/5 (both dimensions) of the work area as the
  // starting size, and applying the size and aspect ratio constraints.
  overlay_window().UpdateNaturalSize({400, 500});
  EXPECT_EQ(gfx::Size(200, 250), overlay_window().GetBounds().size());
  EXPECT_EQ(gfx::Size(200, 250),
            overlay_window().video_layer_for_testing()->size());
}

TEST_F(VideoOverlayWindowViewsTest, Letterboxing) {
  overlay_window().UpdateNaturalSize({400, 10});

  // Must fit within the minimum height of 146. But with the aspect ratio of
  // 40:1 the width gets exceedingly big and must be limited to the maximum of
  // 800. Thus, letterboxing is unavoidable.
  EXPECT_EQ(gfx::Size(800, 100), overlay_window().GetBounds().size());
  EXPECT_EQ(gfx::Size(800, 20),
            overlay_window().video_layer_for_testing()->size());
}

TEST_F(VideoOverlayWindowViewsTest, Pillarboxing) {
  overlay_window().UpdateNaturalSize({10, 400});

  // Must fit within the minimum width of 260. But with the aspect ratio of
  // 1:40 the height gets exceedingly big and must be limited to the maximum of
  // 800. Thus, pillarboxing is unavoidable.
  EXPECT_EQ(gfx::Size(200, 800), overlay_window().GetBounds().size());
  EXPECT_EQ(gfx::Size(20, 800),
            overlay_window().video_layer_for_testing()->size());
}

TEST_F(VideoOverlayWindowViewsTest, Pillarboxing_Square) {
  overlay_window().UpdateNaturalSize({100, 100});

  // Pillarboxing also occurs on Linux even with the square aspect ratio,
  // because the user is allowed to size the window to the rectangular minimum
  // size.
  overlay_window().SetSize({200, 100});
  EXPECT_EQ(gfx::Size(100, 100),
            overlay_window().video_layer_for_testing()->size());
}

TEST_F(VideoOverlayWindowViewsTest, ApproximateAspectRatio_Horizontal) {
  // "Horizontal" video.
  overlay_window().UpdateNaturalSize({320, 240});

  // The user drags the window resizer horizontally and now the integer window
  // dimensions can't reproduce the video aspect ratio exactly. The video
  // should still fill the entire window area.
  overlay_window().SetSize({320, 240});
  EXPECT_EQ(gfx::Size(320, 240),
            overlay_window().video_layer_for_testing()->size());

  overlay_window().SetSize({321, 241});
  EXPECT_EQ(gfx::Size(321, 241),
            overlay_window().video_layer_for_testing()->size());

  // Wide video.
  overlay_window().UpdateNaturalSize({1600, 900});

  overlay_window().SetSize({444, 250});
  EXPECT_EQ(gfx::Size(444, 250),
            overlay_window().video_layer_for_testing()->size());

  overlay_window().SetSize({445, 250});
  EXPECT_EQ(gfx::Size(445, 250),
            overlay_window().video_layer_for_testing()->size());

  // Very wide video.
  overlay_window().UpdateNaturalSize({400, 100});

  overlay_window().SetSize({478, 120});
  EXPECT_EQ(gfx::Size(478, 120),
            overlay_window().video_layer_for_testing()->size());

  overlay_window().SetSize({481, 120});
  EXPECT_EQ(gfx::Size(481, 120),
            overlay_window().video_layer_for_testing()->size());
}

TEST_F(VideoOverlayWindowViewsTest, ApproximateAspectRatio_Vertical) {
  // "Vertical" video.
  overlay_window().UpdateNaturalSize({240, 320});

  // The user dragged the window resizer vertically and now the integer window
  // dimensions can't reproduce the video aspect ratio exactly. The video
  // should still fill the entire window area.
  overlay_window().SetSize({240, 320});
  EXPECT_EQ(gfx::Size(240, 320),
            overlay_window().video_layer_for_testing()->size());

  overlay_window().SetSize({239, 319});
  EXPECT_EQ(gfx::Size(239, 319),
            overlay_window().video_layer_for_testing()->size());

  // Narrow video.
  overlay_window().UpdateNaturalSize({900, 1600});

  overlay_window().SetSize({250, 444});
  EXPECT_EQ(gfx::Size(250, 444),
            overlay_window().video_layer_for_testing()->size());

  overlay_window().SetSize({250, 445});
  EXPECT_EQ(gfx::Size(250, 445),
            overlay_window().video_layer_for_testing()->size());

  // Very narrow video.
  // NOTE: Window width is bounded by the minimum size.
  overlay_window().UpdateNaturalSize({100, 400});

  overlay_window().SetSize({200, 478});
  EXPECT_EQ(gfx::Size(120, 478),
            overlay_window().video_layer_for_testing()->size());

  overlay_window().SetSize({200, 481});
  EXPECT_EQ(gfx::Size(120, 481),
            overlay_window().video_layer_for_testing()->size());
}

TEST_F(VideoOverlayWindowViewsTest, UpdateMaximumSize) {
  SetDisplayWorkArea({0, 0, 4000, 4000});

  overlay_window().UpdateNaturalSize({480, 320});

  // The initial size is determined by the work area and the video natural size
  // (aspect ratio).
  EXPECT_EQ(gfx::Size(1200, 800), overlay_window().GetBounds().size());
  // The initial maximum size is 80% of the work area.
  EXPECT_EQ(gfx::Size(3200, 3200), overlay_window().GetMaximumSize());

  // If the maximum size increases then we should keep the existing window size.
  SetDisplayWorkArea({0, 0, 8000, 8000});
  EXPECT_EQ(gfx::Size(1200, 800), overlay_window().GetBounds().size());
  EXPECT_EQ(gfx::Size(6400, 6400), overlay_window().GetMaximumSize());

  // If the maximum size decreases then we should shrink to fit.
  SetDisplayWorkArea({0, 0, 1000, 2000});
  EXPECT_EQ(gfx::Size(800, 800), overlay_window().GetBounds().size());
  EXPECT_EQ(gfx::Size(800, 1600), overlay_window().GetMaximumSize());
}

TEST_F(VideoOverlayWindowViewsTest, IgnoreInvalidMaximumSize) {
  ASSERT_EQ(gfx::Size(800, 800), overlay_window().GetMaximumSize());

  SetDisplayWorkArea({0, 0, 0, 0});
  EXPECT_EQ(gfx::Size(800, 800), overlay_window().GetMaximumSize());
}

// Tests that Next Track button bounds are updated right away when window
// controls are hidden.
TEST_F(VideoOverlayWindowViewsTest, NextTrackButtonAddedWhenControlsHidden) {
  ASSERT_FALSE(overlay_window().AreControlsVisible());
  ASSERT_TRUE(overlay_window()
                  .next_track_controls_view_for_testing()
                  ->size()
                  .IsEmpty());

  const auto origin_before_layout =
      overlay_window().next_track_controls_view_for_testing()->origin();

  overlay_window().SetNextTrackButtonVisibility(true);
  EXPECT_NE(overlay_window().next_track_controls_view_for_testing()->origin(),
            origin_before_layout);
  EXPECT_FALSE(overlay_window().IsLayoutPendingForTesting());
}

// Tests that Previous Track button bounds are updated right away when window
// controls are hidden.
TEST_F(VideoOverlayWindowViewsTest,
       PreviousTrackButtonAddedWhenControlsHidden) {
  ASSERT_FALSE(overlay_window().AreControlsVisible());
  ASSERT_TRUE(overlay_window()
                  .previous_track_controls_view_for_testing()
                  ->size()
                  .IsEmpty());

  const auto origin_before_layout =
      overlay_window().previous_track_controls_view_for_testing()->origin();

  overlay_window().SetPreviousTrackButtonVisibility(true);
  EXPECT_NE(
      overlay_window().previous_track_controls_view_for_testing()->origin(),
      origin_before_layout);
  EXPECT_FALSE(overlay_window().IsLayoutPendingForTesting());
}

TEST_F(VideoOverlayWindowViewsTest, UpdateNaturalSizeDoesNotMoveWindow) {
  // Enter PiP.
  overlay_window().UpdateNaturalSize({300, 200});
  overlay_window().ShowInactive();

  // Resize the window and move it toward the top-left corner of the work area.
  // In production, resizing preserves the aspect ratio if possible, so we
  // preserve it here too.
  overlay_window().SetBounds({100, 100, 450, 300});

  // Simulate a new surface layer and a change in the aspect ratio.
  overlay_window().UpdateNaturalSize({400, 200});

  // The window should not move.
  // The window size will be adjusted according to the new aspect ratio, and
  // clamped to 600x300 to fit within the maximum size for the work area of
  // 1000x1000.
  EXPECT_EQ(gfx::Rect(100, 100, 600, 300), overlay_window().GetBounds());
}

// Tests that the OverlayWindowFrameView does not accept events so they can
// propagate to the overlay.
TEST_F(VideoOverlayWindowViewsTest, HitTestFrameView) {
  // Since the NonClientFrameView is the only non-custom direct descendent of
  // the NonClientView, we can assume that if the frame does not accept the
  // point but the NonClientView does, then it will be handled by one of the
  // custom overlay views.
  auto point = gfx::Point(50, 50);
  views::NonClientView* non_client_view = overlay_window().non_client_view();
  EXPECT_EQ(non_client_view->frame_view()->HitTestPoint(point), false);
  EXPECT_EQ(non_client_view->HitTestPoint(point), true);
}

// With pillarboxing, the close button doesn't cover the video area. Make sure
// hovering the button doesn't get handled like normal mouse exit events
// causing the controls to hide.
// TODO(http://crbug/1509791): Fix and re-enable.
TEST_F(VideoOverlayWindowViewsTest, DISABLED_NoMouseExitWithinWindowBounds) {
  overlay_window().UpdateNaturalSize({10, 400});
  WaitForMove();

  const auto close_button_bounds = overlay_window().GetCloseControlsBounds();
  const auto video_bounds =
      overlay_window().video_layer_for_testing()->bounds();
  ASSERT_FALSE(video_bounds.Contains(close_button_bounds));

  const gfx::Point moved_location(video_bounds.origin() + gfx::Vector2d(5, 5));
  ui::MouseEvent moved_event(ui::EventType::kMouseMoved, moved_location,
                             moved_location, ui::EventTimeForNow(), ui::EF_NONE,
                             ui::EF_NONE);
  overlay_window().OnMouseEvent(&moved_event);
  ASSERT_TRUE(overlay_window().AreControlsVisible());

  const gfx::Point exited_location(close_button_bounds.CenterPoint());
  ui::MouseEvent exited_event(ui::EventType::kMouseExited, exited_location,
                              exited_location, ui::EventTimeForNow(),
                              ui::EF_NONE, ui::EF_NONE);
  overlay_window().OnMouseEvent(&exited_event);
  EXPECT_TRUE(overlay_window().AreControlsVisible());
}

TEST_F(VideoOverlayWindowViewsTest, ShowControlsOnFocus) {
  EXPECT_FALSE(overlay_window().AreControlsVisible());
  overlay_window().OnNativeFocus();
  EXPECT_TRUE(overlay_window().AreControlsVisible());
}

TEST_F(VideoOverlayWindowViewsTest, OnlyPauseOnCloseWhenPauseIsAvailable) {
  views::test::ButtonTestApi close_button_clicker(
      overlay_window().close_button_for_testing());
  ui::MouseEvent dummy_event(ui::EventType::kMousePressed, gfx::Point(0, 0),
                             gfx::Point(0, 0), ui::EventTimeForNow(), 0, 0);

  // When the play/pause controls are visible, closing via the close button
  // should pause the video.
  overlay_window().SetPlayPauseButtonVisibility(true);
  PictureInPictureWindowManager::GetInstance()
      ->set_window_controller_for_testing(&pip_window_controller());
  EXPECT_CALL(pip_window_controller(), Close(true));
  close_button_clicker.NotifyClick(dummy_event);
  testing::Mock::VerifyAndClearExpectations(&pip_window_controller());

  // Same for tapping the close button. Note that the controls must be visible
  // for the tap to work, otherwise the tap will just show the controls.
  overlay_window().ForceControlsVisibleForTesting(true);
  gfx::Point close_button_center =
      overlay_window().GetCloseControlsBounds().CenterPoint();
  ui::GestureEvent tap_event(
      close_button_center.x(), close_button_center.y(), 0,
      base::TimeTicks::Now(),
      ui::GestureEventDetails(ui::EventType::kGestureTap));
  EXPECT_CALL(pip_window_controller(), Close(true));
  overlay_window().OnGestureEvent(&tap_event);
  testing::Mock::VerifyAndClearExpectations(&pip_window_controller());

  // When the play/pause controls are not visible, closing via the close button
  // should not pause the video.
  overlay_window().SetPlayPauseButtonVisibility(false);
  EXPECT_CALL(pip_window_controller(), Close(false));
  close_button_clicker.NotifyClick(dummy_event);
  testing::Mock::VerifyAndClearExpectations(&pip_window_controller());

  // Same for tapping the close button.
  EXPECT_CALL(pip_window_controller(), Close(false));
  overlay_window().OnGestureEvent(&tap_event);
  testing::Mock::VerifyAndClearExpectations(&pip_window_controller());

  PictureInPictureWindowManager::GetInstance()
      ->set_window_controller_for_testing(nullptr);
}

TEST_F(VideoOverlayWindowViewsTest, PauseOnWidgetCloseWhenPauseAvailable) {
  // When the play/pause controls are visible, when the native widget is
  // destroyed we should pause the underlying video.
  overlay_window().SetPlayPauseButtonVisibility(true);
  EXPECT_CALL(pip_window_controller(), OnWindowDestroyed(true));
  overlay_window().CloseNow();
  testing::Mock::VerifyAndClearExpectations(&pip_window_controller());
}

TEST_F(VideoOverlayWindowViewsTest,
       DontPauseOnWidgetCloseWhenPauseNotAvailable) {
  // When the play/pause controls are not visible, when the native widget is
  // destroyed we should not pause the underlying video.
  overlay_window().SetPlayPauseButtonVisibility(false);
  EXPECT_CALL(pip_window_controller(), OnWindowDestroyed(false));
  overlay_window().CloseNow();
  testing::Mock::VerifyAndClearExpectations(&pip_window_controller());
}

TEST_F(VideoOverlayWindowViewsTest, SmallDisplayWorkAreaDoesNotCrash) {
  SetDisplayWorkArea({0, 0, 240, 120});
  overlay_window().UpdateNaturalSize({400, 300});

  // Since the work area would force a max size smaller than the minimum size,
  // the size is fixed at the minimum size.
  EXPECT_EQ(kMinWindowSize, overlay_window().GetBounds().size());
  EXPECT_EQ(kMinWindowSize, overlay_window().GetMaximumSize());

  // The video should still be letterboxed to the correct aspect ratio.
  EXPECT_EQ(gfx::Size(133, 100),
            overlay_window().video_layer_for_testing()->size());
}

// TODO(http://crbug/1509791): Fix and re-enable.
TEST_F(VideoOverlayWindowViewsTest, DISABLED_ControlsAreHiddenDuringMove) {
  // Set the initial position.
  overlay_window().SetBounds({0, 0, 100, 100});
  WaitForMove();

  // Make the controls visible.
  overlay_window().UpdateControlsVisibility(true);
  ASSERT_TRUE(overlay_window().AreControlsVisible());

  // Now move the window, this should cause the controls to be hidden.
  overlay_window().SetBounds({50, 0, 100, 100});
  EXPECT_FALSE(overlay_window().AreControlsVisible());

  // Should still be hidden with mouse event.
  overlay_window().UpdateControlsVisibility(true);
  EXPECT_FALSE(overlay_window().AreControlsVisible());

  // After moving, overlay should be visible again because of the previous
  // mouse event.
  WaitForMove();
  EXPECT_TRUE(overlay_window().AreControlsVisible());
}

TEST_F(VideoOverlayWindowViewsTest,
       ControlsAreHiddenDuringMove_MultipleUpdates) {
  overlay_window().SetBounds({0, 0, 100, 100});
  WaitForMove();

  // Move the window.
  overlay_window().SetBounds({50, 0, 100, 100});
  EXPECT_FALSE(overlay_window().AreControlsVisible());

  overlay_window().UpdateControlsVisibility(true);
  overlay_window().UpdateControlsVisibility(false);
  overlay_window().UpdateControlsVisibility(true);
  overlay_window().UpdateControlsVisibility(false);

  // Only the last one should have any effect.
  EXPECT_FALSE(overlay_window().AreControlsVisible());
}

TEST_F(VideoOverlayWindowViewsTest, OverlayViewIsSizedCorrectly) {
  // Set the bound of the window before showing it, to make sure the size
  // propagates to the overlay view.  We use the larger-than-bubble size so that
  // it should be an exact match.  If it were too small, then the overlay window
  // might have to be even larger than we request to fit the bubble.

  // Setting the overlay view before show should be sufficient for it to take
  // effect when shown.
  auto* overlay_view = SetOverlayView();
  overlay_window().ShowInactive();
  // Do this after showing it, else the window will size to a default size,
  // rather than the bounds we request.
  const gfx::Rect bounds(gfx::Point(0, 0), kSizeBigEnoughForBubble);
  overlay_window().UpdateNaturalSize(bounds.size());
  overlay_window().SetBounds(bounds);
  EXPECT_TRUE(overlay_view->GetVisible());
  EXPECT_EQ(overlay_view->bounds(), bounds);
}

TEST_F(VideoOverlayWindowViewsTest, OverlayViewCanBeClicked) {
  // Make sure that the overlay view is z-ordered to get input events.
  auto* overlay_view = SetOverlayView();
  overlay_view->SetWantsEvent(true);

  // Add a button!
  base::MockRepeatingCallback<void(const ui::Event&)> cb;
  auto* button = overlay_view->AddChildView(
      std::make_unique<views::LabelButton>(cb.Get()));
  button->SetBounds(0, 0, 50, 50);

  // Show the window and click the button.
  overlay_window().ShowInactive();
  EXPECT_CALL(cb, Run(_));
  event_generator()->MoveMouseTo(button->GetBoundsInScreen().CenterPoint());
  event_generator()->ClickLeftButton();

  // Clear the callback since `cb` is going away.  Note that `DoNothing()`
  // doesn't work here because type inference fails.
  button->SetCallback(base::BindRepeating([](const ui::Event&) {}));
}

TEST_F(VideoOverlayWindowViewsTest, OverlayWindowBlocksInput) {
  // Make sure that the playback controls don't receive input events while the
  // overlay view is visible.
  auto* overlay_view = SetOverlayView();
  overlay_view->SetWantsEvent(true);
  overlay_window().ShowInactive();

  // When the play/pause controls are visible, closing via the close button
  // should pause the video.
  overlay_window().SetPlayPauseButtonVisibility(true);
  EXPECT_CALL(pip_window_controller(), Close(true)).Times(0);
  event_generator()->MoveMouseTo(
      overlay_window().GetCloseControlsBounds().CenterPoint());
  event_generator()->ClickLeftButton();
}

TEST_F(VideoOverlayWindowViewsTest, OverlayWindowFitsInMinimumSize) {
  auto* overlay_view = SetOverlayView();
  overlay_window().ShowInactive();

  // The window size should be strictly greater than the bubble size so that
  // there's some nonzero margin.
  auto window_min_size = overlay_window().GetMinimumSize();
  auto bubble_min_size = overlay_view->GetBubbleSize();
  EXPECT_GT(window_min_size.width(), bubble_min_size.width());
  EXPECT_GT(window_min_size.height(), bubble_min_size.height());

  // When the overlay view is hidden, the minimum size should return to normal.
  overlay_view->SetVisible(false);
  EXPECT_EQ(overlay_window().GetMinimumSize(), kMinWindowSize);
}

TEST_F(VideoOverlayWindowViewsTest, OverlayWindowStopsBlockingInput) {
  auto* overlay_view = SetOverlayView();
  overlay_window().ShowInactive();

  // Make sure that the overlay window blocks input, when the overlay view does
  // not want events.
  const auto close_controls_center_point =
      overlay_window().GetCloseControlsBounds().CenterPoint();
  overlay_view->SetWantsEvent(false);
  EXPECT_FALSE(overlay_window().ControlsHitTestContainsPoint(
      close_controls_center_point));

  // Make sure that the overlay window stops blocking input, when the overlay
  // view wants event.
  overlay_view->SetWantsEvent(true);
  EXPECT_TRUE(overlay_window().ControlsHitTestContainsPoint(
      close_controls_center_point));
}

TEST_F(VideoOverlayWindowViewsTest, IsTrackedByTheOcclusionObserver) {
  overlay_window().ShowInactive();

  PictureInPictureOcclusionTracker* tracker =
      PictureInPictureWindowManager::GetInstance()->GetOcclusionTracker();

  // Check that the PictureInPictureOcclusionTracker is observing the
  // VideoOverlayWindowViews.
  EXPECT_TRUE(base::Contains(tracker->GetPictureInPictureWidgetsForTesting(),
                             &overlay_window()));

  // Check that it's no longer observed when the widget is destroyed.
  DestroyOverlayWindow();
  EXPECT_EQ(0u, tracker->GetPictureInPictureWidgetsForTesting().size());
}

TEST_F(VideoOverlayWindowViewsTest, ProgressBarNotDrawnWhen2024UIIsDisabled) {
  overlay_window().ForceControlsVisibleForTesting(true);
  global_media_controls::MediaProgressView* progress_view =
      overlay_window().progress_view_for_testing();
  ASSERT_EQ(nullptr, progress_view);
}

TEST_F(VideoOverlayWindowViewsTest, TimestampNotDrawnWhen2024UIIsDisabled) {
  overlay_window().ForceControlsVisibleForTesting(true);
  views::Label* timestamp = overlay_window().timestamp_for_testing();
  ASSERT_EQ(nullptr, timestamp);
}

TEST_F(VideoOverlayWindowViewsTest, LiveStatusNotDrawnWhen2024UIIsDisabled) {
  overlay_window().ForceControlsVisibleForTesting(true);
  media_session::MediaPosition media_position(
      /*playback_rate=*/0,
      /*duration=*/base::TimeDelta::Max(),
      /*position=*/base::Seconds(42),
      /*end_of_media=*/false);
  overlay_window().SetMediaPosition(media_position);
  views::Label* live_status = overlay_window().live_status_for_testing();
  ASSERT_EQ(nullptr, live_status);
}

TEST_F(VideoOverlayWindowViewsTest,
       ReplayAndForward10SecondsNotDrawnWhen2024UIIsDisabled) {
  overlay_window().ForceControlsVisibleForTesting(true);
  SimpleOverlayWindowImageButton* replay_10_seconds_button =
      overlay_window().replay_10_seconds_button_for_testing();
  SimpleOverlayWindowImageButton* forward_10_seconds_button =
      overlay_window().forward_10_seconds_button_for_testing();
  ASSERT_EQ(nullptr, replay_10_seconds_button);
  ASSERT_EQ(nullptr, forward_10_seconds_button);
}

TEST_F(VideoOverlayWindowViewsTest, FaviconNotDrawnWhen2024UIIsDisabled) {
  overlay_window().ForceControlsVisibleForTesting(true);
  views::ImageView* favicon = overlay_window().favicon_view_for_testing();
  ASSERT_EQ(nullptr, favicon);
}

TEST_F(VideoOverlayWindowViewsTest, OriginNotDrawnWhen2024UIIsDisabled) {
  overlay_window().ForceControlsVisibleForTesting(true);
  views::Label* origin = overlay_window().origin_for_testing();
  ASSERT_EQ(nullptr, origin);
}

TEST_F(VideoOverlayWindowViewsTest,
       LiveCaptionButtonNotDrawnWhen2024UIIsDisabled) {
  overlay_window().ForceControlsVisibleForTesting(true);
  SimpleOverlayWindowImageButton* live_caption_button =
      overlay_window().live_caption_button_for_testing();
  ASSERT_EQ(nullptr, live_caption_button);
}

TEST_F(VideoOverlayWindowViewsTest,
       LiveCaptionDialogNotDrawnWhen2024UIIsDisabled) {
  overlay_window().ForceControlsVisibleForTesting(true);
  OverlayWindowLiveCaptionDialog* live_caption_dialog =
      overlay_window().live_caption_dialog_for_testing();
  ASSERT_EQ(nullptr, live_caption_dialog);
}

class VideoOverlayWindowViewsWith2024UITest
    : public VideoOverlayWindowViewsTest {
 public:
  void SetUp() override {
    AddEnabledFeature(media::kVideoPictureInPictureControlsUpdate2024);
    VideoOverlayWindowViewsTest::SetUp();
  }
};

TEST_F(VideoOverlayWindowViewsWith2024UITest,
       MinimizeButtonClosesWithoutPausing) {
  views::test::ButtonTestApi minimize_button_clicker(
      overlay_window().minimize_button_for_testing());
  ui::MouseEvent dummy_event(ui::EventType::kMousePressed, gfx::Point(0, 0),
                             gfx::Point(0, 0), ui::EventTimeForNow(), 0, 0);

  // Even when play/pause is available, the minimize button should not pause the
  // video.
  overlay_window().SetPlayPauseButtonVisibility(true);
  PictureInPictureWindowManager::GetInstance()
      ->set_window_controller_for_testing(&pip_window_controller());
  EXPECT_CALL(pip_window_controller(), Close(false));
  minimize_button_clicker.NotifyClick(dummy_event);
  testing::Mock::VerifyAndClearExpectations(&pip_window_controller());
}

TEST_F(VideoOverlayWindowViewsWith2024UITest, ShowsBackToTabImageButton) {
  overlay_window().ForceControlsVisibleForTesting(true);
  OverlayWindowBackToTabButton* back_to_tab_image_button =
      overlay_window().back_to_tab_button_for_testing();
  ASSERT_NE(nullptr, back_to_tab_image_button);
  EXPECT_TRUE(back_to_tab_image_button->IsDrawn());
  views::test::ButtonTestApi button_clicker(back_to_tab_image_button);
  ui::MouseEvent dummy_event(ui::EventType::kMousePressed, gfx::Point(0, 0),
                             gfx::Point(0, 0), ui::EventTimeForNow(), 0, 0);

  PictureInPictureWindowManager::GetInstance()
      ->set_window_controller_for_testing(&pip_window_controller());
  EXPECT_CALL(pip_window_controller(), CloseAndFocusInitiator());
  button_clicker.NotifyClick(dummy_event);
  testing::Mock::VerifyAndClearExpectations(&pip_window_controller());
}

TEST_F(VideoOverlayWindowViewsWith2024UITest, ProgressBarSeeksVideo) {
  overlay_window().ShowInactive();
  overlay_window().SetPlayPauseButtonVisibility(true);
  overlay_window().ForceControlsVisibleForTesting(true);

  // Move time forward to ensure controls layout is completed.
  WaitForLayout();

  global_media_controls::MediaProgressView* progress_view =
      overlay_window().progress_view_for_testing();
  ASSERT_NE(nullptr, progress_view);
  EXPECT_TRUE(progress_view->IsDrawn());

  // Before dragging the progress bar, the center controls should be visible.
  PlaybackImageButton* play_button =
      overlay_window().play_pause_controls_view_for_testing();
  SimpleOverlayWindowImageButton* replay_10_seconds_button =
      overlay_window().replay_10_seconds_button_for_testing();
  SimpleOverlayWindowImageButton* forward_10_seconds_button =
      overlay_window().forward_10_seconds_button_for_testing();
  ASSERT_NE(nullptr, play_button);
  ASSERT_NE(nullptr, replay_10_seconds_button);
  ASSERT_NE(nullptr, forward_10_seconds_button);
  EXPECT_TRUE(play_button->IsDrawn());
  EXPECT_TRUE(replay_10_seconds_button->IsDrawn());
  EXPECT_TRUE(forward_10_seconds_button->IsDrawn());

  gfx::Point point(progress_view->width() / 2, progress_view->height() / 2);
  ui::MouseEvent pressed_event(ui::EventType::kMousePressed, point, point,
                               ui::EventTimeForNow(), ui::EF_LEFT_MOUSE_BUTTON,
                               ui::EF_LEFT_MOUSE_BUTTON);
  EXPECT_CALL(pip_window_controller(), SeekTo(_));
  progress_view->OnMousePressed(pressed_event);

  // Move time forward to ensure drag delay timer has fired.
  task_environment()->FastForwardBy(base::Seconds(1));
  testing::Mock::VerifyAndClearExpectations(&pip_window_controller());

  // While dragging, the center controls should not be visible.
  EXPECT_FALSE(play_button->IsDrawn());
  EXPECT_FALSE(replay_10_seconds_button->IsDrawn());
  EXPECT_FALSE(forward_10_seconds_button->IsDrawn());

  ui::MouseEvent released_event = ui::MouseEvent(
      ui::EventType::kMouseReleased, point, point, ui::EventTimeForNow(),
      ui::EF_LEFT_MOUSE_BUTTON, ui::EF_LEFT_MOUSE_BUTTON);
  progress_view->OnMouseReleased(released_event);

  // Once the drag ends, the center controls should be visible again.
  EXPECT_TRUE(play_button->IsDrawn());
  EXPECT_TRUE(replay_10_seconds_button->IsDrawn());
  EXPECT_TRUE(forward_10_seconds_button->IsDrawn());
}

TEST_F(VideoOverlayWindowViewsWith2024UITest, TimestampDisplaysCurrentTime) {
  overlay_window().ForceControlsVisibleForTesting(true);
  media_session::MediaPosition media_position(/*playback_rate=*/0,
                                              /*duration=*/base::Seconds(100),
                                              /*position=*/base::Seconds(42),
                                              /*end_of_media=*/false);
  overlay_window().SetMediaPosition(media_position);

  views::Label* timestamp = overlay_window().timestamp_for_testing();
  ASSERT_NE(nullptr, timestamp);
  EXPECT_TRUE(timestamp->IsDrawn());
  EXPECT_EQ(u"0:42 / 1:40", timestamp->GetText());
}

TEST_F(VideoOverlayWindowViewsWith2024UITest,
       ReplayAndForward10SecondsSeekVideo) {
  overlay_window().ForceControlsVisibleForTesting(true);
  media_session::MediaPosition media_position(/*playback_rate=*/0,
                                              /*duration=*/base::Seconds(100),
                                              /*position=*/base::Seconds(42),
                                              /*end_of_media=*/false);
  overlay_window().SetMediaPosition(media_position);
  SimpleOverlayWindowImageButton* replay_10_seconds_button =
      overlay_window().replay_10_seconds_button_for_testing();
  SimpleOverlayWindowImageButton* forward_10_seconds_button =
      overlay_window().forward_10_seconds_button_for_testing();
  ASSERT_NE(nullptr, replay_10_seconds_button);
  ASSERT_NE(nullptr, forward_10_seconds_button);
  EXPECT_TRUE(replay_10_seconds_button->IsDrawn());
  EXPECT_TRUE(forward_10_seconds_button->IsDrawn());

  views::test::ButtonTestApi replay_button_clicker(replay_10_seconds_button);
  views::test::ButtonTestApi forward_button_clicker(forward_10_seconds_button);
  ui::MouseEvent dummy_event(ui::EventType::kMousePressed, gfx::Point(0, 0),
                             gfx::Point(0, 0), ui::EventTimeForNow(), 0, 0);

  // Clicking the replay 10 seconds button should seek backwards 10 seconds.
  PictureInPictureWindowManager::GetInstance()
      ->set_window_controller_for_testing(&pip_window_controller());
  EXPECT_CALL(pip_window_controller(), SeekTo(base::Seconds(32)));
  replay_button_clicker.NotifyClick(dummy_event);
  testing::Mock::VerifyAndClearExpectations(&pip_window_controller());

  // Clicking the forward 10 seconds button should seek forwards 10 seconds.
  PictureInPictureWindowManager::GetInstance()
      ->set_window_controller_for_testing(&pip_window_controller());
  EXPECT_CALL(pip_window_controller(), SeekTo(base::Seconds(52)));
  forward_button_clicker.NotifyClick(dummy_event);
  testing::Mock::VerifyAndClearExpectations(&pip_window_controller());

  // Clicking the replay 10 seconds button less than 10 seconds into the video
  // should seek to the beginning.
  media_session::MediaPosition early_media_position(
      /*playback_rate=*/0,
      /*duration=*/base::Seconds(100),
      /*position=*/base::Seconds(4),
      /*end_of_media=*/false);
  overlay_window().SetMediaPosition(early_media_position);
  EXPECT_CALL(pip_window_controller(), SeekTo(base::Seconds(0)));
  replay_button_clicker.NotifyClick(dummy_event);
  testing::Mock::VerifyAndClearExpectations(&pip_window_controller());

  // Clicking the forward 10 seconds button with less than 10 seconds left in
  // the video should seek to the end.
  media_session::MediaPosition late_media_position(
      /*playback_rate=*/0,
      /*duration=*/base::Seconds(100),
      /*position=*/base::Seconds(97),
      /*end_of_media=*/false);
  overlay_window().SetMediaPosition(late_media_position);
  EXPECT_CALL(pip_window_controller(), SeekTo(base::Seconds(100)));
  forward_button_clicker.NotifyClick(dummy_event);
  testing::Mock::VerifyAndClearExpectations(&pip_window_controller());
}

TEST_F(VideoOverlayWindowViewsWith2024UITest, DisplaysFavicon) {
  overlay_window().ForceControlsVisibleForTesting(true);
  views::ImageView* favicon_view = overlay_window().favicon_view_for_testing();
  ASSERT_NE(nullptr, favicon_view);
  EXPECT_TRUE(favicon_view->IsDrawn());

  // Before any favicon is set, the default favicon should be displayed.
  {
    ui::ImageModel image_model = favicon_view->GetImageModel();
    EXPECT_TRUE(image_model.IsVectorIcon());
    EXPECT_EQ(image_model.GetVectorIcon().vector_icon(),
              &vector_icons::kGlobeIcon);
  }

  // Setting the favicon should use that instead.
  {
    media_session::MediaImage test_favicon;
    test_favicon.src = GURL("https://google.com");
    SkBitmap bitmap;
    bitmap.allocN32Pixels(100, 100);
    bitmap.eraseColor(SK_ColorBLUE);
    PictureInPictureWindowManager::GetInstance()
        ->set_window_controller_for_testing(&pip_window_controller());
    EXPECT_CALL(pip_window_controller(),
                GetMediaImageImpl(test_favicon, 16, 16))
        .WillOnce(testing::Return(bitmap));

    overlay_window().SetFaviconImages({test_favicon});
    ui::ImageModel image_model = favicon_view->GetImageModel();
    EXPECT_FALSE(image_model.IsVectorIcon());
    EXPECT_TRUE(image_model.IsImage());
  }

  // Setting the favicon back to empty should change back to the default
  // favicon.
  {
    overlay_window().SetFaviconImages({});
    ui::ImageModel image_model = favicon_view->GetImageModel();
    EXPECT_TRUE(image_model.IsVectorIcon());
    EXPECT_EQ(image_model.GetVectorIcon().vector_icon(),
              &vector_icons::kGlobeIcon);
  }
}

TEST_F(VideoOverlayWindowViewsWith2024UITest, DisplaysOrigin) {
  overlay_window().ForceControlsVisibleForTesting(true);
  views::Label* origin = overlay_window().origin_for_testing();
  ASSERT_NE(nullptr, origin);
  EXPECT_TRUE(origin->IsDrawn());

  overlay_window().SetSourceTitle(u"google.com");
  EXPECT_EQ(origin->GetText(), u"google.com");
}

TEST_F(VideoOverlayWindowViewsWith2024UITest,
       ControlsNeverHideWhileProgressBarIsDragged) {
  overlay_window().ShowInactive();
  overlay_window().SetPlayPauseButtonVisibility(true);
  overlay_window().ForceControlsVisibleForTesting(true);

  // Move time forward to ensure controls layout is completed.
  WaitForLayout();

  global_media_controls::MediaProgressView* progress_view =
      overlay_window().progress_view_for_testing();
  ASSERT_NE(nullptr, progress_view);
  EXPECT_TRUE(progress_view->IsDrawn());

  // Start dragging.
  gfx::Point point(progress_view->width() / 2, progress_view->height() / 2);
  ui::MouseEvent pressed_event(ui::EventType::kMousePressed, point, point,
                               ui::EventTimeForNow(), ui::EF_LEFT_MOUSE_BUTTON,
                               ui::EF_LEFT_MOUSE_BUTTON);
  progress_view->OnMousePressed(pressed_event);

  // Move time forward to ensure drag delay timer has fired.
  task_environment()->FastForwardBy(base::Seconds(1));

  // While dragging, the controls should remain visible. We'll need to stop
  // forcing them visible for testing and then wait for the hide timer to fire.
  overlay_window().StopForcingControlsVisibleForTesting();
  task_environment()->FastForwardBy(base::Seconds(7));
  EXPECT_TRUE(overlay_window().GetControlsContainerView()->IsDrawn());

  ui::MouseEvent released_event = ui::MouseEvent(
      ui::EventType::kMouseReleased, point, point, ui::EventTimeForNow(),
      ui::EF_LEFT_MOUSE_BUTTON, ui::EF_LEFT_MOUSE_BUTTON);
  progress_view->OnMouseReleased(released_event);

  // Once the drag ends, the controls should be able to hide.
  task_environment()->FastForwardBy(base::Seconds(7));
  EXPECT_TRUE(overlay_window().GetControlsContainerView()->IsDrawn());
}

TEST_F(VideoOverlayWindowViewsWith2024UITest, LiveStatusShownForLiveVideos) {
  overlay_window().ForceControlsVisibleForTesting(true);
  views::Label* timestamp = overlay_window().timestamp_for_testing();
  views::Label* live_status = overlay_window().live_status_for_testing();
  ASSERT_NE(nullptr, timestamp);
  ASSERT_NE(nullptr, live_status);

  // The timestamp should start out visible while the live status should start
  // out hidden.
  EXPECT_FALSE(live_status->GetVisible());
  EXPECT_TRUE(timestamp->GetVisible());

  // Setting the position to live should hide the timestamp and show the live
  // status.
  media_session::MediaPosition live_media_position(
      /*playback_rate=*/0,
      /*duration=*/base::TimeDelta::Max(),
      /*position=*/base::Seconds(42),
      /*end_of_media=*/false);
  overlay_window().SetMediaPosition(live_media_position);
  EXPECT_TRUE(live_status->GetVisible());
  EXPECT_FALSE(timestamp->GetVisible());

  // Setting the position to a non-live video should hide the live status and
  // show the timestamp.
  media_session::MediaPosition media_position(/*playback_rate=*/0,
                                              /*duration=*/base::Seconds(100),
                                              /*position=*/base::Seconds(42),
                                              /*end_of_media=*/false);
  overlay_window().SetMediaPosition(media_position);
  EXPECT_FALSE(live_status->GetVisible());
  EXPECT_TRUE(timestamp->GetVisible());
}

TEST_F(VideoOverlayWindowViewsWith2024UITest, NextAndPreviousShareVisibility) {
  overlay_window().ForceControlsVisibleForTesting(true);
  SimpleOverlayWindowImageButton* next_button =
      overlay_window().next_track_controls_view_for_testing();
  SimpleOverlayWindowImageButton* prev_button =
      overlay_window().previous_track_controls_view_for_testing();

  ASSERT_NE(nullptr, next_button);
  ASSERT_NE(nullptr, prev_button);

  // If only "nexttrack" is enabled, both buttons are shown but only the next
  // button is enabled.
  overlay_window().SetNextTrackButtonVisibility(true);
  WaitForLayout();
  EXPECT_TRUE(next_button->GetVisible());
  EXPECT_TRUE(prev_button->GetVisible());
  EXPECT_TRUE(next_button->GetEnabled());
  EXPECT_FALSE(prev_button->GetEnabled());

  // If both previous and next track are enabled, then both buttons are shown
  // and enabled.
  overlay_window().SetPreviousTrackButtonVisibility(true);
  WaitForLayout();
  EXPECT_TRUE(next_button->GetVisible());
  EXPECT_TRUE(prev_button->GetVisible());
  EXPECT_TRUE(next_button->GetEnabled());
  EXPECT_TRUE(prev_button->GetEnabled());

  // When disabled again, the buttons are hidden.
  overlay_window().SetNextTrackButtonVisibility(false);
  overlay_window().SetPreviousTrackButtonVisibility(false);
  WaitForLayout();
  EXPECT_FALSE(next_button->GetVisible());
  EXPECT_FALSE(prev_button->GetVisible());

  // If only "previousslide" is enabled, both buttons are shown but only the
  // previous button is enabled.
  overlay_window().SetPreviousSlideButtonVisibility(true);
  WaitForLayout();
  EXPECT_TRUE(next_button->GetVisible());
  EXPECT_TRUE(prev_button->GetVisible());
  EXPECT_FALSE(next_button->GetEnabled());
  EXPECT_TRUE(prev_button->GetEnabled());

  // If both previous and next slide are enabled, then both buttons are shown
  // and enabled.
  overlay_window().SetNextSlideButtonVisibility(true);
  WaitForLayout();
  EXPECT_TRUE(next_button->GetVisible());
  EXPECT_TRUE(prev_button->GetVisible());
  EXPECT_TRUE(next_button->GetEnabled());
  EXPECT_TRUE(prev_button->GetEnabled());

  // When disabled again, the buttons are hidden.
  overlay_window().SetNextSlideButtonVisibility(false);
  overlay_window().SetPreviousSlideButtonVisibility(false);
  WaitForLayout();
  EXPECT_FALSE(next_button->GetVisible());
  EXPECT_FALSE(prev_button->GetVisible());
}

TEST_F(VideoOverlayWindowViewsWith2024UITest,
       FastForwardAndRewindAreHiddenForLiveVideos) {
  overlay_window().ForceControlsVisibleForTesting(true);
  SimpleOverlayWindowImageButton* replay_10_seconds_button =
      overlay_window().replay_10_seconds_button_for_testing();
  SimpleOverlayWindowImageButton* forward_10_seconds_button =
      overlay_window().forward_10_seconds_button_for_testing();
  ASSERT_NE(nullptr, replay_10_seconds_button);
  ASSERT_NE(nullptr, forward_10_seconds_button);

  // For live media, the fast-forward and rewind buttons should be hidden.
  media_session::MediaPosition live_media_position(
      /*playback_rate=*/0,
      /*duration=*/base::TimeDelta::Max(),
      /*position=*/base::Seconds(42),
      /*end_of_media=*/false);
  overlay_window().SetMediaPosition(live_media_position);
  EXPECT_FALSE(replay_10_seconds_button->GetVisible());
  EXPECT_FALSE(forward_10_seconds_button->GetVisible());
}

TEST_F(VideoOverlayWindowViewsWith2024UITest, VideoConferencingUI) {
  overlay_window().ForceControlsVisibleForTesting(true);
  ToggleCameraButton* toggle_camera_button =
      overlay_window().toggle_camera_button_for_testing();
  ToggleMicrophoneButton* toggle_microphone_button =
      overlay_window().toggle_microphone_button_for_testing();
  HangUpButton* hang_up_button = overlay_window().hang_up_button_for_testing();
  global_media_controls::MediaProgressView* progress_view =
      overlay_window().progress_view_for_testing();

  ASSERT_NE(nullptr, toggle_camera_button);
  ASSERT_NE(nullptr, toggle_microphone_button);
  ASSERT_NE(nullptr, hang_up_button);
  ASSERT_NE(nullptr, progress_view);

  // The VC controls should start hidden, and other controls should be shown.
  WaitForLayout();
  EXPECT_FALSE(toggle_camera_button->IsDrawn());
  EXPECT_FALSE(toggle_microphone_button->IsDrawn());
  EXPECT_FALSE(hang_up_button->IsDrawn());
  EXPECT_TRUE(progress_view->IsDrawn());

  // If at least one VC control is available, it should be shown and the non-VC
  // controls should be hidden.
  overlay_window().SetHangUpButtonVisibility(true);
  WaitForLayout();
  EXPECT_FALSE(toggle_camera_button->IsDrawn());
  EXPECT_FALSE(toggle_microphone_button->IsDrawn());
  EXPECT_TRUE(hang_up_button->IsDrawn());
  EXPECT_FALSE(progress_view->IsDrawn());

  // Enabling other VC controls should show those.
  overlay_window().SetToggleCameraButtonVisibility(true);
  overlay_window().SetToggleMicrophoneButtonVisibility(true);
  WaitForLayout();
  EXPECT_TRUE(toggle_camera_button->IsDrawn());
  EXPECT_TRUE(toggle_microphone_button->IsDrawn());
  EXPECT_TRUE(hang_up_button->IsDrawn());
  EXPECT_FALSE(progress_view->IsDrawn());

  // Disabling all VC controls should show the non-VC controls again.
  overlay_window().SetHangUpButtonVisibility(false);
  overlay_window().SetToggleCameraButtonVisibility(false);
  overlay_window().SetToggleMicrophoneButtonVisibility(false);
  WaitForLayout();
  EXPECT_FALSE(toggle_camera_button->IsDrawn());
  EXPECT_FALSE(toggle_microphone_button->IsDrawn());
  EXPECT_FALSE(hang_up_button->IsDrawn());
  EXPECT_TRUE(progress_view->IsDrawn());
}

TEST_F(VideoOverlayWindowViewsWith2024UITest, LiveCaption) {
  overlay_window().ForceControlsVisibleForTesting(true);
  profile().GetPrefs()->SetBoolean(prefs::kLiveCaptionEnabled, false);
  SimpleOverlayWindowImageButton* live_caption_button =
      overlay_window().live_caption_button_for_testing();
  OverlayWindowLiveCaptionDialog* live_caption_dialog =
      overlay_window().live_caption_dialog_for_testing();

  ASSERT_NE(nullptr, live_caption_button);
  ASSERT_NE(nullptr, live_caption_dialog);

  // The live caption button should start visible and the live caption dialog
  // should start invisible.
  WaitForLayout();
  EXPECT_TRUE(live_caption_button->IsDrawn());
  EXPECT_FALSE(live_caption_dialog->IsDrawn());

  // Pressing the live caption button should display the live caption dialog.
  views::test::ButtonTestApi live_caption_button_clicker(live_caption_button);
  ui::MouseEvent dummy_event(ui::EventType::kMousePressed, gfx::Point(0, 0),
                             gfx::Point(0, 0), ui::EventTimeForNow(), 0, 0);
  live_caption_button_clicker.NotifyClick(dummy_event);
  WaitForLayout();
  EXPECT_TRUE(live_caption_dialog->IsDrawn());

  // The live caption button should be enabled and toggled off, while the live
  // translate button should be disabled and toggled off.
  EXPECT_FALSE(profile().GetPrefs()->GetBoolean(prefs::kLiveCaptionEnabled));
  views::ToggleButton* live_caption_toggle_button =
      live_caption_dialog->live_caption_button_for_testing();
  views::ToggleButton* live_translate_toggle_button =
      live_caption_dialog->live_translate_button_for_testing();
  EXPECT_TRUE(live_caption_toggle_button->GetEnabled());
  EXPECT_FALSE(live_caption_toggle_button->GetIsOn());
  EXPECT_FALSE(live_translate_toggle_button->GetEnabled());
  EXPECT_FALSE(live_translate_toggle_button->GetIsOn());

  // Toggling the live caption button should enable the live translate button
  // and also enable the live caption pref.
  views::test::ButtonTestApi live_caption_toggle_button_clicker(
      live_caption_toggle_button);
  live_caption_toggle_button_clicker.NotifyClick(dummy_event);
  WaitForLayout();
  EXPECT_TRUE(profile().GetPrefs()->GetBoolean(prefs::kLiveCaptionEnabled));
  EXPECT_TRUE(live_caption_toggle_button->GetIsOn());
  EXPECT_TRUE(live_translate_toggle_button->GetEnabled());
  EXPECT_FALSE(live_translate_toggle_button->GetIsOn());
}