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

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: Addressed comments 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_() {}
sky 2013/09/05 23:39:14 nit: no need to explicitly initialize preferred_si
Rune Fevang 2013/09/06 00:04:26 Done.
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 Widget parent;
79 parent.Init(params);
80
81 DialogContents* contents = new DialogContents;
82 Widget* dialog =
83 CreateBrowserModalDialogViews(contents, parent.GetNativeWindow());
84 DialogHost dialog_host(parent.GetNativeView());
85 UpdateWebContentsModalDialogPosition(dialog, &dialog_host);
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);
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);
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);
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 full_dialog_size = dialog->GetClientAreaBoundsInScreen().size();
113 gfx::Size max_dialog_size = full_dialog_size;
114 max_dialog_size.Enlarge(-100, -100);
115 dialog_host.set_max_dialog_size(max_dialog_size);
116 UpdateWebContentsModalDialogPosition(dialog, &dialog_host);
117 // The top border of the dialog is intentionally drawn outside the area
118 // specified by the dialog host, so add it to the size the dialog is expected
119 // to occupy.
120 expected_size = max_dialog_size;
121 Border* border = dialog->non_client_view()->frame_view()->border();
122 if (border)
123 expected_size.Enlarge(0, border->GetInsets().top());
124 EXPECT_EQ(expected_size,
125 dialog->non_client_view()->GetBoundsInScreen().size());
126
127 // Enlarge the max area again and make sure the dialog again uses its
128 // preferred size.
129 dialog_host.set_max_dialog_size(gfx::Size(5000, 5000));
130 UpdateWebContentsModalDialogPosition(dialog, &dialog_host);
131 EXPECT_EQ(full_dialog_size, dialog->GetClientAreaBoundsInScreen().size());
132 }
133
134 } // namespace views
OLDNEW
« no previous file with comments | « chrome/browser/ui/views/constrained_window_views.cc ('k') | chrome/browser/ui/views/frame/browser_view_layout.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698