File: fullscreen_controller_unittest.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; 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,806; 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 (812 lines) | stat: -rw-r--r-- 37,761 bytes parent folder | download | duplicates (5)
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
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/test/task_environment.h"
#include "components/remote_cocoa/app_shim/native_widget_ns_window_fullscreen_controller.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace remote_cocoa::test {

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

class MockClient : public NativeWidgetNSWindowFullscreenController::Client {
 public:
  MOCK_METHOD1(FullscreenControllerTransitionStart, void(bool));
  MOCK_METHOD1(FullscreenControllerTransitionComplete, void(bool));
  MOCK_METHOD3(FullscreenControllerSetFrame,
               void(const gfx::Rect&,
                    bool animate,
                    base::OnceCallback<void()> animation_complete));

  MOCK_METHOD0(FullscreenControllerToggleFullscreen, void());
  MOCK_METHOD0(FullscreenControllerCloseWindow, void());
  MOCK_CONST_METHOD0(FullscreenControllerGetDisplayId, int64_t());
  MOCK_CONST_METHOD1(FullscreenControllerGetFrameForDisplay,
                     gfx::Rect(int64_t display_id));
  MOCK_CONST_METHOD0(FullscreenControllerGetFrame, gfx::Rect());
};

class MacFullscreenControllerTest : public testing::Test {
 public:
  void SetUp() override {
    EXPECT_CALL(mock_client_, FullscreenControllerToggleFullscreen()).Times(0);
    EXPECT_CALL(mock_client_, FullscreenControllerSetFrame(_, _, _)).Times(0);
    EXPECT_CALL(mock_client_, FullscreenControllerCloseWindow()).Times(0);
  }

 protected:
  const gfx::Rect kWindowRect{1, 2, 3, 4};
  const int64_t kDisplay0Id = 0;
  const int64_t kDisplay1Id = 1;
  const gfx::Rect kDisplay0Frame{9, 10, 11, 12};
  const gfx::Rect kDisplay1Frame{13, 14, 15, 16};

  MockClient mock_client_;
  NativeWidgetNSWindowFullscreenController controller_{&mock_client_};
  base::test::SingleThreadTaskEnvironment task_environment_;
};

// Fake implementation of FullscreenControllerSetFrame
// which executes the given callback immediately.
void FullscreenControllerSetFrameFake(const gfx::Rect&,
                                      bool,
                                      base::OnceCallback<void()> callback) {
  std::move(callback).Run();
}

// Simple enter-and-exit fullscreen via the green traffic light button.
TEST_F(MacFullscreenControllerTest, SimpleUserInitiated) {
  // Enter fullscreen the way that clicking the green traffic light does it.
  EXPECT_CALL(mock_client_, FullscreenControllerGetFrame())
      .Times(1)
      .WillOnce(Return(kWindowRect));
  EXPECT_CALL(mock_client_, FullscreenControllerTransitionStart(true)).Times(1);
  controller_.OnWindowWillEnterFullscreen();
  EXPECT_TRUE(controller_.IsInFullscreenTransition());
  EXPECT_TRUE(controller_.GetTargetFullscreenState());

  // The controller should do nothing, and wait until it receives
  // OnWindowDidEnterFullscreen.
  task_environment_.RunUntilIdle();
  EXPECT_TRUE(controller_.IsInFullscreenTransition());

  // Calling EnterFullscreen inside the transition will do nothing.
  controller_.EnterFullscreen(display::kInvalidDisplayId);
  task_environment_.RunUntilIdle();
  EXPECT_TRUE(controller_.IsInFullscreenTransition());
  EXPECT_TRUE(controller_.GetTargetFullscreenState());

  // Complete the transition.
  EXPECT_CALL(mock_client_, FullscreenControllerTransitionComplete(true))
      .Times(1);
  controller_.OnWindowDidEnterFullscreen();
  EXPECT_FALSE(controller_.IsInFullscreenTransition());
  EXPECT_TRUE(controller_.GetTargetFullscreenState());

  // Calling EnterFullscreen while fullscreen should do nothing.
  controller_.EnterFullscreen(display::kInvalidDisplayId);
  EXPECT_FALSE(controller_.IsInFullscreenTransition());
  task_environment_.RunUntilIdle();
  EXPECT_FALSE(controller_.IsInFullscreenTransition());
  EXPECT_TRUE(controller_.GetTargetFullscreenState());

  // Calling EnterFullscreen specifying the display that we are currently on
  // will also be a no-op.
  EXPECT_CALL(mock_client_, FullscreenControllerGetDisplayId())
      .WillOnce(Return(kDisplay0Id));
  controller_.EnterFullscreen(kDisplay0Id);
  task_environment_.RunUntilIdle();
  EXPECT_FALSE(controller_.IsInFullscreenTransition());
  EXPECT_TRUE(controller_.GetTargetFullscreenState());

  // Exit fullscreen via the green traffic light.
  EXPECT_CALL(mock_client_, FullscreenControllerTransitionStart(false))
      .Times(1);
  controller_.OnWindowWillExitFullscreen();
  EXPECT_TRUE(controller_.IsInFullscreenTransition());
  EXPECT_FALSE(controller_.GetTargetFullscreenState());

  // Calling ExitFullscreen inside the transition will do nothing.
  controller_.ExitFullscreen();
  task_environment_.RunUntilIdle();
  EXPECT_TRUE(controller_.IsInFullscreenTransition());
  EXPECT_FALSE(controller_.GetTargetFullscreenState());

  // Complete the transition.
  EXPECT_CALL(mock_client_, FullscreenControllerTransitionComplete(false))
      .Times(1);
  controller_.OnWindowDidExitFullscreen();
  EXPECT_FALSE(controller_.IsInFullscreenTransition());
  EXPECT_FALSE(controller_.GetTargetFullscreenState());
  task_environment_.RunUntilIdle();
}

