OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "components/constrained_window/constrained_window_views.h" | 5 #include "components/constrained_window/constrained_window_views.h" |
6 | 6 |
7 #include <memory> | 7 #include <memory> |
8 | 8 |
9 #include "base/macros.h" | 9 #include "base/macros.h" |
| 10 #include "components/constrained_window/constrained_window_views_client.h" |
10 #include "components/web_modal/test_web_contents_modal_dialog_host.h" | 11 #include "components/web_modal/test_web_contents_modal_dialog_host.h" |
11 #include "ui/gfx/geometry/point.h" | 12 #include "ui/gfx/geometry/point.h" |
12 #include "ui/gfx/geometry/rect.h" | 13 #include "ui/gfx/geometry/rect.h" |
13 #include "ui/gfx/geometry/size.h" | 14 #include "ui/gfx/geometry/size.h" |
14 #include "ui/gfx/native_widget_types.h" | 15 #include "ui/gfx/native_widget_types.h" |
15 #include "ui/views/border.h" | 16 #include "ui/views/border.h" |
16 #include "ui/views/test/views_test_base.h" | 17 #include "ui/views/test/views_test_base.h" |
17 #include "ui/views/widget/widget.h" | 18 #include "ui/views/widget/widget.h" |
18 #include "ui/views/window/dialog_delegate.h" | 19 #include "ui/views/window/dialog_delegate.h" |
19 | 20 |
20 using views::Widget; | 21 using views::Widget; |
21 | 22 |
22 namespace constrained_window { | 23 namespace constrained_window { |
23 namespace { | 24 namespace { |
24 | 25 |
25 class DialogContents : public views::DialogDelegateView { | 26 class DialogContents : public views::DialogDelegateView { |
26 public: | 27 public: |
27 DialogContents() {} | 28 DialogContents() {} |
28 ~DialogContents() override {} | 29 ~DialogContents() override {} |
29 | 30 |
30 void set_preferred_size(const gfx::Size& preferred_size) { | 31 void set_preferred_size(const gfx::Size& preferred_size) { |
31 preferred_size_ = preferred_size; | 32 preferred_size_ = preferred_size; |
32 } | 33 } |
33 | 34 |
34 // Overriden from DialogDelegateView: | 35 void set_modal_type(ui::ModalType modal_type) { modal_type_ = modal_type; } |
| 36 |
| 37 // DialogDelegateView: |
35 views::View* GetContentsView() override { return this; } | 38 views::View* GetContentsView() override { return this; } |
36 gfx::Size GetPreferredSize() const override { return preferred_size_; } | 39 gfx::Size GetPreferredSize() const override { return preferred_size_; } |
37 gfx::Size GetMinimumSize() const override { return gfx::Size(); } | 40 gfx::Size GetMinimumSize() const override { return gfx::Size(); } |
38 | 41 |
| 42 // WidgetDelegate: |
| 43 ui::ModalType GetModalType() const override { return modal_type_; } |
| 44 |
39 private: | 45 private: |
40 gfx::Size preferred_size_; | 46 gfx::Size preferred_size_; |
| 47 ui::ModalType modal_type_ = ui::MODAL_TYPE_NONE; |
41 | 48 |
42 DISALLOW_COPY_AND_ASSIGN(DialogContents); | 49 DISALLOW_COPY_AND_ASSIGN(DialogContents); |
43 }; | 50 }; |
44 | 51 |
| 52 // Dummy client that returns a null modal dialog host and host view. |
| 53 class TestConstrainedWindowViewsClient |
| 54 : public constrained_window::ConstrainedWindowViewsClient { |
| 55 public: |
| 56 TestConstrainedWindowViewsClient() {} |
| 57 |
| 58 // ConstrainedWindowViewsClient: |
| 59 web_modal::ModalDialogHost* GetModalDialogHost( |
| 60 gfx::NativeWindow parent) override { |
| 61 return nullptr; |
| 62 } |
| 63 gfx::NativeView GetDialogHostView(gfx::NativeWindow parent) override { |
| 64 return nullptr; |
| 65 } |
| 66 |
| 67 private: |
| 68 DISALLOW_COPY_AND_ASSIGN(TestConstrainedWindowViewsClient); |
| 69 }; |
| 70 |
| 71 // ViewsDelegate to provide context to dialog creation functions such as |
| 72 // CreateBrowserModalDialogViews() which do not allow InitParams to be set, and |
| 73 // pass a null |context| argument to DialogDelegate::CreateDialogWidget(). |
| 74 class TestViewsDelegateWithContext : public views::TestViewsDelegate { |
| 75 public: |
| 76 TestViewsDelegateWithContext() {} |
| 77 |
| 78 void set_context(gfx::NativeWindow context) { context_ = context; } |
| 79 |
| 80 // ViewsDelegate: |
| 81 void OnBeforeWidgetInit( |
| 82 views::Widget::InitParams* params, |
| 83 views::internal::NativeWidgetDelegate* delegate) override { |
| 84 if (!params->context) |
| 85 params->context = context_; |
| 86 TestViewsDelegate::OnBeforeWidgetInit(params, delegate); |
| 87 } |
| 88 |
| 89 private: |
| 90 gfx::NativeWindow context_ = nullptr; |
| 91 |
| 92 DISALLOW_COPY_AND_ASSIGN(TestViewsDelegateWithContext); |
| 93 }; |
| 94 |
45 class ConstrainedWindowViewsTest : public views::ViewsTestBase { | 95 class ConstrainedWindowViewsTest : public views::ViewsTestBase { |
46 public: | 96 public: |
47 ConstrainedWindowViewsTest() : contents_(nullptr), dialog_(nullptr) {} | 97 ConstrainedWindowViewsTest() : contents_(nullptr), dialog_(nullptr) {} |
48 ~ConstrainedWindowViewsTest() override {} | 98 ~ConstrainedWindowViewsTest() override {} |
49 | 99 |
50 void SetUp() override { | 100 void SetUp() override { |
| 101 std::unique_ptr<TestViewsDelegateWithContext> views_delegate( |
| 102 new TestViewsDelegateWithContext); |
| 103 |
| 104 // set_views_delegate() must be called before SetUp(), and GetContext() is |
| 105 // null before that, so take a reference. |
| 106 TestViewsDelegateWithContext* views_delegate_weak = views_delegate.get(); |
| 107 set_views_delegate(std::move(views_delegate)); |
51 views::ViewsTestBase::SetUp(); | 108 views::ViewsTestBase::SetUp(); |
| 109 views_delegate_weak->set_context(GetContext()); |
| 110 |
52 contents_ = new DialogContents; | 111 contents_ = new DialogContents; |
53 dialog_ = views::DialogDelegate::CreateDialogWidget( | 112 dialog_ = views::DialogDelegate::CreateDialogWidget( |
54 contents_, GetContext(), nullptr); | 113 contents_, GetContext(), nullptr); |
55 dialog_host_.reset(new web_modal::TestWebContentsModalDialogHost( | 114 dialog_host_.reset(new web_modal::TestWebContentsModalDialogHost( |
56 dialog_->GetNativeView())); | 115 dialog_->GetNativeView())); |
57 dialog_host_->set_max_dialog_size(gfx::Size(5000, 5000)); | 116 dialog_host_->set_max_dialog_size(gfx::Size(5000, 5000)); |
58 | 117 |
59 // Make sure the dialog size is dominated by the preferred size of the | 118 // Make sure the dialog size is dominated by the preferred size of the |
60 // contents. | 119 // contents. |
61 gfx::Size preferred_size = dialog()->GetRootView()->GetPreferredSize(); | 120 gfx::Size preferred_size = dialog()->GetRootView()->GetPreferredSize(); |
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
147 EXPECT_EQ(expected_size.ToString(), GetDialogSize().ToString()); | 206 EXPECT_EQ(expected_size.ToString(), GetDialogSize().ToString()); |
148 | 207 |
149 // Increasing the maximum dialog size should bring the dialog back to its | 208 // Increasing the maximum dialog size should bring the dialog back to its |
150 // original size. | 209 // original size. |
151 max_dialog_size.Enlarge(100, 100); | 210 max_dialog_size.Enlarge(100, 100); |
152 dialog_host()->set_max_dialog_size(max_dialog_size); | 211 dialog_host()->set_max_dialog_size(max_dialog_size); |
153 UpdateWebContentsModalDialogPosition(dialog(), dialog_host()); | 212 UpdateWebContentsModalDialogPosition(dialog(), dialog_host()); |
154 EXPECT_EQ(full_dialog_size.ToString(), GetDialogSize().ToString()); | 213 EXPECT_EQ(full_dialog_size.ToString(), GetDialogSize().ToString()); |
155 } | 214 } |
156 | 215 |
| 216 // Ensure CreateBrowserModalDialogViews() works correctly with a null parent. |
| 217 TEST_F(ConstrainedWindowViewsTest, NullModalParent) { |
| 218 // Use desktop widgets (except on ChromeOS) for extra coverage. |
| 219 views_delegate()->set_use_desktop_native_widgets(true); |
| 220 |
| 221 SetConstrainedWindowViewsClient( |
| 222 base::MakeUnique<TestConstrainedWindowViewsClient>()); |
| 223 DialogContents* contents = new DialogContents; |
| 224 contents->set_modal_type(ui::MODAL_TYPE_WINDOW); |
| 225 views::Widget* widget = CreateBrowserModalDialogViews(contents, nullptr); |
| 226 widget->Show(); |
| 227 EXPECT_TRUE(widget->IsVisible()); |
| 228 widget->CloseNow(); |
| 229 } |
| 230 |
157 } // namespace constrained_window | 231 } // namespace constrained_window |
OLD | NEW |