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
ash / wm / screen_pinning_controller_unittest.cc [blame]
// Copyright 2016 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/screen_pinning_controller.h"
#include <vector>
#include "ash/accelerators/accelerator_controller_impl.h"
#include "ash/shell.h"
#include "ash/test/ash_test_base.h"
#include "ash/wm/client_controlled_state.h"
#include "ash/wm/window_state.h"
#include "ash/wm/window_util.h"
#include "ash/wm/wm_event.h"
#include "base/memory/raw_ptr.h"
#include "base/ranges/algorithm.h"
#include "ui/aura/window.h"
namespace ash {
namespace {
int FindIndex(
const std::vector<raw_ptr<aura::Window, VectorExperimental>>& windows,
const aura::Window* target) {
auto iter = base::ranges::find(windows, target);
return iter != windows.end() ? iter - windows.begin() : -1;
}
class TestClientControlledStateDelegate
: public ClientControlledState::Delegate {
public:
~TestClientControlledStateDelegate() override = default;
void HandleWindowStateRequest(WindowState* state,
chromeos::WindowStateType type) override {}
void HandleBoundsRequest(WindowState* state,
chromeos::WindowStateType type,
const gfx::Rect& requested_bounds,
int64_t display_id) override {}
};
} // namespace
using ScreenPinningControllerTest = AshTestBase;
TEST_F(ScreenPinningControllerTest, IsPinned) {
aura::Window* w1 = CreateTestWindowInShellWithId(0);
wm::ActivateWindow(w1);
window_util::PinWindow(w1, /* trusted */ false);
EXPECT_TRUE(Shell::Get()->screen_pinning_controller()->IsPinned());
}
TEST_F(ScreenPinningControllerTest, OnlyOnePinnedWindow) {
aura::Window* w1 = CreateTestWindowInShellWithId(0);
aura::Window* w2 = CreateTestWindowInShellWithId(1);
wm::ActivateWindow(w1);
window_util::PinWindow(w1, /* trusted */ false);
EXPECT_TRUE(WindowState::Get(w1)->IsPinned());
EXPECT_FALSE(WindowState::Get(w2)->IsPinned());
// Prohibit to pin two (or more) windows.
window_util::PinWindow(w2, /* trusted */ false);
EXPECT_TRUE(WindowState::Get(w1)->IsPinned());
EXPECT_FALSE(WindowState::Get(w2)->IsPinned());
}
TEST_F(ScreenPinningControllerTest, FullscreenInPinnedMode) {
aura::Window* w1 = CreateTestWindowInShellWithId(0);
aura::Window* w2 = CreateTestWindowInShellWithId(1);
wm::ActivateWindow(w1);
window_util::PinWindow(w1, /* trusted */ false);
{
// Window w1 should be in front of w2.
std::vector<raw_ptr<aura::Window, VectorExperimental>> siblings =
w1->parent()->children();
int index1 = FindIndex(siblings, w1);
int index2 = FindIndex(siblings, w2);
EXPECT_NE(-1, index1);
EXPECT_NE(-1, index2);
EXPECT_GT(index1, index2);
}
// Set w2 to fullscreen.
{
wm::ActivateWindow(w2);
const WMEvent event(WM_EVENT_TOGGLE_FULLSCREEN);
WindowState::Get(w2)->OnWMEvent(&event);
}
{
// Verify that w1 is still in front of w2.
std::vector<raw_ptr<aura::Window, VectorExperimental>> siblings =
w1->parent()->children();
int index1 = FindIndex(siblings, w1);
int index2 = FindIndex(siblings, w2);
EXPECT_NE(-1, index1);
EXPECT_NE(-1, index2);
EXPECT_GT(index1, index2);
}
// Unset w2's fullscreen.
{
wm::ActivateWindow(w2);
const WMEvent event(WM_EVENT_TOGGLE_FULLSCREEN);
WindowState::Get(w2)->OnWMEvent(&event);
}
{
// Verify that w1 is still in front of w2.
std::vector<raw_ptr<aura::Window, VectorExperimental>> siblings =
w1->parent()->children();
int index1 = FindIndex(siblings, w1);
int index2 = FindIndex(siblings, w2);
EXPECT_NE(-1, index1);
EXPECT_NE(-1, index2);
EXPECT_GT(index1, index2);
}
// Maximize w2.
{
wm::ActivateWindow(w2);
const WMEvent event(WM_EVENT_TOGGLE_MAXIMIZE);
WindowState::Get(w2)->OnWMEvent(&event);
}
{
// Verify that w1 is still in front of w2.
std::vector<raw_ptr<aura::Window, VectorExperimental>> siblings =
w1->parent()->children();
int index1 = FindIndex(siblings, w1);
int index2 = FindIndex(siblings, w2);
EXPECT_NE(-1, index1);
EXPECT_NE(-1, index2);
EXPECT_GT(index1, index2);
}
// Unset w2's maximize.
{
wm::ActivateWindow(w2);
const WMEvent event(WM_EVENT_TOGGLE_MAXIMIZE);
WindowState::Get(w2)->OnWMEvent(&event);
}
{
// Verify that w1 is still in front of w2.
std::vector<raw_ptr<aura::Window, VectorExperimental>> siblings =
w1->parent()->children();
int index1 = FindIndex(siblings, w1);
int index2 = FindIndex(siblings, w2);
EXPECT_NE(-1, index1);
EXPECT_NE(-1, index2);
EXPECT_GT(index1, index2);
}
// Restore w1.
WindowState::Get(w1)->Restore();
// Now, fullscreen-ize w2 should put it in front of w1.
{
wm::ActivateWindow(w2);
const WMEvent event(WM_EVENT_TOGGLE_FULLSCREEN);
WindowState::Get(w2)->OnWMEvent(&event);
}
{
// Verify that w1 is still in front of w2.
std::vector<raw_ptr<aura::Window, VectorExperimental>> siblings =
w1->parent()->children();
int index1 = FindIndex(siblings, w1);
int index2 = FindIndex(siblings, w2);
EXPECT_NE(-1, index1);
EXPECT_NE(-1, index2);
EXPECT_GT(index2, index1);
}
}
TEST_F(ScreenPinningControllerTest, TrustedPinnedWithAccelerator) {
aura::Window* w1 = CreateTestWindowInShellWithId(0);
wm::ActivateWindow(w1);
window_util::PinWindow(w1, /* trusted */ true);
EXPECT_TRUE(Shell::Get()->screen_pinning_controller()->IsPinned());
Shell::Get()->accelerator_controller()->PerformActionIfEnabled(
AcceleratorAction::kUnpin, {});
// The AcceleratorAction::kUnpin accelerator key is disabled for trusted
// pinned and the window must be still pinned.
EXPECT_TRUE(Shell::Get()->screen_pinning_controller()->IsPinned());
}
TEST_F(ScreenPinningControllerTest, ExitUnifiedDisplay) {
display_manager()->SetUnifiedDesktopEnabled(true);
UpdateDisplay("400x300, 500x400");
aura::Window* w1 = CreateTestWindowInShellWithId(0);
wm::ActivateWindow(w1);
auto* window_state = WindowState::Get(w1);
window_util::PinWindow(w1, /*trusted=*/true);
EXPECT_TRUE(window_state->IsPinned());
EXPECT_TRUE(Shell::Get()->screen_pinning_controller()->IsPinned());
UpdateDisplay("300x200");
EXPECT_TRUE(window_state->IsPinned());
EXPECT_TRUE(Shell::Get()->screen_pinning_controller()->IsPinned());
}
TEST_F(ScreenPinningControllerTest, CleanUpObserversAndDimmer) {
// Create a window with ClientControlledState.
auto w = CreateAppWindow(gfx::Rect(), chromeos::AppType::CHROME_APP, 0);
ash::WindowState* ws = ash::WindowState::Get(w.get());
auto delegate = std::make_unique<TestClientControlledStateDelegate>();
auto state = std::make_unique<ClientControlledState>(std::move(delegate));
auto* state_raw = state.get();
ws->SetStateObject(std::move(state));
wm::ActivateWindow(w.get());
// Observer should be added to |w|, and |w->parent()|.
state_raw->EnterNextState(ws, chromeos::WindowStateType::kPinned);
EXPECT_TRUE(WindowState::Get(w.get())->IsPinned());
const aura::Window* container = w->parent();
// Destroying |w| clears |pinned_window_|. The observers should be removed
// even if ClientControlledState doesn't call SetPinnedWindow when
// WindowState::Restore() is called.
w.reset();
// It should clear all child windows in |container| when the pinned window is
// destroyed.
EXPECT_EQ(container->children().size(), 0u);
// Add a sibling window. It should not crash.
CreateTestWindowInShellWithId(2);
}
TEST_F(ScreenPinningControllerTest, AllowWindowOnTopOfPinnedWindowForOnTask) {
aura::Window* const w1 = CreateTestWindowInShellWithId(0);
aura::Window* const w2 = CreateTestWindowInShellWithId(1);
wm::ActivateWindow(w1);
window_util::PinWindow(w1, /*trusted=*/false);
EXPECT_TRUE(WindowState::Get(w1)->IsPinned());
EXPECT_FALSE(WindowState::Get(w2)->IsPinned());
Shell::Get()
->screen_pinning_controller()
->SetAllowWindowStackingWithPinnedWindow(true);
aura::Window* const top_container = Shell::GetContainer(
Shell::GetPrimaryRootWindow(), kShellWindowId_AlwaysOnTopContainer);
top_container->StackChildAtTop(w2);
EXPECT_TRUE(WindowState::Get(w1)->IsPinned());
// Verify that w2 is in front of w1.
aura::Window::Windows siblings = w2->parent()->children();
int index1 = FindIndex(siblings, w1);
int index2 = FindIndex(siblings, w2);
EXPECT_NE(-1, index1);
EXPECT_NE(-1, index2);
EXPECT_GT(index1, index2);
}
} // namespace ash