// Simple enter-and-exit fullscreen programmatically.
TEST_F(MacFullscreenControllerTest, SimpleProgrammatic) {
  // Enter fullscreen.
  EXPECT_CALL(mock_client_, FullscreenControllerGetFrame())
      .Times(1)
      .WillOnce(Return(kWindowRect));
  EXPECT_CALL(mock_client_, FullscreenControllerTransitionStart(true)).Times(1);
  controller_.EnterFullscreen(display::kInvalidDisplayId);
  EXPECT_TRUE(controller_.IsInFullscreenTransition());

  // The call to ToggleFullscreen is sent via a posted task, so it shouldn't
  // happen until after the run loop is pumped. The function
  // OnWindowWillEnterFullscreen is called from within ToggleFullscreen.
  EXPECT_CALL(mock_client_, FullscreenControllerToggleFullscreen())
      .WillOnce(Invoke(&controller_, &NativeWidgetNSWindowFullscreenController::
                                         OnWindowWillEnterFullscreen));
  task_environment_.RunUntilIdle();
  EXPECT_TRUE(controller_.IsInFullscreenTransition());

  // Complete the transition.
  EXPECT_CALL(mock_client_, FullscreenControllerTransitionComplete(true))
      .Times(1);
  controller_.OnWindowDidEnterFullscreen();
  EXPECT_FALSE(controller_.IsInFullscreenTransition());

  // Exit fullscreen.
  EXPECT_CALL(mock_client_, FullscreenControllerTransitionStart(false))
      .Times(1);
  controller_.ExitFullscreen();
  EXPECT_TRUE(controller_.IsInFullscreenTransition());

  // Make ToggleFullscreen happen, and invoke OnWindowWillExitFullscreen.
  EXPECT_CALL(mock_client_, FullscreenControllerToggleFullscreen())
      .WillOnce(Invoke(&controller_, &NativeWidgetNSWindowFullscreenController::
                                         OnWindowWillExitFullscreen));
  task_environment_.RunUntilIdle();
  EXPECT_TRUE(controller_.IsInFullscreenTransition());

  // Complete the transition.
  EXPECT_CALL(mock_client_, FullscreenControllerTransitionComplete(false))
      .Times(1);
  controller_.OnWindowDidExitFullscreen();
  EXPECT_FALSE(controller_.IsInFullscreenTransition());
  task_environment_.RunUntilIdle();
}

// A transition that fails to enter fullscreen.
TEST_F(MacFullscreenControllerTest, FailEnterFullscreenSimple) {
  // Enter fullscreen.
  EXPECT_CALL(mock_client_, FullscreenControllerGetFrame())
      .Times(1)
      .WillOnce(Return(kWindowRect));
  EXPECT_CALL(mock_client_, FullscreenControllerTransitionStart(true)).Times(1);
  controller_.EnterFullscreen(display::kInvalidDisplayId);
  EXPECT_CALL(mock_client_, FullscreenControllerToggleFullscreen())
      .WillOnce(Invoke(&controller_, &NativeWidgetNSWindowFullscreenController::
                                         OnWindowWillEnterFullscreen));
  task_environment_.RunUntilIdle();

  // Fail the transition.
  EXPECT_CALL(mock_client_, FullscreenControllerTransitionComplete(false))
      .Times(1);
  controller_.OnWindowDidExitFullscreen();
  EXPECT_FALSE(controller_.IsInFullscreenTransition());
  EXPECT_FALSE(controller_.GetTargetFullscreenState());
  task_environment_.RunUntilIdle();
  EXPECT_FALSE(controller_.IsInFullscreenTransition());
  EXPECT_FALSE(controller_.GetTargetFullscreenState());
}

// A transition that fails to exit fullscreen.
TEST_F(MacFullscreenControllerTest, FailExitFullscreen) {
  // Enter fullscreen.
  EXPECT_CALL(mock_client_, FullscreenControllerGetFrame())
      .Times(1)
      .WillOnce(Return(kWindowRect));
  EXPECT_CALL(mock_client_, FullscreenControllerTransitionStart(true)).Times(1);
  controller_.EnterFullscreen(display::kInvalidDisplayId);
  EXPECT_CALL(mock_client_, FullscreenControllerToggleFullscreen())
      .WillOnce(Invoke(&controller_, &NativeWidgetNSWindowFullscreenController::
                                         OnWindowWillEnterFullscreen));
  task_environment_.RunUntilIdle();
  EXPECT_CALL(mock_client_, FullscreenControllerTransitionComplete(true))
      .Times(1);
  controller_.OnWindowDidEnterFullscreen();
  EXPECT_FALSE(controller_.IsInFullscreenTransition());
  EXPECT_TRUE(controller_.GetTargetFullscreenState());

  // Start to exit fullscreen, through OnWindowWillExitFullscreen.
  EXPECT_CALL(mock_client_, FullscreenControllerTransitionStart(false))
      .Times(1);
  controller_.ExitFullscreen();
  EXPECT_TRUE(controller_.IsInFullscreenTransition());
  EXPECT_CALL(mock_client_, FullscreenControllerToggleFullscreen())
      .WillOnce(Invoke(&controller_, &NativeWidgetNSWindowFullscreenController::
                                         OnWindowWillExitFullscreen));
  task_environment_.RunUntilIdle();
  EXPECT_TRUE(controller_.IsInFullscreenTransition());

  // Fail the transition.
  EXPECT_CALL(mock_client_, FullscreenControllerTransitionComplete(true))
      .Times(1);
  controller_.OnWindowDidEnterFullscreen();
  EXPECT_FALSE(controller_.IsInFullscreenTransition());
  EXPECT_TRUE(controller_.GetTargetFullscreenState());
  task_environment_.RunUntilIdle();
  EXPECT_FALSE(controller_.IsInFullscreenTransition());
  EXPECT_TRUE(controller_.GetTargetFullscreenState());
}

