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 { |
30 if (destroy_watcher_) | |
31 *destroy_watcher_ = true; | |
32 } | |
29 | 33 |
30 void set_preferred_size(const gfx::Size& preferred_size) { | 34 void set_preferred_size(const gfx::Size& preferred_size) { |
31 preferred_size_ = preferred_size; | 35 preferred_size_ = preferred_size; |
32 } | 36 } |
33 | 37 |
34 // Overriden from DialogDelegateView: | 38 void set_modal_type(ui::ModalType modal_type) { modal_type_ = modal_type; } |
39 void set_destroy_watcher(bool* destroy_watcher) { | |
40 destroy_watcher_ = destroy_watcher; | |
41 } | |
42 | |
43 // DialogDelegateView: | |
35 views::View* GetContentsView() override { return this; } | 44 views::View* GetContentsView() override { return this; } |
36 gfx::Size GetPreferredSize() const override { return preferred_size_; } | 45 gfx::Size GetPreferredSize() const override { return preferred_size_; } |
37 gfx::Size GetMinimumSize() const override { return gfx::Size(); } | 46 gfx::Size GetMinimumSize() const override { return gfx::Size(); } |
38 | 47 |
48 // WidgetDelegate: | |
49 ui::ModalType GetModalType() const override { return modal_type_; } | |
50 | |
39 private: | 51 private: |
40 gfx::Size preferred_size_; | 52 gfx::Size preferred_size_; |
53 ui::ModalType modal_type_ = ui::MODAL_TYPE_NONE; | |
54 bool* destroy_watcher_ = nullptr; | |
41 | 55 |
42 DISALLOW_COPY_AND_ASSIGN(DialogContents); | 56 DISALLOW_COPY_AND_ASSIGN(DialogContents); |
43 }; | 57 }; |
44 | 58 |
59 class TestConstrainedWindowViewsClient | |
msw
2016/10/18 23:47:15
nit: add a simple class comment, event something l
tapted
2016/10/19 08:13:48
Done.
| |
60 : public constrained_window::ConstrainedWindowViewsClient { | |
61 public: | |
62 TestConstrainedWindowViewsClient() {} | |
63 | |
64 // ConstrainedWindowViewsClient: | |
65 web_modal::ModalDialogHost* GetModalDialogHost( | |
66 gfx::NativeWindow parent) override { | |
67 return nullptr; | |
68 } | |
69 gfx::NativeView GetDialogHostView(gfx::NativeWindow parent) override { | |
70 return nullptr; | |
71 } | |
72 | |
73 private: | |
74 DISALLOW_COPY_AND_ASSIGN(TestConstrainedWindowViewsClient); | |
75 }; | |
76 | |
77 // ViewsDelegate to provide context to dialog creation functions such as | |
78 // CreateBrowserModalDialogViews() which do not allow InitParams to be set, and | |
79 // pass a null |context| argument to DialogDelegate::CreateDialogWidget(). | |
80 class TestViewsDelegateWithContext : public views::TestViewsDelegate { | |
81 public: | |
82 TestViewsDelegateWithContext() {} | |
83 | |
84 void set_context(gfx::NativeWindow context) { context_ = context; } | |
85 | |
86 // ViewsDelegate: | |
87 void OnBeforeWidgetInit( | |
88 views::Widget::InitParams* params, | |
89 views::internal::NativeWidgetDelegate* delegate) override { | |
90 if (!params->context) | |
msw
2016/10/18 23:47:15
It's a bummer that we need this, but IIRC a null c
tapted
2016/10/19 08:13:48
It was certainly true that Weird Things would happ
msw
2016/10/19 19:02:07
This is certainly fine as-is, it's a much more dir
| |
91 params->context = context_; | |
92 TestViewsDelegate::OnBeforeWidgetInit(params, delegate); | |
93 } | |
94 | |
95 private: | |
96 gfx::NativeWindow context_ = nullptr; | |
97 | |
98 DISALLOW_COPY_AND_ASSIGN(TestViewsDelegateWithContext); | |
99 }; | |
100 | |
45 class ConstrainedWindowViewsTest : public views::ViewsTestBase { | 101 class ConstrainedWindowViewsTest : public views::ViewsTestBase { |
46 public: | 102 public: |
47 ConstrainedWindowViewsTest() : contents_(nullptr), dialog_(nullptr) {} | 103 ConstrainedWindowViewsTest() : contents_(nullptr), dialog_(nullptr) {} |
48 ~ConstrainedWindowViewsTest() override {} | 104 ~ConstrainedWindowViewsTest() override {} |
49 | 105 |
50 void SetUp() override { | 106 void SetUp() override { |
107 std::unique_ptr<TestViewsDelegateWithContext> views_delegate( | |
108 new TestViewsDelegateWithContext); | |
109 | |
110 // set_views_delegate() must be called before SetUp(), and GetContext() is | |
111 // null before that, so take a reference. | |
112 TestViewsDelegateWithContext* views_delegate_weak = views_delegate.get(); | |
113 set_views_delegate(std::move(views_delegate)); | |
51 views::ViewsTestBase::SetUp(); | 114 views::ViewsTestBase::SetUp(); |
115 views_delegate_weak->set_context(GetContext()); | |
116 | |
52 contents_ = new DialogContents; | 117 contents_ = new DialogContents; |
53 dialog_ = views::DialogDelegate::CreateDialogWidget( | 118 dialog_ = views::DialogDelegate::CreateDialogWidget( |
54 contents_, GetContext(), nullptr); | 119 contents_, GetContext(), nullptr); |
55 dialog_host_.reset(new web_modal::TestWebContentsModalDialogHost( | 120 dialog_host_.reset(new web_modal::TestWebContentsModalDialogHost( |
56 dialog_->GetNativeView())); | 121 dialog_->GetNativeView())); |
57 dialog_host_->set_max_dialog_size(gfx::Size(5000, 5000)); | 122 dialog_host_->set_max_dialog_size(gfx::Size(5000, 5000)); |
58 | 123 |
59 // Make sure the dialog size is dominated by the preferred size of the | 124 // Make sure the dialog size is dominated by the preferred size of the |
60 // contents. | 125 // contents. |
61 gfx::Size preferred_size = dialog()->GetRootView()->GetPreferredSize(); | 126 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()); | 212 EXPECT_EQ(expected_size.ToString(), GetDialogSize().ToString()); |
148 | 213 |
149 // Increasing the maximum dialog size should bring the dialog back to its | 214 // Increasing the maximum dialog size should bring the dialog back to its |
150 // original size. | 215 // original size. |
151 max_dialog_size.Enlarge(100, 100); | 216 max_dialog_size.Enlarge(100, 100); |
152 dialog_host()->set_max_dialog_size(max_dialog_size); | 217 dialog_host()->set_max_dialog_size(max_dialog_size); |
153 UpdateWebContentsModalDialogPosition(dialog(), dialog_host()); | 218 UpdateWebContentsModalDialogPosition(dialog(), dialog_host()); |
154 EXPECT_EQ(full_dialog_size.ToString(), GetDialogSize().ToString()); | 219 EXPECT_EQ(full_dialog_size.ToString(), GetDialogSize().ToString()); |
155 } | 220 } |
156 | 221 |
222 // Ensure CreateBrowserModalDialogViews() works correctly with a null parent. | |
223 TEST_F(ConstrainedWindowViewsTest, NullModalParent) { | |
224 bool destroyed = false; | |
225 SetConstrainedWindowViewsClient( | |
226 base::MakeUnique<TestConstrainedWindowViewsClient>()); | |
227 DialogContents* contents = new DialogContents; | |
228 contents->set_destroy_watcher(&destroyed); | |
229 contents->set_modal_type(ui::MODAL_TYPE_WINDOW); | |
230 views::Widget* widget = CreateBrowserModalDialogViews(contents, nullptr); | |
231 widget->Show(); | |
232 EXPECT_TRUE(widget->IsVisible()); | |
233 widget->CloseNow(); | |
234 EXPECT_TRUE(destroyed); | |
msw
2016/10/18 23:47:15
nit: this (and the supporting code) is probably un
tapted
2016/10/19 08:13:48
Done. (I think I added this to satisfy myself that
| |
235 } | |
236 | |
157 } // namespace constrained_window | 237 } // namespace constrained_window |
OLD | NEW |