Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(145)

Side by Side Diff: chrome/browser/ui/views/constrained_window_views_unittest.cc

Issue 22903022: Limit constrained windows to the size of the parent view. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Missing OVERRIDE Created 7 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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.
4
5 #include "chrome/browser/ui/views/constrained_window_views.h"
6
7 #include "components/web_modal/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/test/views_test_base.h"
13 #include "ui/views/widget/widget.h"
14 #include "ui/views/window/dialog_delegate.h"
15
16 namespace web_modal {
17 class WebContentsModalDialogHostObserver;
18 }
19
20 namespace views {
21
22 class DialogContents : public DialogDelegateView {
23 public:
24 DialogContents() : preferred_size_() {}
25 virtual ~DialogContents() {}
26
27 void set_preferred_size(const gfx::Size& preferred_size) {
28 preferred_size_ = preferred_size;
29 }
30
31 // Overriden from DialogDelegateView:
32 virtual View* GetContentsView() OVERRIDE { return this; }
33 virtual gfx::Size GetPreferredSize() OVERRIDE { return preferred_size_; }
34 virtual gfx::Size GetMinimumSize() OVERRIDE { return gfx::Size(); }
35
36 private:
37 gfx::Size preferred_size_;
38
39 DISALLOW_COPY_AND_ASSIGN(DialogContents);
40 };
41
42 class DialogHost : public web_modal::WebContentsModalDialogHost {
43 public:
44 explicit DialogHost(gfx::NativeView host_view)
45 : host_view_(host_view),
46 max_dialog_size_(5000, 5000) {
47 }
48
49 virtual ~DialogHost() {}
50
51 void set_max_dialog_size(const gfx::Size& max_dialog_size) {
52 max_dialog_size_ = max_dialog_size;
53 }
54
55 // Overridden from WebContentsModalDialogHost:
56 virtual gfx::NativeView GetHostView() const OVERRIDE { return host_view_; }
57 virtual gfx::Point GetDialogPosition(const gfx::Size& size) OVERRIDE {
58 return gfx::Point();
59 }
60 virtual gfx::Size GetMaximumDialogSize() OVERRIDE { return max_dialog_size_; }
61 virtual void AddObserver(
62 web_modal::WebContentsModalDialogHostObserver* observer) OVERRIDE {};
63 virtual void RemoveObserver(
64 web_modal::WebContentsModalDialogHostObserver* observer) OVERRIDE {};
65
66 private:
67 gfx::NativeView host_view_;
68 gfx::Size max_dialog_size_;
69
70 DISALLOW_COPY_AND_ASSIGN(DialogHost);
71 };
72
73 typedef ViewsTestBase ConstrainedWindowViewsTest;
74
75 TEST_F(ConstrainedWindowViewsTest, UpdateDialogPosition) {
76 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_WINDOW);
77 params.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
78 scoped_ptr<Widget> parent(new Widget);
sky 2013/08/30 16:49:21 nit: you can declare parent on the stack since you
Rune Fevang 2013/08/30 21:08:52 Done.
79 parent->Init(params);
80
81 DialogContents* contents = new DialogContents;
82 Widget* dialog =
83 CreateBrowserModalDialogViews(contents, parent->GetNativeWindow());
84 scoped_ptr<DialogHost> dialog_host(new DialogHost(parent->GetNativeView()));
85 UpdateWebContentsModalDialogPosition(dialog, dialog_host.get());
86
87 // Set the preferred size to something larger than the size of a dialog with
88 // no content.
89 gfx::Size preferred_size = dialog->GetClientAreaBoundsInScreen().size();
90 preferred_size.Enlarge(50, 50);
91 contents->set_preferred_size(preferred_size);
92 UpdateWebContentsModalDialogPosition(dialog, dialog_host.get());
Mike Wittman 2013/08/30 17:53:35 add an expectation on the dialog size here also
Rune Fevang 2013/08/30 21:08:52 I don't know what to expect at this point, since i
Mike Wittman 2013/08/30 22:00:26 Ah, makes sense.
93
94 // Now increase the preferred content area and make sure the dialog grows by
95 // the same amount after the position is updated.
96 gfx::Size expected_size = dialog->GetClientAreaBoundsInScreen().size();
97 expected_size.Enlarge(200, 200);
98 preferred_size.Enlarge(200, 200);
99 contents->set_preferred_size(preferred_size);
100 UpdateWebContentsModalDialogPosition(dialog, dialog_host.get());
101 EXPECT_EQ(expected_size, dialog->GetClientAreaBoundsInScreen().size());
102
103 // Make sure the dialog shrinks when the preferred content area shrinks.
104 expected_size.Enlarge(-200, -200);
105 preferred_size.Enlarge(-200, -200);
106 contents->set_preferred_size(preferred_size);
107 UpdateWebContentsModalDialogPosition(dialog, dialog_host.get());
108 EXPECT_EQ(expected_size, dialog->GetClientAreaBoundsInScreen().size());
109
110 // Make sure the dialog is never larger than the max dialog size the dialog
111 // host can handle.
112 gfx::Size max_dialog_size = dialog->GetClientAreaBoundsInScreen().size();
113 max_dialog_size.Enlarge(-100, -100);
114 dialog_host->set_max_dialog_size(max_dialog_size);
115 UpdateWebContentsModalDialogPosition(dialog, dialog_host.get());
116 // The top border of the dialog is intentionally drawn outside the area
117 // specified by the dialog host, so add it to the size the dialog is expected
118 // to occupy.
119 expected_size = max_dialog_size;
120 Border* border = dialog->non_client_view()->frame_view()->border();
121 expected_size.Enlarge(0, border->GetInsets().top());
Mike Wittman 2013/08/30 17:53:35 nit: assert border != NULL
Rune Fevang 2013/08/30 21:08:52 Done.
122 EXPECT_EQ(expected_size,
123 dialog->non_client_view()->GetBoundsInScreen().size());
124 }
Mike Wittman 2013/08/30 17:53:35 Can you add a final test with the maximum size inc
Rune Fevang 2013/08/30 21:08:52 Done.
125
126 } // namespace views
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698