// A simple cross-screen transition.
TEST_F(MacFullscreenControllerTest, SimpleCrossScreen) {
  // Enter fullscreen on display 1, from display 0.
  EXPECT_CALL(mock_client_, FullscreenControllerGetFrame())
      .Times(1)
      .WillOnce(Return(kWindowRect));
  EXPECT_CALL(mock_client_, FullscreenControllerTransitionStart(true)).Times(1);
  controller_.EnterFullscreen(kDisplay1Id);
  EXPECT_TRUE(controller_.IsInFullscreenTransition());

  // This will trigger a call to MoveToTargetDisplayThenToggleFullscreen, even
  // though we are not actually moving displays. It will also then post a task
  // to ToggleFullscreen (because RunUntilIdle will pick up that posted task).
  EXPECT_CALL(mock_client_, FullscreenControllerGetFrameForDisplay(kDisplay1Id))
      .Times(1)
      .WillOnce(Return(kDisplay1Frame));
  EXPECT_CALL(mock_client_,
              FullscreenControllerSetFrame(kDisplay1Frame, true, _))
      .WillOnce(FullscreenControllerSetFrameFake);
  EXPECT_CALL(mock_client_, FullscreenControllerToggleFullscreen())
      .WillOnce(Invoke(&controller_, &NativeWidgetNSWindowFullscreenController::
                                         OnWindowWillEnterFullscreen));
  task_environment_.RunUntilIdle();
  EXPECT_TRUE(controller_.IsInFullscreenTransition());
  EXPECT_TRUE(controller_.GetTargetFullscreenState());

  // Complete the transition to fullscreen on the new display.
  EXPECT_CALL(mock_client_, FullscreenControllerTransitionComplete(true))
      .Times(1);
  controller_.OnWindowDidEnterFullscreen();
  EXPECT_FALSE(controller_.IsInFullscreenTransition());
  EXPECT_TRUE(controller_.GetTargetFullscreenState());

  // Re-entering fullscreen on our current display should be a no-op.
  EXPECT_CALL(mock_client_, FullscreenControllerGetDisplayId())
      .WillOnce(Return(kDisplay1Id));
  controller_.EnterFullscreen(kDisplay1Id);
  task_environment_.RunUntilIdle();
  EXPECT_FALSE(controller_.IsInFullscreenTransition());
  EXPECT_TRUE(controller_.GetTargetFullscreenState());

  // Exit fullscreen. This will post a task to restore bounds.
  EXPECT_CALL(mock_client_, FullscreenControllerTransitionStart(false))
      .Times(1);
  controller_.ExitFullscreen();
  EXPECT_CALL(mock_client_, FullscreenControllerToggleFullscreen())
      .WillOnce(Invoke(&controller_, &NativeWidgetNSWindowFullscreenController::
                                         OnWindowWillExitFullscreen));
  task_environment_.RunUntilIdle();
  controller_.OnWindowDidExitFullscreen();
  EXPECT_TRUE(controller_.IsInFullscreenTransition());
  EXPECT_FALSE(controller_.GetTargetFullscreenState());

  // Let the run loop run, it will restore the bounds.
  EXPECT_CALL(mock_client_, FullscreenControllerSetFrame(kWindowRect, true, _))
      .WillOnce(FullscreenControllerSetFrameFake);
  EXPECT_CALL(mock_client_, FullscreenControllerTransitionComplete(false))
      .Times(1);
  task_environment_.RunUntilIdle();
  EXPECT_FALSE(controller_.IsInFullscreenTransition());
  EXPECT_FALSE(controller_.GetTargetFullscreenState());
}

