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