Index: components/constrained_window/constrained_window_views_unittest.cc |
diff --git a/components/constrained_window/constrained_window_views_unittest.cc b/components/constrained_window/constrained_window_views_unittest.cc |
index e929867eb8175a7a8d8ebd8e0206e879f0b8dd0e..5ff3034e3d3decc86b888c28b2797449c0fea61d 100644 |
--- a/components/constrained_window/constrained_window_views_unittest.cc |
+++ b/components/constrained_window/constrained_window_views_unittest.cc |
@@ -7,6 +7,7 @@ |
#include <memory> |
#include "base/macros.h" |
+#include "components/constrained_window/constrained_window_views_client.h" |
#include "components/web_modal/test_web_contents_modal_dialog_host.h" |
#include "ui/gfx/geometry/point.h" |
#include "ui/gfx/geometry/rect.h" |
@@ -25,30 +26,94 @@ namespace { |
class DialogContents : public views::DialogDelegateView { |
public: |
DialogContents() {} |
- ~DialogContents() override {} |
+ ~DialogContents() override { |
+ if (destroy_watcher_) |
+ *destroy_watcher_ = true; |
+ } |
void set_preferred_size(const gfx::Size& preferred_size) { |
preferred_size_ = preferred_size; |
} |
- // Overriden from DialogDelegateView: |
+ void set_modal_type(ui::ModalType modal_type) { modal_type_ = modal_type; } |
+ void set_destroy_watcher(bool* destroy_watcher) { |
+ destroy_watcher_ = destroy_watcher; |
+ } |
+ |
+ // DialogDelegateView: |
views::View* GetContentsView() override { return this; } |
gfx::Size GetPreferredSize() const override { return preferred_size_; } |
gfx::Size GetMinimumSize() const override { return gfx::Size(); } |
+ // WidgetDelegate: |
+ ui::ModalType GetModalType() const override { return modal_type_; } |
+ |
private: |
gfx::Size preferred_size_; |
+ ui::ModalType modal_type_ = ui::MODAL_TYPE_NONE; |
+ bool* destroy_watcher_ = nullptr; |
DISALLOW_COPY_AND_ASSIGN(DialogContents); |
}; |
+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.
|
+ : public constrained_window::ConstrainedWindowViewsClient { |
+ public: |
+ TestConstrainedWindowViewsClient() {} |
+ |
+ // ConstrainedWindowViewsClient: |
+ web_modal::ModalDialogHost* GetModalDialogHost( |
+ gfx::NativeWindow parent) override { |
+ return nullptr; |
+ } |
+ gfx::NativeView GetDialogHostView(gfx::NativeWindow parent) override { |
+ return nullptr; |
+ } |
+ |
+ private: |
+ DISALLOW_COPY_AND_ASSIGN(TestConstrainedWindowViewsClient); |
+}; |
+ |
+// ViewsDelegate to provide context to dialog creation functions such as |
+// CreateBrowserModalDialogViews() which do not allow InitParams to be set, and |
+// pass a null |context| argument to DialogDelegate::CreateDialogWidget(). |
+class TestViewsDelegateWithContext : public views::TestViewsDelegate { |
+ public: |
+ TestViewsDelegateWithContext() {} |
+ |
+ void set_context(gfx::NativeWindow context) { context_ = context; } |
+ |
+ // ViewsDelegate: |
+ void OnBeforeWidgetInit( |
+ views::Widget::InitParams* params, |
+ views::internal::NativeWidgetDelegate* delegate) override { |
+ 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
|
+ params->context = context_; |
+ TestViewsDelegate::OnBeforeWidgetInit(params, delegate); |
+ } |
+ |
+ private: |
+ gfx::NativeWindow context_ = nullptr; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(TestViewsDelegateWithContext); |
+}; |
+ |
class ConstrainedWindowViewsTest : public views::ViewsTestBase { |
public: |
ConstrainedWindowViewsTest() : contents_(nullptr), dialog_(nullptr) {} |
~ConstrainedWindowViewsTest() override {} |
void SetUp() override { |
+ std::unique_ptr<TestViewsDelegateWithContext> views_delegate( |
+ new TestViewsDelegateWithContext); |
+ |
+ // set_views_delegate() must be called before SetUp(), and GetContext() is |
+ // null before that, so take a reference. |
+ TestViewsDelegateWithContext* views_delegate_weak = views_delegate.get(); |
+ set_views_delegate(std::move(views_delegate)); |
views::ViewsTestBase::SetUp(); |
+ views_delegate_weak->set_context(GetContext()); |
+ |
contents_ = new DialogContents; |
dialog_ = views::DialogDelegate::CreateDialogWidget( |
contents_, GetContext(), nullptr); |
@@ -154,4 +219,19 @@ TEST_F(ConstrainedWindowViewsTest, MaximumWebContentsDialogSize) { |
EXPECT_EQ(full_dialog_size.ToString(), GetDialogSize().ToString()); |
} |
+// Ensure CreateBrowserModalDialogViews() works correctly with a null parent. |
+TEST_F(ConstrainedWindowViewsTest, NullModalParent) { |
+ bool destroyed = false; |
+ SetConstrainedWindowViewsClient( |
+ base::MakeUnique<TestConstrainedWindowViewsClient>()); |
+ DialogContents* contents = new DialogContents; |
+ contents->set_destroy_watcher(&destroyed); |
+ contents->set_modal_type(ui::MODAL_TYPE_WINDOW); |
+ views::Widget* widget = CreateBrowserModalDialogViews(contents, nullptr); |
+ widget->Show(); |
+ EXPECT_TRUE(widget->IsVisible()); |
+ widget->CloseNow(); |
+ 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
|
+} |
+ |
} // namespace constrained_window |