// A cross-screen transition after having entered fullscreen.
TEST_F(MacFullscreenControllerTest, CrossScreenFromFullscreen) {
  // Enter fullscreen the way that clicking the green traffic light does it.
  EXPECT_CALL(mock_client_, FullscreenControllerGetFrame())
      .Times(1)
      .WillOnce(Return(kWindowRect));
  EXPECT_CALL(mock_client_, FullscreenControllerTransitionStart(true)).Times(1);
  controller_.OnWindowWillEnterFullscreen();
  EXPECT_CALL(mock_client_, FullscreenControllerTransitionComplete(true))
      .Times(1);
  controller_.OnWindowDidEnterFullscreen();
  EXPECT_FALSE(controller_.IsInFullscreenTransition());
  EXPECT_TRUE(controller_.GetTargetFullscreenState());

  // Enter fullscreen on a different display.
  EXPECT_CALL(mock_client_, FullscreenControllerTransitionStart(true)).Times(1);
  EXPECT_CALL(mock_client_, FullscreenControllerGetDisplayId())
      .WillRepeatedly(Return(kDisplay0Id));
  controller_.EnterFullscreen(kDisplay1Id);
  EXPECT_TRUE(controller_.IsInFullscreenTransition());
  EXPECT_TRUE(controller_.GetTargetFullscreenState());

  // Execute the task posted to ToggleFullscreen.
  EXPECT_CALL(mock_client_, FullscreenControllerToggleFullscreen())
      .WillOnce(Invoke(&controller_, &NativeWidgetNSWindowFullscreenController::
                                         OnWindowWillExitFullscreen));
  task_environment_.RunUntilIdle();
  EXPECT_TRUE(controller_.IsInFullscreenTransition());
  EXPECT_TRUE(controller_.GetTargetFullscreenState());

  // Complete the transition to windowed. This will post a task to move to the
  // target display's frame.
  EXPECT_CALL(mock_client_, FullscreenControllerGetFrameForDisplay(kDisplay1Id))
      .Times(1)
      .WillOnce(Return(kDisplay1Frame));
  controller_.OnWindowDidExitFullscreen();
  EXPECT_TRUE(controller_.IsInFullscreenTransition());
  EXPECT_TRUE(controller_.GetTargetFullscreenState());

  // Execute the posted task to set the new frame. This will also re-toggle
  // fullscreen in the RunUntilIdle.
  EXPECT_CALL(mock_client_,
              FullscreenControllerSetFrame(kDisplay1Frame, true, _))
      .WillOnce(FullscreenControllerSetFrameFake);
  EXPECT_CALL(mock_client_, FullscreenControllerToggleFullscreen())
      .WillOnce(Invoke(&controller_, &NativeWidgetNSWindowFullscreenController::
                                         OnWindowWillEnterFullscreen));
  task_environment_.RunUntilIdle();
  EXPECT_TRUE(controller_.GetTargetFullscreenState());

  // Complete the transition.
  EXPECT_CALL(mock_client_, FullscreenControllerTransitionComplete(true))
      .Times(1);
  controller_.OnWindowDidEnterFullscreen();
  task_environment_.RunUntilIdle();
  EXPECT_FALSE(controller_.IsInFullscreenTransition());
  EXPECT_TRUE(controller_.GetTargetFullscreenState());

  // Exit fullscreen. This will restore bounds.
  EXPECT_CALL(mock_client_, FullscreenControllerTransitionStart(false))
      .Times(1);
  controller_.ExitFullscreen();
  EXPECT_CALL(mock_client_, FullscreenControllerToggleFullscreen())
      .WillOnce(Invoke(&controller_, &NativeWidgetNSWindowFullscreenController::
                                         OnWindowWillExitFullscreen));
  task_environment_.RunUntilIdle();
  controller_.OnWindowDidExitFullscreen();
  EXPECT_CALL(mock_client_, FullscreenControllerSetFrame(kWindowRect, true, _))
      .WillOnce(FullscreenControllerSetFrameFake);
  EXPECT_CALL(mock_client_, FullscreenControllerTransitionComplete(false))
      .Times(1);
  task_environment_.RunUntilIdle();
  EXPECT_FALSE(controller_.IsInFullscreenTransition());
  EXPECT_FALSE(controller_.GetTargetFullscreenState());
}

// Fail a cross-screen transition when exiting fullscreen.
TEST_F(MacFullscreenControllerTest, CrossScreenFromFullscreenFailExit) {
  // Enter fullscreen the way that clicking the green traffic light does it.
  EXPECT_CALL(mock_client_, FullscreenControllerGetFrame())
      .Times(1)
      .WillOnce(Return(kWindowRect));
  EXPECT_CALL(mock_client_, FullscreenControllerTransitionStart(true)).Times(1);
  controller_.OnWindowWillEnterFullscreen();
  EXPECT_CALL(mock_client_, FullscreenControllerTransitionComplete(true))
      .Times(1);
  controller_.OnWindowDidEnterFullscreen();
  EXPECT_FALSE(controller_.IsInFullscreenTransition());
  EXPECT_TRUE(controller_.GetTargetFullscreenState());

  // Attempt to enter fullscreen on a different display. Get as far as toggling
  // fullscreen to get back to windowed mode.
  EXPECT_CALL(mock_client_, FullscreenControllerTransitionStart(true)).Times(1);
  EXPECT_CALL(mock_client_, FullscreenControllerGetDisplayId())
      .WillRepeatedly(Return(kDisplay0Id));
  controller_.EnterFullscreen(kDisplay1Id);
  EXPECT_CALL(mock_client_, FullscreenControllerToggleFullscreen())
      .WillOnce(Invoke(&controller_, &NativeWidgetNSWindowFullscreenController::
                                         OnWindowWillExitFullscreen));
  task_environment_.RunUntilIdle();
  EXPECT_TRUE(controller_.IsInFullscreenTransition());
  EXPECT_TRUE(controller_.GetTargetFullscreenState());

  // Fail the transition back to windowed. This should just leave us as being
  // fullscreen. It will issue the TransitionComplete notification indicating
  // that the transition is over, and that we're in fullscreen mode (no mention
  // of which display).
  EXPECT_CALL(mock_client_, FullscreenControllerTransitionComplete(true))
      .Times(1);
  controller_.OnWindowDidEnterFullscreen();
  EXPECT_FALSE(controller_.IsInFullscreenTransition());
  EXPECT_TRUE(controller_.GetTargetFullscreenState());

  // No further tasks should run.
  task_environment_.RunUntilIdle();
  EXPECT_FALSE(controller_.IsInFullscreenTransition());
  EXPECT_TRUE(controller_.GetTargetFullscreenState());
}

