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

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

Issue 2605273002: DialogDelegate unittests: Remove dead code. (Closed)
Patch Set: Created 3 years, 11 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
« no previous file with comments | « no previous file | 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 <stddef.h> 5 #include <stddef.h>
6 6
7 #include "base/macros.h" 7 #include "base/macros.h"
8 #include "base/strings/utf_string_conversions.h" 8 #include "base/strings/utf_string_conversions.h"
9 #include "ui/base/hit_test.h" 9 #include "ui/base/hit_test.h"
10 #include "ui/events/event_processor.h" 10 #include "ui/events/event_processor.h"
11 #include "ui/views/bubble/bubble_border.h" 11 #include "ui/views/bubble/bubble_border.h"
12 #include "ui/views/bubble/bubble_frame_view.h" 12 #include "ui/views/bubble/bubble_frame_view.h"
13 #include "ui/views/controls/button/checkbox.h" 13 #include "ui/views/controls/button/checkbox.h"
14 #include "ui/views/controls/button/label_button.h" 14 #include "ui/views/controls/button/label_button.h"
15 #include "ui/views/controls/textfield/textfield.h" 15 #include "ui/views/controls/textfield/textfield.h"
16 #include "ui/views/test/views_test_base.h" 16 #include "ui/views/test/views_test_base.h"
17 #include "ui/views/widget/widget.h" 17 #include "ui/views/widget/widget.h"
18 #include "ui/views/window/dialog_client_view.h" 18 #include "ui/views/window/dialog_client_view.h"
19 #include "ui/views/window/dialog_delegate.h" 19 #include "ui/views/window/dialog_delegate.h"
20 20
21 namespace views { 21 namespace views {
22 22
23 namespace { 23 namespace {
24 24
25 class TestDialog : public DialogDelegateView, public ButtonListener { 25 class TestDialog : public DialogDelegateView {
26 public: 26 public:
27 TestDialog() 27 TestDialog()
28 : input_(new views::Textfield()), 28 : input_(new views::Textfield()),
29 canceled_(false), 29 canceled_(false),
30 accepted_(false), 30 accepted_(false),
31 closed_(false), 31 closed_(false),
32 closeable_(false), 32 closeable_(false),
33 last_pressed_button_(nullptr),
34 should_handle_escape_(false) { 33 should_handle_escape_(false) {
35 AddChildView(input_); 34 AddChildView(input_);
36 } 35 }
37 ~TestDialog() override {} 36 ~TestDialog() override {}
38 37
39 void Init() { 38 void Init() {
40 // Add the accelerator before being added to the widget hierarchy (before 39 // Add the accelerator before being added to the widget hierarchy (before
41 // DCV has registered its accelerator) to make sure accelerator handling is 40 // DCV has registered its accelerator) to make sure accelerator handling is
42 // not dependent on the order of AddAccelerator calls. 41 // not dependent on the order of AddAccelerator calls.
43 EXPECT_FALSE(GetWidget()); 42 EXPECT_FALSE(GetWidget());
(...skipping 21 matching lines...) Expand all
65 64
66 // DialogDelegateView overrides: 65 // DialogDelegateView overrides:
67 gfx::Size GetPreferredSize() const override { return gfx::Size(200, 200); } 66 gfx::Size GetPreferredSize() const override { return gfx::Size(200, 200); }
68 bool AcceleratorPressed(const ui::Accelerator& accelerator) override { 67 bool AcceleratorPressed(const ui::Accelerator& accelerator) override {
69 return should_handle_escape_; 68 return should_handle_escape_;
70 } 69 }
71 base::string16 GetWindowTitle() const override { return title_; } 70 base::string16 GetWindowTitle() const override { return title_; }
72 View* GetInitiallyFocusedView() override { return input_; } 71 View* GetInitiallyFocusedView() override { return input_; }
73 bool ShouldUseCustomFrame() const override { return true; } 72 bool ShouldUseCustomFrame() const override { return true; }
74 73
75 // ButtonListener override:
76 void ButtonPressed(Button* sender, const ui::Event& event) override {
77 last_pressed_button_ = sender;
78 }
79
80 Button* last_pressed_button() const { return last_pressed_button_; }
81
82 void CheckAndResetStates(bool canceled, 74 void CheckAndResetStates(bool canceled,
83 bool accepted, 75 bool accepted,
84 bool closed, 76 bool closed) {
85 Button* last_pressed) {
86 EXPECT_EQ(canceled, canceled_); 77 EXPECT_EQ(canceled, canceled_);
87 canceled_ = false; 78 canceled_ = false;
88 EXPECT_EQ(accepted, accepted_); 79 EXPECT_EQ(accepted, accepted_);
89 accepted_ = false; 80 accepted_ = false;
90 EXPECT_EQ(last_pressed, last_pressed_button_);
91 last_pressed_button_ = nullptr;
92 EXPECT_EQ(closed, closed_); 81 EXPECT_EQ(closed, closed_);
93 closed_ = false; 82 closed_ = false;
94 } 83 }
95 84
96 void TearDown() { 85 void TearDown() {
97 closeable_ = true; 86 closeable_ = true;
98 GetWidget()->Close(); 87 GetWidget()->Close();
99 } 88 }
100 89
101 void set_title(const base::string16& title) { title_ = title; } 90 void set_title(const base::string16& title) { title_ = title; }
102 void set_should_handle_escape(bool should_handle_escape) { 91 void set_should_handle_escape(bool should_handle_escape) {
103 should_handle_escape_ = should_handle_escape; 92 should_handle_escape_ = should_handle_escape;
104 } 93 }
105 94
106 views::Textfield* input() { return input_; } 95 views::Textfield* input() { return input_; }
107 96
108 private: 97 private:
109 views::Textfield* input_; 98 views::Textfield* input_;
110 bool canceled_; 99 bool canceled_;
111 bool accepted_; 100 bool accepted_;
112 bool closed_; 101 bool closed_;
113 // Prevent the dialog from closing, for repeated ok and cancel button clicks. 102 // Prevent the dialog from closing, for repeated ok and cancel button clicks.
114 bool closeable_; 103 bool closeable_;
115 Button* last_pressed_button_;
116 base::string16 title_; 104 base::string16 title_;
117 bool should_handle_escape_; 105 bool should_handle_escape_;
118 106
119 DISALLOW_COPY_AND_ASSIGN(TestDialog); 107 DISALLOW_COPY_AND_ASSIGN(TestDialog);
120 }; 108 };
121 109
122 class DialogTest : public ViewsTestBase { 110 class DialogTest : public ViewsTestBase {
123 public: 111 public:
124 DialogTest() : dialog_(nullptr) {} 112 DialogTest() : dialog_(nullptr) {}
125 ~DialogTest() override {} 113 ~DialogTest() override {}
(...skipping 29 matching lines...) Expand all
155 TEST_F(DialogTest, AcceptAndCancel) { 143 TEST_F(DialogTest, AcceptAndCancel) {
156 DialogClientView* client_view = dialog()->GetDialogClientView(); 144 DialogClientView* client_view = dialog()->GetDialogClientView();
157 LabelButton* ok_button = client_view->ok_button(); 145 LabelButton* ok_button = client_view->ok_button();
158 LabelButton* cancel_button = client_view->cancel_button(); 146 LabelButton* cancel_button = client_view->cancel_button();
159 147
160 // Check that return/escape accelerators accept/close dialogs. 148 // Check that return/escape accelerators accept/close dialogs.
161 EXPECT_EQ(dialog()->input(), dialog()->GetFocusManager()->GetFocusedView()); 149 EXPECT_EQ(dialog()->input(), dialog()->GetFocusManager()->GetFocusedView());
162 const ui::KeyEvent return_event( 150 const ui::KeyEvent return_event(
163 ui::ET_KEY_PRESSED, ui::VKEY_RETURN, ui::EF_NONE); 151 ui::ET_KEY_PRESSED, ui::VKEY_RETURN, ui::EF_NONE);
164 SimulateKeyEvent(return_event); 152 SimulateKeyEvent(return_event);
165 dialog()->CheckAndResetStates(false, true, false, nullptr); 153 dialog()->CheckAndResetStates(false, true, false);
166 const ui::KeyEvent escape_event( 154 const ui::KeyEvent escape_event(
167 ui::ET_KEY_PRESSED, ui::VKEY_ESCAPE, ui::EF_NONE); 155 ui::ET_KEY_PRESSED, ui::VKEY_ESCAPE, ui::EF_NONE);
168 SimulateKeyEvent(escape_event); 156 SimulateKeyEvent(escape_event);
169 dialog()->CheckAndResetStates(false, false, true, nullptr); 157 dialog()->CheckAndResetStates(false, false, true);
170 158
171 // Check ok and cancel button behavior on a directed return key events. 159 // Check ok and cancel button behavior on a directed return key events.
172 ok_button->OnKeyPressed(return_event); 160 ok_button->OnKeyPressed(return_event);
173 dialog()->CheckAndResetStates(false, true, false, nullptr); 161 dialog()->CheckAndResetStates(false, true, false);
174 cancel_button->OnKeyPressed(return_event); 162 cancel_button->OnKeyPressed(return_event);
175 dialog()->CheckAndResetStates(true, false, false, nullptr); 163 dialog()->CheckAndResetStates(true, false, false);
176 164
177 // Check that return accelerators cancel dialogs if cancel is focused. 165 // Check that return accelerators cancel dialogs if cancel is focused.
178 cancel_button->RequestFocus(); 166 cancel_button->RequestFocus();
179 EXPECT_EQ(cancel_button, dialog()->GetFocusManager()->GetFocusedView()); 167 EXPECT_EQ(cancel_button, dialog()->GetFocusManager()->GetFocusedView());
180 SimulateKeyEvent(return_event); 168 SimulateKeyEvent(return_event);
181 dialog()->CheckAndResetStates(true, false, false, nullptr); 169 dialog()->CheckAndResetStates(true, false, false);
182 170
183 // Check that escape can be overridden. 171 // Check that escape can be overridden.
184 dialog()->set_should_handle_escape(true); 172 dialog()->set_should_handle_escape(true);
185 SimulateKeyEvent(escape_event); 173 SimulateKeyEvent(escape_event);
186 dialog()->CheckAndResetStates(false, false, false, nullptr); 174 dialog()->CheckAndResetStates(false, false, false);
187 } 175 }
188 176
189 TEST_F(DialogTest, RemoveDefaultButton) { 177 TEST_F(DialogTest, RemoveDefaultButton) {
190 // Removing buttons from the dialog here should not cause a crash on close. 178 // Removing buttons from the dialog here should not cause a crash on close.
191 delete dialog()->GetDialogClientView()->ok_button(); 179 delete dialog()->GetDialogClientView()->ok_button();
192 delete dialog()->GetDialogClientView()->cancel_button(); 180 delete dialog()->GetDialogClientView()->cancel_button();
193 } 181 }
194 182
195 TEST_F(DialogTest, HitTest_HiddenTitle) { 183 TEST_F(DialogTest, HitTest_HiddenTitle) {
196 // Ensure that BubbleFrameView hit-tests as expected when the title is hidden. 184 // Ensure that BubbleFrameView hit-tests as expected when the title is hidden.
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
265 dialog2->TearDown(); 253 dialog2->TearDown();
266 } 254 }
267 255
268 // Tests default focus is assigned correctly when showing a new dialog. 256 // Tests default focus is assigned correctly when showing a new dialog.
269 TEST_F(DialogTest, InitialFocus) { 257 TEST_F(DialogTest, InitialFocus) {
270 EXPECT_TRUE(dialog()->input()->HasFocus()); 258 EXPECT_TRUE(dialog()->input()->HasFocus());
271 EXPECT_EQ(dialog()->input(), dialog()->GetFocusManager()->GetFocusedView()); 259 EXPECT_EQ(dialog()->input(), dialog()->GetFocusManager()->GetFocusedView());
272 } 260 }
273 261
274 } // namespace views 262 } // namespace views
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698