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

Side by Side Diff: ui/views/window/dialog_client_view_unittest.cc

Issue 2705553002: Cleanups for DialogClientView, DialogClientViewTest. (Closed)
Patch Set: Better comments, clearer diff Created 3 years, 10 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
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 "ui/views/window/dialog_client_view.h"
6
5 #include "base/macros.h" 7 #include "base/macros.h"
6 #include "base/strings/utf_string_conversions.h" 8 #include "base/strings/utf_string_conversions.h"
7 #include "build/build_config.h" 9 #include "build/build_config.h"
8 #include "ui/base/ui_base_types.h" 10 #include "ui/base/ui_base_types.h"
9 #include "ui/views/controls/button/label_button.h" 11 #include "ui/views/controls/button/label_button.h"
10 #include "ui/views/style/platform_style.h" 12 #include "ui/views/style/platform_style.h"
11 #include "ui/views/test/test_views.h" 13 #include "ui/views/test/test_views.h"
12 #include "ui/views/test/views_test_base.h" 14 #include "ui/views/test/widget_test.h"
13 #include "ui/views/widget/widget.h" 15 #include "ui/views/widget/widget.h"
14 #include "ui/views/window/dialog_client_view.h"
15 #include "ui/views/window/dialog_delegate.h" 16 #include "ui/views/window/dialog_delegate.h"
16 17
17 namespace views { 18 namespace views {
18 19
19 class TestDialogClientView : public DialogClientView { 20 // Base class for tests. Also acts as the dialog delegate and contents view for
21 // TestDialogClientView.
22 class DialogClientViewTest : public test::WidgetTest,
23 public DialogDelegateView {
20 public: 24 public:
21 explicit TestDialogClientView(DialogDelegateView* dialog_delegate_view) 25 DialogClientViewTest() {}
22 : DialogClientView(dialog_delegate_view),
23 dialog_delegate_view_(dialog_delegate_view) {}
24 ~TestDialogClientView() override {}
25 26
26 // DialogClientView implementation. 27 // testing::Test:
27 DialogDelegate* GetDialogDelegate() const override { 28 void SetUp() override {
28 return dialog_delegate_view_; 29 WidgetTest::SetUp();
30 // Note: not using DialogDelegate::CreateDialogWidget(..), since that can
31 // alter the frame type according to the platform.
32 widget_ = new views::Widget;
33 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_WINDOW);
34 params.delegate = this;
35 widget_->Init(params);
36 EXPECT_EQ(this, GetContentsView());
29 } 37 }
30 38
31 View* GetContentsView() { return contents_view(); } 39 void TearDown() override {
32 40 widget_->CloseNow();
33 void CreateExtraViews() { 41 WidgetTest::TearDown();
34 CreateExtraView();
35 } 42 }
36 43
37 private: 44 // DialogDelegateView:
38 DialogDelegateView* dialog_delegate_view_; 45 ClientView* CreateClientView(Widget* widget) override {
39 46 client_view_ = new DialogClientView(widget, this);
40 DISALLOW_COPY_AND_ASSIGN(TestDialogClientView); 47 return client_view_;
41 };
42
43 // Base class for tests. Also acts as the dialog delegate and contents view for
44 // TestDialogClientView.
45 class DialogClientViewTest : public ViewsTestBase,
46 public DialogDelegateView {
47 public:
48 DialogClientViewTest()
49 : dialog_buttons_(ui::DIALOG_BUTTON_NONE),
50 extra_view_(nullptr) {}
51 ~DialogClientViewTest() override {}
52
53 // testing::Test implementation.
54 void SetUp() override {
55 dialog_buttons_ = ui::DIALOG_BUTTON_NONE;
56 client_view_.reset(new TestDialogClientView(this));
57 // Add this (i.e. the contents view) as a child of |client_view_|. This is
58 // generally done when the client view is added to the view hierarchy.
59 client_view_->AddChildViewAt(this, 0);
60 ViewsTestBase::SetUp();
61 } 48 }
62 49
63 // DialogDelegateView implementation. 50 bool ShouldUseCustomFrame() const override { return false; }
64 View* CreateExtraView() override { return extra_view_; } 51
52 void DeleteDelegate() override {
53 // DialogDelegateView would delete this, but |this| is owned by the test.
54 }
55
56 View* CreateExtraView() override { return next_extra_view_.release(); }
57
65 bool GetExtraViewPadding(int* padding) override { 58 bool GetExtraViewPadding(int* padding) override {
66 if (extra_view_padding_) 59 if (extra_view_padding_)
67 *padding = *extra_view_padding_; 60 *padding = *extra_view_padding_;
68 return extra_view_padding_.get() != nullptr; 61 return extra_view_padding_.get() != nullptr;
69 } 62 }
63
70 int GetDialogButtons() const override { return dialog_buttons_; } 64 int GetDialogButtons() const override { return dialog_buttons_; }
71 65
72 protected: 66 protected:
73 gfx::Rect GetUpdatedClientBounds() { 67 gfx::Rect GetUpdatedClientBounds() {
74 client_view_->SizeToPreferredSize(); 68 client_view_->SizeToPreferredSize();
75 client_view_->Layout(); 69 client_view_->Layout();
76 return client_view_->bounds(); 70 return client_view_->bounds();
77 } 71 }
78 72
79 // Makes sure that the content view is sized correctly. Width must be at least 73 // Makes sure that the content view is sized correctly. Width must be at least
80 // the requested amount, but height should always match exactly. 74 // the requested amount, but height should always match exactly.
81 void CheckContentsIsSetToPreferredSize() { 75 void CheckContentsIsSetToPreferredSize() {
82 const gfx::Rect client_bounds = GetUpdatedClientBounds(); 76 const gfx::Rect client_bounds = GetUpdatedClientBounds();
83 const gfx::Size preferred_size = this->GetPreferredSize(); 77 const gfx::Size preferred_size = this->GetPreferredSize();
84 EXPECT_EQ(preferred_size.height(), this->bounds().height()); 78 EXPECT_EQ(preferred_size.height(), this->bounds().height());
85 EXPECT_LE(preferred_size.width(), this->bounds().width()); 79 EXPECT_LE(preferred_size.width(), this->bounds().width());
86 EXPECT_EQ(this->origin(), client_bounds.origin()); 80 EXPECT_EQ(this->origin(), client_bounds.origin());
87 EXPECT_EQ(this->bounds().right(), client_bounds.right()); 81 EXPECT_EQ(this->bounds().right(), client_bounds.right());
88 } 82 }
89 83
90 // Sets the buttons to show in the dialog and refreshes the dialog. 84 // Sets the buttons to show in the dialog and refreshes the dialog.
91 void SetDialogButtons(int dialog_buttons) { 85 void SetDialogButtons(int dialog_buttons) {
92 dialog_buttons_ = dialog_buttons; 86 dialog_buttons_ = dialog_buttons;
93 client_view_->UpdateDialogButtons(); 87 client_view_->UpdateDialogButtons();
94 } 88 }
95 89
96 // Sets the extra view. 90 // Sets the view to provide to CreateExtraView() and updates the dialog. This
91 // can only be called a single time because DialogClientView caches the result
92 // of CreateExtraView() and never calls it again.
97 void SetExtraView(View* view) { 93 void SetExtraView(View* view) {
98 DCHECK(!extra_view_); 94 EXPECT_FALSE(next_extra_view_);
99 extra_view_ = view; 95 next_extra_view_ = base::WrapUnique(view);
100 client_view_->CreateExtraViews(); 96 client_view_->UpdateDialogButtons();
97 EXPECT_FALSE(next_extra_view_);
101 } 98 }
102 99
103 // Sets the extra view padding. 100 // Sets the extra view padding.
104 void SetExtraViewPadding(int padding) { 101 void SetExtraViewPadding(int padding) {
105 DCHECK(!extra_view_padding_); 102 DCHECK(!extra_view_padding_);
106 extra_view_padding_.reset(new int(padding)); 103 extra_view_padding_.reset(new int(padding));
107 client_view_->Layout(); 104 client_view_->Layout();
108 } 105 }
109 106
110 TestDialogClientView* client_view() { return client_view_.get(); } 107 View* FocusableViewAfter(View* view) {
108 const bool dont_loop = false;
109 const bool reverse = false;
110 return GetFocusManager()->GetNextFocusableView(view, GetWidget(), reverse,
111 dont_loop);
112 }
113
114 DialogClientView* client_view() { return client_view_; }
111 115
112 private: 116 private:
113 // The DialogClientView that's being tested. 117 // The dialog Widget.
114 std::unique_ptr<TestDialogClientView> client_view_; 118 Widget* widget_ = nullptr;
119
120 // The DialogClientView that's being tested. Owned by |widget_|.
121 DialogClientView* client_view_;
122
115 // The bitmask of buttons to show in the dialog. 123 // The bitmask of buttons to show in the dialog.
116 int dialog_buttons_; 124 int dialog_buttons_ = ui::DIALOG_BUTTON_NONE;
117 View* extra_view_; // weak 125
118 std::unique_ptr<int> extra_view_padding_; // Null by default. 126 // Set and cleared in SetExtraView().
127 std::unique_ptr<View> next_extra_view_;
128
129 std::unique_ptr<int> extra_view_padding_;
119 130
120 DISALLOW_COPY_AND_ASSIGN(DialogClientViewTest); 131 DISALLOW_COPY_AND_ASSIGN(DialogClientViewTest);
121 }; 132 };
122 133
123 TEST_F(DialogClientViewTest, UpdateButtons) { 134 TEST_F(DialogClientViewTest, UpdateButtons) {
124 // This dialog should start with no buttons. 135 // This dialog should start with no buttons.
125 EXPECT_EQ(GetDialogButtons(), ui::DIALOG_BUTTON_NONE); 136 EXPECT_EQ(GetDialogButtons(), ui::DIALOG_BUTTON_NONE);
126 EXPECT_EQ(NULL, client_view()->ok_button()); 137 EXPECT_EQ(NULL, client_view()->ok_button());
127 EXPECT_EQ(NULL, client_view()->cancel_button()); 138 EXPECT_EQ(NULL, client_view()->cancel_button());
128 const int height_without_buttons = GetUpdatedClientBounds().height(); 139 const int height_without_buttons = GetUpdatedClientBounds().height();
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
170 181
171 // Test that views inside the dialog client view have the correct focus order. 182 // Test that views inside the dialog client view have the correct focus order.
172 TEST_F(DialogClientViewTest, SetupFocusChain) { 183 TEST_F(DialogClientViewTest, SetupFocusChain) {
173 #if defined(OS_WIN) || defined(OS_CHROMEOS) 184 #if defined(OS_WIN) || defined(OS_CHROMEOS)
174 const bool kIsOkButtonOnLeftSide = true; 185 const bool kIsOkButtonOnLeftSide = true;
175 #else 186 #else
176 const bool kIsOkButtonOnLeftSide = false; 187 const bool kIsOkButtonOnLeftSide = false;
177 #endif 188 #endif
178 189
179 // Initially the dialog client view only contains the content view. 190 // Initially the dialog client view only contains the content view.
180 EXPECT_EQ(nullptr, client_view()->GetContentsView()->GetNextFocusableView()); 191 EXPECT_EQ(nullptr, GetContentsView()->GetNextFocusableView());
181 192
182 // Add OK and cancel buttons. 193 // Add OK and cancel buttons.
183 SetDialogButtons(ui::DIALOG_BUTTON_OK | ui::DIALOG_BUTTON_CANCEL); 194 SetDialogButtons(ui::DIALOG_BUTTON_OK | ui::DIALOG_BUTTON_CANCEL);
184 195
185 if (kIsOkButtonOnLeftSide) { 196 if (kIsOkButtonOnLeftSide) {
186 EXPECT_EQ(client_view()->ok_button(), 197 EXPECT_EQ(client_view()->ok_button(),
187 client_view()->GetContentsView()->GetNextFocusableView()); 198 GetContentsView()->GetNextFocusableView());
188 EXPECT_EQ(client_view()->cancel_button(), 199 EXPECT_EQ(client_view()->cancel_button(),
189 client_view()->ok_button()->GetNextFocusableView()); 200 client_view()->ok_button()->GetNextFocusableView());
190 EXPECT_EQ(nullptr, client_view()->cancel_button()->GetNextFocusableView()); 201 EXPECT_EQ(nullptr, client_view()->cancel_button()->GetNextFocusableView());
191 } else { 202 } else {
192 EXPECT_EQ(client_view()->cancel_button(), 203 EXPECT_EQ(client_view()->cancel_button(),
193 client_view()->GetContentsView()->GetNextFocusableView()); 204 GetContentsView()->GetNextFocusableView());
194 EXPECT_EQ(client_view()->ok_button(), 205 EXPECT_EQ(client_view()->ok_button(),
195 client_view()->cancel_button()->GetNextFocusableView()); 206 client_view()->cancel_button()->GetNextFocusableView());
196 EXPECT_EQ(nullptr, client_view()->ok_button()->GetNextFocusableView()); 207 EXPECT_EQ(nullptr, client_view()->ok_button()->GetNextFocusableView());
197 } 208 }
198 209
199 // Add extra view and remove OK button. 210 // Add extra view and remove OK button.
200 View* extra_view = new StaticSizedView(gfx::Size(200, 200)); 211 View* extra_view = new StaticSizedView(gfx::Size(200, 200));
212 extra_view->SetFocusBehavior(View::FocusBehavior::ALWAYS);
201 SetExtraView(extra_view); 213 SetExtraView(extra_view);
202 SetDialogButtons(ui::DIALOG_BUTTON_CANCEL); 214 SetDialogButtons(ui::DIALOG_BUTTON_CANCEL);
203 215
204 EXPECT_EQ(extra_view, 216 EXPECT_EQ(extra_view, GetContentsView()->GetNextFocusableView());
205 client_view()->GetContentsView()->GetNextFocusableView());
206 EXPECT_EQ(client_view()->cancel_button(), extra_view->GetNextFocusableView()); 217 EXPECT_EQ(client_view()->cancel_button(), extra_view->GetNextFocusableView());
207 EXPECT_EQ(nullptr, client_view()->cancel_button()->GetNextFocusableView()); 218 EXPECT_EQ(nullptr, client_view()->cancel_button()->GetNextFocusableView());
208 219
209 // Add a dummy view to the client view. 220 // Add a dummy view to the contents view. Consult the FocusManager for the
221 // traversal order since it now spans different levels of the view hierarchy.
210 View* dummy_view = new StaticSizedView(gfx::Size(200, 200)); 222 View* dummy_view = new StaticSizedView(gfx::Size(200, 200));
211 client_view()->AddChildView(dummy_view); 223 dummy_view->SetFocusBehavior(View::FocusBehavior::ALWAYS);
212 EXPECT_EQ(dummy_view, client_view()->cancel_button()->GetNextFocusableView()); 224 GetContentsView()->AddChildView(dummy_view);
225 EXPECT_EQ(dummy_view, FocusableViewAfter(client_view()->cancel_button()));
226 EXPECT_EQ(extra_view, FocusableViewAfter(dummy_view));
227 EXPECT_EQ(client_view()->cancel_button(), FocusableViewAfter(extra_view));
228
229 // Views are added to the contents view, not the client view, so the focus
230 // chain within the client view is not affected.
231 EXPECT_EQ(nullptr, client_view()->cancel_button()->GetNextFocusableView());
213 } 232 }
214 233
215 // Test that the contents view gets its preferred size in the basic dialog 234 // Test that the contents view gets its preferred size in the basic dialog
216 // configuration. 235 // configuration.
217 TEST_F(DialogClientViewTest, ContentsSize) { 236 TEST_F(DialogClientViewTest, ContentsSize) {
218 CheckContentsIsSetToPreferredSize(); 237 CheckContentsIsSetToPreferredSize();
219 EXPECT_EQ(GetContentsView()->bounds().bottom(), 238 EXPECT_EQ(GetContentsView()->bounds().bottom(),
220 client_view()->bounds().bottom()); 239 client_view()->bounds().bottom());
221 } 240 }
222 241
(...skipping 25 matching lines...) Expand all
248 EXPECT_EQ(no_extra_view_size.width(), client_view()->bounds().width()); 267 EXPECT_EQ(no_extra_view_size.width(), client_view()->bounds().width());
249 268
250 // Try with a reduced-size dialog. 269 // Try with a reduced-size dialog.
251 extra_view->SetVisible(true); 270 extra_view->SetVisible(true);
252 client_view()->SetBoundsRect(gfx::Rect(gfx::Point(0, 0), no_extra_view_size)); 271 client_view()->SetBoundsRect(gfx::Rect(gfx::Point(0, 0), no_extra_view_size));
253 client_view()->Layout(); 272 client_view()->Layout();
254 EXPECT_GT(width_of_extra_view, extra_view->bounds().width()); 273 EXPECT_GT(width_of_extra_view, extra_view->bounds().width());
255 } 274 }
256 275
257 } // namespace views 276 } // namespace views
OLDNEW
« ui/views/window/dialog_client_view.cc ('K') | « ui/views/window/dialog_client_view.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698