forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client_controlled_state_unittest.cc
2691 lines (2290 loc) · 113 KB
/
client_controlled_state_unittest.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2013 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ash/wm/client_controlled_state.h"
#include <queue>
#include "ash/display/screen_orientation_controller.h"
#include "ash/display/screen_orientation_controller_test_api.h"
#include "ash/frame/non_client_frame_view_ash.h"
#include "ash/public/cpp/shelf_config.h"
#include "ash/public/cpp/shell_window_ids.h"
#include "ash/public/cpp/test/shell_test_api.h"
#include "ash/root_window_controller.h"
#include "ash/screen_util.h"
#include "ash/shell.h"
#include "ash/test/ash_test_base.h"
#include "ash/test/ash_test_util.h"
#include "ash/test/test_widget_builder.h"
#include "ash/wm/desks/desks_util.h"
#include "ash/wm/float/float_controller.h"
#include "ash/wm/float/float_test_api.h"
#include "ash/wm/overview/overview_controller.h"
#include "ash/wm/overview/overview_item.h"
#include "ash/wm/overview/overview_test_util.h"
#include "ash/wm/pip/pip_positioner.h"
#include "ash/wm/screen_pinning_controller.h"
#include "ash/wm/snap_group/snap_group.h"
#include "ash/wm/snap_group/snap_group_controller.h"
#include "ash/wm/snap_group/snap_group_test_util.h"
#include "ash/wm/splitview/split_view_constants.h"
#include "ash/wm/splitview/split_view_controller.h"
#include "ash/wm/splitview/split_view_divider.h"
#include "ash/wm/splitview/split_view_test_util.h"
#include "ash/wm/tablet_mode/tablet_mode_controller.h"
#include "ash/wm/test/fake_window_state.h"
#include "ash/wm/window_positioning_utils.h"
#include "ash/wm/window_resizer.h"
#include "ash/wm/window_state.h"
#include "ash/wm/window_state_delegate.h"
#include "ash/wm/window_util.h"
#include "ash/wm/wm_event.h"
#include "base/functional/callback_forward.h"
#include "base/memory/raw_ptr.h"
#include "base/test/bind.h"
#include "base/test/scoped_feature_list.h"
#include "chromeos/ui/base/app_types.h"
#include "chromeos/ui/base/display_util.h"
#include "chromeos/ui/base/window_properties.h"
#include "chromeos/ui/base/window_state_type.h"
#include "chromeos/ui/frame/caption_buttons/frame_caption_button_container_view.h"
#include "chromeos/ui/frame/caption_buttons/snap_controller.h"
#include "chromeos/ui/frame/header_view.h"
#include "chromeos/ui/wm/constants.h"
#include "chromeos/ui/wm/window_util.h"
#include "ui/aura/client/aura_constants.h"
#include "ui/compositor/scoped_animation_duration_scale_mode.h"
#include "ui/display/screen.h"
#include "ui/display/test/display_manager_test_api.h"
#include "ui/events/test/event_generator.h"
#include "ui/gfx/geometry/point.h"
#include "ui/gfx/geometry/point_conversions.h"
#include "ui/gfx/geometry/vector2d.h"
#include "ui/views/test/widget_test.h"
#include "ui/views/widget/widget.h"
#include "ui/views/widget/widget_delegate.h"
#include "ui/wm/core/window_util.h"
namespace ash {
namespace {
using ::chromeos::WindowStateType;
using BoundsRequestCallback =
base::RepeatingCallback<void(const gfx::Rect& bounds)>;
using WindowStateRequestCallback =
base::RepeatingCallback<void(WindowStateType new_state)>;
constexpr gfx::Rect kInitialBounds(0, 0, 100, 100);
class TestClientControlledStateDelegate
: public ClientControlledState::Delegate {
public:
TestClientControlledStateDelegate() = default;
TestClientControlledStateDelegate(const TestClientControlledStateDelegate&) =
delete;
TestClientControlledStateDelegate& operator=(
const TestClientControlledStateDelegate&) = delete;
~TestClientControlledStateDelegate() override = default;
void HandleWindowStateRequest(WindowState* window_state,
WindowStateType next_state) override {
EXPECT_FALSE(deleted_);
old_state_ = window_state->GetStateType();
new_state_ = next_state;
if (window_state_request_callback_) {
window_state_request_callback_.Run(next_state);
}
}
void HandleBoundsRequest(WindowState* window_state,
WindowStateType requested_state,
const gfx::Rect& bounds,
int64_t display_id) override {
requested_bounds_ = bounds;
if (requested_state != window_state->GetStateType()) {
DCHECK(requested_state == WindowStateType::kPrimarySnapped ||
requested_state == WindowStateType::kSecondarySnapped ||
requested_state == WindowStateType::kFloated);
old_state_ = window_state->GetStateType();
new_state_ = requested_state;
}
display_id_ = display_id;
if (bounds_request_callback_) {
bounds_request_callback_.Run(bounds);
}
}
WindowStateType old_state() const { return old_state_; }
WindowStateType new_state() const { return new_state_; }
const gfx::Rect& requested_bounds() const { return requested_bounds_; }
void set_bounds_request_callback(BoundsRequestCallback callback) {
bounds_request_callback_ = std::move(callback);
}
void set_window_state_request_callback(WindowStateRequestCallback callback) {
window_state_request_callback_ = std::move(callback);
}
int64_t display_id() const { return display_id_; }
void Reset() {
old_state_ = WindowStateType::kDefault;
new_state_ = WindowStateType::kDefault;
requested_bounds_.SetRect(0, 0, 0, 0);
display_id_ = display::kInvalidDisplayId;
}
void mark_as_deleted() { deleted_ = true; }
private:
WindowStateType old_state_ = WindowStateType::kDefault;
WindowStateType new_state_ = WindowStateType::kDefault;
int64_t display_id_ = display::kInvalidDisplayId;
gfx::Rect requested_bounds_;
bool deleted_ = false;
BoundsRequestCallback bounds_request_callback_;
WindowStateRequestCallback window_state_request_callback_;
};
class TestWidgetDelegate : public views::WidgetDelegateView {
public:
TestWidgetDelegate() = default;
TestWidgetDelegate(const TestWidgetDelegate&) = delete;
TestWidgetDelegate& operator=(const TestWidgetDelegate&) = delete;
~TestWidgetDelegate() override = default;
void EnableSnap() {
SetCanMaximize(true);
SetCanResize(true);
GetWidget()->OnSizeConstraintsChanged();
}
void EnableFloat() {
SetCanResize(true);
GetWidget()->OnSizeConstraintsChanged();
}
std::unique_ptr<views::NonClientFrameView> CreateNonClientFrameView(
views::Widget* widget) override {
return std::make_unique<NonClientFrameViewAsh>(widget);
}
};
class TestEmptyState : public WindowState::State {
public:
void OnWMEvent(WindowState* window_state, const WMEvent* event) override {}
chromeos::WindowStateType GetType() const override {
return chromeos::WindowStateType::kDefault;
}
void AttachState(WindowState* window_state, State* previous_state) override {}
void DetachState(WindowState* window_state) override {}
void OnWindowDestroying(WindowState* window_state) override {}
};
void VerifySnappedBounds(aura::Window* window, float expected_snap_ratio) {
const WindowState* window_state = WindowState::Get(window);
// `window` must be in any snapped state to use this method.
ASSERT_TRUE(window_state->IsSnapped());
const bool in_tablet = display::Screen::GetScreen()->InTabletMode();
const auto display =
display::Screen::GetScreen()->GetDisplayNearestWindow(window);
const gfx::Rect work_area = display.work_area();
const auto rotation = display.rotation();
const bool is_primary =
window_state->GetStateType() == WindowStateType::kPrimarySnapped;
// Following conditions assume that the natural display orientation is
// landscape.
ASSERT_TRUE(chromeos::IsLandscapeOrientation(
chromeos::GetDisplayNaturalOrientation(display)));
const bool is_landscape = rotation == display::Display::ROTATE_0 ||
rotation == display::Display::ROTATE_180;
const bool is_top_or_left =
(rotation == display::Display::ROTATE_0 && is_primary) ||
(rotation == display::Display::ROTATE_90 && !is_primary) ||
(rotation == display::Display::ROTATE_180 && !is_primary) ||
(rotation == display::Display::ROTATE_270 && is_primary);
// Also consider the divider width if the window is in a snap group.
const bool in_snap_group = [&]() {
auto* snap_group_controller = SnapGroupController::Get();
return snap_group_controller &&
snap_group_controller->GetSnapGroupForGivenWindow(window);
}();
const int divider_margin =
(in_tablet || in_snap_group) ? kSplitviewDividerShortSideLength / 2 : 0;
const gfx::Size expected_size =
is_landscape
? gfx::Size(work_area.width() * expected_snap_ratio - divider_margin,
work_area.height())
: gfx::Size(
work_area.width(),
work_area.height() * expected_snap_ratio - divider_margin);
const gfx::Point expected_origin =
is_landscape
? gfx::Point(is_top_or_left
? work_area.x()
: work_area.right() - expected_size.width(),
work_area.y())
: gfx::Point(work_area.x(),
is_top_or_left
? work_area.y()
: work_area.bottom() - expected_size.height());
const gfx::Rect bounds = window->GetTargetBounds();
// Allow 1px (3px in clamshell) rounding errors for partial snap. Note even if
// `SnapGroup` is enabled, the window may not be in a snap group, so allow 3px
// rounding errors.
// TODO(b/319342277): Investigate why eps can't be 1 when clamshell mode.
const int eps = in_tablet ? 1 : 3;
EXPECT_NEAR(expected_size.width(), bounds.width(), is_landscape ? eps : 0);
EXPECT_NEAR(expected_size.height(), bounds.height(), !is_landscape ? eps : 0);
EXPECT_NEAR(expected_origin.x(), bounds.x(), is_landscape ? eps : 0);
EXPECT_NEAR(expected_origin.y(), bounds.y(), !is_landscape ? eps : 0);
}
} // namespace
class ClientControlledStateTest : public AshTestBase {
public:
ClientControlledStateTest() = default;
ClientControlledStateTest(const ClientControlledStateTest&) = delete;
ClientControlledStateTest& operator=(const ClientControlledStateTest&) =
delete;
~ClientControlledStateTest() override = default;
void SetUp() override {
AshTestBase::SetUp();
widget_delegate_ = new TestWidgetDelegate();
views::Widget::InitParams params(
views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET);
params.parent = Shell::GetPrimaryRootWindow()->GetChildById(
desks_util::GetActiveDeskContainerId());
params.bounds = kInitialBounds;
params.delegate = widget_delegate_.get();
widget_ = std::make_unique<views::Widget>();
widget_->Init(std::move(params));
WindowState* window_state = WindowState::Get(window());
window_state->set_allow_set_bounds_direct(true);
auto delegate = std::make_unique<TestClientControlledStateDelegate>();
state_delegate_ = delegate.get();
auto state = std::make_unique<ClientControlledState>(std::move(delegate));
state_ = state.get();
window_state->SetStateObject(std::move(state));
auto window_state_delegate = std::make_unique<FakeWindowStateDelegate>();
window_state_delegate_ = window_state_delegate.get();
window_state->SetDelegate(std::move(window_state_delegate));
widget_->Show();
}
void TearDown() override {
widget_ = nullptr;
AshTestBase::TearDown();
}
TestWidgetDelegate* widget_delegate() { return widget_delegate_; }
protected:
aura::Window* window() { return widget_->GetNativeWindow(); }
WindowState* window_state() { return WindowState::Get(window()); }
ClientControlledState* state() { return state_; }
TestClientControlledStateDelegate* delegate() { return state_delegate_; }
views::Widget* widget() { return widget_.get(); }
ScreenPinningController* GetScreenPinningController() {
return Shell::Get()->screen_pinning_controller();
}
FakeWindowStateDelegate* window_state_delegate() {
return window_state_delegate_;
}
chromeos::HeaderView* GetHeaderView() {
auto* const frame = NonClientFrameViewAsh::Get(window());
DCHECK(frame);
return frame->GetHeaderView();
}
void ApplyPendingRequestedBounds() {
state()->set_bounds_locally(true);
widget()->SetBounds(delegate()->requested_bounds());
state()->set_bounds_locally(false);
}
void ClickOnOverviewItem(aura::Window* window) {
auto* const overview_controller = OverviewController::Get();
ASSERT_TRUE(overview_controller->InOverviewSession());
auto* const overview_item = GetOverviewItemForWindow(window);
auto* const event_generator = GetEventGenerator();
event_generator->set_current_screen_location(
gfx::ToRoundedPoint(overview_item->target_bounds().CenterPoint()));
event_generator->ClickLeftButton();
}
void SimulateUnminimizeViaShelfIcon(views::Widget* widget) {
// When clicking an app icon on the hotseat to unminimize the window,
// `ChromeShelfController` shows and activates the widget.
// We here simulate the behavior because //ash should not use any component
// from //chrome/browser/ui.
widget->Show();
widget->Activate();
}
void DragResizeSnappedWindow(aura::Window* window, int target_x) {
ASSERT_TRUE(WindowState::Get(window)->IsSnapped());
ui::test::EventGenerator* const generator = GetEventGenerator();
const bool in_tablet = display::Screen::GetScreen()->InTabletMode();
if (in_tablet) {
auto* split_view_controller = SplitViewController::Get(window);
const gfx::Rect divider_bounds =
split_view_controller->split_view_divider()->GetDividerBoundsInScreen(
false);
generator->set_current_screen_location(divider_bounds.CenterPoint());
} else {
generator->set_current_screen_location(
window->GetBoundsInScreen().right_center());
}
generator->DragMouseTo(gfx::Point(target_x, 0));
}
void DragOverviewItemToSnap(aura::Window* window, bool to_left) {
auto* const overview_controller = OverviewController::Get();
ASSERT_TRUE(overview_controller->InOverviewSession());
auto* const overview_item = GetOverviewItemForWindow(window);
auto* const event_generator = GetEventGenerator();
event_generator->set_current_screen_location(
gfx::ToRoundedPoint(overview_item->target_bounds().CenterPoint()));
const gfx::Rect work_area = display::Screen::GetScreen()
->GetDisplayNearestWindow(window)
.work_area();
event_generator->DragMouseTo(to_left ? work_area.left_center()
: work_area.right_center());
}
private:
raw_ptr<ClientControlledState, DanglingUntriaged> state_ = nullptr;
raw_ptr<TestClientControlledStateDelegate, DanglingUntriaged>
state_delegate_ = nullptr;
raw_ptr<TestWidgetDelegate, DanglingUntriaged> widget_delegate_ =
nullptr; // owned by itself.
raw_ptr<FakeWindowStateDelegate, DanglingUntriaged> window_state_delegate_ =
nullptr;
std::unique_ptr<views::Widget> widget_;
};
class SnapGroupClientControlledStateTest : public ClientControlledStateTest {
public:
SnapGroupClientControlledStateTest() = default;
SnapGroupClientControlledStateTest(
const SnapGroupClientControlledStateTest&) = delete;
SnapGroupClientControlledStateTest& operator=(
const SnapGroupClientControlledStateTest&) = delete;
~SnapGroupClientControlledStateTest() override = default;
private:
base::test::ScopedFeatureList scoped_feature_list_{features::kSnapGroup};
};
// This suite runs test cases both in clamshell mode and tablet mode.
class ClientControlledStateTestClamshellAndTablet
: public ClientControlledStateTest,
public testing::WithParamInterface<bool> {
public:
ClientControlledStateTestClamshellAndTablet() = default;
ClientControlledStateTestClamshellAndTablet(
const ClientControlledStateTestClamshellAndTablet&) = delete;
ClientControlledStateTestClamshellAndTablet& operator=(
const ClientControlledStateTestClamshellAndTablet&) = delete;
~ClientControlledStateTestClamshellAndTablet() override = default;
void SetUp() override {
ClientControlledStateTest::SetUp();
Shell::Get()->tablet_mode_controller()->SetEnabledForTest(InTabletMode());
}
protected:
bool InTabletMode() { return GetParam(); }
};
// The parameter indicates whether the tablet mode is enabled.
INSTANTIATE_TEST_SUITE_P(All,
ClientControlledStateTestClamshellAndTablet,
testing::Bool());
TEST_F(ClientControlledStateTest, ClientControlledFlag) {
ASSERT_TRUE(window_state()->is_client_controlled());
// Attach `TestEmptyState` to detach `ClientControlledState`.
window_state()->SetStateObject(std::make_unique<TestEmptyState>());
EXPECT_FALSE(window_state()->is_client_controlled());
// Attach `ClientControlledState` to detach `TestEmptyState`.
window_state()->SetStateObject(std::make_unique<ClientControlledState>(
std::make_unique<TestClientControlledStateDelegate>()));
EXPECT_TRUE(window_state()->is_client_controlled());
}
// Make sure that calling Maximize()/Minimize()/Fullscreen() result in
// sending the state change request and won't change the state immediately.
// The state will be updated when ClientControlledState::EnterToNextState
// is called.
TEST_F(ClientControlledStateTest, Maximize) {
widget()->Maximize();
// The state shouldn't be updated until EnterToNextState is called.
EXPECT_FALSE(widget()->IsMaximized());
EXPECT_EQ(kInitialBounds, widget()->GetWindowBoundsInScreen());
EXPECT_EQ(WindowStateType::kDefault, delegate()->old_state());
EXPECT_EQ(WindowStateType::kMaximized, delegate()->new_state());
// Now enters the new state.
state()->EnterNextState(window_state(), delegate()->new_state());
EXPECT_TRUE(widget()->IsMaximized());
// Bounds is controlled by client.
EXPECT_EQ(kInitialBounds, widget()->GetWindowBoundsInScreen());
// Maximized request should be also sent. It is up to client impl
// how to handle it.
widget()->SetBounds(gfx::Rect(0, 0, 100, 100));
EXPECT_EQ(gfx::Rect(0, 0, 100, 100), delegate()->requested_bounds());
widget()->Restore();
EXPECT_TRUE(widget()->IsMaximized());
EXPECT_EQ(kInitialBounds, widget()->GetWindowBoundsInScreen());
EXPECT_EQ(WindowStateType::kMaximized, delegate()->old_state());
EXPECT_EQ(WindowStateType::kNormal, delegate()->new_state());
state()->EnterNextState(window_state(), delegate()->new_state());
EXPECT_FALSE(widget()->IsMaximized());
EXPECT_EQ(kInitialBounds, widget()->GetWindowBoundsInScreen());
}
TEST_F(ClientControlledStateTest, Minimize) {
widget()->Minimize();
EXPECT_FALSE(widget()->IsMinimized());
EXPECT_EQ(kInitialBounds, widget()->GetWindowBoundsInScreen());
EXPECT_EQ(WindowStateType::kDefault, delegate()->old_state());
EXPECT_EQ(WindowStateType::kMinimized, delegate()->new_state());
state()->EnterNextState(window_state(), delegate()->new_state());
EXPECT_TRUE(widget()->IsMinimized());
EXPECT_EQ(kInitialBounds, widget()->GetWindowBoundsInScreen());
widget()->Restore();
EXPECT_TRUE(widget()->IsMinimized());
EXPECT_EQ(kInitialBounds, widget()->GetWindowBoundsInScreen());
EXPECT_EQ(WindowStateType::kMinimized, delegate()->old_state());
EXPECT_EQ(WindowStateType::kNormal, delegate()->new_state());
state()->EnterNextState(window_state(), delegate()->new_state());
EXPECT_FALSE(widget()->IsMinimized());
EXPECT_EQ(kInitialBounds, widget()->GetWindowBoundsInScreen());
// use wm::Unminimize to unminimize.
widget()->Minimize();
EXPECT_FALSE(widget()->IsMinimized());
EXPECT_EQ(kInitialBounds, widget()->GetWindowBoundsInScreen());
EXPECT_EQ(WindowStateType::kNormal, delegate()->old_state());
EXPECT_EQ(WindowStateType::kMinimized, delegate()->new_state());
state()->EnterNextState(window_state(), delegate()->new_state());
EXPECT_TRUE(widget()->IsMinimized());
EXPECT_EQ(kInitialBounds, widget()->GetWindowBoundsInScreen());
::wm::Unminimize(widget()->GetNativeWindow());
EXPECT_TRUE(widget()->IsMinimized());
EXPECT_EQ(ui::SHOW_STATE_NORMAL, widget()->GetNativeWindow()->GetProperty(
aura::client::kRestoreShowStateKey));
EXPECT_EQ(kInitialBounds, widget()->GetWindowBoundsInScreen());
EXPECT_EQ(WindowStateType::kMinimized, delegate()->old_state());
EXPECT_EQ(WindowStateType::kNormal, delegate()->new_state());
state()->EnterNextState(window_state(), delegate()->new_state());
EXPECT_FALSE(widget()->IsMinimized());
EXPECT_EQ(kInitialBounds, widget()->GetWindowBoundsInScreen());
}
TEST_F(ClientControlledStateTest, Fullscreen) {
widget()->SetFullscreen(true);
EXPECT_FALSE(widget()->IsFullscreen());
EXPECT_EQ(kInitialBounds, widget()->GetWindowBoundsInScreen());
EXPECT_EQ(WindowStateType::kDefault, delegate()->old_state());
EXPECT_EQ(WindowStateType::kFullscreen, delegate()->new_state());
state()->EnterNextState(window_state(), delegate()->new_state());
EXPECT_TRUE(widget()->IsFullscreen());
EXPECT_EQ(kInitialBounds, widget()->GetWindowBoundsInScreen());
widget()->SetFullscreen(false);
EXPECT_TRUE(widget()->IsFullscreen());
EXPECT_EQ(WindowStateType::kFullscreen, delegate()->old_state());
EXPECT_EQ(WindowStateType::kNormal, delegate()->new_state());
state()->EnterNextState(window_state(), delegate()->new_state());
EXPECT_FALSE(widget()->IsFullscreen());
EXPECT_EQ(kInitialBounds, widget()->GetWindowBoundsInScreen());
}
// Make sure toggle fullscreen from maximized state goes back to
// maximized state.
TEST_F(ClientControlledStateTest, MaximizeToFullscreen) {
widget()->Maximize();
EXPECT_FALSE(widget()->IsMaximized());
EXPECT_EQ(kInitialBounds, widget()->GetWindowBoundsInScreen());
EXPECT_EQ(WindowStateType::kDefault, delegate()->old_state());
EXPECT_EQ(WindowStateType::kMaximized, delegate()->new_state());
state()->EnterNextState(window_state(), delegate()->new_state());
EXPECT_TRUE(widget()->IsMaximized());
EXPECT_EQ(kInitialBounds, widget()->GetWindowBoundsInScreen());
widget()->SetFullscreen(true);
EXPECT_TRUE(widget()->IsMaximized());
EXPECT_EQ(kInitialBounds, widget()->GetWindowBoundsInScreen());
EXPECT_EQ(WindowStateType::kMaximized, delegate()->old_state());
EXPECT_EQ(WindowStateType::kFullscreen, delegate()->new_state());
state()->EnterNextState(window_state(), delegate()->new_state());
EXPECT_TRUE(widget()->IsFullscreen());
EXPECT_EQ(kInitialBounds, widget()->GetWindowBoundsInScreen());
widget()->SetFullscreen(false);
EXPECT_TRUE(widget()->IsFullscreen());
EXPECT_EQ(WindowStateType::kFullscreen, delegate()->old_state());
EXPECT_EQ(WindowStateType::kMaximized, delegate()->new_state());
state()->EnterNextState(window_state(), delegate()->new_state());
EXPECT_TRUE(widget()->IsMaximized());
EXPECT_EQ(kInitialBounds, widget()->GetWindowBoundsInScreen());
widget()->Restore();
EXPECT_TRUE(widget()->IsMaximized());
EXPECT_EQ(WindowStateType::kMaximized, delegate()->old_state());
EXPECT_EQ(WindowStateType::kNormal, delegate()->new_state());
state()->EnterNextState(window_state(), delegate()->new_state());
EXPECT_FALSE(widget()->IsMaximized());
EXPECT_EQ(kInitialBounds, widget()->GetWindowBoundsInScreen());
}
TEST_F(ClientControlledStateTest, IgnoreWorkspace) {
widget()->Maximize();
state()->EnterNextState(window_state(), delegate()->new_state());
EXPECT_TRUE(widget()->IsMaximized());
delegate()->Reset();
UpdateDisplay("1000x800");
// Client is responsible to handle workspace change, so
// no action should be taken.
EXPECT_EQ(kInitialBounds, widget()->GetWindowBoundsInScreen());
EXPECT_EQ(WindowStateType::kDefault, delegate()->old_state());
EXPECT_EQ(WindowStateType::kDefault, delegate()->new_state());
EXPECT_EQ(gfx::Rect(), delegate()->requested_bounds());
}
TEST_F(ClientControlledStateTest, SetBounds) {
constexpr gfx::Rect new_bounds(100, 100, 100, 100);
widget()->SetBounds(new_bounds);
EXPECT_EQ(kInitialBounds, widget()->GetWindowBoundsInScreen());
EXPECT_EQ(new_bounds, delegate()->requested_bounds());
state()->set_bounds_locally(true);
widget()->SetBounds(delegate()->requested_bounds());
state()->set_bounds_locally(false);
EXPECT_EQ(new_bounds, widget()->GetWindowBoundsInScreen());
}
TEST_F(ClientControlledStateTest, CenterWindow) {
display::Screen* screen = display::Screen::GetScreen();
const gfx::Rect bounds = screen->GetPrimaryDisplay().work_area();
gfx::Rect center_bounds = bounds;
center_bounds.ClampToCenteredSize(window()->bounds().size());
window()->SetBoundsInScreen(center_bounds, screen->GetPrimaryDisplay());
EXPECT_NEAR(bounds.CenterPoint().x(),
delegate()->requested_bounds().CenterPoint().x(), 1);
EXPECT_NEAR(bounds.CenterPoint().y(),
delegate()->requested_bounds().CenterPoint().y(), 1);
}
TEST_F(ClientControlledStateTest, CycleSnapWindow) {
// Snap disabled.
ASSERT_FALSE(window_state()->CanResize());
ASSERT_FALSE(window_state()->CanSnap());
// The event should be ignored.
const WindowSnapWMEvent snap_left_event(WM_EVENT_CYCLE_SNAP_PRIMARY);
window_state()->OnWMEvent(&snap_left_event);
EXPECT_FALSE(window_state()->IsSnapped());
EXPECT_TRUE(delegate()->requested_bounds().IsEmpty());
const WindowSnapWMEvent snap_right_event(WM_EVENT_CYCLE_SNAP_SECONDARY);
window_state()->OnWMEvent(&snap_right_event);
EXPECT_FALSE(window_state()->IsSnapped());
EXPECT_TRUE(delegate()->requested_bounds().IsEmpty());
// Snap enabled.
widget_delegate()->EnableSnap();
ASSERT_TRUE(window_state()->CanResize());
ASSERT_TRUE(window_state()->CanSnap());
window_state()->OnWMEvent(&snap_left_event);
// No actual state/bounds should be changed until the client applies the
// changes.
EXPECT_NE(WindowStateType::kPrimarySnapped, window_state()->GetStateType());
EXPECT_EQ(kInitialBounds, window()->GetTargetBounds());
EXPECT_EQ(WindowStateType::kDefault, delegate()->old_state());
EXPECT_EQ(WindowStateType::kPrimarySnapped, delegate()->new_state());
// Apply pending requests.
state()->EnterNextState(window_state(), delegate()->new_state());
ApplyPendingRequestedBounds();
VerifySnappedBounds(window(), chromeos::kDefaultSnapRatio);
EXPECT_EQ(WindowStateType::kPrimarySnapped, window_state()->GetStateType());
delegate()->Reset();
window_state()->OnWMEvent(&snap_right_event);
// No actual state/bounds should be changed until the client applies the
// changes.
EXPECT_NE(WindowStateType::kSecondarySnapped, window_state()->GetStateType());
VerifySnappedBounds(window(), chromeos::kDefaultSnapRatio);
EXPECT_EQ(WindowStateType::kPrimarySnapped, delegate()->old_state());
EXPECT_EQ(WindowStateType::kSecondarySnapped, delegate()->new_state());
// Apply pending requests.
state()->EnterNextState(window_state(), delegate()->new_state());
ApplyPendingRequestedBounds();
VerifySnappedBounds(window(), chromeos::kDefaultSnapRatio);
EXPECT_EQ(WindowStateType::kSecondarySnapped, window_state()->GetStateType());
}
// Tests the entry point via selecting a window from partial overview.
TEST_F(SnapGroupClientControlledStateTest, SelectFromOverviewEntryPoint) {
UpdateDisplay("800x600");
// Set the client-controlled window app type so it can be recognized in
// `GetActiveDeskAppWindowsInZOrder()`.
window()->SetProperty(chromeos::kAppTypeKey, chromeos::AppType::ARC_APP);
// Create at least 1 other app window so we can start faster splitview.
widget_delegate()->EnableSnap();
auto non_client_controlled_window = CreateAppWindow();
// Snap the client-controlled window using a snap action source that can start
// faster splitview. Note `SnapOneTestWindow()` would not work here since it
// expects the state type to be updated immediately.
const WindowSnapWMEvent snap_primary_event(
WM_EVENT_SNAP_PRIMARY, chromeos::kDefaultSnapRatio,
WindowSnapActionSource::kSnapByWindowLayoutMenu);
window_state()->OnWMEvent(&snap_primary_event);
// Apply pending requests.
ApplyPendingRequestedBounds();
state()->EnterNextState(window_state(), delegate()->new_state());
EXPECT_EQ(WindowStateType::kPrimarySnapped, window_state()->GetStateType());
VerifySnappedBounds(window(), chromeos::kDefaultSnapRatio);
// Test we start faster splitview, then select the normal window.
VerifySplitViewOverviewSession(window());
ClickOnOverviewItem(non_client_controlled_window.get());
EXPECT_EQ(
WindowStateType::kSecondarySnapped,
WindowState::Get(non_client_controlled_window.get())->GetStateType());
// Apply pending bounds changes and verify the state doesn't change.
ApplyPendingRequestedBounds();
EXPECT_EQ(WindowStateType::kPrimarySnapped, window_state()->GetStateType());
// Test a snap group is created.
auto* snap_group_controller = SnapGroupController::Get();
ASSERT_TRUE(snap_group_controller->AreWindowsInSnapGroup(
window(), non_client_controlled_window.get()));
UnionBoundsEqualToWorkAreaBounds(
snap_group_controller->GetSnapGroupForGivenWindow(window()));
}
// Tests the entry point via auto grouping on window snapped.
TEST_F(SnapGroupClientControlledStateTest, AutoGroupEntryPoint) {
UpdateDisplay("800x600");
// Set the client-controlled window app type so it can be recognized in
// `GetActiveDeskAppWindowsInZOrder()`.
window()->SetProperty(chromeos::kAppTypeKey, chromeos::AppType::ARC_APP);
widget_delegate()->EnableSnap();
// Snap the client-controlled window. Since it's the only window, we don't
// start faster splitview.
const WindowSnapWMEvent snap_primary_event(
WM_EVENT_SNAP_PRIMARY, chromeos::kDefaultSnapRatio,
WindowSnapActionSource::kSnapByWindowLayoutMenu);
window_state()->OnWMEvent(&snap_primary_event);
// Apply pending requests.
ApplyPendingRequestedBounds();
state()->EnterNextState(window_state(), delegate()->new_state());
EXPECT_EQ(WindowStateType::kPrimarySnapped, window_state()->GetStateType());
VerifySnappedBounds(window(), chromeos::kDefaultSnapRatio);
VerifyNotSplitViewOrOverviewSession(window());
// Open a normal window, then snap it to the opposite side of `window()`.
auto non_client_controlled_window = CreateAppWindow();
SnapOneTestWindow(non_client_controlled_window.get(),
WindowStateType::kSecondarySnapped,
chromeos::kDefaultSnapRatio,
WindowSnapActionSource::kSnapByWindowLayoutMenu);
// Apply pending bounds changes and verify the state doesn't change.
ApplyPendingRequestedBounds();
EXPECT_EQ(WindowStateType::kPrimarySnapped, window_state()->GetStateType());
// Test a snap group is created.
auto* snap_group_controller = SnapGroupController::Get();
ASSERT_TRUE(snap_group_controller->AreWindowsInSnapGroup(
window(), non_client_controlled_window.get()));
UnionBoundsEqualToWorkAreaBounds(
snap_group_controller->GetSnapGroupForGivenWindow(window()));
}
// Tests basic snap group divider resizing.
TEST_F(SnapGroupClientControlledStateTest, ResizeViaDivider) {
UpdateDisplay("900x600");
// Create a snap group with a client-controlled and normal state window.
widget_delegate()->EnableSnap();
auto non_client_controlled_window = CreateAppWindow();
SnapOneTestWindow(non_client_controlled_window.get(),
WindowStateType::kSecondarySnapped,
chromeos::kDefaultSnapRatio,
WindowSnapActionSource::kSnapByWindowLayoutMenu);
VerifySplitViewOverviewSession(non_client_controlled_window.get());
ClickOnOverviewItem(window());
// Apply pending requests.
ApplyPendingRequestedBounds();
state()->EnterNextState(window_state(), delegate()->new_state());
EXPECT_EQ(WindowStateType::kPrimarySnapped, window_state()->GetStateType());
SnapGroupController* snap_group_controller = SnapGroupController::Get();
auto* snap_group =
snap_group_controller->GetSnapGroupForGivenWindow(window());
ASSERT_TRUE(snap_group);
auto* snap_group_divider = snap_group->snap_group_divider();
VerifySnappedBounds(window(), chromeos::kDefaultSnapRatio);
// Start a drag on the divider.
auto* event_generator = GetEventGenerator();
// Resize to arbitrary locations with the divider.
for (const float target_width : {300, 450, 600}) {
const gfx::Point divider_center(snap_group_divider
->GetDividerBoundsInScreen(
/*is_dragging=*/false)
.CenterPoint());
event_generator->MoveMouseTo(divider_center);
event_generator->PressLeftButton();
const gfx::Rect bounds_before_resizing(delegate()->requested_bounds());
delegate()->set_bounds_request_callback(
base::BindLambdaForTesting([&](const gfx::Rect& bounds) {
if (bounds == bounds_before_resizing) {
return;
}
// When any new bounds is requested, `OnDragStarted()` should be
// called already.
EXPECT_TRUE(window_state_delegate()->drag_in_progress());
EXPECT_TRUE(window_state()->drag_details()->bounds_change &
WindowResizer::kBoundsChange_Resizes);
delegate()->set_bounds_request_callback(base::NullCallback());
}));
ApplyPendingRequestedBounds();
// Resize with at least 2 steps to simulate the real CUJ of dragging the
// mouse. The default test EventGenerator sends only the start and end
// points which is an abrupt jump between points.
event_generator->MoveMouseTo(gfx::Point(target_width, divider_center.y()),
/*count=*/2);
ASSERT_TRUE(snap_group_divider->is_resizing_with_divider());
EXPECT_TRUE(window_state_delegate()->drag_in_progress());
EXPECT_TRUE(window_state()->drag_details()->bounds_change &
WindowResizer::kBoundsChange_Resizes);
// Apply pending requests.
ApplyPendingRequestedBounds();
const float expected_snap_ratio = target_width / 900;
VerifySnappedBounds(window(), expected_snap_ratio);
EXPECT_NEAR(target_width, window()->GetTargetBounds().width(),
/*abs_error=*/kSplitviewDividerShortSideLength / 2);
event_generator->ReleaseLeftButton();
VerifySnappedBounds(window(), expected_snap_ratio);
// The following drag info is used by client to determine how to handle the
// bounds change.
EXPECT_FALSE(window_state_delegate()->drag_in_progress());
}
}
// Tests the basic functionalities of snap-to-replace.
TEST_F(SnapGroupClientControlledStateTest, SnapToReplace) {
// Create a snap group with 2 normal windows.
auto w1 = CreateAppWindow();
auto w2 = CreateAppWindow();
SnapOneTestWindow(w1.get(), WindowStateType::kPrimarySnapped,
chromeos::kDefaultSnapRatio);
SnapOneTestWindow(w2.get(), WindowStateType::kSecondarySnapped,
chromeos::kDefaultSnapRatio);
SnapGroupController* snap_group_controller = SnapGroupController::Get();
ASSERT_TRUE(snap_group_controller->AreWindowsInSnapGroup(w1.get(), w2.get()));
// Snap `window()` on top of `w1`.
widget_delegate()->EnableSnap();
const WindowSnapWMEvent snap_primary_event(
WM_EVENT_SNAP_PRIMARY, chromeos::kDefaultSnapRatio,
WindowSnapActionSource::kSnapByWindowLayoutMenu);
window_state()->OnWMEvent(&snap_primary_event);
state()->EnterNextState(window_state(), delegate()->new_state());
EXPECT_EQ(WindowStateType::kPrimarySnapped, window_state()->GetStateType());
// Test it replaces `w1` in the group.
EXPECT_FALSE(
snap_group_controller->AreWindowsInSnapGroup(w1.get(), w2.get()));
EXPECT_TRUE(snap_group_controller->AreWindowsInSnapGroup(window(), w2.get()));
}
// Tests that double click on the divider swaps the windows.
TEST_F(SnapGroupClientControlledStateTest, DoubleClickToSwap) {
// Create a snap group.
widget_delegate()->EnableSnap();
auto non_client_controlled_window = CreateAppWindow();
SnapOneTestWindow(non_client_controlled_window.get(),
WindowStateType::kSecondarySnapped,
chromeos::kDefaultSnapRatio,
WindowSnapActionSource::kSnapByWindowLayoutMenu);
VerifySplitViewOverviewSession(non_client_controlled_window.get());
ClickOnOverviewItem(window());
ApplyPendingRequestedBounds();
state()->EnterNextState(window_state(), delegate()->new_state());
EXPECT_EQ(WindowStateType::kPrimarySnapped, window_state()->GetStateType());
SnapGroupController* snap_group_controller = SnapGroupController::Get();
ASSERT_TRUE(snap_group_controller->AreWindowsInSnapGroup(
window(), non_client_controlled_window.get()));
auto* snap_group =
snap_group_controller->GetSnapGroupForGivenWindow(window());
ASSERT_TRUE(snap_group);
EXPECT_EQ(window(), snap_group->window1());
EXPECT_EQ(non_client_controlled_window.get(), snap_group->window2());
UnionBoundsEqualToWorkAreaBounds(snap_group);
// Double click on the divider.
const gfx::Rect divider_bounds(
snap_group->snap_group_divider()->GetDividerBoundsInScreen(
/*is_dragging=*/false));
auto* event_generator = GetEventGenerator();
event_generator->MoveMouseTo(divider_bounds.CenterPoint());
event_generator->DoubleClickLeftButton();
// Apply pending requests.
ApplyPendingRequestedBounds();
state()->EnterNextState(window_state(), delegate()->new_state());
// Test the state types and windows are swapped.
ASSERT_TRUE(snap_group_controller->AreWindowsInSnapGroup(
window(), non_client_controlled_window.get()));
EXPECT_EQ(WindowStateType::kSecondarySnapped, window_state()->GetStateType());
EXPECT_EQ(
WindowStateType::kPrimarySnapped,
WindowState::Get(non_client_controlled_window.get())->GetStateType());
EXPECT_EQ(non_client_controlled_window.get(), snap_group->window1());
EXPECT_EQ(window(), snap_group->window2());
// TODO(b/352621475): Verify `UnionBoundsEqualToWorkAreaBounds()`. Currently
// there may be a 1-px overlap, likely due to rounding.
}
// Tests the snap group window bounds are correct after minimize then
// unminimize.
TEST_F(SnapGroupClientControlledStateTest, SnapThenMinimize) {
UpdateDisplay("800x600");
// Create a snap group with a client-controlled and normal state window.
widget_delegate()->EnableSnap();
auto non_client_controlled_window = CreateAppWindow();
SnapOneTestWindow(non_client_controlled_window.get(),
WindowStateType::kSecondarySnapped,
chromeos::kDefaultSnapRatio,
WindowSnapActionSource::kSnapByWindowLayoutMenu);
VerifySplitViewOverviewSession(non_client_controlled_window.get());
ClickOnOverviewItem(window());
// Apply pending requests. Test the bounds are at 1/2.
ApplyPendingRequestedBounds();
state()->EnterNextState(window_state(), delegate()->new_state());
EXPECT_EQ(WindowStateType::kPrimarySnapped, window_state()->GetStateType());
SnapGroupController* snap_group_controller = SnapGroupController::Get();
ASSERT_TRUE(snap_group_controller->AreWindowsInSnapGroup(
window(), non_client_controlled_window.get()));
VerifySnappedBounds(window(), chromeos::kDefaultSnapRatio);
// Minimize the client-controlled window.
window_state()->Minimize();
ApplyPendingRequestedBounds();
state()->EnterNextState(window_state(), delegate()->new_state());
// Test the group is broken.
ASSERT_FALSE(snap_group_controller->AreWindowsInSnapGroup(
window(), non_client_controlled_window.get()));
// Unminimize the client-controlled window. Test the bounds are back at 1/2.
window_state()->Unminimize();
ApplyPendingRequestedBounds();
state()->EnterNextState(window_state(), delegate()->new_state());
VerifySnappedBounds(window(), chromeos::kDefaultSnapRatio);
}
// Tests that a client-controlled window in a snap group, when snapped to the
// opposite side, will set the correct bounds. Regression test for
// http://b/349774996.
TEST_F(SnapGroupClientControlledStateTest, SnapToOppositeSide) {
UpdateDisplay("800x600");
// Create a snap group with a client-controlled and normal state window.
widget_delegate()->EnableSnap();
auto non_client_controlled_window = CreateAppWindow();
SnapOneTestWindow(non_client_controlled_window.get(),
WindowStateType::kSecondarySnapped,
chromeos::kDefaultSnapRatio,
WindowSnapActionSource::kSnapByWindowLayoutMenu);
VerifySplitViewOverviewSession(non_client_controlled_window.get());
ClickOnOverviewItem(window());
// Apply pending requests. Test the bounds are at 1/2.
ApplyPendingRequestedBounds();
state()->EnterNextState(window_state(), delegate()->new_state());
EXPECT_EQ(WindowStateType::kPrimarySnapped, window_state()->GetStateType());
SnapGroupController* snap_group_controller = SnapGroupController::Get();
ASSERT_TRUE(snap_group_controller->AreWindowsInSnapGroup(
window(), non_client_controlled_window.get()));
VerifySnappedBounds(window(), chromeos::kDefaultSnapRatio);
VerifySnappedBounds(non_client_controlled_window.get(),
chromeos::kDefaultSnapRatio);
auto* snap_group =
snap_group_controller->GetSnapGroupForGivenWindow(window());
ASSERT_TRUE(snap_group);
UnionBoundsEqualToWorkAreaBounds(window(), non_client_controlled_window.get(),
snap_group->snap_group_divider());
// Snap to secondary 1/3.
const WindowSnapWMEvent snap_partial_secondary(
WM_EVENT_SNAP_SECONDARY, chromeos::kOneThirdSnapRatio,
WindowSnapActionSource::kSnapByWindowLayoutMenu);
window_state()->OnWMEvent(&snap_partial_secondary);
// Apply pending requests. Test the bounds are at 1/3.
ApplyPendingRequestedBounds();
state()->EnterNextState(window_state(), delegate()->new_state());
EXPECT_EQ(WindowStateType::kSecondarySnapped, window_state()->GetStateType());
VerifySnappedBounds(window(), chromeos::kOneThirdSnapRatio);
}
TEST_P(ClientControlledStateTestClamshellAndTablet, SnapWindow) {
// Snap disabled.
ASSERT_FALSE(window_state()->CanResize());
ASSERT_FALSE(window_state()->CanSnap());
// The event should be ignored.
const WindowSnapWMEvent snap_primary_event(WM_EVENT_SNAP_PRIMARY);
window_state()->OnWMEvent(&snap_primary_event);
EXPECT_FALSE(window_state()->IsSnapped());
EXPECT_TRUE(delegate()->requested_bounds().IsEmpty());
const WindowSnapWMEvent snap_secondary_event(WM_EVENT_SNAP_SECONDARY);
window_state()->OnWMEvent(&snap_secondary_event);