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

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

Issue 2821463004: Revert of Fix layout of BubbleFrameView when there's only a close button in the (Closed)
Patch Set: Created 3 years, 8 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 | « ui/views/bubble/bubble_frame_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 <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"
(...skipping 10 matching lines...) Expand all
21 #if defined(OS_MACOSX) 21 #if defined(OS_MACOSX)
22 #include "ui/base/test/scoped_fake_full_keyboard_access.h" 22 #include "ui/base/test/scoped_fake_full_keyboard_access.h"
23 #endif 23 #endif
24 24
25 namespace views { 25 namespace views {
26 26
27 namespace { 27 namespace {
28 28
29 class TestDialog : public DialogDelegateView { 29 class TestDialog : public DialogDelegateView {
30 public: 30 public:
31 TestDialog() : input_(new views::Textfield()) { AddChildView(input_); } 31 TestDialog()
32 : input_(new views::Textfield()),
33 canceled_(false),
34 accepted_(false),
35 closed_(false),
36 closeable_(false),
37 should_handle_escape_(false) {
38 AddChildView(input_);
39 }
32 ~TestDialog() override {} 40 ~TestDialog() override {}
33 41
34 void Init() { 42 void Init() {
35 // Add the accelerator before being added to the widget hierarchy (before 43 // Add the accelerator before being added to the widget hierarchy (before
36 // DCV has registered its accelerator) to make sure accelerator handling is 44 // DCV has registered its accelerator) to make sure accelerator handling is
37 // not dependent on the order of AddAccelerator calls. 45 // not dependent on the order of AddAccelerator calls.
38 EXPECT_FALSE(GetWidget()); 46 EXPECT_FALSE(GetWidget());
39 AddAccelerator(ui::Accelerator(ui::VKEY_ESCAPE, ui::EF_NONE)); 47 AddAccelerator(ui::Accelerator(ui::VKEY_ESCAPE, ui::EF_NONE));
40 } 48 }
41 49
42 // WidgetDelegate overrides: 50 // WidgetDelegate overrides:
43 bool ShouldShowWindowTitle() const override { 51 bool ShouldShowWindowTitle() const override {
44 return !title_.empty(); 52 return !title_.empty();
45 } 53 }
46 bool ShouldShowCloseButton() const override { return show_close_button_; }
47 54
48 // DialogDelegateView overrides: 55 // DialogDelegateView overrides:
49 bool Cancel() override { 56 bool Cancel() override {
50 canceled_ = true; 57 canceled_ = true;
51 return closeable_; 58 return closeable_;
52 } 59 }
53 bool Accept() override { 60 bool Accept() override {
54 accepted_ = true; 61 accepted_ = true;
55 return closeable_; 62 return closeable_;
56 } 63 }
57 bool Close() override { 64 bool Close() override {
58 closed_ = true; 65 closed_ = true;
59 return closeable_; 66 return closeable_;
60 } 67 }
61 68
69 // DialogDelegateView overrides:
62 gfx::Size GetPreferredSize() const override { return gfx::Size(200, 200); } 70 gfx::Size GetPreferredSize() const override { return gfx::Size(200, 200); }
63 bool AcceleratorPressed(const ui::Accelerator& accelerator) override { 71 bool AcceleratorPressed(const ui::Accelerator& accelerator) override {
64 return should_handle_escape_; 72 return should_handle_escape_;
65 } 73 }
66 base::string16 GetWindowTitle() const override { return title_; } 74 base::string16 GetWindowTitle() const override { return title_; }
67 View* GetInitiallyFocusedView() override { return input_; } 75 View* GetInitiallyFocusedView() override { return input_; }
68 bool ShouldUseCustomFrame() const override { return true; } 76 bool ShouldUseCustomFrame() const override { return true; }
69 77
70 void CheckAndResetStates(bool canceled, 78 void CheckAndResetStates(bool canceled,
71 bool accepted, 79 bool accepted,
72 bool closed) { 80 bool closed) {
73 EXPECT_EQ(canceled, canceled_); 81 EXPECT_EQ(canceled, canceled_);
74 canceled_ = false; 82 canceled_ = false;
75 EXPECT_EQ(accepted, accepted_); 83 EXPECT_EQ(accepted, accepted_);
76 accepted_ = false; 84 accepted_ = false;
77 EXPECT_EQ(closed, closed_); 85 EXPECT_EQ(closed, closed_);
78 closed_ = false; 86 closed_ = false;
79 } 87 }
80 88
81 void TearDown() { 89 void TearDown() {
82 closeable_ = true; 90 closeable_ = true;
83 GetWidget()->Close(); 91 GetWidget()->Close();
84 } 92 }
85 93
86 void set_title(const base::string16& title) { title_ = title; } 94 void set_title(const base::string16& title) { title_ = title; }
87 void set_show_close_button(bool show_close) {
88 show_close_button_ = show_close;
89 }
90 void set_should_handle_escape(bool should_handle_escape) { 95 void set_should_handle_escape(bool should_handle_escape) {
91 should_handle_escape_ = should_handle_escape; 96 should_handle_escape_ = should_handle_escape;
92 } 97 }
93 98
94 views::Textfield* input() { return input_; } 99 views::Textfield* input() { return input_; }
95 100
96 private: 101 private:
97 views::Textfield* input_; 102 views::Textfield* input_;
98 bool canceled_ = false; 103 bool canceled_;
99 bool accepted_ = false; 104 bool accepted_;
100 bool closed_ = false; 105 bool closed_;
101 // Prevent the dialog from closing, for repeated ok and cancel button clicks. 106 // Prevent the dialog from closing, for repeated ok and cancel button clicks.
102 bool closeable_; 107 bool closeable_;
103 base::string16 title_; 108 base::string16 title_;
104 bool show_close_button_ = true; 109 bool should_handle_escape_;
105 bool should_handle_escape_ = false;
106 110
107 DISALLOW_COPY_AND_ASSIGN(TestDialog); 111 DISALLOW_COPY_AND_ASSIGN(TestDialog);
108 }; 112 };
109 113
110 class DialogTest : public ViewsTestBase { 114 class DialogTest : public ViewsTestBase {
111 public: 115 public:
112 DialogTest() : dialog_(nullptr) {} 116 DialogTest() : dialog_(nullptr) {}
113 ~DialogTest() override {} 117 ~DialogTest() override {}
114 118
115 void SetUp() override { 119 void SetUp() override {
116 ViewsTestBase::SetUp(); 120 ViewsTestBase::SetUp();
117 InitializeDialog(); 121 dialog_ = new TestDialog();
118 ShowDialog(); 122 dialog_->Init();
123 DialogDelegate::CreateDialogWidget(dialog_, GetContext(), nullptr)->Show();
119 } 124 }
120 125
121 void TearDown() override { 126 void TearDown() override {
122 dialog_->TearDown(); 127 dialog_->TearDown();
123 ViewsTestBase::TearDown(); 128 ViewsTestBase::TearDown();
124 } 129 }
125 130
126 void InitializeDialog() {
127 if (dialog_)
128 dialog_->TearDown();
129
130 dialog_ = new TestDialog();
131 dialog_->Init();
132 }
133
134 void ShowDialog() {
135 DialogDelegate::CreateDialogWidget(dialog_, GetContext(), nullptr)->Show();
136 }
137
138 void SimulateKeyEvent(const ui::KeyEvent& event) { 131 void SimulateKeyEvent(const ui::KeyEvent& event) {
139 ui::KeyEvent event_copy = event; 132 ui::KeyEvent event_copy = event;
140 if (dialog()->GetFocusManager()->OnKeyEvent(event_copy)) 133 if (dialog()->GetFocusManager()->OnKeyEvent(event_copy))
141 dialog()->GetWidget()->OnKeyEvent(&event_copy); 134 dialog()->GetWidget()->OnKeyEvent(&event_copy);
142 } 135 }
143 136
144 TestDialog* dialog() const { return dialog_; } 137 TestDialog* dialog() const { return dialog_; }
145 138
146 private: 139 private:
147 TestDialog* dialog_; 140 TestDialog* dialog_;
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after
208 TEST_F(DialogTest, HitTest_HiddenTitle) { 201 TEST_F(DialogTest, HitTest_HiddenTitle) {
209 // Ensure that BubbleFrameView hit-tests as expected when the title is hidden. 202 // Ensure that BubbleFrameView hit-tests as expected when the title is hidden.
210 const NonClientView* view = dialog()->GetWidget()->non_client_view(); 203 const NonClientView* view = dialog()->GetWidget()->non_client_view();
211 BubbleFrameView* frame = static_cast<BubbleFrameView*>(view->frame_view()); 204 BubbleFrameView* frame = static_cast<BubbleFrameView*>(view->frame_view());
212 const int border = frame->bubble_border()->GetBorderThickness(); 205 const int border = frame->bubble_border()->GetBorderThickness();
213 206
214 struct { 207 struct {
215 const int point; 208 const int point;
216 const int hit; 209 const int hit;
217 } cases[] = { 210 } cases[] = {
218 {border, HTSYSMENU}, 211 { border, HTSYSMENU },
219 {border + 10, HTSYSMENU}, 212 { border + 10, HTSYSMENU },
220 {border + 20, HTNOWHERE}, 213 { border + 20, HTCLIENT },
221 {border + 50, HTCLIENT /* Space is reserved for the close button. */}, 214 { border + 50, HTCLIENT },
222 {border + 60, HTCLIENT}, 215 { border + 60, HTCLIENT },
223 {1000, HTNOWHERE}, 216 { 1000, HTNOWHERE },
224 }; 217 };
225 218
226 for (size_t i = 0; i < arraysize(cases); ++i) { 219 for (size_t i = 0; i < arraysize(cases); ++i) {
227 gfx::Point point(cases[i].point, cases[i].point);
228 EXPECT_EQ(cases[i].hit, frame->NonClientHitTest(point))
229 << " case " << i << " with border: " << border << ", at point "
230 << cases[i].point;
231 }
232 }
233
234 TEST_F(DialogTest, HitTest_HiddenTitleNoCloseButton) {
235 InitializeDialog();
236 dialog()->set_show_close_button(false);
237 ShowDialog();
238
239 const NonClientView* view = dialog()->GetWidget()->non_client_view();
240 BubbleFrameView* frame = static_cast<BubbleFrameView*>(view->frame_view());
241 const int border = frame->bubble_border()->GetBorderThickness();
242
243 struct {
244 const int point;
245 const int hit;
246 } cases[] = {
247 {border, HTSYSMENU}, {border + 10, HTSYSMENU},
248 {border + 20, HTCLIENT}, {border + 50, HTCLIENT},
249 {border + 60, HTCLIENT}, {1000, HTNOWHERE},
250 };
251
252 for (size_t i = 0; i < arraysize(cases); ++i) {
253 gfx::Point point(cases[i].point, cases[i].point); 220 gfx::Point point(cases[i].point, cases[i].point);
254 EXPECT_EQ(cases[i].hit, frame->NonClientHitTest(point)) 221 EXPECT_EQ(cases[i].hit, frame->NonClientHitTest(point))
255 << " case " << i << " with border: " << border << ", at point " 222 << " case " << i << " with border: " << border << ", at point "
256 << cases[i].point; 223 << cases[i].point;
257 } 224 }
258 } 225 }
259 226
260 TEST_F(DialogTest, HitTest_WithTitle) { 227 TEST_F(DialogTest, HitTest_WithTitle) {
261 // Ensure that BubbleFrameView hit-tests as expected when the title is shown. 228 // Ensure that BubbleFrameView hit-tests as expected when the title is shown.
262 const NonClientView* view = dialog()->GetWidget()->non_client_view(); 229 const NonClientView* view = dialog()->GetWidget()->non_client_view();
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after
376 // On showing the dialog, the initially focused View will be the OK button. 343 // On showing the dialog, the initially focused View will be the OK button.
377 // Since it is no longer focusable, focus should advance to the next focusable 344 // Since it is no longer focusable, focus should advance to the next focusable
378 // View, which is |textfield|. 345 // View, which is |textfield|.
379 dialog_widget->Show(); 346 dialog_widget->Show();
380 EXPECT_TRUE(textfield->HasFocus()); 347 EXPECT_TRUE(textfield->HasFocus());
381 EXPECT_EQ(textfield, dialog->GetFocusManager()->GetFocusedView()); 348 EXPECT_EQ(textfield, dialog->GetFocusManager()->GetFocusedView());
382 dialog_widget->CloseNow(); 349 dialog_widget->CloseNow();
383 } 350 }
384 351
385 } // namespace views 352 } // namespace views
OLDNEW
« no previous file with comments | « ui/views/bubble/bubble_frame_view_unittest.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698