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/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 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 // We do not want animations while the test is going on.
233 multi_user_window_manager_->SetAnimationSpeedForTest(
234 chrome::MultiUserWindowManagerChromeOS::ANIMATION_SPEED_DISABLED);
235 EXPECT_TRUE(multi_user_window_manager_);
238 void WallpaperPrivateApiMultiUserUnittest::SwitchActiveUser(
239 const std::string& active_user_id) {
240 fake_user_manager()->SwitchActiveUser(active_user_id);
241 multi_user_window_manager_->ActiveUserChanged(active_user_id);
244 // In multi profile mode, user may open wallpaper picker in one profile and
245 // then switch to a different profile and open another wallpaper picker
246 // without closing the first one.
247 TEST_F(WallpaperPrivateApiMultiUserUnittest, HideAndRestoreWindowsTwoUsers) {
248 SetUpMultiUserWindowManager(kTestAccount1,
249 chrome::MultiUserWindowManager::MULTI_PROFILE_MODE_SEPARATED);
251 scoped_ptr<aura::Window> window4(CreateTestWindowInShellWithId(4));
252 scoped_ptr<aura::Window> window3(CreateTestWindowInShellWithId(3));
253 scoped_ptr<aura::Window> window2(CreateTestWindowInShellWithId(2));
254 scoped_ptr<aura::Window> window1(CreateTestWindowInShellWithId(1));
255 scoped_ptr<aura::Window> window0(CreateTestWindowInShellWithId(0));
257 ash::wm::WindowState* window0_state = ash::wm::GetWindowState(window0.get());
258 ash::wm::WindowState* window1_state = ash::wm::GetWindowState(window1.get());
259 ash::wm::WindowState* window2_state = ash::wm::GetWindowState(window2.get());
260 ash::wm::WindowState* window3_state = ash::wm::GetWindowState(window3.get());
261 ash::wm::WindowState* window4_state = ash::wm::GetWindowState(window4.get());
263 multi_user_window_manager()->SetWindowOwner(window0.get(), kTestAccount1);
264 multi_user_window_manager()->SetWindowOwner(window1.get(), kTestAccount1);
266 // Set some windows to an inactive owner.
267 multi_user_window_manager()->SetWindowOwner(window2.get(), kTestAccount2);
268 multi_user_window_manager()->SetWindowOwner(window3.get(), kTestAccount2);
269 multi_user_window_manager()->SetWindowOwner(window4.get(), kTestAccount2);
271 EXPECT_FALSE(window0_state->IsMinimized());
272 EXPECT_FALSE(window1_state->IsMinimized());
273 EXPECT_FALSE(window2_state->IsMinimized());
274 EXPECT_FALSE(window3_state->IsMinimized());
275 EXPECT_FALSE(window4_state->IsMinimized());
277 // We then activate window 0 (i.e. wallpaper picker) and call the minimize
279 window0_state->Activate();
280 EXPECT_TRUE(window0_state->IsActive());
281 scoped_refptr<TestMinimizeFunction> minimize_function_0(
282 new TestMinimizeFunction());
283 EXPECT_TRUE(minimize_function_0->RunAsync());
285 // All windows except window 0 should be minimized.
286 EXPECT_FALSE(window0_state->IsMinimized());
287 EXPECT_TRUE(window1_state->IsMinimized());
289 // All windows that belong to inactive user should not be affected.
290 EXPECT_FALSE(window2_state->IsMinimized());
291 EXPECT_FALSE(window3_state->IsMinimized());
292 EXPECT_FALSE(window4_state->IsMinimized());
294 // Activate kTestAccount2. kTestAccount1 becomes inactive user.
295 SwitchActiveUser(kTestAccount2);
297 window2_state->Activate();
298 EXPECT_TRUE(window2_state->IsActive());
299 scoped_refptr<TestMinimizeFunction> minimize_function_1(
300 new TestMinimizeFunction());
301 EXPECT_TRUE(minimize_function_1->RunAsync());
303 // All windows except window 2 should be minimized.
304 EXPECT_FALSE(window2_state->IsMinimized());
305 EXPECT_TRUE(window3_state->IsMinimized());
306 EXPECT_TRUE(window4_state->IsMinimized());
308 // All windows that belong to inactive user should not be affected.
309 EXPECT_FALSE(window0_state->IsMinimized());
310 EXPECT_TRUE(window1_state->IsMinimized());
312 // Destroy window 4. Nothing should happen to other windows.
313 window4_state->Unminimize();
316 EXPECT_FALSE(window2_state->IsMinimized());
317 EXPECT_TRUE(window3_state->IsMinimized());
318 EXPECT_FALSE(window0_state->IsMinimized());
319 EXPECT_TRUE(window1_state->IsMinimized());
321 // Then we destroy window 2 and call the restore function.
323 scoped_refptr<TestRestoreFunction> restore_function_0(
324 new TestRestoreFunction());
325 EXPECT_TRUE(restore_function_0->RunAsync());
327 EXPECT_FALSE(window3_state->IsMinimized());
329 // All windows that belong to inactive user should not be affected.
330 EXPECT_FALSE(window0_state->IsMinimized());
331 EXPECT_TRUE(window1_state->IsMinimized());
333 SwitchActiveUser(kTestAccount1);
335 // Then we destroy window 0 and call the restore function.
337 scoped_refptr<TestRestoreFunction> restore_function_1(
338 new TestRestoreFunction());
339 EXPECT_TRUE(restore_function_1->RunAsync());
341 EXPECT_FALSE(window1_state->IsMinimized());
342 EXPECT_FALSE(window3_state->IsMinimized());
345 // In multi profile mode, user may teleport windows. Teleported window should
346 // also be minimized when open wallpaper picker.
347 TEST_F(WallpaperPrivateApiMultiUserUnittest, HideTeleportedWindow) {
348 SetUpMultiUserWindowManager(kTestAccount1,
349 chrome::MultiUserWindowManager::MULTI_PROFILE_MODE_MIXED);
351 scoped_ptr<aura::Window> window3(CreateTestWindowInShellWithId(3));
352 scoped_ptr<aura::Window> window2(CreateTestWindowInShellWithId(2));
353 scoped_ptr<aura::Window> window1(CreateTestWindowInShellWithId(1));
354 scoped_ptr<aura::Window> window0(CreateTestWindowInShellWithId(0));
356 ash::wm::WindowState* window0_state = ash::wm::GetWindowState(window0.get());
357 ash::wm::WindowState* window1_state = ash::wm::GetWindowState(window1.get());
358 ash::wm::WindowState* window2_state = ash::wm::GetWindowState(window2.get());
359 ash::wm::WindowState* window3_state = ash::wm::GetWindowState(window3.get());
361 multi_user_window_manager()->SetWindowOwner(window0.get(), kTestAccount1);
362 multi_user_window_manager()->SetWindowOwner(window1.get(), kTestAccount1);
364 // Set some windows to an inactive owner.
365 multi_user_window_manager()->SetWindowOwner(window2.get(), kTestAccount2);
366 multi_user_window_manager()->SetWindowOwner(window3.get(), kTestAccount2);
368 // Teleport window2 to kTestAccount1.
369 multi_user_window_manager()->ShowWindowForUser(window2.get(), kTestAccount1);
371 // Initial window state. All windows shouldn't be minimized.
372 EXPECT_FALSE(window0_state->IsMinimized());
373 EXPECT_FALSE(window1_state->IsMinimized());
374 EXPECT_FALSE(window2_state->IsMinimized());
375 EXPECT_FALSE(window3_state->IsMinimized());
377 // We then activate window 0 (i.e. wallpaper picker) and call the minimize
379 window0_state->Activate();
380 EXPECT_TRUE(window0_state->IsActive());
381 scoped_refptr<TestMinimizeFunction> minimize_function_0(
382 new TestMinimizeFunction());
383 EXPECT_TRUE(minimize_function_0->RunAsync());
385 // All windows except window 0 should be minimized.
386 EXPECT_FALSE(window0_state->IsMinimized());
387 EXPECT_TRUE(window1_state->IsMinimized());
389 // Teleported window should also be minimized.
390 EXPECT_TRUE(window2_state->IsMinimized());
391 // Other window should remain the same.
392 EXPECT_FALSE(window3_state->IsMinimized());
394 // Then we destroy window 0 and call the restore function.
396 scoped_refptr<TestRestoreFunction> restore_function_1(
397 new TestRestoreFunction());
398 EXPECT_TRUE(restore_function_1->RunAsync());
400 EXPECT_FALSE(window1_state->IsMinimized());
401 EXPECT_FALSE(window2_state->IsMinimized());
402 EXPECT_FALSE(window3_state->IsMinimized());
405 } // namespace chromeos