Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / chromeos / extensions / wallpaper_private_api_unittest.cc
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.
4
5 #include "ash/shell.h"
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"
20
21 //#include "base/compiler_specific.h"
22 //#include "base/logging.h"
23 //#include "chrome/browser/ui/ash/multi_user/multi_user_window_manager.h"
24 namespace chromeos {
25 namespace {
26
27 const char* kTestAccount1 = "user1@test.com";
28 const char* kTestAccount2 = "user2@test.com";
29
30 class WallpaperPrivateApiUnittest : public ash::test::AshTestBase {
31  public:
32   WallpaperPrivateApiUnittest()
33       : fake_user_manager_(new FakeUserManager()),
34         scoped_user_manager_(fake_user_manager_) {
35   }
36
37  protected:
38   FakeUserManager* fake_user_manager() {
39     return fake_user_manager_;
40   }
41
42  private:
43   FakeUserManager* fake_user_manager_;
44   ScopedUserManagerEnabler scoped_user_manager_;
45
46   DISALLOW_COPY_AND_ASSIGN(WallpaperPrivateApiUnittest);
47 };
48
49 class TestMinimizeFunction
50     : public WallpaperPrivateMinimizeInactiveWindowsFunction {
51  public:
52   TestMinimizeFunction() {}
53
54   virtual bool RunAsync() OVERRIDE {
55     return WallpaperPrivateMinimizeInactiveWindowsFunction::RunAsync();
56   }
57
58  protected:
59   virtual ~TestMinimizeFunction() {}
60 };
61
62 class TestRestoreFunction
63     : public WallpaperPrivateRestoreMinimizedWindowsFunction {
64  public:
65   TestRestoreFunction() {}
66
67   virtual bool RunAsync() OVERRIDE {
68     return WallpaperPrivateRestoreMinimizedWindowsFunction::RunAsync();
69   }
70  protected:
71   virtual ~TestRestoreFunction() {}
72 };
73
74 }  // namespace
75
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));
82
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());
87
88   window3_state->Minimize();
89   window1_state->Maximize();
90
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());
96
97   // We then activate window 0 (i.e. wallpaper picker) and call the minimize
98   // function.
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());
104
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());
110
111   // Then we destroy window 0 and call the restore function.
112   window0.reset();
113   scoped_refptr<TestRestoreFunction> restore_function(
114       new TestRestoreFunction());
115   EXPECT_TRUE(restore_function->RunAsync());
116
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());
122 }
123
124 // Test for multiple calls to |MinimizeInactiveWindows| before call
125 // |RestoreWindows|:
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));
133
134   ash::wm::WindowState* window0_state = ash::wm::GetWindowState(window0.get());
135   ash::wm::WindowState* window1_state = ash::wm::GetWindowState(window1.get());
136
137   // We then activate window 0 (i.e. wallpaper picker) and call the minimize
138   // function.
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());
144
145   // All windows except window 0 should be minimized.
146   EXPECT_FALSE(window0_state->IsMinimized());
147   EXPECT_TRUE(window1_state->IsMinimized());
148
149   // Calls minimize function again should be an noop if window state didn't
150   // change.
151   scoped_refptr<TestMinimizeFunction> minimize_function_1(
152       new TestMinimizeFunction());
153   EXPECT_TRUE(minimize_function_1->RunAsync());
154
155   // All windows except window 0 should be minimized.
156   EXPECT_FALSE(window0_state->IsMinimized());
157   EXPECT_TRUE(window1_state->IsMinimized());
158
159   // Manually unminimize window 1.
160   window1_state->Unminimize();
161   EXPECT_FALSE(window1_state->IsMinimized());
162   window0_state->Activate();
163
164   scoped_refptr<TestMinimizeFunction> minimize_function_2(
165       new TestMinimizeFunction());
166   EXPECT_TRUE(minimize_function_2->RunAsync());
167
168   // Window 1 should be minimized again.
169   EXPECT_FALSE(window0_state->IsMinimized());
170   EXPECT_TRUE(window1_state->IsMinimized());
171
172   // Then we destroy window 0 and call the restore function.
173   window0.reset();
174   scoped_refptr<TestRestoreFunction> restore_function(
175       new TestRestoreFunction());
176   EXPECT_TRUE(restore_function->RunAsync());
177
178   // Windows 1 should no longer be minimized.
179   EXPECT_FALSE(window1_state->IsMinimized());
180 }
181
182 class WallpaperPrivateApiMultiUserUnittest
183     : public WallpaperPrivateApiUnittest {
184  public:
185   WallpaperPrivateApiMultiUserUnittest()
186       : multi_user_window_manager_(NULL),
187         session_state_delegate_(NULL) {}
188
189   virtual void SetUp() OVERRIDE;
190   virtual void TearDown() OVERRIDE;
191
192  protected:
193   void SetUpMultiUserWindowManager(
194       const std::string& active_user_id,
195       chrome::MultiUserWindowManager::MultiProfileMode mode);
196
197   void SwitchActiveUser(const std::string& active_user_id);
198
199   chrome::MultiUserWindowManagerChromeOS* multi_user_window_manager() {
200     return multi_user_window_manager_;
201   }
202
203  private:
204   chrome::MultiUserWindowManagerChromeOS* multi_user_window_manager_;
205   ash::test::TestSessionStateDelegate* session_state_delegate_;
206
207   DISALLOW_COPY_AND_ASSIGN(WallpaperPrivateApiMultiUserUnittest);
208 };
209
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);
218 }
219
220 void WallpaperPrivateApiMultiUserUnittest::TearDown() {
221   chrome::MultiUserWindowManager::DeleteInstance();
222   AshTestBase::TearDown();
223 }
224
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_);
236 }
237
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);
242 }
243
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);
250
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));
256
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());
262
263   multi_user_window_manager()->SetWindowOwner(window0.get(), kTestAccount1);
264   multi_user_window_manager()->SetWindowOwner(window1.get(), kTestAccount1);
265
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);
270
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());
276
277   // We then activate window 0 (i.e. wallpaper picker) and call the minimize
278   // function.
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());
284
285   // All windows except window 0 should be minimized.
286   EXPECT_FALSE(window0_state->IsMinimized());
287   EXPECT_TRUE(window1_state->IsMinimized());
288
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());
293
294   // Activate kTestAccount2. kTestAccount1 becomes inactive user.
295   SwitchActiveUser(kTestAccount2);
296
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());
302
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());
307
308   // All windows that belong to inactive user should not be affected.
309   EXPECT_FALSE(window0_state->IsMinimized());
310   EXPECT_TRUE(window1_state->IsMinimized());
311
312   // Destroy window 4. Nothing should happen to other windows.
313   window4_state->Unminimize();
314   window4.reset();
315
316   EXPECT_FALSE(window2_state->IsMinimized());
317   EXPECT_TRUE(window3_state->IsMinimized());
318   EXPECT_FALSE(window0_state->IsMinimized());
319   EXPECT_TRUE(window1_state->IsMinimized());
320
321   // Then we destroy window 2 and call the restore function.
322   window2.reset();
323   scoped_refptr<TestRestoreFunction> restore_function_0(
324       new TestRestoreFunction());
325   EXPECT_TRUE(restore_function_0->RunAsync());
326
327   EXPECT_FALSE(window3_state->IsMinimized());
328
329   // All windows that belong to inactive user should not be affected.
330   EXPECT_FALSE(window0_state->IsMinimized());
331   EXPECT_TRUE(window1_state->IsMinimized());
332
333   SwitchActiveUser(kTestAccount1);
334
335   // Then we destroy window 0 and call the restore function.
336   window0.reset();
337   scoped_refptr<TestRestoreFunction> restore_function_1(
338       new TestRestoreFunction());
339   EXPECT_TRUE(restore_function_1->RunAsync());
340
341   EXPECT_FALSE(window1_state->IsMinimized());
342   EXPECT_FALSE(window3_state->IsMinimized());
343 }
344
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);
350
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));
355
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());
360
361   multi_user_window_manager()->SetWindowOwner(window0.get(), kTestAccount1);
362   multi_user_window_manager()->SetWindowOwner(window1.get(), kTestAccount1);
363
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);
367
368   // Teleport window2 to kTestAccount1.
369   multi_user_window_manager()->ShowWindowForUser(window2.get(), kTestAccount1);
370
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());
376
377   // We then activate window 0 (i.e. wallpaper picker) and call the minimize
378   // function.
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());
384
385   // All windows except window 0 should be minimized.
386   EXPECT_FALSE(window0_state->IsMinimized());
387   EXPECT_TRUE(window1_state->IsMinimized());
388
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());
393
394   // Then we destroy window 0 and call the restore function.
395   window0.reset();
396   scoped_refptr<TestRestoreFunction> restore_function_1(
397       new TestRestoreFunction());
398   EXPECT_TRUE(restore_function_1->RunAsync());
399
400   EXPECT_FALSE(window1_state->IsMinimized());
401   EXPECT_FALSE(window2_state->IsMinimized());
402   EXPECT_FALSE(window3_state->IsMinimized());
403 }
404
405 }  // namespace chromeos