Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / ash / wm / workspace / workspace_layout_manager_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/wm/workspace/workspace_layout_manager.h"
6
7 #include <string>
8
9 #include "ash/display/display_layout.h"
10 #include "ash/display/display_manager.h"
11 #include "ash/root_window_controller.h"
12 #include "ash/screen_util.h"
13 #include "ash/session/session_state_delegate.h"
14 #include "ash/shelf/shelf_layout_manager.h"
15 #include "ash/shell.h"
16 #include "ash/shell_observer.h"
17 #include "ash/shell_window_ids.h"
18 #include "ash/test/ash_test_base.h"
19 #include "ash/wm/maximize_mode/workspace_backdrop_delegate.h"
20 #include "ash/wm/window_state.h"
21 #include "ash/wm/window_util.h"
22 #include "ash/wm/wm_event.h"
23 #include "ash/wm/workspace/workspace_window_resizer.h"
24 #include "base/basictypes.h"
25 #include "base/compiler_specific.h"
26 #include "ui/aura/client/aura_constants.h"
27 #include "ui/aura/test/test_windows.h"
28 #include "ui/aura/window.h"
29 #include "ui/aura/window_event_dispatcher.h"
30 #include "ui/base/ime/dummy_text_input_client.h"
31 #include "ui/base/ime/input_method.h"
32 #include "ui/base/ime/text_input_focus_manager.h"
33 #include "ui/base/ui_base_switches_util.h"
34 #include "ui/base/ui_base_types.h"
35 #include "ui/gfx/insets.h"
36 #include "ui/gfx/screen.h"
37 #include "ui/views/widget/widget.h"
38 #include "ui/views/widget/widget_delegate.h"
39 #include "ui/wm/core/window_util.h"
40
41 namespace ash {
42 namespace {
43
44 class MaximizeDelegateView : public views::WidgetDelegateView {
45  public:
46   explicit MaximizeDelegateView(const gfx::Rect& initial_bounds)
47       : initial_bounds_(initial_bounds) {
48   }
49   ~MaximizeDelegateView() override {}
50
51   bool GetSavedWindowPlacement(const views::Widget* widget,
52                                gfx::Rect* bounds,
53                                ui::WindowShowState* show_state) const override {
54     *bounds = initial_bounds_;
55     *show_state = ui::SHOW_STATE_MAXIMIZED;
56     return true;
57   }
58
59  private:
60   const gfx::Rect initial_bounds_;
61
62   DISALLOW_COPY_AND_ASSIGN(MaximizeDelegateView);
63 };
64
65 class TestShellObserver : public ShellObserver {
66  public:
67   TestShellObserver() : call_count_(0),
68                         is_fullscreen_(false) {
69     Shell::GetInstance()->AddShellObserver(this);
70   }
71
72   ~TestShellObserver() override {
73     Shell::GetInstance()->RemoveShellObserver(this);
74   }
75
76   void OnFullscreenStateChanged(bool is_fullscreen,
77                                 aura::Window* root_window) override {
78     call_count_++;
79     is_fullscreen_ = is_fullscreen;
80   }
81
82   int call_count() const {
83     return call_count_;
84   }
85
86   bool is_fullscreen() const {
87     return is_fullscreen_;
88   }
89
90  private:
91   int call_count_;
92   bool is_fullscreen_;
93
94   DISALLOW_COPY_AND_ASSIGN(TestShellObserver);
95 };
96
97 }  // namespace
98
99 typedef test::AshTestBase WorkspaceLayoutManagerTest;
100
101 // Verifies that a window containing a restore coordinate will be restored to
102 // to the size prior to minimize, keeping the restore rectangle in tact (if
103 // there is one).
104 TEST_F(WorkspaceLayoutManagerTest, RestoreFromMinimizeKeepsRestore) {
105   scoped_ptr<aura::Window> window(
106       CreateTestWindowInShellWithBounds(gfx::Rect(1, 2, 3, 4)));
107   gfx::Rect bounds(10, 15, 25, 35);
108   window->SetBounds(bounds);
109
110   wm::WindowState* window_state = wm::GetWindowState(window.get());
111
112   // This will not be used for un-minimizing window.
113   window_state->SetRestoreBoundsInScreen(gfx::Rect(0, 0, 100, 100));
114   window_state->Minimize();
115   window_state->Restore();
116   EXPECT_EQ("0,0 100x100", window_state->GetRestoreBoundsInScreen().ToString());
117   EXPECT_EQ("10,15 25x35", window.get()->bounds().ToString());
118
119   if (!SupportsMultipleDisplays())
120     return;
121
122   UpdateDisplay("400x300,500x400");
123   window->SetBoundsInScreen(gfx::Rect(600, 0, 100, 100),
124                             ScreenUtil::GetSecondaryDisplay());
125   EXPECT_EQ(Shell::GetAllRootWindows()[1], window->GetRootWindow());
126   window_state->Minimize();
127   // This will not be used for un-minimizing window.
128   window_state->SetRestoreBoundsInScreen(gfx::Rect(0, 0, 100, 100));
129   window_state->Restore();
130   EXPECT_EQ("600,0 100x100", window->GetBoundsInScreen().ToString());
131
132   // Make sure the unminimized window moves inside the display when
133   // 2nd display is disconnected.
134   window_state->Minimize();
135   UpdateDisplay("400x300");
136   window_state->Restore();
137   EXPECT_EQ(Shell::GetPrimaryRootWindow(), window->GetRootWindow());
138   EXPECT_TRUE(
139       Shell::GetPrimaryRootWindow()->bounds().Intersects(window->bounds()));
140 }
141
142 TEST_F(WorkspaceLayoutManagerTest, KeepMinimumVisibilityInDisplays) {
143   if (!SupportsMultipleDisplays())
144     return;
145
146   UpdateDisplay("300x400,400x500");
147   aura::Window::Windows root_windows = Shell::GetAllRootWindows();
148
149   DisplayLayout layout(DisplayLayout::TOP, 0);
150   Shell::GetInstance()->display_manager()->
151       SetLayoutForCurrentDisplays(layout);
152   EXPECT_EQ("0,-500 400x500", root_windows[1]->GetBoundsInScreen().ToString());
153
154   scoped_ptr<aura::Window> window1(
155       CreateTestWindowInShellWithBounds(gfx::Rect(10, -400, 200, 200)));
156   EXPECT_EQ("10,-400 200x200", window1->GetBoundsInScreen().ToString());
157
158   // Make sure the caption is visible.
159   scoped_ptr<aura::Window> window2(
160       CreateTestWindowInShellWithBounds(gfx::Rect(10, -600, 200, 200)));
161   EXPECT_EQ("10,-500 200x200", window2->GetBoundsInScreen().ToString());
162 }
163
164 TEST_F(WorkspaceLayoutManagerTest, NoMinimumVisibilityForPopupWindows) {
165   UpdateDisplay("300x400");
166
167   // Create a popup window out of display boundaries and make sure it is not
168   // moved to have minimum visibility.
169   scoped_ptr<aura::Window> window(
170       CreateTestWindowInShellWithDelegateAndType(NULL,
171                                                  ui::wm::WINDOW_TYPE_POPUP,
172                                                  0,
173                                                  gfx::Rect(400, 100, 50, 50)));
174   EXPECT_EQ("400,100 50x50", window->GetBoundsInScreen().ToString());
175 }
176
177 TEST_F(WorkspaceLayoutManagerTest, KeepRestoredWindowInDisplay) {
178   if (!SupportsHostWindowResize())
179     return;
180   scoped_ptr<aura::Window> window(
181       CreateTestWindowInShellWithBounds(gfx::Rect(1, 2, 30, 40)));
182   wm::WindowState* window_state = wm::GetWindowState(window.get());
183
184   // Maximized -> Normal transition.
185   window_state->Maximize();
186   window_state->SetRestoreBoundsInScreen(gfx::Rect(-100, -100, 30, 40));
187   window_state->Restore();
188   EXPECT_TRUE(
189       Shell::GetPrimaryRootWindow()->bounds().Intersects(window->bounds()));
190   // Y bounds should not be negative.
191   EXPECT_EQ("-20,0 30x40", window->bounds().ToString());
192
193   // Minimized -> Normal transition.
194   window->SetBounds(gfx::Rect(-100, -100, 30, 40));
195   window_state->Minimize();
196   EXPECT_FALSE(
197       Shell::GetPrimaryRootWindow()->bounds().Intersects(window->bounds()));
198   EXPECT_EQ("-100,-100 30x40", window->bounds().ToString());
199   window->Show();
200   EXPECT_TRUE(
201       Shell::GetPrimaryRootWindow()->bounds().Intersects(window->bounds()));
202   // Y bounds should not be negative.
203   EXPECT_EQ("-20,0 30x40", window->bounds().ToString());
204
205   // Fullscreen -> Normal transition.
206   window->SetBounds(gfx::Rect(0, 0, 30, 40));  // reset bounds.
207   ASSERT_EQ("0,0 30x40", window->bounds().ToString());
208   window->SetProperty(aura::client::kShowStateKey, ui::SHOW_STATE_FULLSCREEN);
209   EXPECT_EQ(window->bounds(), window->GetRootWindow()->bounds());
210   window_state->SetRestoreBoundsInScreen(gfx::Rect(-100, -100, 30, 40));
211   window_state->Restore();
212   EXPECT_TRUE(
213       Shell::GetPrimaryRootWindow()->bounds().Intersects(window->bounds()));
214   // Y bounds should not be negative.
215   EXPECT_EQ("-20,0 30x40", window->bounds().ToString());
216 }
217
218 TEST_F(WorkspaceLayoutManagerTest, MaximizeInDisplayToBeRestored) {
219   if (!SupportsMultipleDisplays())
220     return;
221   UpdateDisplay("300x400,400x500");
222
223   aura::Window::Windows root_windows = Shell::GetAllRootWindows();
224
225   scoped_ptr<aura::Window> window(
226       CreateTestWindowInShellWithBounds(gfx::Rect(1, 2, 30, 40)));
227   EXPECT_EQ(root_windows[0], window->GetRootWindow());
228
229   wm::WindowState* window_state = wm::GetWindowState(window.get());
230   window_state->SetRestoreBoundsInScreen(gfx::Rect(400, 0, 30, 40));
231   // Maximize the window in 2nd display as the restore bounds
232   // is inside 2nd display.
233   window_state->Maximize();
234   EXPECT_EQ(root_windows[1], window->GetRootWindow());
235   EXPECT_EQ("300,0 400x453", window->GetBoundsInScreen().ToString());
236
237   window_state->Restore();
238   EXPECT_EQ(root_windows[1], window->GetRootWindow());
239   EXPECT_EQ("400,0 30x40", window->GetBoundsInScreen().ToString());
240
241   // If the restore bounds intersects with the current display,
242   // don't move.
243   window_state->SetRestoreBoundsInScreen(gfx::Rect(280, 0, 30, 40));
244   window_state->Maximize();
245   EXPECT_EQ(root_windows[1], window->GetRootWindow());
246   EXPECT_EQ("300,0 400x453", window->GetBoundsInScreen().ToString());
247
248   window_state->Restore();
249   EXPECT_EQ(root_windows[1], window->GetRootWindow());
250   EXPECT_EQ("280,0 30x40", window->GetBoundsInScreen().ToString());
251
252   // Restoring widget state.
253   scoped_ptr<views::Widget> w1(new views::Widget);
254   views::Widget::InitParams params;
255   params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
256   params.delegate = new MaximizeDelegateView(gfx::Rect(400, 0, 30, 40));
257   params.context = root_windows[0];
258   w1->Init(params);
259   w1->Show();
260   EXPECT_TRUE(w1->IsMaximized());
261   EXPECT_EQ(root_windows[1], w1->GetNativeView()->GetRootWindow());
262   EXPECT_EQ("300,0 400x453", w1->GetWindowBoundsInScreen().ToString());
263   w1->Restore();
264   EXPECT_EQ(root_windows[1], w1->GetNativeView()->GetRootWindow());
265   EXPECT_EQ("400,0 30x40", w1->GetWindowBoundsInScreen().ToString());
266 }
267
268 TEST_F(WorkspaceLayoutManagerTest, FullscreenInDisplayToBeRestored) {
269   if (!SupportsMultipleDisplays())
270     return;
271   UpdateDisplay("300x400,400x500");
272
273   aura::Window::Windows root_windows = Shell::GetAllRootWindows();
274
275   scoped_ptr<aura::Window> window(
276       CreateTestWindowInShellWithBounds(gfx::Rect(1, 2, 30, 40)));
277   EXPECT_EQ(root_windows[0], window->GetRootWindow());
278
279   wm::WindowState* window_state = wm::GetWindowState(window.get());
280   window_state->SetRestoreBoundsInScreen(gfx::Rect(400, 0, 30, 40));
281   // Maximize the window in 2nd display as the restore bounds
282   // is inside 2nd display.
283   window->SetProperty(aura::client::kShowStateKey,
284                       ui::SHOW_STATE_FULLSCREEN);
285   EXPECT_EQ(root_windows[1], window->GetRootWindow());
286   EXPECT_EQ("300,0 400x500", window->GetBoundsInScreen().ToString());
287
288   window_state->Restore();
289   EXPECT_EQ(root_windows[1], window->GetRootWindow());
290   EXPECT_EQ("400,0 30x40", window->GetBoundsInScreen().ToString());
291
292   // If the restore bounds intersects with the current display,
293   // don't move.
294   window_state->SetRestoreBoundsInScreen(gfx::Rect(280, 0, 30, 40));
295   window->SetProperty(aura::client::kShowStateKey,
296                       ui::SHOW_STATE_FULLSCREEN);
297   EXPECT_EQ(root_windows[1], window->GetRootWindow());
298   EXPECT_EQ("300,0 400x500", window->GetBoundsInScreen().ToString());
299
300   window_state->Restore();
301   EXPECT_EQ(root_windows[1], window->GetRootWindow());
302   EXPECT_EQ("280,0 30x40", window->GetBoundsInScreen().ToString());
303 }
304
305 // WindowObserver implementation used by DontClobberRestoreBoundsWindowObserver.
306 // This code mirrors what BrowserFrameAsh does. In particular when this code
307 // sees the window was maximized it changes the bounds of a secondary
308 // window. The secondary window mirrors the status window.
309 class DontClobberRestoreBoundsWindowObserver : public aura::WindowObserver {
310  public:
311   DontClobberRestoreBoundsWindowObserver() : window_(NULL) {}
312
313   void set_window(aura::Window* window) { window_ = window; }
314
315   void OnWindowPropertyChanged(aura::Window* window,
316                                const void* key,
317                                intptr_t old) override {
318     if (!window_)
319       return;
320
321     if (wm::GetWindowState(window)->IsMaximized()) {
322       aura::Window* w = window_;
323       window_ = NULL;
324
325       gfx::Rect shelf_bounds(Shell::GetPrimaryRootWindowController()->
326                              GetShelfLayoutManager()->GetIdealBounds());
327       const gfx::Rect& window_bounds(w->bounds());
328       w->SetBounds(gfx::Rect(window_bounds.x(), shelf_bounds.y() - 1,
329                              window_bounds.width(), window_bounds.height()));
330     }
331   }
332
333  private:
334   aura::Window* window_;
335
336   DISALLOW_COPY_AND_ASSIGN(DontClobberRestoreBoundsWindowObserver);
337 };
338
339 // Creates a window, maximized the window and from within the maximized
340 // notification sets the bounds of a window to overlap the shelf. Verifies this
341 // doesn't effect the restore bounds.
342 TEST_F(WorkspaceLayoutManagerTest, DontClobberRestoreBounds) {
343   DontClobberRestoreBoundsWindowObserver window_observer;
344   scoped_ptr<aura::Window> window(new aura::Window(NULL));
345   window->SetType(ui::wm::WINDOW_TYPE_NORMAL);
346   window->Init(aura::WINDOW_LAYER_TEXTURED);
347   window->SetBounds(gfx::Rect(10, 20, 30, 40));
348   // NOTE: for this test to exercise the failure the observer needs to be added
349   // before the parent set. This mimics what BrowserFrameAsh does.
350   window->AddObserver(&window_observer);
351   ParentWindowInPrimaryRootWindow(window.get());
352   window->Show();
353
354   wm::WindowState* window_state = wm::GetWindowState(window.get());
355   window_state->Activate();
356
357   scoped_ptr<aura::Window> window2(
358       CreateTestWindowInShellWithBounds(gfx::Rect(12, 20, 30, 40)));
359   ::wm::AddTransientChild(window.get(), window2.get());
360   window2->Show();
361
362   window_observer.set_window(window2.get());
363   window_state->Maximize();
364   EXPECT_EQ("10,20 30x40",
365             window_state->GetRestoreBoundsInScreen().ToString());
366   window->RemoveObserver(&window_observer);
367 }
368
369 // Verifies when a window is maximized all descendant windows have a size.
370 TEST_F(WorkspaceLayoutManagerTest, ChildBoundsResetOnMaximize) {
371   scoped_ptr<aura::Window> window(
372       CreateTestWindowInShellWithBounds(gfx::Rect(10, 20, 30, 40)));
373   window->Show();
374   wm::WindowState* window_state = wm::GetWindowState(window.get());
375   window_state->Activate();
376   scoped_ptr<aura::Window> child_window(
377       aura::test::CreateTestWindowWithBounds(gfx::Rect(5, 6, 7, 8),
378                                              window.get()));
379   child_window->Show();
380   window_state->Maximize();
381   EXPECT_EQ("5,6 7x8", child_window->bounds().ToString());
382 }
383
384 // Verifies a window created with maximized state has the maximized
385 // bounds.
386 TEST_F(WorkspaceLayoutManagerTest, MaximizeWithEmptySize) {
387   scoped_ptr<aura::Window> window(
388       aura::test::CreateTestWindowWithBounds(gfx::Rect(0, 0, 0, 0),
389                                              NULL));
390   wm::GetWindowState(window.get())->Maximize();
391   aura::Window* default_container = Shell::GetContainer(
392       Shell::GetPrimaryRootWindow(), kShellWindowId_DefaultContainer);
393   default_container->AddChild(window.get());
394   window->Show();
395   gfx::Rect work_area(
396       Shell::GetScreen()->GetPrimaryDisplay().work_area());
397   EXPECT_EQ(work_area.ToString(), window->GetBoundsInScreen().ToString());
398 }
399
400 TEST_F(WorkspaceLayoutManagerTest, WindowShouldBeOnScreenWhenAdded) {
401   // Normal window bounds shouldn't be changed.
402   gfx::Rect window_bounds(100, 100, 200, 200);
403   scoped_ptr<aura::Window> window(
404       CreateTestWindowInShellWithBounds(window_bounds));
405   EXPECT_EQ(window_bounds, window->bounds());
406
407   // If the window is out of the workspace, it would be moved on screen.
408   gfx::Rect root_window_bounds =
409       Shell::GetInstance()->GetPrimaryRootWindow()->bounds();
410   window_bounds.Offset(root_window_bounds.width(), root_window_bounds.height());
411   ASSERT_FALSE(window_bounds.Intersects(root_window_bounds));
412   scoped_ptr<aura::Window> out_window(
413       CreateTestWindowInShellWithBounds(window_bounds));
414   EXPECT_EQ(window_bounds.size(), out_window->bounds().size());
415   gfx::Rect bounds = out_window->bounds();
416   bounds.Intersect(root_window_bounds);
417
418   // 30% of the window edge must be visible.
419   EXPECT_GT(bounds.width(), out_window->bounds().width() * 0.29);
420   EXPECT_GT(bounds.height(), out_window->bounds().height() * 0.29);
421
422   aura::Window* parent = out_window->parent();
423   parent->RemoveChild(out_window.get());
424   out_window->SetBounds(gfx::Rect(-200, -200, 200, 200));
425   // UserHasChangedWindowPositionOrSize flag shouldn't turn off this behavior.
426   wm::GetWindowState(window.get())->set_bounds_changed_by_user(true);
427   parent->AddChild(out_window.get());
428   EXPECT_GT(bounds.width(), out_window->bounds().width() * 0.29);
429   EXPECT_GT(bounds.height(), out_window->bounds().height() * 0.29);
430
431   // Make sure we always make more than 1/3 of the window edge visible even
432   // if the initial bounds intersects with display.
433   window_bounds.SetRect(-150, -150, 200, 200);
434   bounds = window_bounds;
435   bounds.Intersect(root_window_bounds);
436
437   // Make sure that the initial bounds' visible area is less than 26%
438   // so that the auto adjustment logic kicks in.
439   ASSERT_LT(bounds.width(), out_window->bounds().width() * 0.26);
440   ASSERT_LT(bounds.height(), out_window->bounds().height() * 0.26);
441   ASSERT_TRUE(window_bounds.Intersects(root_window_bounds));
442
443   scoped_ptr<aura::Window> partially_out_window(
444       CreateTestWindowInShellWithBounds(window_bounds));
445   EXPECT_EQ(window_bounds.size(), partially_out_window->bounds().size());
446   bounds = partially_out_window->bounds();
447   bounds.Intersect(root_window_bounds);
448   EXPECT_GT(bounds.width(), out_window->bounds().width() * 0.29);
449   EXPECT_GT(bounds.height(), out_window->bounds().height() * 0.29);
450
451   // Make sure the window whose 30% width/height is bigger than display
452   // will be placed correctly.
453   window_bounds.SetRect(-1900, -1900, 3000, 3000);
454   scoped_ptr<aura::Window> window_bigger_than_display(
455       CreateTestWindowInShellWithBounds(window_bounds));
456   EXPECT_GE(root_window_bounds.width(),
457             window_bigger_than_display->bounds().width());
458   EXPECT_GE(root_window_bounds.height(),
459             window_bigger_than_display->bounds().height());
460
461   bounds = window_bigger_than_display->bounds();
462   bounds.Intersect(root_window_bounds);
463   EXPECT_GT(bounds.width(), out_window->bounds().width() * 0.29);
464   EXPECT_GT(bounds.height(), out_window->bounds().height() * 0.29);
465 }
466
467 // Verifies the size of a window is enforced to be smaller than the work area.
468 TEST_F(WorkspaceLayoutManagerTest, SizeToWorkArea) {
469   // Normal window bounds shouldn't be changed.
470   gfx::Size work_area(
471       Shell::GetScreen()->GetPrimaryDisplay().work_area().size());
472   const gfx::Rect window_bounds(
473       100, 101, work_area.width() + 1, work_area.height() + 2);
474   scoped_ptr<aura::Window> window(
475       CreateTestWindowInShellWithBounds(window_bounds));
476   EXPECT_EQ(gfx::Rect(gfx::Point(100, 101), work_area).ToString(),
477       window->bounds().ToString());
478
479   // Directly setting the bounds triggers a slightly different code path. Verify
480   // that too.
481   window->SetBounds(window_bounds);
482   EXPECT_EQ(gfx::Rect(gfx::Point(100, 101), work_area).ToString(),
483       window->bounds().ToString());
484 }
485
486 TEST_F(WorkspaceLayoutManagerTest, NotifyFullscreenChanges) {
487   TestShellObserver observer;
488   scoped_ptr<aura::Window> window1(
489       CreateTestWindowInShellWithBounds(gfx::Rect(1, 2, 30, 40)));
490   scoped_ptr<aura::Window> window2(
491       CreateTestWindowInShellWithBounds(gfx::Rect(1, 2, 30, 40)));
492   wm::WindowState* window_state1 = wm::GetWindowState(window1.get());
493   wm::WindowState* window_state2 = wm::GetWindowState(window2.get());
494   window_state2->Activate();
495
496   const wm::WMEvent toggle_fullscreen_event(wm::WM_EVENT_TOGGLE_FULLSCREEN);
497   window_state2->OnWMEvent(&toggle_fullscreen_event);
498   EXPECT_EQ(1, observer.call_count());
499   EXPECT_TRUE(observer.is_fullscreen());
500
501   // When window1 moves to the front the fullscreen state should change.
502   window_state1->Activate();
503   EXPECT_EQ(2, observer.call_count());
504   EXPECT_FALSE(observer.is_fullscreen());
505
506   // It should change back if window2 becomes active again.
507   window_state2->Activate();
508   EXPECT_EQ(3, observer.call_count());
509   EXPECT_TRUE(observer.is_fullscreen());
510
511   window_state2->OnWMEvent(&toggle_fullscreen_event);
512   EXPECT_EQ(4, observer.call_count());
513   EXPECT_FALSE(observer.is_fullscreen());
514
515   window_state2->OnWMEvent(&toggle_fullscreen_event);
516   EXPECT_EQ(5, observer.call_count());
517   EXPECT_TRUE(observer.is_fullscreen());
518
519   // Closing the window should change the fullscreen state.
520   window2.reset();
521   EXPECT_EQ(6, observer.call_count());
522   EXPECT_FALSE(observer.is_fullscreen());
523 }
524
525 // Following "Solo" tests were originally written for BaseLayoutManager.
526 namespace {
527
528 class WorkspaceLayoutManagerSoloTest : public test::AshTestBase {
529  public:
530   WorkspaceLayoutManagerSoloTest() {}
531   ~WorkspaceLayoutManagerSoloTest() override {}
532
533   aura::Window* CreateTestWindow(const gfx::Rect& bounds) {
534     return CreateTestWindowInShellWithBounds(bounds);
535   }
536
537  private:
538   DISALLOW_COPY_AND_ASSIGN(WorkspaceLayoutManagerSoloTest);
539 };
540
541 }  // namespace
542
543 // Tests normal->maximize->normal.
544 TEST_F(WorkspaceLayoutManagerSoloTest, Maximize) {
545   gfx::Rect bounds(100, 100, 200, 200);
546   scoped_ptr<aura::Window> window(CreateTestWindow(bounds));
547   window->SetProperty(aura::client::kShowStateKey, ui::SHOW_STATE_MAXIMIZED);
548   // Maximized window fills the work area, not the whole display.
549   EXPECT_EQ(
550       ScreenUtil::GetMaximizedWindowBoundsInParent(window.get()).ToString(),
551       window->bounds().ToString());
552   window->SetProperty(aura::client::kShowStateKey, ui::SHOW_STATE_NORMAL);
553   EXPECT_EQ(bounds.ToString(), window->bounds().ToString());
554 }
555
556 // Tests normal->minimize->normal.
557 TEST_F(WorkspaceLayoutManagerSoloTest, Minimize) {
558   gfx::Rect bounds(100, 100, 200, 200);
559   scoped_ptr<aura::Window> window(CreateTestWindow(bounds));
560   window->SetProperty(aura::client::kShowStateKey, ui::SHOW_STATE_MINIMIZED);
561   // Note: Currently minimize doesn't do anything except set the state.
562   // See crbug.com/104571.
563   EXPECT_EQ(bounds.ToString(), window->bounds().ToString());
564   window->SetProperty(aura::client::kShowStateKey, ui::SHOW_STATE_NORMAL);
565   EXPECT_EQ(bounds.ToString(), window->bounds().ToString());
566 }
567
568 // A WindowDelegate which sets the focus when the window
569 // becomes visible.
570 class FocusDelegate : public aura::test::TestWindowDelegate {
571  public:
572   FocusDelegate()
573       : window_(NULL),
574         show_state_(ui::SHOW_STATE_END) {
575   }
576   ~FocusDelegate() override {}
577
578   void set_window(aura::Window* window) { window_ = window; }
579
580   // aura::test::TestWindowDelegate overrides:
581   void OnWindowTargetVisibilityChanged(bool visible) override {
582     if (window_) {
583       if (visible)
584         window_->Focus();
585       show_state_ = window_->GetProperty(aura::client::kShowStateKey);
586     }
587   }
588
589   ui::WindowShowState GetShowStateAndReset() {
590     ui::WindowShowState ret = show_state_;
591     show_state_ = ui::SHOW_STATE_END;
592     return ret;
593   }
594
595  private:
596   aura::Window* window_;
597   ui::WindowShowState show_state_;
598
599   DISALLOW_COPY_AND_ASSIGN(FocusDelegate);
600 };
601
602 // Make sure that the window's show state is correct in
603 // |WindowDelegate::OnWindowTargetVisibilityChanged|, and setting
604 // focus in this callback doesn't cause DCHECK error.  See
605 // crbug.com/168383.
606 TEST_F(WorkspaceLayoutManagerSoloTest, FocusDuringUnminimize) {
607   FocusDelegate delegate;
608   scoped_ptr<aura::Window> window(CreateTestWindowInShellWithDelegate(
609       &delegate, 0, gfx::Rect(100, 100, 100, 100)));
610   delegate.set_window(window.get());
611   window->SetProperty(aura::client::kShowStateKey, ui::SHOW_STATE_MINIMIZED);
612   EXPECT_FALSE(window->IsVisible());
613   EXPECT_EQ(ui::SHOW_STATE_MINIMIZED, delegate.GetShowStateAndReset());
614   window->Show();
615   EXPECT_TRUE(window->IsVisible());
616   EXPECT_EQ(ui::SHOW_STATE_NORMAL, delegate.GetShowStateAndReset());
617 }
618
619 // Tests maximized window size during root window resize.
620 TEST_F(WorkspaceLayoutManagerSoloTest, MaximizeRootWindowResize) {
621   gfx::Rect bounds(100, 100, 200, 200);
622   scoped_ptr<aura::Window> window(CreateTestWindow(bounds));
623   window->SetProperty(aura::client::kShowStateKey, ui::SHOW_STATE_MAXIMIZED);
624   gfx::Rect initial_work_area_bounds =
625       ScreenUtil::GetMaximizedWindowBoundsInParent(window.get());
626   EXPECT_EQ(initial_work_area_bounds.ToString(), window->bounds().ToString());
627   // Enlarge the root window.  We should still match the work area size.
628   UpdateDisplay("900x700");
629   EXPECT_EQ(
630       ScreenUtil::GetMaximizedWindowBoundsInParent(window.get()).ToString(),
631       window->bounds().ToString());
632   EXPECT_NE(
633       initial_work_area_bounds.ToString(),
634       ScreenUtil::GetMaximizedWindowBoundsInParent(window.get()).ToString());
635 }
636
637 // Tests normal->fullscreen->normal.
638 TEST_F(WorkspaceLayoutManagerSoloTest, Fullscreen) {
639   gfx::Rect bounds(100, 100, 200, 200);
640   scoped_ptr<aura::Window> window(CreateTestWindow(bounds));
641   window->SetProperty(aura::client::kShowStateKey, ui::SHOW_STATE_FULLSCREEN);
642   // Fullscreen window fills the whole display.
643   EXPECT_EQ(Shell::GetScreen()->GetDisplayNearestWindow(
644                 window.get()).bounds().ToString(),
645             window->bounds().ToString());
646   window->SetProperty(aura::client::kShowStateKey, ui::SHOW_STATE_NORMAL);
647   EXPECT_EQ(bounds.ToString(), window->bounds().ToString());
648 }
649
650 // Tests fullscreen window size during root window resize.
651 TEST_F(WorkspaceLayoutManagerSoloTest, FullscreenRootWindowResize) {
652   gfx::Rect bounds(100, 100, 200, 200);
653   scoped_ptr<aura::Window> window(CreateTestWindow(bounds));
654   // Fullscreen window fills the whole display.
655   window->SetProperty(aura::client::kShowStateKey, ui::SHOW_STATE_FULLSCREEN);
656   EXPECT_EQ(Shell::GetScreen()->GetDisplayNearestWindow(
657                 window.get()).bounds().ToString(),
658             window->bounds().ToString());
659   // Enlarge the root window.  We should still match the display size.
660   UpdateDisplay("800x600");
661   EXPECT_EQ(Shell::GetScreen()->GetDisplayNearestWindow(
662                 window.get()).bounds().ToString(),
663             window->bounds().ToString());
664 }
665
666 // Tests that when the screen gets smaller the windows aren't bigger than
667 // the screen.
668 TEST_F(WorkspaceLayoutManagerSoloTest, RootWindowResizeShrinksWindows) {
669   scoped_ptr<aura::Window> window(
670       CreateTestWindow(gfx::Rect(10, 20, 500, 400)));
671   gfx::Rect work_area = Shell::GetScreen()->GetDisplayNearestWindow(
672       window.get()).work_area();
673   // Invariant: Window is smaller than work area.
674   EXPECT_LE(window->bounds().width(), work_area.width());
675   EXPECT_LE(window->bounds().height(), work_area.height());
676
677   // Make the root window narrower than our window.
678   UpdateDisplay("300x400");
679   work_area = Shell::GetScreen()->GetDisplayNearestWindow(
680       window.get()).work_area();
681   EXPECT_LE(window->bounds().width(), work_area.width());
682   EXPECT_LE(window->bounds().height(), work_area.height());
683
684   // Make the root window shorter than our window.
685   UpdateDisplay("300x200");
686   work_area = Shell::GetScreen()->GetDisplayNearestWindow(
687       window.get()).work_area();
688   EXPECT_LE(window->bounds().width(), work_area.width());
689   EXPECT_LE(window->bounds().height(), work_area.height());
690
691   // Enlarging the root window does not change the window bounds.
692   gfx::Rect old_bounds = window->bounds();
693   UpdateDisplay("800x600");
694   EXPECT_EQ(old_bounds.width(), window->bounds().width());
695   EXPECT_EQ(old_bounds.height(), window->bounds().height());
696 }
697
698 // Verifies maximizing sets the restore bounds, and restoring
699 // restores the bounds.
700 TEST_F(WorkspaceLayoutManagerSoloTest, MaximizeSetsRestoreBounds) {
701   scoped_ptr<aura::Window> window(CreateTestWindow(gfx::Rect(10, 20, 30, 40)));
702   wm::WindowState* window_state = wm::GetWindowState(window.get());
703
704   // Maximize it, which will keep the previous restore bounds.
705   window->SetProperty(aura::client::kShowStateKey, ui::SHOW_STATE_MAXIMIZED);
706   EXPECT_EQ("10,20 30x40", window_state->GetRestoreBoundsInParent().ToString());
707
708   // Restore it, which should restore bounds and reset restore bounds.
709   window->SetProperty(aura::client::kShowStateKey, ui::SHOW_STATE_NORMAL);
710   EXPECT_EQ("10,20 30x40", window->bounds().ToString());
711   EXPECT_FALSE(window_state->HasRestoreBounds());
712 }
713
714 // Verifies maximizing keeps the restore bounds if set.
715 TEST_F(WorkspaceLayoutManagerSoloTest, MaximizeResetsRestoreBounds) {
716   scoped_ptr<aura::Window> window(CreateTestWindow(gfx::Rect(1, 2, 3, 4)));
717
718   wm::WindowState* window_state = wm::GetWindowState(window.get());
719   window_state->SetRestoreBoundsInParent(gfx::Rect(10, 11, 12, 13));
720
721   // Maximize it, which will keep the previous restore bounds.
722   window->SetProperty(aura::client::kShowStateKey, ui::SHOW_STATE_MAXIMIZED);
723   EXPECT_EQ("10,11 12x13", window_state->GetRestoreBoundsInParent().ToString());
724 }
725
726 // Verifies that the restore bounds do not get reset when restoring to a
727 // maximzied state from a minimized state.
728 TEST_F(WorkspaceLayoutManagerSoloTest,
729        BoundsAfterRestoringToMaximizeFromMinimize) {
730   scoped_ptr<aura::Window> window(CreateTestWindow(gfx::Rect(1, 2, 3, 4)));
731   gfx::Rect bounds(10, 15, 25, 35);
732   window->SetBounds(bounds);
733
734   wm::WindowState* window_state = wm::GetWindowState(window.get());
735   // Maximize it, which should reset restore bounds.
736   window_state->Maximize();
737   EXPECT_EQ(bounds.ToString(),
738             window_state->GetRestoreBoundsInParent().ToString());
739   // Minimize the window. The restore bounds should not change.
740   window_state->Minimize();
741   EXPECT_EQ(bounds.ToString(),
742             window_state->GetRestoreBoundsInParent().ToString());
743
744   // Show the window again. The window should be maximized, and the restore
745   // bounds should not change.
746   window->Show();
747   EXPECT_EQ(bounds.ToString(),
748             window_state->GetRestoreBoundsInParent().ToString());
749   EXPECT_TRUE(window_state->IsMaximized());
750
751   window_state->Restore();
752   EXPECT_EQ(bounds.ToString(), window->bounds().ToString());
753 }
754
755 // Verify if the window is not resized during screen lock. See: crbug.com/173127
756 TEST_F(WorkspaceLayoutManagerSoloTest, NotResizeWhenScreenIsLocked) {
757   SetCanLockScreen(true);
758   scoped_ptr<aura::Window> window(CreateTestWindow(gfx::Rect(1, 2, 3, 4)));
759   // window with AlwaysOnTop will be managed by BaseLayoutManager.
760   window->SetProperty(aura::client::kAlwaysOnTopKey, true);
761   window->Show();
762
763   ShelfLayoutManager* shelf = ShelfLayoutManager::ForShelf(window.get());
764   shelf->SetAutoHideBehavior(SHELF_AUTO_HIDE_BEHAVIOR_ALWAYS);
765
766   window->SetBounds(ScreenUtil::GetMaximizedWindowBoundsInParent(window.get()));
767   gfx::Rect window_bounds = window->bounds();
768   EXPECT_EQ(
769       ScreenUtil::GetMaximizedWindowBoundsInParent(window.get()).ToString(),
770       window_bounds.ToString());
771
772   Shell::GetInstance()->session_state_delegate()->LockScreen();
773   shelf->UpdateVisibilityState();
774   EXPECT_NE(
775       ScreenUtil::GetMaximizedWindowBoundsInParent(window.get()).ToString(),
776       window_bounds.ToString());
777
778   Shell::GetInstance()->session_state_delegate()->UnlockScreen();
779   shelf->UpdateVisibilityState();
780   EXPECT_EQ(window_bounds.ToString(), window->bounds().ToString());
781 }
782
783 // Following tests are written to test the backdrop functionality.
784
785 namespace {
786
787 class WorkspaceLayoutManagerBackdropTest : public test::AshTestBase {
788  public:
789   WorkspaceLayoutManagerBackdropTest() {}
790   ~WorkspaceLayoutManagerBackdropTest() override {}
791
792   void SetUp() override {
793     test::AshTestBase::SetUp();
794     UpdateDisplay("800x600");
795     default_container_ = Shell::GetContainer(Shell::GetPrimaryRootWindow(),
796                                              kShellWindowId_DefaultContainer);
797   }
798
799   aura::Window* CreateTestWindow(const gfx::Rect& bounds) {
800     aura::Window* window = CreateTestWindowInShellWithBounds(bounds);
801     return window;
802   }
803
804   // Turn the top window back drop on / off.
805   void ShowTopWindowBackdrop(bool show) {
806     scoped_ptr<ash::WorkspaceLayoutManagerDelegate> backdrop;
807     if (show) {
808       backdrop.reset(new ash::WorkspaceBackdropDelegate(default_container_));
809     }
810     (static_cast<WorkspaceLayoutManager*>(default_container_->layout_manager()))
811         ->SetMaximizeBackdropDelegate(backdrop.Pass());
812     // Closing and / or opening can be a delayed operation.
813     base::MessageLoop::current()->RunUntilIdle();
814   }
815
816   // Return the default container.
817   aura::Window* default_container() { return default_container_; }
818
819   // Return the order of windows (top most first) as they are in the default
820   // container. If the window is visible it will be a big letter, otherwise a
821   // small one. The backdrop will be an X and unknown windows will be shown as
822   // '!'.
823   std::string GetWindowOrderAsString(aura::Window* backdrop,
824                                      aura::Window* wa,
825                                      aura::Window* wb,
826                                      aura::Window* wc) {
827     std::string result;
828     for (int i = static_cast<int>(default_container()->children().size()) - 1;
829          i >= 0;
830          --i) {
831       if (!result.empty())
832         result += ",";
833       if (default_container()->children()[i] == wa)
834         result += default_container()->children()[i]->IsVisible() ? "A" : "a";
835       else if (default_container()->children()[i] == wb)
836         result += default_container()->children()[i]->IsVisible() ? "B" : "b";
837       else if (default_container()->children()[i] == wc)
838         result += default_container()->children()[i]->IsVisible() ? "C" : "c";
839       else if (default_container()->children()[i] == backdrop)
840         result += default_container()->children()[i]->IsVisible() ? "X" : "x";
841       else
842         result += "!";
843     }
844     return result;
845   }
846
847  private:
848   // The default container.
849   aura::Window* default_container_;
850
851   DISALLOW_COPY_AND_ASSIGN(WorkspaceLayoutManagerBackdropTest);
852 };
853
854 }  // namespace
855
856 // Check that creating the BackDrop without destroying it does not lead into
857 // a crash.
858 TEST_F(WorkspaceLayoutManagerBackdropTest, BackdropCrashTest) {
859   ShowTopWindowBackdrop(true);
860 }
861
862 // Verify basic assumptions about the backdrop.
863 TEST_F(WorkspaceLayoutManagerBackdropTest, BasicBackdropTests) {
864   // Create a backdrop and see that there is one window (the backdrop) and
865   // that the size is the same as the default container as well as that it is
866   // not visible.
867   ShowTopWindowBackdrop(true);
868   ASSERT_EQ(1U, default_container()->children().size());
869   EXPECT_FALSE(default_container()->children()[0]->IsVisible());
870
871   {
872     // Add a window and make sure that the backdrop is the second child.
873     scoped_ptr<aura::Window> window(CreateTestWindow(gfx::Rect(1, 2, 3, 4)));
874     window->Show();
875     ASSERT_EQ(2U, default_container()->children().size());
876     EXPECT_TRUE(default_container()->children()[0]->IsVisible());
877     EXPECT_TRUE(default_container()->children()[1]->IsVisible());
878     EXPECT_EQ(window.get(), default_container()->children()[1]);
879     EXPECT_EQ(default_container()->bounds().ToString(),
880               default_container()->children()[0]->bounds().ToString());
881   }
882
883   // With the window gone the backdrop should be invisible again.
884   ASSERT_EQ(1U, default_container()->children().size());
885   EXPECT_FALSE(default_container()->children()[0]->IsVisible());
886
887   // Destroying the Backdrop should empty the container.
888   ShowTopWindowBackdrop(false);
889   ASSERT_EQ(0U, default_container()->children().size());
890 }
891
892 // Verify that the backdrop gets properly created and placed.
893 TEST_F(WorkspaceLayoutManagerBackdropTest, VerifyBackdropAndItsStacking) {
894   scoped_ptr<aura::Window> window1(CreateTestWindow(gfx::Rect(1, 2, 3, 4)));
895   window1->Show();
896
897   // Get the default container and check that only a single window is in there.
898   ASSERT_EQ(1U, default_container()->children().size());
899   EXPECT_EQ(window1.get(), default_container()->children()[0]);
900   EXPECT_EQ("A", GetWindowOrderAsString(NULL, window1.get(), NULL, NULL));
901
902   // Create 2 more windows and check that they are also in the container.
903   scoped_ptr<aura::Window> window2(CreateTestWindow(gfx::Rect(10, 2, 3, 4)));
904   scoped_ptr<aura::Window> window3(CreateTestWindow(gfx::Rect(20, 2, 3, 4)));
905   window2->Show();
906   window3->Show();
907
908   aura::Window* backdrop = NULL;
909   EXPECT_EQ("C,B,A",
910             GetWindowOrderAsString(backdrop, window1.get(), window2.get(),
911                                    window3.get()));
912
913   // Turn on the backdrop mode and check that the window shows up where it
914   // should be (second highest number).
915   ShowTopWindowBackdrop(true);
916   backdrop = default_container()->children()[2];
917   EXPECT_EQ("C,X,B,A",
918             GetWindowOrderAsString(backdrop, window1.get(), window2.get(),
919                                    window3.get()));
920
921   // Switch the order of windows and check that it still remains in that
922   // location.
923   default_container()->StackChildAtTop(window2.get());
924   EXPECT_EQ("B,X,C,A",
925             GetWindowOrderAsString(backdrop, window1.get(), window2.get(),
926                                    window3.get()));
927
928   // Make the top window invisible and check.
929   window2.get()->Hide();
930   EXPECT_EQ("b,C,X,A",
931             GetWindowOrderAsString(backdrop, window1.get(), window2.get(),
932                                    window3.get()));
933   // Then delete window after window and see that everything is in order.
934   window1.reset();
935   EXPECT_EQ("b,C,X",
936             GetWindowOrderAsString(backdrop, window1.get(), window2.get(),
937                                    window3.get()));
938   window3.reset();
939   EXPECT_EQ("b,x",
940             GetWindowOrderAsString(backdrop, window1.get(), window2.get(),
941                                    window3.get()));
942   ShowTopWindowBackdrop(false);
943   EXPECT_EQ("b",
944             GetWindowOrderAsString(NULL, window1.get(), window2.get(),
945                                    window3.get()));
946 }
947
948 // Tests that when hidding the shelf, that the backdrop resizes to fill the
949 // entire workspace area.
950 TEST_F(WorkspaceLayoutManagerBackdropTest, ShelfVisibilityChangesBounds) {
951   ShelfLayoutManager* shelf_layout_manager =
952       Shell::GetPrimaryRootWindowController()->GetShelfLayoutManager();
953   ShowTopWindowBackdrop(true);
954   RunAllPendingInMessageLoop();
955
956   ASSERT_EQ(SHELF_VISIBLE, shelf_layout_manager->visibility_state());
957   gfx::Rect initial_bounds = default_container()->children()[0]->bounds();
958   shelf_layout_manager->SetAutoHideBehavior(SHELF_AUTO_HIDE_ALWAYS_HIDDEN);
959   shelf_layout_manager->UpdateVisibilityState();
960
961   // When the shelf is re-shown WorkspaceLayoutManager shrinks all children
962   // including the backdrop.
963   shelf_layout_manager->SetAutoHideBehavior(SHELF_AUTO_HIDE_BEHAVIOR_NEVER);
964   shelf_layout_manager->UpdateVisibilityState();
965   gfx::Rect reduced_bounds = default_container()->children()[0]->bounds();
966   EXPECT_LT(reduced_bounds.height(), initial_bounds.height());
967
968   shelf_layout_manager->SetAutoHideBehavior(SHELF_AUTO_HIDE_ALWAYS_HIDDEN);
969   shelf_layout_manager->UpdateVisibilityState();
970
971   EXPECT_GT(default_container()->children()[0]->bounds().height(),
972             reduced_bounds.height());
973 }
974
975 class WorkspaceLayoutManagerKeyboardTest : public test::AshTestBase {
976  public:
977   WorkspaceLayoutManagerKeyboardTest() {}
978   ~WorkspaceLayoutManagerKeyboardTest() override {}
979
980   void SetUp() override {
981     test::AshTestBase::SetUp();
982     UpdateDisplay("800x600");
983     aura::Window* default_container = Shell::GetContainer(
984         Shell::GetPrimaryRootWindow(), kShellWindowId_DefaultContainer);
985     layout_manager_ = static_cast<WorkspaceLayoutManager*>(
986         default_container->layout_manager());
987   }
988
989   aura::Window* CreateTestWindow(const gfx::Rect& bounds) {
990     return CreateTestWindowInShellWithBounds(bounds);
991   }
992
993   void ShowKeyboard() {
994     layout_manager_->OnKeyboardBoundsChanging(keyboard_bounds_);
995     restore_work_area_insets_ = Shell::GetScreen()->GetPrimaryDisplay().
996         GetWorkAreaInsets();
997     Shell::GetInstance()->SetDisplayWorkAreaInsets(
998         Shell::GetPrimaryRootWindow(),
999         gfx::Insets(0, 0, keyboard_bounds_.height(), 0));
1000   }
1001
1002   void HideKeyboard() {
1003     Shell::GetInstance()->SetDisplayWorkAreaInsets(
1004         Shell::GetPrimaryRootWindow(),
1005         restore_work_area_insets_);
1006     layout_manager_->OnKeyboardBoundsChanging(gfx::Rect());
1007   }
1008
1009   void SetKeyboardBounds(const gfx::Rect& bounds) {
1010     keyboard_bounds_ = bounds;
1011   }
1012
1013   void Focus(ui::TextInputClient* text_input_client) {
1014     if (switches::IsTextInputFocusManagerEnabled()) {
1015       ui::TextInputFocusManager::GetInstance()->FocusTextInputClient(
1016           text_input_client);
1017     } else {
1018       aura::Window* root_window =
1019           ash::Shell::GetInstance()->GetPrimaryRootWindow();
1020       ui::InputMethod* input_method =
1021           root_window->GetProperty(aura::client::kRootWindowInputMethodKey);
1022       input_method->SetFocusedTextInputClient(text_input_client);
1023     }
1024   }
1025
1026   void Blur(ui::TextInputClient* text_input_client) {
1027     if (switches::IsTextInputFocusManagerEnabled()) {
1028       ui::TextInputFocusManager::GetInstance()->BlurTextInputClient(
1029           text_input_client);
1030     } else {
1031       aura::Window* root_window =
1032           ash::Shell::GetInstance()->GetPrimaryRootWindow();
1033       ui::InputMethod* input_method =
1034           root_window->GetProperty(aura::client::kRootWindowInputMethodKey);
1035       input_method->SetFocusedTextInputClient(NULL);
1036     }
1037   }
1038
1039  private:
1040   gfx::Insets restore_work_area_insets_;
1041   gfx::Rect keyboard_bounds_;
1042   WorkspaceLayoutManager* layout_manager_;
1043
1044   DISALLOW_COPY_AND_ASSIGN(WorkspaceLayoutManagerKeyboardTest);
1045 };
1046
1047 class FakeTextInputClient : public ui::DummyTextInputClient {
1048  public:
1049   explicit FakeTextInputClient(gfx::NativeWindow window) : window_(window) {}
1050   ~FakeTextInputClient() override {}
1051
1052   gfx::NativeWindow GetAttachedWindow() const override { return window_; }
1053
1054  private:
1055   gfx::NativeWindow window_;
1056
1057   DISALLOW_COPY_AND_ASSIGN(FakeTextInputClient);
1058 };
1059
1060 // Tests that when a child window gains focus the top level window containing it
1061 // is resized to fit the remaining workspace area.
1062 TEST_F(WorkspaceLayoutManagerKeyboardTest, ChildWindowFocused) {
1063   gfx::Rect work_area(
1064       Shell::GetScreen()->GetPrimaryDisplay().work_area());
1065   gfx::Rect keyboard_bounds(work_area.x(),
1066                             work_area.y() + work_area.height() / 2,
1067                             work_area.width(),
1068                             work_area.height() / 2);
1069
1070   SetKeyboardBounds(keyboard_bounds);
1071
1072   aura::test::TestWindowDelegate delegate1;
1073   scoped_ptr<aura::Window> parent_window(CreateTestWindowInShellWithDelegate(
1074       &delegate1, -1, work_area));
1075   aura::test::TestWindowDelegate delegate2;
1076   scoped_ptr<aura::Window> window(CreateTestWindowInShellWithDelegate(
1077       &delegate2, -1, work_area));
1078   parent_window->AddChild(window.get());
1079
1080   FakeTextInputClient text_input_client(window.get());
1081   Focus(&text_input_client);
1082
1083   int available_height =
1084       Shell::GetScreen()->GetPrimaryDisplay().bounds().height() -
1085       keyboard_bounds.height();
1086
1087   gfx::Rect initial_window_bounds(50, 50, 100, 500);
1088   parent_window->SetBounds(initial_window_bounds);
1089   EXPECT_EQ(initial_window_bounds.ToString(),
1090             parent_window->bounds().ToString());
1091   ShowKeyboard();
1092   EXPECT_EQ(gfx::Rect(50, 0, 100, available_height).ToString(),
1093             parent_window->bounds().ToString());
1094   HideKeyboard();
1095   EXPECT_EQ(initial_window_bounds.ToString(),
1096             parent_window->bounds().ToString());
1097
1098   Blur(&text_input_client);
1099 }
1100
1101 TEST_F(WorkspaceLayoutManagerKeyboardTest, AdjustWindowForA11yKeyboard) {
1102   gfx::Rect work_area(
1103       Shell::GetScreen()->GetPrimaryDisplay().work_area());
1104   gfx::Rect keyboard_bounds(work_area.x(),
1105                             work_area.y() + work_area.height() / 2,
1106                             work_area.width(),
1107                             work_area.height() / 2);
1108
1109   SetKeyboardBounds(keyboard_bounds);
1110
1111   aura::test::TestWindowDelegate delegate;
1112   scoped_ptr<aura::Window> window(CreateTestWindowInShellWithDelegate(
1113       &delegate, -1, work_area));
1114
1115   FakeTextInputClient text_input_client(window.get());
1116   Focus(&text_input_client);
1117
1118   int available_height =
1119       Shell::GetScreen()->GetPrimaryDisplay().bounds().height() -
1120       keyboard_bounds.height();
1121
1122   EXPECT_EQ(gfx::Rect(work_area).ToString(), window->bounds().ToString());
1123   ShowKeyboard();
1124   EXPECT_EQ(gfx::Rect(work_area.origin(),
1125             gfx::Size(work_area.width(), available_height)).ToString(),
1126             window->bounds().ToString());
1127   HideKeyboard();
1128   EXPECT_EQ(gfx::Rect(work_area).ToString(), window->bounds().ToString());
1129
1130   gfx::Rect small_window_bound(50, 50, 100, 500);
1131   window->SetBounds(small_window_bound);
1132   EXPECT_EQ(small_window_bound.ToString(), window->bounds().ToString());
1133   ShowKeyboard();
1134   EXPECT_EQ(gfx::Rect(50, 0, 100, available_height).ToString(),
1135             window->bounds().ToString());
1136   HideKeyboard();
1137   EXPECT_EQ(small_window_bound.ToString(), window->bounds().ToString());
1138
1139   gfx::Rect occluded_window_bounds(50,
1140       keyboard_bounds.y() + keyboard_bounds.height()/2, 50,
1141       keyboard_bounds.height()/2);
1142   window->SetBounds(occluded_window_bounds);
1143   EXPECT_EQ(occluded_window_bounds.ToString(),
1144       occluded_window_bounds.ToString());
1145   ShowKeyboard();
1146   EXPECT_EQ(gfx::Rect(50,
1147                       keyboard_bounds.y() - keyboard_bounds.height()/2,
1148                       occluded_window_bounds.width(),
1149                       occluded_window_bounds.height()).ToString(),
1150             window->bounds().ToString());
1151   HideKeyboard();
1152   EXPECT_EQ(occluded_window_bounds.ToString(), window->bounds().ToString());
1153
1154   Blur(&text_input_client);
1155 }
1156
1157 }  // namespace ash