[Bluetooth, Performance] Cache the Bluetooth adapter name on Mac.
[chromium-blink-merge.git] / components / constrained_window / constrained_window_views_unittest.cc
blob645e5599e7c84b0b8fcc17e0ca040850e1cb6e90
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 "components/constrained_window/constrained_window_views.h"
7 #include "components/web_modal/test_web_contents_modal_dialog_host.h"
8 #include "ui/gfx/geometry/point.h"
9 #include "ui/gfx/geometry/rect.h"
10 #include "ui/gfx/geometry/size.h"
11 #include "ui/gfx/native_widget_types.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 using views::Widget;
19 namespace constrained_window {
20 namespace {
22 class DialogContents : public views::DialogDelegateView {
23 public:
24 DialogContents() {}
25 ~DialogContents() override {}
27 void set_preferred_size(const gfx::Size& preferred_size) {
28 preferred_size_ = preferred_size;
31 // Overriden from DialogDelegateView:
32 views::View* GetContentsView() override { return this; }
33 gfx::Size GetPreferredSize() const override { return preferred_size_; }
34 gfx::Size GetMinimumSize() const override { return gfx::Size(); }
36 private:
37 gfx::Size preferred_size_;
39 DISALLOW_COPY_AND_ASSIGN(DialogContents);
42 class ConstrainedWindowViewsTest : public views::ViewsTestBase {
43 public:
44 ConstrainedWindowViewsTest() : contents_(nullptr), dialog_(nullptr) {}
45 ~ConstrainedWindowViewsTest() override {}
47 void SetUp() override {
48 views::ViewsTestBase::SetUp();
49 contents_ = new DialogContents;
50 dialog_ = views::DialogDelegate::CreateDialogWidget(
51 contents_, GetContext(), nullptr);
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 void TearDown() override {
64 contents_ = nullptr;
65 dialog_host_.reset();
66 dialog_->CloseNow();
67 ViewsTestBase::TearDown();
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_; }
80 private:
81 DialogContents* contents_;
82 scoped_ptr<web_modal::TestWebContentsModalDialogHost> dialog_host_;
83 Widget* dialog_;
85 DISALLOW_COPY_AND_ASSIGN(ConstrainedWindowViewsTest);
88 } // namespace
90 // Make sure a dialog that increases its preferred size grows on the next
91 // position update.
92 TEST_F(ConstrainedWindowViewsTest, GrowModalDialogSize) {
93 UpdateWidgetModalDialogPosition(dialog(), dialog_host());
94 gfx::Size expected_size = GetDialogSize();
95 gfx::Size preferred_size = contents()->GetPreferredSize();
96 expected_size.Enlarge(50, 50);
97 preferred_size.Enlarge(50, 50);
98 contents()->set_preferred_size(preferred_size);
99 UpdateWidgetModalDialogPosition(dialog(), dialog_host());
100 EXPECT_EQ(expected_size.ToString(), GetDialogSize().ToString());
103 // Make sure a dialog that reduces its preferred size shrinks on the next
104 // position update.
105 TEST_F(ConstrainedWindowViewsTest, ShrinkModalDialogSize) {
106 UpdateWidgetModalDialogPosition(dialog(), dialog_host());
107 gfx::Size expected_size = GetDialogSize();
108 gfx::Size preferred_size = contents()->GetPreferredSize();
109 expected_size.Enlarge(-50, -50);
110 preferred_size.Enlarge(-50, -50);
111 contents()->set_preferred_size(preferred_size);
112 UpdateWidgetModalDialogPosition(dialog(), dialog_host());
113 EXPECT_EQ(expected_size.ToString(), GetDialogSize().ToString());
116 // Make sure browser modal dialogs are not affected by restrictions on web
117 // content modal dialog maximum sizes.
118 TEST_F(ConstrainedWindowViewsTest, MaximumBrowserDialogSize) {
119 UpdateWidgetModalDialogPosition(dialog(), dialog_host());
120 gfx::Size dialog_size = GetDialogSize();
121 gfx::Size max_dialog_size = dialog_size;
122 max_dialog_size.Enlarge(-50, -50);
123 dialog_host()->set_max_dialog_size(max_dialog_size);
124 UpdateWidgetModalDialogPosition(dialog(), dialog_host());
125 EXPECT_EQ(dialog_size.ToString(), GetDialogSize().ToString());
128 // Web content modal dialogs should not get a size larger than what the dialog
129 // host gives as the maximum size.
130 TEST_F(ConstrainedWindowViewsTest, MaximumWebContentsDialogSize) {
131 UpdateWebContentsModalDialogPosition(dialog(), dialog_host());
132 gfx::Size full_dialog_size = GetDialogSize();
133 gfx::Size max_dialog_size = full_dialog_size;
134 max_dialog_size.Enlarge(-50, -50);
135 dialog_host()->set_max_dialog_size(max_dialog_size);
136 UpdateWebContentsModalDialogPosition(dialog(), dialog_host());
137 // The top border of the dialog is intentionally drawn outside the area
138 // specified by the dialog host, so add it to the size the dialog is expected
139 // to occupy.
140 gfx::Size expected_size = max_dialog_size;
141 views::Border* border = dialog()->non_client_view()->frame_view()->border();
142 if (border)
143 expected_size.Enlarge(0, border->GetInsets().top());
144 EXPECT_EQ(expected_size.ToString(), GetDialogSize().ToString());
146 // Increasing the maximum dialog size should bring the dialog back to its
147 // original size.
148 max_dialog_size.Enlarge(100, 100);
149 dialog_host()->set_max_dialog_size(max_dialog_size);
150 UpdateWebContentsModalDialogPosition(dialog(), dialog_host());
151 EXPECT_EQ(full_dialog_size.ToString(), GetDialogSize().ToString());
154 } // namespace constrained_window