• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2013 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/window_positioner.h"
6 
7 #include "ash/shell.h"
8 #include "ash/shell/toplevel_window.h"
9 #include "ash/test/ash_test_base.h"
10 #include "ash/wm/window_positioner.h"
11 #include "ash/wm/window_state.h"
12 #include "ui/aura/root_window.h"
13 #include "ui/gfx/screen.h"
14 #include "ui/views/widget/widget.h"
15 #include "ui/views/widget/widget_delegate.h"
16 
17 namespace ash {
18 
19 typedef test::AshTestBase WindowPositionerTest;
20 
TEST_F(WindowPositionerTest,OpenMaximizedWindowOnSecondDisplay)21 TEST_F(WindowPositionerTest, OpenMaximizedWindowOnSecondDisplay) {
22   if (!SupportsMultipleDisplays())
23     return;
24   UpdateDisplay("400x400,500x500");
25   Shell::GetInstance()->set_target_root_window(
26       Shell::GetAllRootWindows()[1]);
27   shell::ToplevelWindow::CreateParams params;
28   params.can_resize = true;
29   params.can_maximize = true;
30   views::Widget* widget =
31       shell::ToplevelWindow::CreateToplevelWindow(params);
32   EXPECT_EQ("400,0 500x453", widget->GetWindowBoundsInScreen().ToString());
33 }
34 
TEST_F(WindowPositionerTest,OpenDefaultWindowOnSecondDisplay)35 TEST_F(WindowPositionerTest, OpenDefaultWindowOnSecondDisplay) {
36   if (!SupportsMultipleDisplays())
37     return;
38 #if defined(OS_WIN)
39   ash::WindowPositioner::SetMaximizeFirstWindow(true);
40 #endif
41   UpdateDisplay("400x400,1400x900");
42   aura::Window* second_root_window = Shell::GetAllRootWindows()[1];
43   Shell::GetInstance()->set_target_root_window(
44       second_root_window);
45   shell::ToplevelWindow::CreateParams params;
46   params.can_resize = true;
47   params.can_maximize = true;
48   views::Widget* widget =
49       shell::ToplevelWindow::CreateToplevelWindow(params);
50   gfx::Rect bounds = widget->GetWindowBoundsInScreen();
51 #if defined(OS_WIN)
52   EXPECT_TRUE(widget->IsMaximized());
53 #else
54   // The window should be in the 2nd display with the default size.
55   EXPECT_EQ("300x300", bounds.size().ToString());
56 #endif
57   EXPECT_TRUE(Shell::GetScreen()->GetDisplayNearestWindow(
58       second_root_window).bounds().Contains(bounds));
59 }
60 
61 namespace {
62 
63 // A WidgetDelegate that returns the out of display saved bounds.
64 class OutOfDisplayDelegate : public views::WidgetDelegate {
65  public:
OutOfDisplayDelegate(views::Widget * widget)66   explicit OutOfDisplayDelegate(views::Widget* widget) : widget_(widget) {}
~OutOfDisplayDelegate()67   virtual ~OutOfDisplayDelegate() {}
68 
69   // Overridden from WidgetDelegate:
DeleteDelegate()70   virtual void DeleteDelegate() OVERRIDE {
71     delete this;
72   }
GetWidget()73   virtual views::Widget* GetWidget() OVERRIDE {
74     return widget_;
75   }
GetWidget() const76   virtual const views::Widget* GetWidget() const OVERRIDE {
77     return widget_;
78   }
GetSavedWindowPlacement(const views::Widget * widget,gfx::Rect * bounds,ui::WindowShowState * show_state) const79   virtual bool GetSavedWindowPlacement(
80       const views::Widget* widget,
81       gfx::Rect* bounds,
82       ui::WindowShowState* show_state) const OVERRIDE {
83     bounds->SetRect(450, 10, 100, 100);
84     *show_state = ui::SHOW_STATE_NORMAL;
85     return true;
86   }
87 
88  private:
89   views::Widget* widget_;
90 
91   DISALLOW_COPY_AND_ASSIGN(OutOfDisplayDelegate);
92 };
93 
94 }  // namespace
95 
TEST_F(WindowPositionerTest,EnsureMinimumVisibility)96 TEST_F(WindowPositionerTest, EnsureMinimumVisibility) {
97   if (!SupportsHostWindowResize())
98     return;
99 
100   UpdateDisplay("400x400");
101   views::Widget* widget = new views::Widget();
102   views::Widget::InitParams params(views::Widget::InitParams::TYPE_WINDOW);
103   params.delegate = new OutOfDisplayDelegate(widget);
104   params.context = Shell::GetPrimaryRootWindow();
105   widget->Init(params);
106   widget->SetBounds(gfx::Rect(450,10, 100, 100));
107   wm::GetWindowState(widget->GetNativeView())->set_minimum_visibility(true);
108   widget->Show();
109   // Make sure the bounds is adjusted to be inside the work area.
110   EXPECT_EQ("390,10 100x100", widget->GetWindowBoundsInScreen().ToString());
111   widget->CloseNow();
112 }
113 
114 }  // namespace
115