1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
6 #include "ash/test/ash_test_base.h"
7 #include "ash/test/test_session_state_delegate.h"
8 #include "ash/test/test_shell_delegate.h"
9 #include "ash/wm/window_state.h"
10 #include "base/command_line.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "chrome/browser/chromeos/extensions/wallpaper_private_api.h"
13 #include "chrome/browser/chromeos/login/users/fake_chrome_user_manager.h"
14 #include "chrome/browser/chromeos/login/users/scoped_user_manager_enabler.h"
15 #include "chrome/browser/ui/ash/multi_user/multi_user_window_manager.h"
16 #include "chrome/browser/ui/ash/multi_user/multi_user_window_manager_chromeos.h"
17 #include "ui/aura/test/test_windows.h"
18 #include "ui/aura/window.h"
19 #include "ui/aura/window_event_dispatcher.h"
24 const char kTestAccount1
[] = "user1@test.com";
25 const char kTestAccount2
[] = "user2@test.com";
27 class WallpaperPrivateApiUnittest
: public ash::test::AshTestBase
{
29 WallpaperPrivateApiUnittest()
30 : fake_user_manager_(new FakeChromeUserManager()),
31 scoped_user_manager_(fake_user_manager_
) {}
34 FakeChromeUserManager
* fake_user_manager() { return fake_user_manager_
; }
37 FakeChromeUserManager
* fake_user_manager_
;
38 ScopedUserManagerEnabler scoped_user_manager_
;
40 DISALLOW_COPY_AND_ASSIGN(WallpaperPrivateApiUnittest
);
43 class TestMinimizeFunction
44 : public WallpaperPrivateMinimizeInactiveWindowsFunction
{
46 TestMinimizeFunction() {}
48 bool RunAsync() override
{
49 return WallpaperPrivateMinimizeInactiveWindowsFunction::RunAsync();
53 ~TestMinimizeFunction() override
{}
56 class TestRestoreFunction
57 : public WallpaperPrivateRestoreMinimizedWindowsFunction
{
59 TestRestoreFunction() {}
61 bool RunAsync() override
{
62 return WallpaperPrivateRestoreMinimizedWindowsFunction::RunAsync();
65 ~TestRestoreFunction() override
{}
70 TEST_F(WallpaperPrivateApiUnittest
, HideAndRestoreWindows
) {
71 fake_user_manager()->AddUser(kTestAccount1
);
72 scoped_ptr
<aura::Window
> window3(CreateTestWindowInShellWithId(3));
73 scoped_ptr
<aura::Window
> window2(CreateTestWindowInShellWithId(2));
74 scoped_ptr
<aura::Window
> window1(CreateTestWindowInShellWithId(1));
75 scoped_ptr
<aura::Window
> window0(CreateTestWindowInShellWithId(0));
77 ash::wm::WindowState
* window0_state
= ash::wm::GetWindowState(window0
.get());
78 ash::wm::WindowState
* window1_state
= ash::wm::GetWindowState(window1
.get());
79 ash::wm::WindowState
* window2_state
= ash::wm::GetWindowState(window2
.get());
80 ash::wm::WindowState
* window3_state
= ash::wm::GetWindowState(window3
.get());
82 window3_state
->Minimize();
83 window1_state
->Maximize();
85 // Window 3 starts minimized, window 1 starts maximized.
86 EXPECT_FALSE(window0_state
->IsMinimized());
87 EXPECT_FALSE(window1_state
->IsMinimized());
88 EXPECT_FALSE(window2_state
->IsMinimized());
89 EXPECT_TRUE(window3_state
->IsMinimized());
91 // We then activate window 0 (i.e. wallpaper picker) and call the minimize
93 window0_state
->Activate();
94 EXPECT_TRUE(window0_state
->IsActive());
95 scoped_refptr
<TestMinimizeFunction
> minimize_function(
96 new TestMinimizeFunction());
97 EXPECT_TRUE(minimize_function
->RunAsync());
99 // All windows except window 0 should be minimized.
100 EXPECT_FALSE(window0_state
->IsMinimized());
101 EXPECT_TRUE(window1_state
->IsMinimized());
102 EXPECT_TRUE(window2_state
->IsMinimized());
103 EXPECT_TRUE(window3_state
->IsMinimized());
105 // Then we destroy window 0 and call the restore function.
107 scoped_refptr
<TestRestoreFunction
> restore_function(
108 new TestRestoreFunction());
109 EXPECT_TRUE(restore_function
->RunAsync());
111 // Windows 1 and 2 should no longer be minimized. Window 1 should again
112 // be maximized. Window 3 should still be minimized.
113 EXPECT_TRUE(window1_state
->IsMaximized());
114 EXPECT_FALSE(window2_state
->IsMinimized());
115 EXPECT_TRUE(window3_state
->IsMinimized());
118 // Test for multiple calls to |MinimizeInactiveWindows| before call
120 // 1. If all window hasn't change their states, the following calls are noops.
121 // 2. If some windows are manually unminimized, the following call will minimize
122 // all the unminimized windows.
123 TEST_F(WallpaperPrivateApiUnittest
, HideAndManualUnminimizeWindows
) {
124 fake_user_manager()->AddUser(kTestAccount1
);
125 scoped_ptr
<aura::Window
> window1(CreateTestWindowInShellWithId(1));
126 scoped_ptr
<aura::Window
> window0(CreateTestWindowInShellWithId(0));
128 ash::wm::WindowState
* window0_state
= ash::wm::GetWindowState(window0
.get());
129 ash::wm::WindowState
* window1_state
= ash::wm::GetWindowState(window1
.get());
131 // We then activate window 0 (i.e. wallpaper picker) and call the minimize
133 window0_state
->Activate();
134 EXPECT_TRUE(window0_state
->IsActive());
135 scoped_refptr
<TestMinimizeFunction
> minimize_function_0(
136 new TestMinimizeFunction());
137 EXPECT_TRUE(minimize_function_0
->RunAsync());
139 // All windows except window 0 should be minimized.
140 EXPECT_FALSE(window0_state
->IsMinimized());
141 EXPECT_TRUE(window1_state
->IsMinimized());
143 // Calls minimize function again should be an noop if window state didn't
145 scoped_refptr
<TestMinimizeFunction
> minimize_function_1(
146 new TestMinimizeFunction());
147 EXPECT_TRUE(minimize_function_1
->RunAsync());
149 // All windows except window 0 should be minimized.
150 EXPECT_FALSE(window0_state
->IsMinimized());
151 EXPECT_TRUE(window1_state
->IsMinimized());
153 // Manually unminimize window 1.
154 window1_state
->Unminimize();
155 EXPECT_FALSE(window1_state
->IsMinimized());
156 window0_state
->Activate();
158 scoped_refptr
<TestMinimizeFunction
> minimize_function_2(
159 new TestMinimizeFunction());
160 EXPECT_TRUE(minimize_function_2
->RunAsync());
162 // Window 1 should be minimized again.
163 EXPECT_FALSE(window0_state
->IsMinimized());
164 EXPECT_TRUE(window1_state
->IsMinimized());
166 // Then we destroy window 0 and call the restore function.
168 scoped_refptr
<TestRestoreFunction
> restore_function(
169 new TestRestoreFunction());
170 EXPECT_TRUE(restore_function
->RunAsync());
172 // Windows 1 should no longer be minimized.
173 EXPECT_FALSE(window1_state
->IsMinimized());
176 class WallpaperPrivateApiMultiUserUnittest
177 : public WallpaperPrivateApiUnittest
{
179 WallpaperPrivateApiMultiUserUnittest()
180 : multi_user_window_manager_(NULL
),
181 session_state_delegate_(NULL
) {}
183 void SetUp() override
;
184 void TearDown() override
;
187 void SetUpMultiUserWindowManager(
188 const std::string
& active_user_id
,
189 chrome::MultiUserWindowManager::MultiProfileMode mode
);
191 void SwitchActiveUser(const std::string
& active_user_id
);
193 chrome::MultiUserWindowManagerChromeOS
* multi_user_window_manager() {
194 return multi_user_window_manager_
;
198 chrome::MultiUserWindowManagerChromeOS
* multi_user_window_manager_
;
199 ash::test::TestSessionStateDelegate
* session_state_delegate_
;
201 DISALLOW_COPY_AND_ASSIGN(WallpaperPrivateApiMultiUserUnittest
);
204 void WallpaperPrivateApiMultiUserUnittest::SetUp() {
205 AshTestBase::SetUp();
206 session_state_delegate_
=
207 static_cast<ash::test::TestSessionStateDelegate
*> (
208 ash::Shell::GetInstance()->session_state_delegate());
209 fake_user_manager()->AddUser(kTestAccount1
);
210 fake_user_manager()->AddUser(kTestAccount2
);
213 void WallpaperPrivateApiMultiUserUnittest::TearDown() {
214 chrome::MultiUserWindowManager::DeleteInstance();
215 AshTestBase::TearDown();
218 void WallpaperPrivateApiMultiUserUnittest::SetUpMultiUserWindowManager(
219 const std::string
& active_user_id
,
220 chrome::MultiUserWindowManager::MultiProfileMode mode
) {
221 multi_user_window_manager_
=
222 new chrome::MultiUserWindowManagerChromeOS(active_user_id
);
223 multi_user_window_manager_
->Init();
224 chrome::MultiUserWindowManager::SetInstanceForTest(
225 multi_user_window_manager_
, mode
);
226 // We do not want animations while the test is going on.
227 multi_user_window_manager_
->SetAnimationSpeedForTest(
228 chrome::MultiUserWindowManagerChromeOS::ANIMATION_SPEED_DISABLED
);
229 EXPECT_TRUE(multi_user_window_manager_
);
232 void WallpaperPrivateApiMultiUserUnittest::SwitchActiveUser(
233 const std::string
& active_user_id
) {
234 fake_user_manager()->SwitchActiveUser(active_user_id
);
235 multi_user_window_manager_
->ActiveUserChanged(active_user_id
);
238 // In multi profile mode, user may open wallpaper picker in one profile and
239 // then switch to a different profile and open another wallpaper picker
240 // without closing the first one.
241 TEST_F(WallpaperPrivateApiMultiUserUnittest
, HideAndRestoreWindowsTwoUsers
) {
242 SetUpMultiUserWindowManager(kTestAccount1
,
243 chrome::MultiUserWindowManager::MULTI_PROFILE_MODE_SEPARATED
);
245 scoped_ptr
<aura::Window
> window4(CreateTestWindowInShellWithId(4));
246 scoped_ptr
<aura::Window
> window3(CreateTestWindowInShellWithId(3));
247 scoped_ptr
<aura::Window
> window2(CreateTestWindowInShellWithId(2));
248 scoped_ptr
<aura::Window
> window1(CreateTestWindowInShellWithId(1));
249 scoped_ptr
<aura::Window
> window0(CreateTestWindowInShellWithId(0));
251 ash::wm::WindowState
* window0_state
= ash::wm::GetWindowState(window0
.get());
252 ash::wm::WindowState
* window1_state
= ash::wm::GetWindowState(window1
.get());
253 ash::wm::WindowState
* window2_state
= ash::wm::GetWindowState(window2
.get());
254 ash::wm::WindowState
* window3_state
= ash::wm::GetWindowState(window3
.get());
255 ash::wm::WindowState
* window4_state
= ash::wm::GetWindowState(window4
.get());
257 multi_user_window_manager()->SetWindowOwner(window0
.get(), kTestAccount1
);
258 multi_user_window_manager()->SetWindowOwner(window1
.get(), kTestAccount1
);
260 // Set some windows to an inactive owner.
261 multi_user_window_manager()->SetWindowOwner(window2
.get(), kTestAccount2
);
262 multi_user_window_manager()->SetWindowOwner(window3
.get(), kTestAccount2
);
263 multi_user_window_manager()->SetWindowOwner(window4
.get(), kTestAccount2
);
265 EXPECT_FALSE(window0_state
->IsMinimized());
266 EXPECT_FALSE(window1_state
->IsMinimized());
267 EXPECT_FALSE(window2_state
->IsMinimized());
268 EXPECT_FALSE(window3_state
->IsMinimized());
269 EXPECT_FALSE(window4_state
->IsMinimized());
271 // We then activate window 0 (i.e. wallpaper picker) and call the minimize
273 window0_state
->Activate();
274 EXPECT_TRUE(window0_state
->IsActive());
275 scoped_refptr
<TestMinimizeFunction
> minimize_function_0(
276 new TestMinimizeFunction());
277 EXPECT_TRUE(minimize_function_0
->RunAsync());
279 // All windows except window 0 should be minimized.
280 EXPECT_FALSE(window0_state
->IsMinimized());
281 EXPECT_TRUE(window1_state
->IsMinimized());
283 // All windows that belong to inactive user should not be affected.
284 EXPECT_FALSE(window2_state
->IsMinimized());
285 EXPECT_FALSE(window3_state
->IsMinimized());
286 EXPECT_FALSE(window4_state
->IsMinimized());
288 // Activate kTestAccount2. kTestAccount1 becomes inactive user.
289 SwitchActiveUser(kTestAccount2
);
291 window2_state
->Activate();
292 EXPECT_TRUE(window2_state
->IsActive());
293 scoped_refptr
<TestMinimizeFunction
> minimize_function_1(
294 new TestMinimizeFunction());
295 EXPECT_TRUE(minimize_function_1
->RunAsync());
297 // All windows except window 2 should be minimized.
298 EXPECT_FALSE(window2_state
->IsMinimized());
299 EXPECT_TRUE(window3_state
->IsMinimized());
300 EXPECT_TRUE(window4_state
->IsMinimized());
302 // All windows that belong to inactive user should not be affected.
303 EXPECT_FALSE(window0_state
->IsMinimized());
304 EXPECT_TRUE(window1_state
->IsMinimized());
306 // Destroy window 4. Nothing should happen to other windows.
307 window4_state
->Unminimize();
310 EXPECT_FALSE(window2_state
->IsMinimized());
311 EXPECT_TRUE(window3_state
->IsMinimized());
312 EXPECT_FALSE(window0_state
->IsMinimized());
313 EXPECT_TRUE(window1_state
->IsMinimized());
315 // Then we destroy window 2 and call the restore function.
317 scoped_refptr
<TestRestoreFunction
> restore_function_0(
318 new TestRestoreFunction());
319 EXPECT_TRUE(restore_function_0
->RunAsync());
321 EXPECT_FALSE(window3_state
->IsMinimized());
323 // All windows that belong to inactive user should not be affected.
324 EXPECT_FALSE(window0_state
->IsMinimized());
325 EXPECT_TRUE(window1_state
->IsMinimized());
327 SwitchActiveUser(kTestAccount1
);
329 // Then we destroy window 0 and call the restore function.
331 scoped_refptr
<TestRestoreFunction
> restore_function_1(
332 new TestRestoreFunction());
333 EXPECT_TRUE(restore_function_1
->RunAsync());
335 EXPECT_FALSE(window1_state
->IsMinimized());
336 EXPECT_FALSE(window3_state
->IsMinimized());
339 // In multi profile mode, user may teleport windows. Teleported window should
340 // also be minimized when open wallpaper picker.
341 TEST_F(WallpaperPrivateApiMultiUserUnittest
, HideTeleportedWindow
) {
342 SetUpMultiUserWindowManager(kTestAccount1
,
343 chrome::MultiUserWindowManager::MULTI_PROFILE_MODE_MIXED
);
345 scoped_ptr
<aura::Window
> window3(CreateTestWindowInShellWithId(3));
346 scoped_ptr
<aura::Window
> window2(CreateTestWindowInShellWithId(2));
347 scoped_ptr
<aura::Window
> window1(CreateTestWindowInShellWithId(1));
348 scoped_ptr
<aura::Window
> window0(CreateTestWindowInShellWithId(0));
350 ash::wm::WindowState
* window0_state
= ash::wm::GetWindowState(window0
.get());
351 ash::wm::WindowState
* window1_state
= ash::wm::GetWindowState(window1
.get());
352 ash::wm::WindowState
* window2_state
= ash::wm::GetWindowState(window2
.get());
353 ash::wm::WindowState
* window3_state
= ash::wm::GetWindowState(window3
.get());
355 multi_user_window_manager()->SetWindowOwner(window0
.get(), kTestAccount1
);
356 multi_user_window_manager()->SetWindowOwner(window1
.get(), kTestAccount1
);
358 // Set some windows to an inactive owner.
359 multi_user_window_manager()->SetWindowOwner(window2
.get(), kTestAccount2
);
360 multi_user_window_manager()->SetWindowOwner(window3
.get(), kTestAccount2
);
362 // Teleport window2 to kTestAccount1.
363 multi_user_window_manager()->ShowWindowForUser(window2
.get(), kTestAccount1
);
365 // Initial window state. All windows shouldn't be minimized.
366 EXPECT_FALSE(window0_state
->IsMinimized());
367 EXPECT_FALSE(window1_state
->IsMinimized());
368 EXPECT_FALSE(window2_state
->IsMinimized());
369 EXPECT_FALSE(window3_state
->IsMinimized());
371 // We then activate window 0 (i.e. wallpaper picker) and call the minimize
373 window0_state
->Activate();
374 EXPECT_TRUE(window0_state
->IsActive());
375 scoped_refptr
<TestMinimizeFunction
> minimize_function_0(
376 new TestMinimizeFunction());
377 EXPECT_TRUE(minimize_function_0
->RunAsync());
379 // All windows except window 0 should be minimized.
380 EXPECT_FALSE(window0_state
->IsMinimized());
381 EXPECT_TRUE(window1_state
->IsMinimized());
383 // Teleported window should also be minimized.
384 EXPECT_TRUE(window2_state
->IsMinimized());
385 // Other window should remain the same.
386 EXPECT_FALSE(window3_state
->IsMinimized());
388 // Then we destroy window 0 and call the restore function.
390 scoped_refptr
<TestRestoreFunction
> restore_function_1(
391 new TestRestoreFunction());
392 EXPECT_TRUE(restore_function_1
->RunAsync());
394 EXPECT_FALSE(window1_state
->IsMinimized());
395 EXPECT_FALSE(window2_state
->IsMinimized());
396 EXPECT_FALSE(window3_state
->IsMinimized());
399 } // namespace chromeos