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/chromeos/login/users/wallpaper/wallpaper_manager.h"
16 #include "chrome/browser/ui/ash/multi_user/multi_user_window_manager.h"
17 #include "chrome/browser/ui/ash/multi_user/multi_user_window_manager_chromeos.h"
18 #include "ui/aura/test/test_windows.h"
19 #include "ui/aura/window.h"
20 #include "ui/aura/window_event_dispatcher.h"
25 const char kTestAccount1
[] = "user1@test.com";
26 const char kTestAccount2
[] = "user2@test.com";
28 class WallpaperPrivateApiUnittest
: public ash::test::AshTestBase
{
30 WallpaperPrivateApiUnittest()
31 : fake_user_manager_(new FakeChromeUserManager()),
32 scoped_user_manager_(fake_user_manager_
) {}
35 FakeChromeUserManager
* fake_user_manager() { return fake_user_manager_
; }
38 FakeChromeUserManager
* fake_user_manager_
;
39 ScopedUserManagerEnabler scoped_user_manager_
;
41 DISALLOW_COPY_AND_ASSIGN(WallpaperPrivateApiUnittest
);
44 class TestMinimizeFunction
45 : public WallpaperPrivateMinimizeInactiveWindowsFunction
{
47 TestMinimizeFunction() {}
49 bool RunAsync() override
{
50 return WallpaperPrivateMinimizeInactiveWindowsFunction::RunAsync();
54 ~TestMinimizeFunction() override
{}
57 class TestRestoreFunction
58 : public WallpaperPrivateRestoreMinimizedWindowsFunction
{
60 TestRestoreFunction() {}
62 bool RunAsync() override
{
63 return WallpaperPrivateRestoreMinimizedWindowsFunction::RunAsync();
66 ~TestRestoreFunction() override
{}
71 TEST_F(WallpaperPrivateApiUnittest
, HideAndRestoreWindows
) {
72 fake_user_manager()->AddUser(kTestAccount1
);
73 scoped_ptr
<aura::Window
> window4(CreateTestWindowInShellWithId(4));
74 scoped_ptr
<aura::Window
> window3(CreateTestWindowInShellWithId(3));
75 scoped_ptr
<aura::Window
> window2(CreateTestWindowInShellWithId(2));
76 scoped_ptr
<aura::Window
> window1(CreateTestWindowInShellWithId(1));
77 scoped_ptr
<aura::Window
> window0(CreateTestWindowInShellWithId(0));
79 ash::wm::WindowState
* window0_state
= ash::wm::GetWindowState(window0
.get());
80 ash::wm::WindowState
* window1_state
= ash::wm::GetWindowState(window1
.get());
81 ash::wm::WindowState
* window2_state
= ash::wm::GetWindowState(window2
.get());
82 ash::wm::WindowState
* window3_state
= ash::wm::GetWindowState(window3
.get());
83 ash::wm::WindowState
* window4_state
= ash::wm::GetWindowState(window4
.get());
85 window3_state
->Minimize();
86 window1_state
->Maximize();
88 // Window 3 starts minimized, window 1 starts maximized.
89 EXPECT_FALSE(window0_state
->IsMinimized());
90 EXPECT_FALSE(window1_state
->IsMinimized());
91 EXPECT_FALSE(window2_state
->IsMinimized());
92 EXPECT_TRUE(window3_state
->IsMinimized());
93 EXPECT_FALSE(window4_state
->IsMinimized());
95 // We then activate window 0 (i.e. wallpaper picker) and call the minimize
97 window0_state
->Activate();
98 EXPECT_TRUE(window0_state
->IsActive());
99 scoped_refptr
<TestMinimizeFunction
> minimize_function(
100 new TestMinimizeFunction());
101 EXPECT_TRUE(minimize_function
->RunAsync());
103 // All windows except window 0 should be minimized.
104 EXPECT_FALSE(window0_state
->IsMinimized());
105 EXPECT_TRUE(window1_state
->IsMinimized());
106 EXPECT_TRUE(window2_state
->IsMinimized());
107 EXPECT_TRUE(window3_state
->IsMinimized());
108 EXPECT_TRUE(window4_state
->IsMinimized());
110 // Activates window 4 and then minimizes it.
111 window4_state
->Activate();
112 window4_state
->Minimize();
114 // Then we destroy window 0 and call the restore function.
116 scoped_refptr
<TestRestoreFunction
> restore_function(
117 new TestRestoreFunction());
118 EXPECT_TRUE(restore_function
->RunAsync());
120 // Windows 1 and 2 should no longer be minimized. Window 1 should again
121 // be maximized. Window 3 should still be minimized. Window 4 should remain
122 // minimized since user interacted with it while wallpaper picker was open.
123 EXPECT_TRUE(window1_state
->IsMaximized());
124 EXPECT_FALSE(window2_state
->IsMinimized());
125 EXPECT_TRUE(window3_state
->IsMinimized());
126 EXPECT_TRUE(window4_state
->IsMinimized());
129 // Test for multiple calls to |MinimizeInactiveWindows| before call
131 // 1. If all window hasn't change their states, the following calls are noops.
132 // 2. If some windows are manually unminimized, the following call will minimize
133 // all the unminimized windows.
134 TEST_F(WallpaperPrivateApiUnittest
, HideAndManualUnminimizeWindows
) {
135 fake_user_manager()->AddUser(kTestAccount1
);
136 scoped_ptr
<aura::Window
> window1(CreateTestWindowInShellWithId(1));
137 scoped_ptr
<aura::Window
> window0(CreateTestWindowInShellWithId(0));
139 ash::wm::WindowState
* window0_state
= ash::wm::GetWindowState(window0
.get());
140 ash::wm::WindowState
* window1_state
= ash::wm::GetWindowState(window1
.get());
142 // We then activate window 0 (i.e. wallpaper picker) and call the minimize
144 window0_state
->Activate();
145 EXPECT_TRUE(window0_state
->IsActive());
146 scoped_refptr
<TestMinimizeFunction
> minimize_function_0(
147 new TestMinimizeFunction());
148 EXPECT_TRUE(minimize_function_0
->RunAsync());
150 // All windows except window 0 should be minimized.
151 EXPECT_FALSE(window0_state
->IsMinimized());
152 EXPECT_TRUE(window1_state
->IsMinimized());
154 // Calls minimize function again should be an noop if window state didn't
156 scoped_refptr
<TestMinimizeFunction
> minimize_function_1(
157 new TestMinimizeFunction());
158 EXPECT_TRUE(minimize_function_1
->RunAsync());
160 // All windows except window 0 should be minimized.
161 EXPECT_FALSE(window0_state
->IsMinimized());
162 EXPECT_TRUE(window1_state
->IsMinimized());
164 // Manually unminimize window 1.
165 window1_state
->Unminimize();
166 EXPECT_FALSE(window1_state
->IsMinimized());
167 window0_state
->Activate();
169 scoped_refptr
<TestMinimizeFunction
> minimize_function_2(
170 new TestMinimizeFunction());
171 EXPECT_TRUE(minimize_function_2
->RunAsync());
173 // Window 1 should be minimized again.
174 EXPECT_FALSE(window0_state
->IsMinimized());
175 EXPECT_TRUE(window1_state
->IsMinimized());
177 // Then we destroy window 0 and call the restore function.
179 scoped_refptr
<TestRestoreFunction
> restore_function(
180 new TestRestoreFunction());
181 EXPECT_TRUE(restore_function
->RunAsync());
183 // Windows 1 should no longer be minimized.
184 EXPECT_FALSE(window1_state
->IsMinimized());
187 class WallpaperPrivateApiMultiUserUnittest
188 : public WallpaperPrivateApiUnittest
{
190 WallpaperPrivateApiMultiUserUnittest()
191 : multi_user_window_manager_(NULL
),
192 session_state_delegate_(NULL
) {}
194 void SetUp() override
;
195 void TearDown() override
;
198 void SetUpMultiUserWindowManager(
199 const std::string
& active_user_id
,
200 chrome::MultiUserWindowManager::MultiProfileMode mode
);
202 void SwitchActiveUser(const std::string
& active_user_id
);
204 chrome::MultiUserWindowManagerChromeOS
* multi_user_window_manager() {
205 return multi_user_window_manager_
;
209 chrome::MultiUserWindowManagerChromeOS
* multi_user_window_manager_
;
210 ash::test::TestSessionStateDelegate
* session_state_delegate_
;
212 DISALLOW_COPY_AND_ASSIGN(WallpaperPrivateApiMultiUserUnittest
);
215 void WallpaperPrivateApiMultiUserUnittest::SetUp() {
216 AshTestBase::SetUp();
217 WallpaperManager::Initialize();
218 session_state_delegate_
=
219 static_cast<ash::test::TestSessionStateDelegate
*> (
220 ash::Shell::GetInstance()->session_state_delegate());
221 fake_user_manager()->AddUser(kTestAccount1
);
222 fake_user_manager()->AddUser(kTestAccount2
);
225 void WallpaperPrivateApiMultiUserUnittest::TearDown() {
226 chrome::MultiUserWindowManager::DeleteInstance();
227 AshTestBase::TearDown();
228 WallpaperManager::Shutdown();
231 void WallpaperPrivateApiMultiUserUnittest::SetUpMultiUserWindowManager(
232 const std::string
& active_user_id
,
233 chrome::MultiUserWindowManager::MultiProfileMode mode
) {
234 multi_user_window_manager_
=
235 new chrome::MultiUserWindowManagerChromeOS(active_user_id
);
236 multi_user_window_manager_
->Init();
237 chrome::MultiUserWindowManager::SetInstanceForTest(
238 multi_user_window_manager_
, mode
);
239 // We do not want animations while the test is going on.
240 multi_user_window_manager_
->SetAnimationSpeedForTest(
241 chrome::MultiUserWindowManagerChromeOS::ANIMATION_SPEED_DISABLED
);
242 EXPECT_TRUE(multi_user_window_manager_
);
245 void WallpaperPrivateApiMultiUserUnittest::SwitchActiveUser(
246 const std::string
& active_user_id
) {
247 fake_user_manager()->SwitchActiveUser(active_user_id
);
248 multi_user_window_manager_
->ActiveUserChanged(active_user_id
);
251 // In multi profile mode, user may open wallpaper picker in one profile and
252 // then switch to a different profile and open another wallpaper picker
253 // without closing the first one.
254 TEST_F(WallpaperPrivateApiMultiUserUnittest
, HideAndRestoreWindowsTwoUsers
) {
255 SetUpMultiUserWindowManager(kTestAccount1
,
256 chrome::MultiUserWindowManager::MULTI_PROFILE_MODE_SEPARATED
);
258 scoped_ptr
<aura::Window
> window4(CreateTestWindowInShellWithId(4));
259 scoped_ptr
<aura::Window
> window3(CreateTestWindowInShellWithId(3));
260 scoped_ptr
<aura::Window
> window2(CreateTestWindowInShellWithId(2));
261 scoped_ptr
<aura::Window
> window1(CreateTestWindowInShellWithId(1));
262 scoped_ptr
<aura::Window
> window0(CreateTestWindowInShellWithId(0));
264 ash::wm::WindowState
* window0_state
= ash::wm::GetWindowState(window0
.get());
265 ash::wm::WindowState
* window1_state
= ash::wm::GetWindowState(window1
.get());
266 ash::wm::WindowState
* window2_state
= ash::wm::GetWindowState(window2
.get());
267 ash::wm::WindowState
* window3_state
= ash::wm::GetWindowState(window3
.get());
268 ash::wm::WindowState
* window4_state
= ash::wm::GetWindowState(window4
.get());
270 multi_user_window_manager()->SetWindowOwner(window0
.get(), kTestAccount1
);
271 multi_user_window_manager()->SetWindowOwner(window1
.get(), kTestAccount1
);
273 // Set some windows to an inactive owner.
274 multi_user_window_manager()->SetWindowOwner(window2
.get(), kTestAccount2
);
275 multi_user_window_manager()->SetWindowOwner(window3
.get(), kTestAccount2
);
276 multi_user_window_manager()->SetWindowOwner(window4
.get(), kTestAccount2
);
278 EXPECT_FALSE(window0_state
->IsMinimized());
279 EXPECT_FALSE(window1_state
->IsMinimized());
280 EXPECT_FALSE(window2_state
->IsMinimized());
281 EXPECT_FALSE(window3_state
->IsMinimized());
282 EXPECT_FALSE(window4_state
->IsMinimized());
284 // We then activate window 0 (i.e. wallpaper picker) and call the minimize
286 window0_state
->Activate();
287 EXPECT_TRUE(window0_state
->IsActive());
288 scoped_refptr
<TestMinimizeFunction
> minimize_function_0(
289 new TestMinimizeFunction());
290 EXPECT_TRUE(minimize_function_0
->RunAsync());
292 // All windows except window 0 should be minimized.
293 EXPECT_FALSE(window0_state
->IsMinimized());
294 EXPECT_TRUE(window1_state
->IsMinimized());
296 // All windows that belong to inactive user should not be affected.
297 EXPECT_FALSE(window2_state
->IsMinimized());
298 EXPECT_FALSE(window3_state
->IsMinimized());
299 EXPECT_FALSE(window4_state
->IsMinimized());
301 // Activate kTestAccount2. kTestAccount1 becomes inactive user.
302 SwitchActiveUser(kTestAccount2
);
304 window2_state
->Activate();
305 EXPECT_TRUE(window2_state
->IsActive());
306 scoped_refptr
<TestMinimizeFunction
> minimize_function_1(
307 new TestMinimizeFunction());
308 EXPECT_TRUE(minimize_function_1
->RunAsync());
310 // All windows except window 2 should be minimized.
311 EXPECT_FALSE(window2_state
->IsMinimized());
312 EXPECT_TRUE(window3_state
->IsMinimized());
313 EXPECT_TRUE(window4_state
->IsMinimized());
315 // All windows that belong to inactive user should not be affected.
316 EXPECT_FALSE(window0_state
->IsMinimized());
317 EXPECT_TRUE(window1_state
->IsMinimized());
319 // Destroy window 4. Nothing should happen to other windows.
320 window4_state
->Unminimize();
323 EXPECT_FALSE(window2_state
->IsMinimized());
324 EXPECT_TRUE(window3_state
->IsMinimized());
325 EXPECT_FALSE(window0_state
->IsMinimized());
326 EXPECT_TRUE(window1_state
->IsMinimized());
328 // Then we destroy window 2 and call the restore function.
330 scoped_refptr
<TestRestoreFunction
> restore_function_0(
331 new TestRestoreFunction());
332 EXPECT_TRUE(restore_function_0
->RunAsync());
334 EXPECT_FALSE(window3_state
->IsMinimized());
336 // All windows that belong to inactive user should not be affected.
337 EXPECT_FALSE(window0_state
->IsMinimized());
338 EXPECT_TRUE(window1_state
->IsMinimized());
340 SwitchActiveUser(kTestAccount1
);
342 // Then we destroy window 0 and call the restore function.
344 scoped_refptr
<TestRestoreFunction
> restore_function_1(
345 new TestRestoreFunction());
346 EXPECT_TRUE(restore_function_1
->RunAsync());
348 EXPECT_FALSE(window1_state
->IsMinimized());
349 EXPECT_FALSE(window3_state
->IsMinimized());
352 // In multi profile mode, user may teleport windows. Teleported window should
353 // also be minimized when open wallpaper picker.
354 TEST_F(WallpaperPrivateApiMultiUserUnittest
, HideTeleportedWindow
) {
355 SetUpMultiUserWindowManager(kTestAccount1
,
356 chrome::MultiUserWindowManager::MULTI_PROFILE_MODE_MIXED
);
358 scoped_ptr
<aura::Window
> window3(CreateTestWindowInShellWithId(3));
359 scoped_ptr
<aura::Window
> window2(CreateTestWindowInShellWithId(2));
360 scoped_ptr
<aura::Window
> window1(CreateTestWindowInShellWithId(1));
361 scoped_ptr
<aura::Window
> window0(CreateTestWindowInShellWithId(0));
363 ash::wm::WindowState
* window0_state
= ash::wm::GetWindowState(window0
.get());
364 ash::wm::WindowState
* window1_state
= ash::wm::GetWindowState(window1
.get());
365 ash::wm::WindowState
* window2_state
= ash::wm::GetWindowState(window2
.get());
366 ash::wm::WindowState
* window3_state
= ash::wm::GetWindowState(window3
.get());
368 multi_user_window_manager()->SetWindowOwner(window0
.get(), kTestAccount1
);
369 multi_user_window_manager()->SetWindowOwner(window1
.get(), kTestAccount1
);
371 // Set some windows to an inactive owner.
372 multi_user_window_manager()->SetWindowOwner(window2
.get(), kTestAccount2
);
373 multi_user_window_manager()->SetWindowOwner(window3
.get(), kTestAccount2
);
375 // Teleport window2 to kTestAccount1.
376 multi_user_window_manager()->ShowWindowForUser(window2
.get(), kTestAccount1
);
378 // Initial window state. All windows shouldn't be minimized.
379 EXPECT_FALSE(window0_state
->IsMinimized());
380 EXPECT_FALSE(window1_state
->IsMinimized());
381 EXPECT_FALSE(window2_state
->IsMinimized());
382 EXPECT_FALSE(window3_state
->IsMinimized());
384 // We then activate window 0 (i.e. wallpaper picker) and call the minimize
386 window0_state
->Activate();
387 EXPECT_TRUE(window0_state
->IsActive());
388 scoped_refptr
<TestMinimizeFunction
> minimize_function_0(
389 new TestMinimizeFunction());
390 EXPECT_TRUE(minimize_function_0
->RunAsync());
392 // All windows except window 0 should be minimized.
393 EXPECT_FALSE(window0_state
->IsMinimized());
394 EXPECT_TRUE(window1_state
->IsMinimized());
396 // Teleported window should also be minimized.
397 EXPECT_TRUE(window2_state
->IsMinimized());
398 // Other window should remain the same.
399 EXPECT_FALSE(window3_state
->IsMinimized());
401 // Then we destroy window 0 and call the restore function.
403 scoped_refptr
<TestRestoreFunction
> restore_function_1(
404 new TestRestoreFunction());
405 EXPECT_TRUE(restore_function_1
->RunAsync());
407 EXPECT_FALSE(window1_state
->IsMinimized());
408 EXPECT_FALSE(window2_state
->IsMinimized());
409 EXPECT_FALSE(window3_state
->IsMinimized());
412 } // namespace chromeos