// Fail a cross-screen transition when entering fullscreen.
TEST_F(MacFullscreenControllerTest, CrossScreenFromFullscreenFailEnter) {
  // Enter fullscreen the way that clicking the green traffic light does it.
  EXPECT_CALL(mock_client_, FullscreenControllerGetFrame())
      .Times(1)
      .WillOnce(Return(kWindowRect));
  EXPECT_CALL(mock_client_, FullscreenControllerTransitionStart(true)).Times(1);
  controller_.OnWindowWillEnterFullscreen();
  EXPECT_CALL(mock_client_, FullscreenControllerTransitionComplete(true))
      .Times(1);
  controller_.OnWindowDidEnterFullscreen();
  EXPECT_FALSE(controller_.IsInFullscreenTransition());
  EXPECT_TRUE(controller_.GetTargetFullscreenState());

  // Enter fullscreen on a different display. Get as far as toggling fullscreen
  // to get back to windowed mode, moving the window, and then toggling
  // fullscreen on the new display.
  EXPECT_CALL(mock_client_, FullscreenControllerTransitionStart(true)).Times(1);
  EXPECT_CALL(mock_client_, FullscreenControllerGetDisplayId())
      .WillRepeatedly(Return(kDisplay0Id));
  controller_.EnterFullscreen(kDisplay1Id);
  EXPECT_CALL(mock_client_, FullscreenControllerToggleFullscreen())
      .WillOnce(Invoke(&controller_, &NativeWidgetNSWindowFullscreenController::
                                         OnWindowWillExitFullscreen));
  task_environment_.RunUntilIdle();
  EXPECT_CALL(mock_client_, FullscreenControllerGetFrameForDisplay(kDisplay1Id))
      .Times(1)
      .WillOnce(Return(kDisplay1Frame));
  controller_.OnWindowDidExitFullscreen();
  EXPECT_CALL(mock_client_,
              FullscreenControllerSetFrame(kDisplay1Frame, true, _))
      .WillOnce(FullscreenControllerSetFrameFake);
  EXPECT_CALL(mock_client_, FullscreenControllerToggleFullscreen())
      .WillOnce(Invoke(&controller_, &NativeWidgetNSWindowFullscreenController::
                                         OnWindowWillEnterFullscreen));
  task_environment_.RunUntilIdle();
  EXPECT_TRUE(controller_.IsInFullscreenTransition());
  EXPECT_TRUE(controller_.GetTargetFullscreenState());

  // Fail the transition to fullscreen mode. This will cause us to post tasks
  // to stay in windowed mode.
  controller_.OnWindowDidExitFullscreen();
  EXPECT_TRUE(controller_.IsInFullscreenTransition());
  EXPECT_FALSE(controller_.GetTargetFullscreenState());

  // This will cause us to restore the window's original frame, and then declare
  // the transition complete, with the final state after transition as being
  // windowed.
  EXPECT_CALL(mock_client_, FullscreenControllerSetFrame(kWindowRect, true, _))
      .WillOnce(FullscreenControllerSetFrameFake);
  EXPECT_CALL(mock_client_, FullscreenControllerTransitionComplete(false))
      .Times(1);
  task_environment_.RunUntilIdle();
  EXPECT_FALSE(controller_.IsInFullscreenTransition());
  EXPECT_FALSE(controller_.GetTargetFullscreenState());
}

// Be instructed to exit fullscreen while entering fullscreen.
TEST_F(MacFullscreenControllerTest, ExitWhileEntering) {
  // Enter fullscreen the way that clicking the green traffic light does it.
  EXPECT_CALL(mock_client_, FullscreenControllerGetFrame())
      .Times(1)
      .WillOnce(Return(kWindowRect));
  EXPECT_CALL(mock_client_, FullscreenControllerTransitionStart(true)).Times(1);
  controller_.OnWindowWillEnterFullscreen();
  task_environment_.RunUntilIdle();
  EXPECT_TRUE(controller_.IsInFullscreenTransition());

  // Call ExitFullscreen inside the transition.
  controller_.ExitFullscreen();
  task_environment_.RunUntilIdle();
  EXPECT_TRUE(controller_.IsInFullscreenTransition());
  EXPECT_FALSE(controller_.GetTargetFullscreenState());

  // Complete the transition to fullscreen. This will report that it is still
  // in transition to windowed.
  controller_.OnWindowDidEnterFullscreen();
  EXPECT_TRUE(controller_.IsInFullscreenTransition());
  EXPECT_FALSE(controller_.GetTargetFullscreenState());

  // The above call will have posted a ToggleFullscreen task.
  EXPECT_CALL(mock_client_, FullscreenControllerToggleFullscreen())
      .WillOnce(Invoke(&controller_, &NativeWidgetNSWindowFullscreenController::
                                         OnWindowWillExitFullscreen));
  task_environment_.RunUntilIdle();

  // Complete the transition to windowed mode.
  EXPECT_CALL(mock_client_, FullscreenControllerTransitionComplete(false))
      .Times(1);
  controller_.OnWindowDidExitFullscreen();
  EXPECT_FALSE(controller_.IsInFullscreenTransition());
  EXPECT_FALSE(controller_.GetTargetFullscreenState());

  // Nothing more should happen.
  task_environment_.RunUntilIdle();
}

