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_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"
21 //#include "base/compiler_specific.h"
22 //#include "base/logging.h"
23 //#include "chrome/browser/ui/ash/multi_user/multi_user_window_manager.h"
27 const char* kTestAccount1
= "user1@test.com";
28 const char* kTestAccount2
= "user2@test.com";
30 class WallpaperPrivateApiUnittest
: public ash::test::AshTestBase
{
32 WallpaperPrivateApiUnittest()
33 : fake_user_manager_(new FakeUserManager()),
34 scoped_user_manager_(fake_user_manager_
) {
38 FakeUserManager
* fake_user_manager() {
39 return fake_user_manager_
;
43 FakeUserManager
* fake_user_manager_
;
44 ScopedUserManagerEnabler scoped_user_manager_
;
46 DISALLOW_COPY_AND_ASSIGN(WallpaperPrivateApiUnittest
);
49 class TestMinimizeFunction
50 : public WallpaperPrivateMinimizeInactiveWindowsFunction
{
52 TestMinimizeFunction() {}
54 virtual bool RunAsync() override
{
55 return WallpaperPrivateMinimizeInactiveWindowsFunction::RunAsync();
59 virtual ~TestMinimizeFunction() {}
62 class TestRestoreFunction
63 : public WallpaperPrivateRestoreMinimizedWindowsFunction
{
65 TestRestoreFunction() {}
67 virtual bool RunAsync() override
{
68 return WallpaperPrivateRestoreMinimizedWindowsFunction::RunAsync();
71 virtual ~TestRestoreFunction() {}
76 TEST_F(WallpaperPrivateApiUnittest
, HideAndRestoreWindows
) {
77 fake_user_manager()->AddUser(kTestAccount1
);
78 scoped_ptr
<aura::Window
> window3(CreateTestWindowInShellWithId(3));
79 scoped_ptr
<aura::Window
> window2(CreateTestWindowInShellWithId(2));
80 scoped_ptr
<aura::Window
> window1(CreateTestWindowInShellWithId(1));
81 scoped_ptr
<aura::Window
> window0(CreateTestWindowInShellWithId(0));
83 ash::wm::WindowState
* window0_state
= ash::wm::GetWindowState(window0
.get());
84 ash::wm::WindowState
* window1_state
= ash::wm::GetWindowState(window1
.get());
85 ash::wm::WindowState
* window2_state
= ash::wm::GetWindowState(window2
.get());
86 ash::wm::WindowState
* window3_state
= ash::wm::GetWindowState(window3
.get());
88 window3_state
->Minimize();
89 window1_state
->Maximize();
91 // Window 3 starts minimized, window 1 starts maximized.
92 EXPECT_FALSE(window0_state
->IsMinimized());
93 EXPECT_FALSE(window1_state
->IsMinimized());
94 EXPECT_FALSE(window2_state
->IsMinimized());
95 EXPECT_TRUE(window3_state
->IsMinimized());
97 // We then activate window 0 (i.e. wallpaper picker) and call the minimize
99 window0_state
->Activate();
100 EXPECT_TRUE(window0_state
->IsActive());
101 scoped_refptr
<TestMinimizeFunction
> minimize_function(
102 new TestMinimizeFunction());
103 EXPECT_TRUE(minimize_function
->RunAsync());
105 // All windows except window 0 should be minimized.
106 EXPECT_FALSE(window0_state
->IsMinimized());
107 EXPECT_TRUE(window1_state
->IsMinimized());
108 EXPECT_TRUE(window2_state
->IsMinimized());
109 EXPECT_TRUE(window3_state
->IsMinimized());
111 // Then we destroy window 0 and call the restore function.
113 scoped_refptr
<TestRestoreFunction
> restore_function(
114 new TestRestoreFunction());
115 EXPECT_TRUE(restore_function
->RunAsync());
117 // Windows 1 and 2 should no longer be minimized. Window 1 should again
118 // be maximized. Window 3 should still be minimized.
119 EXPECT_TRUE(window1_state
->IsMaximized());
120 EXPECT_FALSE(window2_state
->IsMinimized());
121 EXPECT_TRUE(window3_state
->IsMinimized());
124 // Test for multiple calls to |MinimizeInactiveWindows| before call
126 // 1. If all window hasn't change their states, the following calls are noops.
127 // 2. If some windows are manually unminimized, the following call will minimize
128 // all the unminimized windows.
129 TEST_F(WallpaperPrivateApiUnittest
, HideAndManualUnminimizeWindows
) {
130 fake_user_manager()->AddUser(kTestAccount1
);
131 scoped_ptr
<aura::Window
> window1(CreateTestWindowInShellWithId(1));
132 scoped_ptr
<aura::Window
> window0(CreateTestWindowInShellWithId(0));
134 ash::wm::WindowState
* window0_state
= ash::wm::GetWindowState(window0
.get());
135 ash::wm::WindowState
* window1_state
= ash::wm::GetWindowState(window1
.get());
137 // We then activate window 0 (i.e. wallpaper picker) and call the minimize
139 window0_state
->Activate();
140 EXPECT_TRUE(window0_state
->IsActive());
141 scoped_refptr
<TestMinimizeFunction
> minimize_function_0(
142 new TestMinimizeFunction());
143 EXPECT_TRUE(minimize_function_0
->RunAsync());
145 // All windows except window 0 should be minimized.
146 EXPECT_FALSE(window0_state
->IsMinimized());
147 EXPECT_TRUE(window1_state
->IsMinimized());
149 // Calls minimize function again should be an noop if window state didn't
151 scoped_refptr
<TestMinimizeFunction
> minimize_function_1(
152 new TestMinimizeFunction());
153 EXPECT_TRUE(minimize_function_1
->RunAsync());
155 // All windows except window 0 should be minimized.
156 EXPECT_FALSE(window0_state
->IsMinimized());
157 EXPECT_TRUE(window1_state
->IsMinimized());
159 // Manually unminimize window 1.
160 window1_state
->Unminimize();
161 EXPECT_FALSE(window1_state
->IsMinimized());
162 window0_state
->Activate();
164 scoped_refptr
<TestMinimizeFunction
> minimize_function_2(
165 new TestMinimizeFunction());
166 EXPECT_TRUE(minimize_function_2
->RunAsync());
168 // Window 1 should be minimized again.
169 EXPECT_FALSE(window0_state
->IsMinimized());
170 EXPECT_TRUE(window1_state
->IsMinimized());
172 // Then we destroy window 0 and call the restore function.
174 scoped_refptr
<TestRestoreFunction
> restore_function(
175 new TestRestoreFunction());
176 EXPECT_TRUE(restore_function
->RunAsync());
178 // Windows 1 should no longer be minimized.
179 EXPECT_FALSE(window1_state
->IsMinimized());
182 class WallpaperPrivateApiMultiUserUnittest
183 : public WallpaperPrivateApiUnittest
{
185 WallpaperPrivateApiMultiUserUnittest()
186 : multi_user_window_manager_(NULL
),
187 session_state_delegate_(NULL
) {}
189 virtual void SetUp() override
;
190 virtual void TearDown() override
;
193 void SetUpMultiUserWindowManager(
194 const std::string
& active_user_id
,
195 chrome::MultiUserWindowManager::MultiProfileMode mode
);
197 void SwitchActiveUser(const std::string
& active_user_id
);
199 chrome::MultiUserWindowManagerChromeOS
* multi_user_window_manager() {
200 return multi_user_window_manager_
;
204 chrome::MultiUserWindowManagerChromeOS
* multi_user_window_manager_
;
205 ash::test::TestSessionStateDelegate
* session_state_delegate_
;
207 DISALLOW_COPY_AND_ASSIGN(WallpaperPrivateApiMultiUserUnittest
);
210 void WallpaperPrivateApiMultiUserUnittest::SetUp() {
211 AshTestBase::SetUp();
212 session_state_delegate_
=
213 static_cast<ash::test::TestSessionStateDelegate
*> (
214 ash::Shell::GetInstance()->session_state_delegate());
215 fake_user_manager()->AddUser(kTestAccount1
);
216 fake_user_manager()->AddUser(kTestAccount2
);
219 void WallpaperPrivateApiMultiUserUnittest::TearDown() {
220 chrome::MultiUserWindowManager::DeleteInstance();
221 AshTestBase::TearDown();
224 void WallpaperPrivateApiMultiUserUnittest::SetUpMultiUserWindowManager(
225 const std::string
& active_user_id
,
226 chrome::MultiUserWindowManager::MultiProfileMode mode
) {
227 multi_user_window_manager_
=
228 new chrome::MultiUserWindowManagerChromeOS(active_user_id
);
229 chrome::MultiUserWindowManager::SetInstanceForTest(
230 multi_user_window_manager_
, mode
);
231 // We do not want animations while the test is going on.
232 multi_user_window_manager_
->SetAnimationSpeedForTest(
233 chrome::MultiUserWindowManagerChromeOS::ANIMATION_SPEED_DISABLED
);
234 EXPECT_TRUE(multi_user_window_manager_
);
237 void WallpaperPrivateApiMultiUserUnittest::SwitchActiveUser(
238 const std::string
& active_user_id
) {
239 fake_user_manager()->SwitchActiveUser(active_user_id
);
240 multi_user_window_manager_
->ActiveUserChanged(active_user_id
);
243 // In multi profile mode, user may open wallpaper picker in one profile and
244 // then switch to a different profile and open another wallpaper picker
245 // without closing the first one.
246 TEST_F(WallpaperPrivateApiMultiUserUnittest
, HideAndRestoreWindowsTwoUsers
) {
247 SetUpMultiUserWindowManager(kTestAccount1
,
248 chrome::MultiUserWindowManager::MULTI_PROFILE_MODE_SEPARATED
);
250 scoped_ptr
<aura::Window
> window4(CreateTestWindowInShellWithId(4));
251 scoped_ptr
<aura::Window
> window3(CreateTestWindowInShellWithId(3));
252 scoped_ptr
<aura::Window
> window2(CreateTestWindowInShellWithId(2));
253 scoped_ptr
<aura::Window
> window1(CreateTestWindowInShellWithId(1));
254 scoped_ptr
<aura::Window
> window0(CreateTestWindowInShellWithId(0));
256 ash::wm::WindowState
* window0_state
= ash::wm::GetWindowState(window0
.get());
257 ash::wm::WindowState
* window1_state
= ash::wm::GetWindowState(window1
.get());
258 ash::wm::WindowState
* window2_state
= ash::wm::GetWindowState(window2
.get());
259 ash::wm::WindowState
* window3_state
= ash::wm::GetWindowState(window3
.get());
260 ash::wm::WindowState
* window4_state
= ash::wm::GetWindowState(window4
.get());
262 multi_user_window_manager()->SetWindowOwner(window0
.get(), kTestAccount1
);
263 multi_user_window_manager()->SetWindowOwner(window1
.get(), kTestAccount1
);
265 // Set some windows to an inactive owner.
266 multi_user_window_manager()->SetWindowOwner(window2
.get(), kTestAccount2
);
267 multi_user_window_manager()->SetWindowOwner(window3
.get(), kTestAccount2
);
268 multi_user_window_manager()->SetWindowOwner(window4
.get(), kTestAccount2
);
270 EXPECT_FALSE(window0_state
->IsMinimized());
271 EXPECT_FALSE(window1_state
->IsMinimized());
272 EXPECT_FALSE(window2_state
->IsMinimized());
273 EXPECT_FALSE(window3_state
->IsMinimized());
274 EXPECT_FALSE(window4_state
->IsMinimized());
276 // We then activate window 0 (i.e. wallpaper picker) and call the minimize
278 window0_state
->Activate();
279 EXPECT_TRUE(window0_state
->IsActive());
280 scoped_refptr
<TestMinimizeFunction
> minimize_function_0(
281 new TestMinimizeFunction());
282 EXPECT_TRUE(minimize_function_0
->RunAsync());
284 // All windows except window 0 should be minimized.
285 EXPECT_FALSE(window0_state
->IsMinimized());
286 EXPECT_TRUE(window1_state
->IsMinimized());
288 // All windows that belong to inactive user should not be affected.
289 EXPECT_FALSE(window2_state
->IsMinimized());
290 EXPECT_FALSE(window3_state
->IsMinimized());
291 EXPECT_FALSE(window4_state
->IsMinimized());
293 // Activate kTestAccount2. kTestAccount1 becomes inactive user.
294 SwitchActiveUser(kTestAccount2
);
296 window2_state
->Activate();
297 EXPECT_TRUE(window2_state
->IsActive());
298 scoped_refptr
<TestMinimizeFunction
> minimize_function_1(
299 new TestMinimizeFunction());
300 EXPECT_TRUE(minimize_function_1
->RunAsync());
302 // All windows except window 2 should be minimized.
303 EXPECT_FALSE(window2_state
->IsMinimized());
304 EXPECT_TRUE(window3_state
->IsMinimized());
305 EXPECT_TRUE(window4_state
->IsMinimized());
307 // All windows that belong to inactive user should not be affected.
308 EXPECT_FALSE(window0_state
->IsMinimized());
309 EXPECT_TRUE(window1_state
->IsMinimized());
311 // Destroy window 4. Nothing should happen to other windows.
312 window4_state
->Unminimize();
315 EXPECT_FALSE(window2_state
->IsMinimized());
316 EXPECT_TRUE(window3_state
->IsMinimized());
317 EXPECT_FALSE(window0_state
->IsMinimized());
318 EXPECT_TRUE(window1_state
->IsMinimized());
320 // Then we destroy window 2 and call the restore function.
322 scoped_refptr
<TestRestoreFunction
> restore_function_0(
323 new TestRestoreFunction());
324 EXPECT_TRUE(restore_function_0
->RunAsync());
326 EXPECT_FALSE(window3_state
->IsMinimized());
328 // All windows that belong to inactive user should not be affected.
329 EXPECT_FALSE(window0_state
->IsMinimized());
330 EXPECT_TRUE(window1_state
->IsMinimized());
332 SwitchActiveUser(kTestAccount1
);
334 // Then we destroy window 0 and call the restore function.
336 scoped_refptr
<TestRestoreFunction
> restore_function_1(
337 new TestRestoreFunction());
338 EXPECT_TRUE(restore_function_1
->RunAsync());
340 EXPECT_FALSE(window1_state
->IsMinimized());
341 EXPECT_FALSE(window3_state
->IsMinimized());
344 // In multi profile mode, user may teleport windows. Teleported window should
345 // also be minimized when open wallpaper picker.
346 TEST_F(WallpaperPrivateApiMultiUserUnittest
, HideTeleportedWindow
) {
347 SetUpMultiUserWindowManager(kTestAccount1
,
348 chrome::MultiUserWindowManager::MULTI_PROFILE_MODE_MIXED
);
350 scoped_ptr
<aura::Window
> window3(CreateTestWindowInShellWithId(3));
351 scoped_ptr
<aura::Window
> window2(CreateTestWindowInShellWithId(2));
352 scoped_ptr
<aura::Window
> window1(CreateTestWindowInShellWithId(1));
353 scoped_ptr
<aura::Window
> window0(CreateTestWindowInShellWithId(0));
355 ash::wm::WindowState
* window0_state
= ash::wm::GetWindowState(window0
.get());
356 ash::wm::WindowState
* window1_state
= ash::wm::GetWindowState(window1
.get());
357 ash::wm::WindowState
* window2_state
= ash::wm::GetWindowState(window2
.get());
358 ash::wm::WindowState
* window3_state
= ash::wm::GetWindowState(window3
.get());
360 multi_user_window_manager()->SetWindowOwner(window0
.get(), kTestAccount1
);
361 multi_user_window_manager()->SetWindowOwner(window1
.get(), kTestAccount1
);
363 // Set some windows to an inactive owner.
364 multi_user_window_manager()->SetWindowOwner(window2
.get(), kTestAccount2
);
365 multi_user_window_manager()->SetWindowOwner(window3
.get(), kTestAccount2
);
367 // Teleport window2 to kTestAccount1.
368 multi_user_window_manager()->ShowWindowForUser(window2
.get(), kTestAccount1
);
370 // Initial window state. All windows shouldn't be minimized.
371 EXPECT_FALSE(window0_state
->IsMinimized());
372 EXPECT_FALSE(window1_state
->IsMinimized());
373 EXPECT_FALSE(window2_state
->IsMinimized());
374 EXPECT_FALSE(window3_state
->IsMinimized());
376 // We then activate window 0 (i.e. wallpaper picker) and call the minimize
378 window0_state
->Activate();
379 EXPECT_TRUE(window0_state
->IsActive());
380 scoped_refptr
<TestMinimizeFunction
> minimize_function_0(
381 new TestMinimizeFunction());
382 EXPECT_TRUE(minimize_function_0
->RunAsync());
384 // All windows except window 0 should be minimized.
385 EXPECT_FALSE(window0_state
->IsMinimized());
386 EXPECT_TRUE(window1_state
->IsMinimized());
388 // Teleported window should also be minimized.
389 EXPECT_TRUE(window2_state
->IsMinimized());
390 // Other window should remain the same.
391 EXPECT_FALSE(window3_state
->IsMinimized());
393 // Then we destroy window 0 and call the restore function.
395 scoped_refptr
<TestRestoreFunction
> restore_function_1(
396 new TestRestoreFunction());
397 EXPECT_TRUE(restore_function_1
->RunAsync());
399 EXPECT_FALSE(window1_state
->IsMinimized());
400 EXPECT_FALSE(window2_state
->IsMinimized());
401 EXPECT_FALSE(window3_state
->IsMinimized());
404 } // namespace chromeos