Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / ui / views / constrained_window_views_unittest.cc
blobe6a50a701e80f41f6aae6c00e744d1cc8a259a09
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.
5 #include "chrome/browser/ui/views/constrained_window_views.h"
7 #include "components/web_modal/test_web_contents_modal_dialog_host.h"
8 #include "ui/gfx/native_widget_types.h"
9 #include "ui/gfx/point.h"
10 #include "ui/gfx/rect.h"
11 #include "ui/gfx/size.h"
12 #include "ui/views/border.h"
13 #include "ui/views/test/views_test_base.h"
14 #include "ui/views/widget/widget.h"
15 #include "ui/views/window/dialog_delegate.h"
17 namespace views {
19 class DialogContents : public DialogDelegateView {
20 public:
21 DialogContents() {}
22 virtual ~DialogContents() {}
24 void set_preferred_size(const gfx::Size& preferred_size) {
25 preferred_size_ = preferred_size;
28 // Overriden from DialogDelegateView:
29 virtual View* GetContentsView() OVERRIDE { return this; }
30 virtual gfx::Size GetPreferredSize() OVERRIDE { return preferred_size_; }
31 virtual gfx::Size GetMinimumSize() OVERRIDE { return gfx::Size(); }
33 private:
34 gfx::Size preferred_size_;
36 DISALLOW_COPY_AND_ASSIGN(DialogContents);
39 class ConstrainedWindowViewsTest : public ViewsTestBase {
40 public:
41 ConstrainedWindowViewsTest() : contents_(NULL) {}
42 virtual ~ConstrainedWindowViewsTest() {}
44 virtual void SetUp() OVERRIDE {
45 ViewsTestBase::SetUp();
46 contents_ = new DialogContents;
47 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_WINDOW);
48 params.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
49 params.delegate = contents_;
50 dialog_.reset(new Widget);
51 dialog_->Init(params);
52 dialog_host_.reset(new web_modal::TestWebContentsModalDialogHost(
53 dialog_->GetNativeView()));
54 dialog_host_->set_max_dialog_size(gfx::Size(5000, 5000));
56 // Make sure the dialog size is dominated by the preferred size of the
57 // contents.
58 gfx::Size preferred_size = dialog()->GetRootView()->GetPreferredSize();
59 preferred_size.Enlarge(500, 500);
60 contents()->set_preferred_size(preferred_size);
63 virtual void TearDown() OVERRIDE {
64 ViewsTestBase::TearDown();
65 contents_ = NULL;
66 dialog_host_.reset();
67 dialog_.reset();
70 gfx::Size GetDialogSize() {
71 return dialog()->GetRootView()->GetBoundsInScreen().size();
74 DialogContents* contents() { return contents_; }
75 web_modal::TestWebContentsModalDialogHost* dialog_host() {
76 return dialog_host_.get();
78 Widget* dialog() { return dialog_.get(); }
80 private:
81 DialogContents* contents_;
82 scoped_ptr<web_modal::TestWebContentsModalDialogHost> dialog_host_;
83 scoped_ptr<Widget> dialog_;
85 DISALLOW_COPY_AND_ASSIGN(ConstrainedWindowViewsTest);
88 // Make sure a dialog that increases its preferred size grows on the next
89 // position update.
90 TEST_F(ConstrainedWindowViewsTest, GrowModalDialogSize) {
91 UpdateBrowserModalDialogPosition(dialog(), dialog_host());
92 gfx::Size expected_size = GetDialogSize();
93 gfx::Size preferred_size = contents()->GetPreferredSize();
94 expected_size.Enlarge(50, 50);
95 preferred_size.Enlarge(50, 50);
96 contents()->set_preferred_size(preferred_size);
97 UpdateBrowserModalDialogPosition(dialog(), dialog_host());
98 EXPECT_EQ(expected_size.ToString(), GetDialogSize().ToString());
101 // Make sure a dialog that reduces its preferred size shrinks on the next
102 // position update.
103 TEST_F(ConstrainedWindowViewsTest, ShrinkModalDialogSize) {
104 UpdateBrowserModalDialogPosition(dialog(), dialog_host());
105 gfx::Size expected_size = GetDialogSize();
106 gfx::Size preferred_size = contents()->GetPreferredSize();
107 expected_size.Enlarge(-50, -50);
108 preferred_size.Enlarge(-50, -50);
109 contents()->set_preferred_size(preferred_size);
110 UpdateBrowserModalDialogPosition(dialog(), dialog_host());
111 EXPECT_EQ(expected_size.ToString(), GetDialogSize().ToString());
114 // Make sure browser modal dialogs are not affected by restrictions on web
115 // content modal dialog maximum sizes.
116 TEST_F(ConstrainedWindowViewsTest, MaximumBrowserDialogSize) {
117 UpdateBrowserModalDialogPosition(dialog(), dialog_host());
118 gfx::Size dialog_size = GetDialogSize();
119 gfx::Size max_dialog_size = dialog_size;
120 max_dialog_size.Enlarge(-50, -50);
121 dialog_host()->set_max_dialog_size(max_dialog_size);
122 UpdateBrowserModalDialogPosition(dialog(), dialog_host());
123 EXPECT_EQ(dialog_size.ToString(), GetDialogSize().ToString());
126 // Web content modal dialogs should not get a size larger than what the dialog
127 // host gives as the maximum size.
128 TEST_F(ConstrainedWindowViewsTest, MaximumWebContentsDialogSize) {
129 UpdateWebContentsModalDialogPosition(dialog(), dialog_host());
130 gfx::Size full_dialog_size = GetDialogSize();
131 gfx::Size max_dialog_size = full_dialog_size;
132 max_dialog_size.Enlarge(-50, -50);
133 dialog_host()->set_max_dialog_size(max_dialog_size);
134 UpdateWebContentsModalDialogPosition(dialog(), dialog_host());
135 // The top border of the dialog is intentionally drawn outside the area
136 // specified by the dialog host, so add it to the size the dialog is expected
137 // to occupy.
138 gfx::Size expected_size = max_dialog_size;
139 Border* border = dialog()->non_client_view()->frame_view()->border();
140 if (border)
141 expected_size.Enlarge(0, border->GetInsets().top());
142 EXPECT_EQ(expected_size.ToString(), GetDialogSize().ToString());
144 // Increasing the maximum dialog size should bring the dialog back to its
145 // original size.
146 max_dialog_size.Enlarge(100, 100);
147 dialog_host()->set_max_dialog_size(max_dialog_size);
148 UpdateWebContentsModalDialogPosition(dialog(), dialog_host());
149 EXPECT_EQ(full_dialog_size.ToString(), GetDialogSize().ToString());
152 } // namespace views