// Be instructed to enter fullscreen while exiting fullscreen.
TEST_F(MacFullscreenControllerTest, EnterWhileExiting) {
  // Enter fullscreen the way that clicking the green traffic light does it.
  EXPECT_CALL(mock_client_, FullscreenControllerGetFrame())
      .Times(1)
      .WillOnce(Return(kWindowRect));
  EXPECT_CALL(mock_client_, FullscreenControllerTransitionStart(true)).Times(1);
  controller_.OnWindowWillEnterFullscreen();
  task_environment_.RunUntilIdle();
  EXPECT_TRUE(controller_.IsInFullscreenTransition());
  EXPECT_CALL(mock_client_, FullscreenControllerTransitionComplete(true))
      .Times(1);
  controller_.OnWindowDidEnterFullscreen();
  EXPECT_FALSE(controller_.IsInFullscreenTransition());
  EXPECT_TRUE(controller_.GetTargetFullscreenState());

  // Exit fullscreen.
  EXPECT_CALL(mock_client_, FullscreenControllerTransitionStart(false))
      .Times(1);
  controller_.OnWindowWillExitFullscreen();
  EXPECT_TRUE(controller_.IsInFullscreenTransition());
  EXPECT_FALSE(controller_.GetTargetFullscreenState());

  // Call EnterFullscreen inside the transition.
  controller_.EnterFullscreen(display::kInvalidDisplayId);
  EXPECT_TRUE(controller_.IsInFullscreenTransition());
  EXPECT_TRUE(controller_.GetTargetFullscreenState());

  // Complete the transition to windowed mode. This will report that it is
  // still in transition to fullscreen.
  controller_.OnWindowDidExitFullscreen();
  EXPECT_TRUE(controller_.IsInFullscreenTransition());
  EXPECT_TRUE(controller_.GetTargetFullscreenState());

  // The above call will have posted a ToggleFullscreen task.
  EXPECT_CALL(mock_client_, FullscreenControllerToggleFullscreen())
      .WillOnce(Invoke(&controller_, &NativeWidgetNSWindowFullscreenController::
                                         OnWindowWillEnterFullscreen));
  task_environment_.RunUntilIdle();
  EXPECT_TRUE(controller_.IsInFullscreenTransition());
  EXPECT_TRUE(controller_.GetTargetFullscreenState());

  // Complete the transition to fullscreen mode.
  EXPECT_CALL(mock_client_, FullscreenControllerTransitionComplete(true))
      .Times(1);
  controller_.OnWindowDidEnterFullscreen();
  EXPECT_FALSE(controller_.IsInFullscreenTransition());
  EXPECT_TRUE(controller_.GetTargetFullscreenState());

  // Nothing more should happen.
  task_environment_.RunUntilIdle();
}

// Be instructed to enter fullscreen on a different screen, while exiting
// fullscreen.
TEST_F(MacFullscreenControllerTest, EnterCrossScreenWhileExiting) {
  // Enter fullscreen the way that clicking the green traffic light does it.
  EXPECT_CALL(mock_client_, FullscreenControllerGetFrame())
      .Times(1)
      .WillOnce(Return(kWindowRect));
  EXPECT_CALL(mock_client_, FullscreenControllerTransitionStart(true)).Times(1);
  controller_.OnWindowWillEnterFullscreen();
  task_environment_.RunUntilIdle();
  EXPECT_TRUE(controller_.IsInFullscreenTransition());
  EXPECT_CALL(mock_client_, FullscreenControllerTransitionComplete(true))
      .Times(1);
  controller_.OnWindowDidEnterFullscreen();
  EXPECT_FALSE(controller_.IsInFullscreenTransition());
  EXPECT_TRUE(controller_.GetTargetFullscreenState());

  // Exit fullscreen.
  EXPECT_CALL(mock_client_, FullscreenControllerTransitionStart(false))
      .Times(1);
  controller_.OnWindowWillExitFullscreen();
  EXPECT_TRUE(controller_.IsInFullscreenTransition());
  EXPECT_FALSE(controller_.GetTargetFullscreenState());

  // Call EnterFullscreen inside the transition.
  controller_.EnterFullscreen(kDisplay1Id);
  EXPECT_TRUE(controller_.IsInFullscreenTransition());
  EXPECT_TRUE(controller_.GetTargetFullscreenState());

  // Complete the transition to windowed mode. This will report that it is
  // still in transition to fullscreen.
  controller_.OnWindowDidExitFullscreen();
  EXPECT_TRUE(controller_.IsInFullscreenTransition());
  EXPECT_TRUE(controller_.GetTargetFullscreenState());

  // The above call will have posted a MoveToTargetDisplayThenToggleFullscreen
  // task, which will then post a ToggleFullscreen task.
  EXPECT_CALL(mock_client_, FullscreenControllerGetFrameForDisplay(kDisplay1Id))
      .Times(1)
      .WillOnce(Return(kDisplay1Frame));
  EXPECT_CALL(mock_client_,
              FullscreenControllerSetFrame(kDisplay1Frame, true, _))
      .WillOnce(FullscreenControllerSetFrameFake);
  EXPECT_CALL(mock_client_, FullscreenControllerToggleFullscreen())
      .WillOnce(Invoke(&controller_, &NativeWidgetNSWindowFullscreenController::
                                         OnWindowWillEnterFullscreen));
  task_environment_.RunUntilIdle();
  EXPECT_TRUE(controller_.IsInFullscreenTransition());
  EXPECT_TRUE(controller_.GetTargetFullscreenState());

  // Complete the transition to fullscreen mode.
  EXPECT_CALL(mock_client_, FullscreenControllerTransitionComplete(true))
      .Times(1);
  controller_.OnWindowDidEnterFullscreen();
  EXPECT_FALSE(controller_.IsInFullscreenTransition());
  EXPECT_TRUE(controller_.GetTargetFullscreenState());

  // Nothing more should happen.
  task_environment_.RunUntilIdle();
}

