Upstream version 5.34.92.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/root_window.h"
18 #include "ui/aura/test/test_windows.h"
19 #include "ui/aura/window.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 RunImpl() OVERRIDE {
55     return WallpaperPrivateMinimizeInactiveWindowsFunction::RunImpl();
56   }
57
58  protected:
59   virtual ~TestMinimizeFunction() {}
60 };
61
62 class TestRestoreFunction
63     : public WallpaperPrivateRestoreMinimizedWindowsFunction {
64  public:
65   TestRestoreFunction() {}
66
67   virtual bool RunImpl() OVERRIDE {
68     return WallpaperPrivateRestoreMinimizedWindowsFunction::RunImpl();
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->RunImpl());
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->RunImpl());
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->RunImpl());
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->RunImpl());
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->RunImpl());
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->RunImpl());
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   EXPECT_TRUE(multi_user_window_manager_);
233 }
234
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);
239 }
240
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);
247
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));
253
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());
259
260   multi_user_window_manager()->SetWindowOwner(window0.get(), kTestAccount1);
261   multi_user_window_manager()->SetWindowOwner(window1.get(), kTestAccount1);
262
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);
267
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());
273
274   // We then activate window 0 (i.e. wallpaper picker) and call the minimize
275   // function.
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());
281
282   // All windows except window 0 should be minimized.
283   EXPECT_FALSE(window0_state->IsMinimized());
284   EXPECT_TRUE(window1_state->IsMinimized());
285
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());
290
291   // Activate kTestAccount2. kTestAccount1 becomes inactive user.
292   SwitchActiveUser(kTestAccount2);
293
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());
299
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());
304
305   // All windows that belong to inactive user should not be affected.
306   EXPECT_FALSE(window0_state->IsMinimized());
307   EXPECT_TRUE(window1_state->IsMinimized());
308
309   // Destroy window 4. Nothing should happen to other windows.
310   window4_state->Unminimize();
311   window4.reset();
312
313   EXPECT_FALSE(window2_state->IsMinimized());
314   EXPECT_TRUE(window3_state->IsMinimized());
315   EXPECT_FALSE(window0_state->IsMinimized());
316   EXPECT_TRUE(window1_state->IsMinimized());
317
318   // Then we destroy window 2 and call the restore function.
319   window2.reset();
320   scoped_refptr<TestRestoreFunction> restore_function_0(
321       new TestRestoreFunction());
322   EXPECT_TRUE(restore_function_0->RunImpl());
323
324   EXPECT_FALSE(window3_state->IsMinimized());
325
326   // All windows that belong to inactive user should not be affected.
327   EXPECT_FALSE(window0_state->IsMinimized());
328   EXPECT_TRUE(window1_state->IsMinimized());
329
330   SwitchActiveUser(kTestAccount1);
331
332   // Then we destroy window 0 and call the restore function.
333   window0.reset();
334   scoped_refptr<TestRestoreFunction> restore_function_1(
335       new TestRestoreFunction());
336   EXPECT_TRUE(restore_function_1->RunImpl());
337
338   EXPECT_FALSE(window1_state->IsMinimized());
339   EXPECT_FALSE(window3_state->IsMinimized());
340 }
341
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);
347
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));
352
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());
357
358   multi_user_window_manager()->SetWindowOwner(window0.get(), kTestAccount1);
359   multi_user_window_manager()->SetWindowOwner(window1.get(), kTestAccount1);
360
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);
364
365   // Teleport window2 to kTestAccount1.
366   multi_user_window_manager()->ShowWindowForUser(window2.get(), kTestAccount1);
367
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());
373
374   // We then activate window 0 (i.e. wallpaper picker) and call the minimize
375   // function.
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());
381
382   // All windows except window 0 should be minimized.
383   EXPECT_FALSE(window0_state->IsMinimized());
384   EXPECT_TRUE(window1_state->IsMinimized());
385
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());
390
391   // Then we destroy window 0 and call the restore function.
392   window0.reset();
393   scoped_refptr<TestRestoreFunction> restore_function_1(
394       new TestRestoreFunction());
395   EXPECT_TRUE(restore_function_1->RunImpl());
396
397   EXPECT_FALSE(window1_state->IsMinimized());
398   EXPECT_FALSE(window2_state->IsMinimized());
399   EXPECT_FALSE(window3_state->IsMinimized());
400 }
401
402 }  // namespace chromeos