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/fake_user_manager.h"
14 #include "chrome/browser/ui/ash/multi_user/multi_user_window_manager.h"
15 #include "chrome/browser/ui/ash/multi_user/multi_user_window_manager_chromeos.h"
16 #include "chrome/common/chrome_switches.h"
17 #include "ui/aura/root_window.h"
18 #include "ui/aura/test/test_windows.h"
19 #include "ui/aura/window.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 RunImpl() OVERRIDE
{
55 return WallpaperPrivateMinimizeInactiveWindowsFunction::RunImpl();
59 virtual ~TestMinimizeFunction() {}
62 class TestRestoreFunction
63 : public WallpaperPrivateRestoreMinimizedWindowsFunction
{
65 TestRestoreFunction() {}
67 virtual bool RunImpl() OVERRIDE
{
68 return WallpaperPrivateRestoreMinimizedWindowsFunction::RunImpl();
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
->RunImpl());
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
->RunImpl());
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
->RunImpl());
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
->RunImpl());
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
->RunImpl());
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
->RunImpl());
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 CommandLine::ForCurrentProcess()->AppendSwitch(switches::kMultiProfiles
);
212 AshTestBase::SetUp();
213 session_state_delegate_
=
214 static_cast<ash::test::TestSessionStateDelegate
*> (
215 ash::Shell::GetInstance()->session_state_delegate());
216 fake_user_manager()->AddUser(kTestAccount1
);
217 fake_user_manager()->AddUser(kTestAccount2
);
220 void WallpaperPrivateApiMultiUserUnittest::TearDown() {
221 chrome::MultiUserWindowManager::DeleteInstance();
222 AshTestBase::TearDown();
225 void WallpaperPrivateApiMultiUserUnittest::SetUpMultiUserWindowManager(
226 const std::string
& active_user_id
,
227 chrome::MultiUserWindowManager::MultiProfileMode mode
) {
228 multi_user_window_manager_
=
229 new chrome::MultiUserWindowManagerChromeOS(active_user_id
);
230 chrome::MultiUserWindowManager::SetInstanceForTest(
231 multi_user_window_manager_
, mode
);
232 EXPECT_TRUE(multi_user_window_manager_
);
235 void WallpaperPrivateApiMultiUserUnittest::SwitchActiveUser(
236 const std::string
& active_user_id
) {
237 fake_user_manager()->SwitchActiveUser(active_user_id
);
238 multi_user_window_manager_
->ActiveUserChanged(active_user_id
);
241 // In multi profile mode, user may open wallpaper picker in one profile and
242 // then switch to a different profile and open another wallpaper picker
243 // without closing the first one.
244 TEST_F(WallpaperPrivateApiMultiUserUnittest
, HideAndRestoreWindowsTwoUsers
) {
245 SetUpMultiUserWindowManager(kTestAccount1
,
246 chrome::MultiUserWindowManager::MULTI_PROFILE_MODE_SEPARATED
);
248 scoped_ptr
<aura::Window
> window4(CreateTestWindowInShellWithId(4));
249 scoped_ptr
<aura::Window
> window3(CreateTestWindowInShellWithId(3));
250 scoped_ptr
<aura::Window
> window2(CreateTestWindowInShellWithId(2));
251 scoped_ptr
<aura::Window
> window1(CreateTestWindowInShellWithId(1));
252 scoped_ptr
<aura::Window
> window0(CreateTestWindowInShellWithId(0));
254 ash::wm::WindowState
* window0_state
= ash::wm::GetWindowState(window0
.get());
255 ash::wm::WindowState
* window1_state
= ash::wm::GetWindowState(window1
.get());
256 ash::wm::WindowState
* window2_state
= ash::wm::GetWindowState(window2
.get());
257 ash::wm::WindowState
* window3_state
= ash::wm::GetWindowState(window3
.get());
258 ash::wm::WindowState
* window4_state
= ash::wm::GetWindowState(window4
.get());
260 multi_user_window_manager()->SetWindowOwner(window0
.get(), kTestAccount1
);
261 multi_user_window_manager()->SetWindowOwner(window1
.get(), kTestAccount1
);
263 // Set some windows to an inactive owner.
264 multi_user_window_manager()->SetWindowOwner(window2
.get(), kTestAccount2
);
265 multi_user_window_manager()->SetWindowOwner(window3
.get(), kTestAccount2
);
266 multi_user_window_manager()->SetWindowOwner(window4
.get(), kTestAccount2
);
268 EXPECT_FALSE(window0_state
->IsMinimized());
269 EXPECT_FALSE(window1_state
->IsMinimized());
270 EXPECT_FALSE(window2_state
->IsMinimized());
271 EXPECT_FALSE(window3_state
->IsMinimized());
272 EXPECT_FALSE(window4_state
->IsMinimized());
274 // We then activate window 0 (i.e. wallpaper picker) and call the minimize
276 window0_state
->Activate();
277 EXPECT_TRUE(window0_state
->IsActive());
278 scoped_refptr
<TestMinimizeFunction
> minimize_function_0(
279 new TestMinimizeFunction());
280 EXPECT_TRUE(minimize_function_0
->RunImpl());
282 // All windows except window 0 should be minimized.
283 EXPECT_FALSE(window0_state
->IsMinimized());
284 EXPECT_TRUE(window1_state
->IsMinimized());
286 // All windows that belong to inactive user should not be affected.
287 EXPECT_FALSE(window2_state
->IsMinimized());
288 EXPECT_FALSE(window3_state
->IsMinimized());
289 EXPECT_FALSE(window4_state
->IsMinimized());
291 // Activate kTestAccount2. kTestAccount1 becomes inactive user.
292 SwitchActiveUser(kTestAccount2
);
294 window2_state
->Activate();
295 EXPECT_TRUE(window2_state
->IsActive());
296 scoped_refptr
<TestMinimizeFunction
> minimize_function_1(
297 new TestMinimizeFunction());
298 EXPECT_TRUE(minimize_function_1
->RunImpl());
300 // All windows except window 2 should be minimized.
301 EXPECT_FALSE(window2_state
->IsMinimized());
302 EXPECT_TRUE(window3_state
->IsMinimized());
303 EXPECT_TRUE(window4_state
->IsMinimized());
305 // All windows that belong to inactive user should not be affected.
306 EXPECT_FALSE(window0_state
->IsMinimized());
307 EXPECT_TRUE(window1_state
->IsMinimized());
309 // Destroy window 4. Nothing should happen to other windows.
310 window4_state
->Unminimize();
313 EXPECT_FALSE(window2_state
->IsMinimized());
314 EXPECT_TRUE(window3_state
->IsMinimized());
315 EXPECT_FALSE(window0_state
->IsMinimized());
316 EXPECT_TRUE(window1_state
->IsMinimized());
318 // Then we destroy window 2 and call the restore function.
320 scoped_refptr
<TestRestoreFunction
> restore_function_0(
321 new TestRestoreFunction());
322 EXPECT_TRUE(restore_function_0
->RunImpl());
324 EXPECT_FALSE(window3_state
->IsMinimized());
326 // All windows that belong to inactive user should not be affected.
327 EXPECT_FALSE(window0_state
->IsMinimized());
328 EXPECT_TRUE(window1_state
->IsMinimized());
330 SwitchActiveUser(kTestAccount1
);
332 // Then we destroy window 0 and call the restore function.
334 scoped_refptr
<TestRestoreFunction
> restore_function_1(
335 new TestRestoreFunction());
336 EXPECT_TRUE(restore_function_1
->RunImpl());
338 EXPECT_FALSE(window1_state
->IsMinimized());
339 EXPECT_FALSE(window3_state
->IsMinimized());
342 // In multi profile mode, user may teleport windows. Teleported window should
343 // also be minimized when open wallpaper picker.
344 TEST_F(WallpaperPrivateApiMultiUserUnittest
, HideTeleportedWindow
) {
345 SetUpMultiUserWindowManager(kTestAccount1
,
346 chrome::MultiUserWindowManager::MULTI_PROFILE_MODE_MIXED
);
348 scoped_ptr
<aura::Window
> window3(CreateTestWindowInShellWithId(3));
349 scoped_ptr
<aura::Window
> window2(CreateTestWindowInShellWithId(2));
350 scoped_ptr
<aura::Window
> window1(CreateTestWindowInShellWithId(1));
351 scoped_ptr
<aura::Window
> window0(CreateTestWindowInShellWithId(0));
353 ash::wm::WindowState
* window0_state
= ash::wm::GetWindowState(window0
.get());
354 ash::wm::WindowState
* window1_state
= ash::wm::GetWindowState(window1
.get());
355 ash::wm::WindowState
* window2_state
= ash::wm::GetWindowState(window2
.get());
356 ash::wm::WindowState
* window3_state
= ash::wm::GetWindowState(window3
.get());
358 multi_user_window_manager()->SetWindowOwner(window0
.get(), kTestAccount1
);
359 multi_user_window_manager()->SetWindowOwner(window1
.get(), kTestAccount1
);
361 // Set some windows to an inactive owner.
362 multi_user_window_manager()->SetWindowOwner(window2
.get(), kTestAccount2
);
363 multi_user_window_manager()->SetWindowOwner(window3
.get(), kTestAccount2
);
365 // Teleport window2 to kTestAccount1.
366 multi_user_window_manager()->ShowWindowForUser(window2
.get(), kTestAccount1
);
368 // Initial window state. All windows shouldn't be minimized.
369 EXPECT_FALSE(window0_state
->IsMinimized());
370 EXPECT_FALSE(window1_state
->IsMinimized());
371 EXPECT_FALSE(window2_state
->IsMinimized());
372 EXPECT_FALSE(window3_state
->IsMinimized());
374 // We then activate window 0 (i.e. wallpaper picker) and call the minimize
376 window0_state
->Activate();
377 EXPECT_TRUE(window0_state
->IsActive());
378 scoped_refptr
<TestMinimizeFunction
> minimize_function_0(
379 new TestMinimizeFunction());
380 EXPECT_TRUE(minimize_function_0
->RunImpl());
382 // All windows except window 0 should be minimized.
383 EXPECT_FALSE(window0_state
->IsMinimized());
384 EXPECT_TRUE(window1_state
->IsMinimized());
386 // Teleported window should also be minimized.
387 EXPECT_TRUE(window2_state
->IsMinimized());
388 // Other window should remain the same.
389 EXPECT_FALSE(window3_state
->IsMinimized());
391 // Then we destroy window 0 and call the restore function.
393 scoped_refptr
<TestRestoreFunction
> restore_function_1(
394 new TestRestoreFunction());
395 EXPECT_TRUE(restore_function_1
->RunImpl());
397 EXPECT_FALSE(window1_state
->IsMinimized());
398 EXPECT_FALSE(window2_state
->IsMinimized());
399 EXPECT_FALSE(window3_state
->IsMinimized());
402 } // namespace chromeos