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

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

Issue 17127003: Refine DialogClientView button code and unit tests. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Revert explicit RemoveChildView calls. Created 7 years, 6 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 | Annotate | Revision Log
« no previous file with comments | « ui/views/window/dialog_client_view_unittest.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 "base/strings/utf_string_conversions.h"
5 #include "ui/base/hit_test.h" 6 #include "ui/base/hit_test.h"
6 #include "ui/views/bubble/bubble_border.h" 7 #include "ui/views/bubble/bubble_border.h"
7 #include "ui/views/bubble/bubble_frame_view.h" 8 #include "ui/views/bubble/bubble_frame_view.h"
9 #include "ui/views/controls/button/button_dropdown.h"
10 #include "ui/views/controls/button/checkbox.h"
11 #include "ui/views/controls/button/label_button.h"
8 #include "ui/views/test/views_test_base.h" 12 #include "ui/views/test/views_test_base.h"
9 #include "ui/views/widget/widget.h" 13 #include "ui/views/widget/widget.h"
14 #include "ui/views/window/dialog_client_view.h"
10 #include "ui/views/window/dialog_delegate.h" 15 #include "ui/views/window/dialog_delegate.h"
11 16
12 namespace views { 17 namespace views {
13 18
14 typedef ViewsTestBase DialogTest;
15
16 namespace { 19 namespace {
17 20
18 class TestDialog : public DialogDelegateView { 21 class TestDialog : public DialogDelegateView, public ButtonListener {
19 public: 22 public:
20 TestDialog() {} 23 TestDialog()
24 : canceled_(false),
25 accepted_(false),
26 closeable_(false),
27 last_pressed_button_(NULL) {}
21 virtual ~TestDialog() {} 28 virtual ~TestDialog() {}
22 29
23 // BubbleDelegateView overrides: 30 // DialogDelegateView overrides:
31 virtual bool Cancel() OVERRIDE {
32 canceled_ = true;
33 return closeable_;
34 }
35 virtual bool Accept() OVERRIDE {
36 accepted_ = true;
37 return closeable_;
38 }
39
40 // View override:
24 virtual gfx::Size GetPreferredSize() OVERRIDE { return gfx::Size(200, 200); } 41 virtual gfx::Size GetPreferredSize() OVERRIDE { return gfx::Size(200, 200); }
25 42
43 // ButtonListener override:
44 virtual void ButtonPressed(Button* sender, const ui::Event& event) OVERRIDE {
45 last_pressed_button_ = sender;
46 }
47
48 Button* last_pressed_button() const { return last_pressed_button_; }
49
50 void PressEnterAndCheckStates(Button* button) {
51 ui::KeyEvent key_event(ui::ET_KEY_PRESSED, ui::VKEY_RETURN, 0, false);
52 GetFocusManager()->OnKeyEvent(key_event);
53 const DialogClientView* client_view = GetDialogClientView();
54 EXPECT_EQ(canceled_, client_view->cancel_button()->is_default());
55 EXPECT_EQ(accepted_, client_view->ok_button()->is_default());
56 // This view does not listen for ok or cancel clicks, DialogClientView does.
57 CheckAndResetStates(button == client_view->cancel_button(),
58 button == client_view->ok_button(),
59 (canceled_ || accepted_ ) ? NULL : button);
60 }
61
62 void CheckAndResetStates(bool canceled, bool accepted, Button* last_pressed) {
63 EXPECT_EQ(canceled, canceled_);
64 canceled_ = false;
65 EXPECT_EQ(accepted, accepted_);
66 accepted_ = false;
67 EXPECT_EQ(last_pressed, last_pressed_button_);
68 last_pressed_button_ = NULL;
69 }
70
71 void TearDown() {
72 closeable_ = true;
73 GetWidget()->Close();
74 }
75
26 private: 76 private:
77 bool canceled_;
78 bool accepted_;
79 // Prevent the dialog from closing, for repeated ok and cancel button clicks.
80 bool closeable_;
81 Button* last_pressed_button_;
82
27 DISALLOW_COPY_AND_ASSIGN(TestDialog); 83 DISALLOW_COPY_AND_ASSIGN(TestDialog);
28 }; 84 };
29 85
86 class DialogTest : public ViewsTestBase {
87 public:
88 DialogTest() : dialog_(NULL) {}
89 virtual ~DialogTest() {}
90
91 virtual void SetUp() OVERRIDE {
92 ViewsTestBase::SetUp();
93 dialog_ = new TestDialog();
94 DialogDelegate::CreateDialogWidget(dialog_, GetContext(), NULL)->Show();
95 }
96
97 virtual void TearDown() OVERRIDE {
98 dialog_->TearDown();
99 ViewsTestBase::TearDown();
100 }
101
102 TestDialog* dialog() const { return dialog_; }
103
104 private:
105 TestDialog* dialog_;
106
107 DISALLOW_COPY_AND_ASSIGN(DialogTest);
108 };
109
30 } // namespace 110 } // namespace
31 111
112 TEST_F(DialogTest, DefaultButtons) {
113 DialogClientView* client_view = dialog()->GetDialogClientView();
114 LabelButton* ok_button = client_view->ok_button();
115 LabelButton* cancel_button = client_view->cancel_button();
116
117 // DialogDelegate's default button (ok) should be default (and handle enter).
118 EXPECT_EQ(ui::DIALOG_BUTTON_OK, dialog()->GetDefaultDialogButton());
119 dialog()->PressEnterAndCheckStates(ok_button);
120
121 // Focus another button in the dialog, it should become the default.
122 LabelButton* button_1 = new LabelButton(dialog(), string16());
123 client_view->AddChildView(button_1);
124 client_view->OnWillChangeFocus(ok_button, button_1);
125 EXPECT_TRUE(button_1->is_default());
126 dialog()->PressEnterAndCheckStates(button_1);
127
128 // Focus a Checkbox (not a push button), OK should become the default again.
129 Checkbox* checkbox = new Checkbox(string16());
130 client_view->AddChildView(checkbox);
131 client_view->OnWillChangeFocus(button_1, checkbox);
132 EXPECT_FALSE(button_1->is_default());
133 dialog()->PressEnterAndCheckStates(ok_button);
134
135 // Focus yet another button in the dialog, it should become the default.
136 LabelButton* button_2 = new LabelButton(dialog(), string16());
137 client_view->AddChildView(button_2);
138 client_view->OnWillChangeFocus(checkbox, button_2);
139 EXPECT_FALSE(button_1->is_default());
140 EXPECT_TRUE(button_2->is_default());
141 dialog()->PressEnterAndCheckStates(button_2);
142
143 // Focus nothing, OK should become the default again.
144 client_view->OnWillChangeFocus(button_2, NULL);
145 EXPECT_FALSE(button_1->is_default());
146 EXPECT_FALSE(button_2->is_default());
147 dialog()->PressEnterAndCheckStates(ok_button);
148
149 // A ButtonDropDown will handle events, but ok will be still be default.
150 ButtonDropDown* drop_down = new ButtonDropDown(dialog(), NULL);
151 dialog()->AddChildView(drop_down);
152 drop_down->SetBoundsRect(gfx::Rect(0, 0, 100, 100));
153 const gfx::Point point(1, 1);
154 client_view->OnWillChangeFocus(NULL, drop_down);
155 drop_down->OnMousePressed(ui::MouseEvent(
156 ui::ET_MOUSE_PRESSED, point, point, ui::EF_LEFT_MOUSE_BUTTON));
157 drop_down->OnMouseReleased(ui::MouseEvent(
158 ui::ET_MOUSE_RELEASED, point, point, ui::EF_LEFT_MOUSE_BUTTON));
159 dialog()->CheckAndResetStates(false, false, drop_down);
160 EXPECT_FALSE(button_1->is_default());
161 EXPECT_FALSE(button_2->is_default());
162 dialog()->PressEnterAndCheckStates(ok_button);
163
164 // Focus the cancel button, it should become the default.
165 client_view->OnWillChangeFocus(drop_down, cancel_button);
166 EXPECT_FALSE(button_1->is_default());
167 EXPECT_FALSE(button_2->is_default());
168 dialog()->PressEnterAndCheckStates(cancel_button);
169 }
170
171 TEST_F(DialogTest, AcceptAndCancel) {
172 DialogClientView* client_view = dialog()->GetDialogClientView();
173 LabelButton* ok_button = client_view->ok_button();
174 LabelButton* cancel_button = client_view->cancel_button();
175
176 // Check that return/escape accelerators accept/cancel dialogs.
177 const ui::KeyEvent return_key(ui::ET_KEY_PRESSED, ui::VKEY_RETURN, 0, false);
178 dialog()->GetFocusManager()->OnKeyEvent(return_key);
179 dialog()->CheckAndResetStates(false, true, NULL);
180 const ui::KeyEvent escape_key(ui::ET_KEY_PRESSED, ui::VKEY_ESCAPE, 0, false);
181 dialog()->GetFocusManager()->OnKeyEvent(escape_key);
182 dialog()->CheckAndResetStates(true, false, NULL);
183
184 // Check ok and cancel button behavior on a directed return key events.
185 ok_button->OnKeyPressed(return_key);
186 dialog()->CheckAndResetStates(false, true, NULL);
187 cancel_button->OnKeyPressed(return_key);
188 dialog()->CheckAndResetStates(true, false, NULL);
189
190 // Check that return accelerators cancel dialogs if cancel is focused.
191 cancel_button->RequestFocus();
192 dialog()->GetFocusManager()->OnKeyEvent(return_key);
193 dialog()->CheckAndResetStates(true, false, NULL);
194 }
195
196 TEST_F(DialogTest, RemoveDefaultButton) {
197 // Removing buttons from the dialog here should not cause a crash on close.
198 delete dialog()->GetDialogClientView()->ok_button();
199 delete dialog()->GetDialogClientView()->cancel_button();
200 }
201
32 TEST_F(DialogTest, HitTest) { 202 TEST_F(DialogTest, HitTest) {
33 TestDialog* dialog = new TestDialog(); 203 const NonClientView* view = dialog()->GetWidget()->non_client_view();
34 DialogDelegate::CreateDialogWidget(dialog, NULL, GetContext());
35 const NonClientView* view = dialog->GetWidget()->non_client_view();
36 204
37 if (DialogDelegate::UseNewStyle()) { 205 if (DialogDelegate::UseNewStyle()) {
38 // Ensure that the new style's BubbleFrameView hit-tests as expected. 206 // Ensure that the new style's BubbleFrameView hit-tests as expected.
39 BubbleFrameView* frame = static_cast<BubbleFrameView*>(view->frame_view()); 207 BubbleFrameView* frame = static_cast<BubbleFrameView*>(view->frame_view());
40 const int border = frame->bubble_border()->GetBorderThickness(); 208 const int border = frame->bubble_border()->GetBorderThickness();
41 209
42 struct { 210 struct {
43 const int point; 211 const int point;
44 const int hit; 212 const int hit;
45 } cases[] = { 213 } cases[] = {
46 { border, HTSYSMENU }, 214 { border, HTSYSMENU },
47 { border + 10, HTSYSMENU }, 215 { border + 10, HTSYSMENU },
48 { border + 20, HTCAPTION }, 216 { border + 20, HTCAPTION },
49 { border + 40, HTCLIENT }, 217 { border + 40, HTCLIENT },
50 { border + 50, HTCLIENT }, 218 { border + 50, HTCLIENT },
51 { 1000, HTNOWHERE }, 219 { 1000, HTNOWHERE },
52 }; 220 };
53 221
54 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) { 222 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) {
55 gfx::Point point(cases[i].point, cases[i].point); 223 gfx::Point point(cases[i].point, cases[i].point);
56 EXPECT_EQ(cases[i].hit, frame->NonClientHitTest(point)) 224 EXPECT_EQ(cases[i].hit, frame->NonClientHitTest(point))
57 << " with border: " << border << ", at point " << cases[i].point; 225 << " with border: " << border << ", at point " << cases[i].point;
58 } 226 }
59 } 227 }
60 } 228 }
61 229
62 } // namespace views 230 } // namespace views
OLDNEW
« no previous file with comments | « ui/views/window/dialog_client_view_unittest.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698