// Be instructed to enter fullscreen on a different screen, while entering
// fullscreen.
TEST_F(MacFullscreenControllerTest, EnterCrossScreenWhileEntering) {
  // Enter fullscreen the way that clicking the green traffic light does it.
  EXPECT_CALL(mock_client_, FullscreenControllerGetFrame())
      .Times(1)
      .WillOnce(Return(kWindowRect));
  EXPECT_CALL(mock_client_, FullscreenControllerTransitionStart(true)).Times(1);
  controller_.OnWindowWillEnterFullscreen();
  task_environment_.RunUntilIdle();

  // Call EnterFullscreen inside the transition.
  controller_.EnterFullscreen(kDisplay1Id);
  EXPECT_TRUE(controller_.IsInFullscreenTransition());
  EXPECT_TRUE(controller_.GetTargetFullscreenState());

  // Complete the original fullscreen transition. This will check to see if
  // the display we are on is the display we wanted to be on. Seeing that it
  // isn't, it will post a task to exit fullscreen before moving to the correct
  // display.
  EXPECT_CALL(mock_client_, FullscreenControllerGetDisplayId())
      .WillOnce(Return(kDisplay0Id));
  controller_.OnWindowDidEnterFullscreen();
  EXPECT_TRUE(controller_.IsInFullscreenTransition());
  EXPECT_TRUE(controller_.GetTargetFullscreenState());

  // The above will have posted a task to ToggleFullscreen.
  EXPECT_CALL(mock_client_, FullscreenControllerToggleFullscreen())
      .WillOnce(Invoke(&controller_, &NativeWidgetNSWindowFullscreenController::
                                         OnWindowWillExitFullscreen));
  task_environment_.RunUntilIdle();

  // Complete the transition to windowed mode. This will once again check to see
  // if the display we are on is the display we wanted to be on. Seeing that it
  // isn't, it will move to the correct display and toggle fullscreen.
  EXPECT_CALL(mock_client_, FullscreenControllerGetDisplayId())
      .WillOnce(Return(kDisplay0Id));
  controller_.OnWindowDidExitFullscreen();
  EXPECT_CALL(mock_client_, FullscreenControllerGetFrameForDisplay(kDisplay1Id))
      .Times(1)
      .WillOnce(Return(kDisplay1Frame));
  EXPECT_CALL(mock_client_,
              FullscreenControllerSetFrame(kDisplay1Frame, true, _))
      .WillOnce(FullscreenControllerSetFrameFake);
  EXPECT_CALL(mock_client_, FullscreenControllerToggleFullscreen())
      .WillOnce(Invoke(&controller_, &NativeWidgetNSWindowFullscreenController::
                                         OnWindowWillEnterFullscreen));
  task_environment_.RunUntilIdle();
  EXPECT_TRUE(controller_.IsInFullscreenTransition());
  EXPECT_TRUE(controller_.GetTargetFullscreenState());

  // Complete the transition to fullscreen.
  EXPECT_CALL(mock_client_, FullscreenControllerTransitionComplete(true))
      .Times(1);
  controller_.OnWindowDidEnterFullscreen();
  EXPECT_FALSE(controller_.IsInFullscreenTransition());
  EXPECT_TRUE(controller_.GetTargetFullscreenState());

  // Exit fullscreen.
  EXPECT_CALL(mock_client_, FullscreenControllerTransitionStart(false))
      .Times(1);
  controller_.OnWindowWillExitFullscreen();
  EXPECT_TRUE(controller_.IsInFullscreenTransition());
  EXPECT_FALSE(controller_.GetTargetFullscreenState());

  // Complete the transition to windowed mode. The frame frame should be reset.
  controller_.OnWindowDidExitFullscreen();
  EXPECT_TRUE(controller_.IsInFullscreenTransition());
  EXPECT_CALL(mock_client_, FullscreenControllerSetFrame(kWindowRect, true, _))
      .WillOnce(FullscreenControllerSetFrameFake);
  EXPECT_CALL(mock_client_, FullscreenControllerTransitionComplete(false))
      .Times(1);
  task_environment_.RunUntilIdle();
  EXPECT_FALSE(controller_.IsInFullscreenTransition());
  EXPECT_FALSE(controller_.GetTargetFullscreenState());
}

