| 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 15 matching lines...) Expand all Loading... |
| 26 | 26 |
| 27 namespace { | 27 namespace { |
| 28 | 28 |
| 29 class TestDialog : public DialogDelegateView, public ButtonListener { | 29 class TestDialog : public DialogDelegateView, public ButtonListener { |
| 30 public: | 30 public: |
| 31 TestDialog() | 31 TestDialog() |
| 32 : input_(new views::Textfield()), | 32 : input_(new views::Textfield()), |
| 33 canceled_(false), | 33 canceled_(false), |
| 34 accepted_(false), | 34 accepted_(false), |
| 35 closeable_(false), | 35 closeable_(false), |
| 36 last_pressed_button_(NULL) { | 36 last_pressed_button_(nullptr), |
| 37 should_handle_escape_(false) { |
| 37 AddChildView(input_); | 38 AddChildView(input_); |
| 38 } | 39 } |
| 39 ~TestDialog() override {} | 40 ~TestDialog() override {} |
| 40 | 41 |
| 42 void Init() { |
| 43 // Add the accelerator before being added to the widget hierarchy (before |
| 44 // DCV has registered its accelerator) to make sure accelerator handling is |
| 45 // not dependent on the order of AddAccelerator calls. |
| 46 EXPECT_FALSE(GetWidget()); |
| 47 AddAccelerator(ui::Accelerator(ui::VKEY_ESCAPE, ui::EF_NONE)); |
| 48 } |
| 49 |
| 41 // WidgetDelegate overrides: | 50 // WidgetDelegate overrides: |
| 42 bool ShouldShowWindowTitle() const override { | 51 bool ShouldShowWindowTitle() const override { |
| 43 return !title_.empty(); | 52 return !title_.empty(); |
| 44 } | 53 } |
| 45 | 54 |
| 46 // DialogDelegateView overrides: | 55 // DialogDelegateView overrides: |
| 47 bool Cancel() override { | 56 bool Cancel() override { |
| 48 canceled_ = true; | 57 canceled_ = true; |
| 49 return closeable_; | 58 return closeable_; |
| 50 } | 59 } |
| 51 bool Accept() override { | 60 bool Accept() override { |
| 52 accepted_ = true; | 61 accepted_ = true; |
| 53 return closeable_; | 62 return closeable_; |
| 54 } | 63 } |
| 55 | 64 |
| 56 // DialogDelegateView overrides: | 65 // DialogDelegateView overrides: |
| 57 gfx::Size GetPreferredSize() const override { return gfx::Size(200, 200); } | 66 gfx::Size GetPreferredSize() const override { return gfx::Size(200, 200); } |
| 67 bool AcceleratorPressed(const ui::Accelerator& accelerator) override { |
| 68 return should_handle_escape_; |
| 69 } |
| 58 base::string16 GetWindowTitle() const override { return title_; } | 70 base::string16 GetWindowTitle() const override { return title_; } |
| 59 View* GetInitiallyFocusedView() override { return input_; } | 71 View* GetInitiallyFocusedView() override { return input_; } |
| 60 bool UseNewStyleForThisDialog() const override { return true; } | 72 bool UseNewStyleForThisDialog() const override { return true; } |
| 61 | 73 |
| 62 // ButtonListener override: | 74 // ButtonListener override: |
| 63 void ButtonPressed(Button* sender, const ui::Event& event) override { | 75 void ButtonPressed(Button* sender, const ui::Event& event) override { |
| 64 last_pressed_button_ = sender; | 76 last_pressed_button_ = sender; |
| 65 } | 77 } |
| 66 | 78 |
| 67 Button* last_pressed_button() const { return last_pressed_button_; } | 79 Button* last_pressed_button() const { return last_pressed_button_; } |
| 68 | 80 |
| 69 void CheckAndResetStates(bool canceled, bool accepted, Button* last_pressed) { | 81 void CheckAndResetStates(bool canceled, bool accepted, Button* last_pressed) { |
| 70 EXPECT_EQ(canceled, canceled_); | 82 EXPECT_EQ(canceled, canceled_); |
| 71 canceled_ = false; | 83 canceled_ = false; |
| 72 EXPECT_EQ(accepted, accepted_); | 84 EXPECT_EQ(accepted, accepted_); |
| 73 accepted_ = false; | 85 accepted_ = false; |
| 74 EXPECT_EQ(last_pressed, last_pressed_button_); | 86 EXPECT_EQ(last_pressed, last_pressed_button_); |
| 75 last_pressed_button_ = NULL; | 87 last_pressed_button_ = nullptr; |
| 76 } | 88 } |
| 77 | 89 |
| 78 void TearDown() { | 90 void TearDown() { |
| 79 closeable_ = true; | 91 closeable_ = true; |
| 80 GetWidget()->Close(); | 92 GetWidget()->Close(); |
| 81 } | 93 } |
| 82 | 94 |
| 83 void set_title(const base::string16& title) { title_ = title; } | 95 void set_title(const base::string16& title) { title_ = title; } |
| 96 void set_should_handle_escape(bool should_handle_escape) { |
| 97 should_handle_escape_ = should_handle_escape; |
| 98 } |
| 84 | 99 |
| 85 views::Textfield* input() { return input_; } | 100 views::Textfield* input() { return input_; } |
| 86 | 101 |
| 87 private: | 102 private: |
| 88 views::Textfield* input_; | 103 views::Textfield* input_; |
| 89 bool canceled_; | 104 bool canceled_; |
| 90 bool accepted_; | 105 bool accepted_; |
| 91 // 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. |
| 92 bool closeable_; | 107 bool closeable_; |
| 93 Button* last_pressed_button_; | 108 Button* last_pressed_button_; |
| 94 base::string16 title_; | 109 base::string16 title_; |
| 110 bool should_handle_escape_; |
| 95 | 111 |
| 96 DISALLOW_COPY_AND_ASSIGN(TestDialog); | 112 DISALLOW_COPY_AND_ASSIGN(TestDialog); |
| 97 }; | 113 }; |
| 98 | 114 |
| 99 class DialogTest : public ViewsTestBase { | 115 class DialogTest : public ViewsTestBase { |
| 100 public: | 116 public: |
| 101 DialogTest() : dialog_(NULL) {} | 117 DialogTest() : dialog_(nullptr) {} |
| 102 ~DialogTest() override {} | 118 ~DialogTest() override {} |
| 103 | 119 |
| 104 void SetUp() override { | 120 void SetUp() override { |
| 105 ViewsTestBase::SetUp(); | 121 ViewsTestBase::SetUp(); |
| 106 dialog_ = new TestDialog(); | 122 dialog_ = new TestDialog(); |
| 107 DialogDelegate::CreateDialogWidget(dialog_, GetContext(), NULL)->Show(); | 123 dialog_->Init(); |
| 124 DialogDelegate::CreateDialogWidget(dialog_, GetContext(), nullptr)->Show(); |
| 108 } | 125 } |
| 109 | 126 |
| 110 void TearDown() override { | 127 void TearDown() override { |
| 111 dialog_->TearDown(); | 128 dialog_->TearDown(); |
| 112 ViewsTestBase::TearDown(); | 129 ViewsTestBase::TearDown(); |
| 113 } | 130 } |
| 114 | 131 |
| 115 void SimulateKeyEvent(const ui::KeyEvent& event) { | 132 void SimulateKeyEvent(const ui::KeyEvent& event) { |
| 116 ui::KeyEvent event_copy = event; | 133 ui::KeyEvent event_copy = event; |
| 117 if (dialog()->GetFocusManager()->OnKeyEvent(event_copy)) | 134 if (dialog()->GetFocusManager()->OnKeyEvent(event_copy)) |
| (...skipping 19 matching lines...) Expand all Loading... |
| 137 TEST_F(DialogTest, AcceptAndCancel) { | 154 TEST_F(DialogTest, AcceptAndCancel) { |
| 138 DialogClientView* client_view = dialog()->GetDialogClientView(); | 155 DialogClientView* client_view = dialog()->GetDialogClientView(); |
| 139 LabelButton* ok_button = client_view->ok_button(); | 156 LabelButton* ok_button = client_view->ok_button(); |
| 140 LabelButton* cancel_button = client_view->cancel_button(); | 157 LabelButton* cancel_button = client_view->cancel_button(); |
| 141 | 158 |
| 142 // Check that return/escape accelerators accept/cancel dialogs. | 159 // Check that return/escape accelerators accept/cancel dialogs. |
| 143 EXPECT_EQ(dialog()->input(), dialog()->GetFocusManager()->GetFocusedView()); | 160 EXPECT_EQ(dialog()->input(), dialog()->GetFocusManager()->GetFocusedView()); |
| 144 const ui::KeyEvent return_event( | 161 const ui::KeyEvent return_event( |
| 145 ui::ET_KEY_PRESSED, ui::VKEY_RETURN, ui::EF_NONE); | 162 ui::ET_KEY_PRESSED, ui::VKEY_RETURN, ui::EF_NONE); |
| 146 SimulateKeyEvent(return_event); | 163 SimulateKeyEvent(return_event); |
| 147 dialog()->CheckAndResetStates(false, true, NULL); | 164 dialog()->CheckAndResetStates(false, true, nullptr); |
| 148 const ui::KeyEvent escape_event( | 165 const ui::KeyEvent escape_event( |
| 149 ui::ET_KEY_PRESSED, ui::VKEY_ESCAPE, ui::EF_NONE); | 166 ui::ET_KEY_PRESSED, ui::VKEY_ESCAPE, ui::EF_NONE); |
| 150 SimulateKeyEvent(escape_event); | 167 SimulateKeyEvent(escape_event); |
| 151 dialog()->CheckAndResetStates(true, false, NULL); | 168 dialog()->CheckAndResetStates(true, false, nullptr); |
| 152 | 169 |
| 153 // Check ok and cancel button behavior on a directed return key events. | 170 // Check ok and cancel button behavior on a directed return key events. |
| 154 ok_button->OnKeyPressed(return_event); | 171 ok_button->OnKeyPressed(return_event); |
| 155 dialog()->CheckAndResetStates(false, true, NULL); | 172 dialog()->CheckAndResetStates(false, true, nullptr); |
| 156 cancel_button->OnKeyPressed(return_event); | 173 cancel_button->OnKeyPressed(return_event); |
| 157 dialog()->CheckAndResetStates(true, false, NULL); | 174 dialog()->CheckAndResetStates(true, false, nullptr); |
| 158 | 175 |
| 159 // Check that return accelerators cancel dialogs if cancel is focused. | 176 // Check that return accelerators cancel dialogs if cancel is focused. |
| 160 cancel_button->RequestFocus(); | 177 cancel_button->RequestFocus(); |
| 161 EXPECT_EQ(cancel_button, dialog()->GetFocusManager()->GetFocusedView()); | 178 EXPECT_EQ(cancel_button, dialog()->GetFocusManager()->GetFocusedView()); |
| 162 SimulateKeyEvent(return_event); | 179 SimulateKeyEvent(return_event); |
| 163 dialog()->CheckAndResetStates(true, false, NULL); | 180 dialog()->CheckAndResetStates(true, false, nullptr); |
| 181 |
| 182 // Check that escape can be overridden. |
| 183 dialog()->set_should_handle_escape(true); |
| 184 SimulateKeyEvent(escape_event); |
| 185 dialog()->CheckAndResetStates(false, false, nullptr); |
| 164 } | 186 } |
| 165 | 187 |
| 166 TEST_F(DialogTest, RemoveDefaultButton) { | 188 TEST_F(DialogTest, RemoveDefaultButton) { |
| 167 // Removing buttons from the dialog here should not cause a crash on close. | 189 // Removing buttons from the dialog here should not cause a crash on close. |
| 168 delete dialog()->GetDialogClientView()->ok_button(); | 190 delete dialog()->GetDialogClientView()->ok_button(); |
| 169 delete dialog()->GetDialogClientView()->cancel_button(); | 191 delete dialog()->GetDialogClientView()->cancel_button(); |
| 170 } | 192 } |
| 171 | 193 |
| 172 TEST_F(DialogTest, HitTest_HiddenTitle) { | 194 TEST_F(DialogTest, HitTest_HiddenTitle) { |
| 173 // Ensure that BubbleFrameView hit-tests as expected when the title is hidden. | 195 // Ensure that BubbleFrameView hit-tests as expected when the title is hidden. |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 217 for (size_t i = 0; i < arraysize(cases); ++i) { | 239 for (size_t i = 0; i < arraysize(cases); ++i) { |
| 218 gfx::Point point(cases[i].point, cases[i].point); | 240 gfx::Point point(cases[i].point, cases[i].point); |
| 219 EXPECT_EQ(cases[i].hit, frame->NonClientHitTest(point)) | 241 EXPECT_EQ(cases[i].hit, frame->NonClientHitTest(point)) |
| 220 << " with border: " << border << ", at point " << cases[i].point; | 242 << " with border: " << border << ", at point " << cases[i].point; |
| 221 } | 243 } |
| 222 } | 244 } |
| 223 | 245 |
| 224 TEST_F(DialogTest, BoundsAccommodateTitle) { | 246 TEST_F(DialogTest, BoundsAccommodateTitle) { |
| 225 TestDialog* dialog2(new TestDialog()); | 247 TestDialog* dialog2(new TestDialog()); |
| 226 dialog2->set_title(base::ASCIIToUTF16("Title")); | 248 dialog2->set_title(base::ASCIIToUTF16("Title")); |
| 227 DialogDelegate::CreateDialogWidget(dialog2, GetContext(), NULL); | 249 DialogDelegate::CreateDialogWidget(dialog2, GetContext(), nullptr); |
| 228 | 250 |
| 229 // Titled dialogs have taller initial frame bounds than untitled dialogs. | 251 // Titled dialogs have taller initial frame bounds than untitled dialogs. |
| 230 View* frame1 = dialog()->GetWidget()->non_client_view()->frame_view(); | 252 View* frame1 = dialog()->GetWidget()->non_client_view()->frame_view(); |
| 231 View* frame2 = dialog2->GetWidget()->non_client_view()->frame_view(); | 253 View* frame2 = dialog2->GetWidget()->non_client_view()->frame_view(); |
| 232 EXPECT_LT(frame1->GetPreferredSize().height(), | 254 EXPECT_LT(frame1->GetPreferredSize().height(), |
| 233 frame2->GetPreferredSize().height()); | 255 frame2->GetPreferredSize().height()); |
| 234 | 256 |
| 235 // Giving the default test dialog a title will yield the same bounds. | 257 // Giving the default test dialog a title will yield the same bounds. |
| 236 dialog()->set_title(base::ASCIIToUTF16("Title")); | 258 dialog()->set_title(base::ASCIIToUTF16("Title")); |
| 237 dialog()->GetWidget()->UpdateWindowTitle(); | 259 dialog()->GetWidget()->UpdateWindowTitle(); |
| 238 EXPECT_EQ(frame1->GetPreferredSize().height(), | 260 EXPECT_EQ(frame1->GetPreferredSize().height(), |
| 239 frame2->GetPreferredSize().height()); | 261 frame2->GetPreferredSize().height()); |
| 240 | 262 |
| 241 dialog2->TearDown(); | 263 dialog2->TearDown(); |
| 242 } | 264 } |
| 243 | 265 |
| 244 // Tests default focus is assigned correctly when showing a new dialog. | 266 // Tests default focus is assigned correctly when showing a new dialog. |
| 245 TEST_F(DialogTest, InitialFocus) { | 267 TEST_F(DialogTest, InitialFocus) { |
| 246 EXPECT_TRUE(dialog()->input()->HasFocus()); | 268 EXPECT_TRUE(dialog()->input()->HasFocus()); |
| 247 EXPECT_EQ(dialog()->input(), dialog()->GetFocusManager()->GetFocusedView()); | 269 EXPECT_EQ(dialog()->input(), dialog()->GetFocusManager()->GetFocusedView()); |
| 248 } | 270 } |
| 249 | 271 |
| 250 } // namespace views | 272 } // namespace views |
| OLD | NEW |