// Be instructed to enter fullscreen while restoring the windowed frame.
TEST_F(MacFullscreenControllerTest, EnterCrossScreenWhileRestoring) {
  // Enter fullscreen on display 1, from display 0.
  EXPECT_CALL(mock_client_, FullscreenControllerGetFrame())
      .WillOnce(Return(kWindowRect));
  EXPECT_CALL(mock_client_, FullscreenControllerTransitionStart(true)).Times(1);
  controller_.EnterFullscreen(kDisplay1Id);
  EXPECT_TRUE(controller_.IsInFullscreenTransition());
  EXPECT_TRUE(controller_.GetTargetFullscreenState());

  // This will trigger a call to MoveToTargetDisplayThenToggleFullscreen, even
  // though we are not actually moving displays. It will also then post a task
  // to ToggleFullscreen (because RunUntilIdle will pick up that posted task).
  EXPECT_CALL(mock_client_, FullscreenControllerGetFrameForDisplay(kDisplay1Id))
      .WillOnce(Return(kDisplay1Frame));
  EXPECT_CALL(mock_client_,
              FullscreenControllerSetFrame(kDisplay1Frame, true, _))
      .WillOnce(FullscreenControllerSetFrameFake);
  EXPECT_CALL(mock_client_, FullscreenControllerToggleFullscreen())
      .WillOnce(Invoke(&controller_, &NativeWidgetNSWindowFullscreenController::
                                         OnWindowWillEnterFullscreen));
  task_environment_.RunUntilIdle();
  EXPECT_TRUE(controller_.IsInFullscreenTransition());
  EXPECT_TRUE(controller_.GetTargetFullscreenState());

  // Complete the transition to fullscreen on the new display.
  EXPECT_CALL(mock_client_, FullscreenControllerTransitionComplete(true))
      .Times(1);
  controller_.OnWindowDidEnterFullscreen();
  EXPECT_FALSE(controller_.IsInFullscreenTransition());
  EXPECT_TRUE(controller_.GetTargetFullscreenState());

  // Exit fullscreen. This will post a task to restore the windowed frame.
  EXPECT_CALL(mock_client_, FullscreenControllerTransitionStart(false))
      .Times(1);
  controller_.OnWindowWillExitFullscreen();
  EXPECT_TRUE(controller_.IsInFullscreenTransition());
  EXPECT_FALSE(controller_.GetTargetFullscreenState());
  task_environment_.RunUntilIdle();
  controller_.OnWindowDidExitFullscreen();
  EXPECT_TRUE(controller_.IsInFullscreenTransition());
  EXPECT_FALSE(controller_.GetTargetFullscreenState());

  // Enter fullscreen on display 1 again, while restoring the windowed frame.
  // This does not call FullscreenControllerGetFrame() to update the cached
  // windowed frame, since the window is still in transition. It also will not
  // call FullscreenControllerTransitionComplete(false) to signal the successful
  // exit, since that might ambiguously signal that entering fullscreen failed.
  // It also will not call FullscreenControllerTransitionStart(true), since the
  // window is already in a transition.
  controller_.EnterFullscreen(kDisplay1Id);
  EXPECT_TRUE(controller_.IsInFullscreenTransition());
  EXPECT_TRUE(controller_.GetTargetFullscreenState());

  // Let the run loop run, the task to restore the windowed frame will execute
  // before handling the new pending state from EnterFullscreen.
  EXPECT_CALL(mock_client_, FullscreenControllerSetFrame(kWindowRect, true, _))
      .WillOnce(FullscreenControllerSetFrameFake);
  // This will trigger a call to MoveToTargetDisplayThenToggleFullscreen, even
  // though we are not actually moving displays. It will also then post a task
  // to ToggleFullscreen (because RunUntilIdle will pick up that posted task).
  EXPECT_CALL(mock_client_, FullscreenControllerGetFrameForDisplay(kDisplay1Id))
      .WillOnce(Return(kDisplay1Frame));
  EXPECT_CALL(mock_client_,
              FullscreenControllerSetFrame(kDisplay1Frame, true, _))
      .WillOnce(FullscreenControllerSetFrameFake);
  EXPECT_CALL(mock_client_, FullscreenControllerToggleFullscreen())
      .WillOnce(Invoke(&controller_, &NativeWidgetNSWindowFullscreenController::
                                         OnWindowWillEnterFullscreen));
  task_environment_.RunUntilIdle();
  EXPECT_TRUE(controller_.IsInFullscreenTransition());
  EXPECT_TRUE(controller_.GetTargetFullscreenState());

  // Complete the transition to fullscreen on the new display.
  EXPECT_CALL(mock_client_, FullscreenControllerTransitionComplete(true))
      .Times(1);
  controller_.OnWindowDidEnterFullscreen();
  EXPECT_FALSE(controller_.IsInFullscreenTransition());
  EXPECT_TRUE(controller_.GetTargetFullscreenState());

  // Exit fullscreen. This will post a task to restore bounds.
  EXPECT_CALL(mock_client_, FullscreenControllerTransitionStart(false))
      .Times(1);
  controller_.OnWindowWillExitFullscreen();
  EXPECT_TRUE(controller_.IsInFullscreenTransition());
  EXPECT_FALSE(controller_.GetTargetFullscreenState());
  task_environment_.RunUntilIdle();
  controller_.OnWindowDidExitFullscreen();
  EXPECT_TRUE(controller_.IsInFullscreenTransition());
  EXPECT_FALSE(controller_.GetTargetFullscreenState());

  // Let the run loop run, it will restore the bounds (and not crash).
  // The original bounds should still be stored, even though they were
  // previously restored, since the second fullscreen transition begun while
  // already in a transition to restore bounds after exiting fullscreen.
  EXPECT_CALL(mock_client_, FullscreenControllerSetFrame(kWindowRect, true, _))
      .WillOnce(FullscreenControllerSetFrameFake);
  EXPECT_CALL(mock_client_, FullscreenControllerTransitionComplete(false))
      .Times(1);
  task_environment_.RunUntilIdle();
  EXPECT_FALSE(controller_.IsInFullscreenTransition());
  EXPECT_FALSE(controller_.GetTargetFullscreenState());
}

}  // namespace remote_cocoa::test