| 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 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, public ButtonListener { |
| 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 closeable_(false), | 32 closeable_(false), |
| 32 last_pressed_button_(nullptr), | 33 last_pressed_button_(nullptr), |
| 33 should_handle_escape_(false) { | 34 should_handle_escape_(false) { |
| 34 AddChildView(input_); | 35 AddChildView(input_); |
| 35 } | 36 } |
| 36 ~TestDialog() override {} | 37 ~TestDialog() override {} |
| 37 | 38 |
| 38 void Init() { | 39 void Init() { |
| 39 // Add the accelerator before being added to the widget hierarchy (before | 40 // Add the accelerator before being added to the widget hierarchy (before |
| 40 // DCV has registered its accelerator) to make sure accelerator handling is | 41 // DCV has registered its accelerator) to make sure accelerator handling is |
| 41 // not dependent on the order of AddAccelerator calls. | 42 // not dependent on the order of AddAccelerator calls. |
| 42 EXPECT_FALSE(GetWidget()); | 43 EXPECT_FALSE(GetWidget()); |
| 43 AddAccelerator(ui::Accelerator(ui::VKEY_ESCAPE, ui::EF_NONE)); | 44 AddAccelerator(ui::Accelerator(ui::VKEY_ESCAPE, ui::EF_NONE)); |
| 44 } | 45 } |
| 45 | 46 |
| 46 // WidgetDelegate overrides: | 47 // WidgetDelegate overrides: |
| 47 bool ShouldShowWindowTitle() const override { | 48 bool ShouldShowWindowTitle() const override { |
| 48 return !title_.empty(); | 49 return !title_.empty(); |
| 49 } | 50 } |
| 50 | 51 |
| 51 // DialogDelegateView overrides: | 52 // DialogDelegateView overrides: |
| 52 bool Cancel() override { | 53 bool Cancel() override { |
| 53 canceled_ = true; | 54 canceled_ = true; |
| 54 return closeable_; | 55 return closeable_; |
| 55 } | 56 } |
| 56 bool Accept() override { | 57 bool Accept() override { |
| 57 accepted_ = true; | 58 accepted_ = true; |
| 58 return closeable_; | 59 return closeable_; |
| 59 } | 60 } |
| 61 bool Close() override { |
| 62 closed_ = true; |
| 63 return closeable_; |
| 64 } |
| 60 | 65 |
| 61 // DialogDelegateView overrides: | 66 // DialogDelegateView overrides: |
| 62 gfx::Size GetPreferredSize() const override { return gfx::Size(200, 200); } | 67 gfx::Size GetPreferredSize() const override { return gfx::Size(200, 200); } |
| 63 bool AcceleratorPressed(const ui::Accelerator& accelerator) override { | 68 bool AcceleratorPressed(const ui::Accelerator& accelerator) override { |
| 64 return should_handle_escape_; | 69 return should_handle_escape_; |
| 65 } | 70 } |
| 66 base::string16 GetWindowTitle() const override { return title_; } | 71 base::string16 GetWindowTitle() const override { return title_; } |
| 67 View* GetInitiallyFocusedView() override { return input_; } | 72 View* GetInitiallyFocusedView() override { return input_; } |
| 68 bool UseNewStyleForThisDialog() const override { return true; } | 73 bool UseNewStyleForThisDialog() const override { return true; } |
| 69 | 74 |
| 70 // ButtonListener override: | 75 // ButtonListener override: |
| 71 void ButtonPressed(Button* sender, const ui::Event& event) override { | 76 void ButtonPressed(Button* sender, const ui::Event& event) override { |
| 72 last_pressed_button_ = sender; | 77 last_pressed_button_ = sender; |
| 73 } | 78 } |
| 74 | 79 |
| 75 Button* last_pressed_button() const { return last_pressed_button_; } | 80 Button* last_pressed_button() const { return last_pressed_button_; } |
| 76 | 81 |
| 77 void CheckAndResetStates(bool canceled, bool accepted, Button* last_pressed) { | 82 void CheckAndResetStates(bool canceled, |
| 83 bool accepted, |
| 84 bool closed, |
| 85 Button* last_pressed) { |
| 78 EXPECT_EQ(canceled, canceled_); | 86 EXPECT_EQ(canceled, canceled_); |
| 79 canceled_ = false; | 87 canceled_ = false; |
| 80 EXPECT_EQ(accepted, accepted_); | 88 EXPECT_EQ(accepted, accepted_); |
| 81 accepted_ = false; | 89 accepted_ = false; |
| 82 EXPECT_EQ(last_pressed, last_pressed_button_); | 90 EXPECT_EQ(last_pressed, last_pressed_button_); |
| 83 last_pressed_button_ = nullptr; | 91 last_pressed_button_ = nullptr; |
| 92 EXPECT_EQ(closed, closed_); |
| 93 closed_ = false; |
| 84 } | 94 } |
| 85 | 95 |
| 86 void TearDown() { | 96 void TearDown() { |
| 87 closeable_ = true; | 97 closeable_ = true; |
| 88 GetWidget()->Close(); | 98 GetWidget()->Close(); |
| 89 } | 99 } |
| 90 | 100 |
| 91 void set_title(const base::string16& title) { title_ = title; } | 101 void set_title(const base::string16& title) { title_ = title; } |
| 92 void set_should_handle_escape(bool should_handle_escape) { | 102 void set_should_handle_escape(bool should_handle_escape) { |
| 93 should_handle_escape_ = should_handle_escape; | 103 should_handle_escape_ = should_handle_escape; |
| 94 } | 104 } |
| 95 | 105 |
| 96 views::Textfield* input() { return input_; } | 106 views::Textfield* input() { return input_; } |
| 97 | 107 |
| 98 private: | 108 private: |
| 99 views::Textfield* input_; | 109 views::Textfield* input_; |
| 100 bool canceled_; | 110 bool canceled_; |
| 101 bool accepted_; | 111 bool accepted_; |
| 112 bool closed_; |
| 102 // Prevent the dialog from closing, for repeated ok and cancel button clicks. | 113 // Prevent the dialog from closing, for repeated ok and cancel button clicks. |
| 103 bool closeable_; | 114 bool closeable_; |
| 104 Button* last_pressed_button_; | 115 Button* last_pressed_button_; |
| 105 base::string16 title_; | 116 base::string16 title_; |
| 106 bool should_handle_escape_; | 117 bool should_handle_escape_; |
| 107 | 118 |
| 108 DISALLOW_COPY_AND_ASSIGN(TestDialog); | 119 DISALLOW_COPY_AND_ASSIGN(TestDialog); |
| 109 }; | 120 }; |
| 110 | 121 |
| 111 class DialogTest : public ViewsTestBase { | 122 class DialogTest : public ViewsTestBase { |
| (...skipping 27 matching lines...) Expand all Loading... |
| 139 DISALLOW_COPY_AND_ASSIGN(DialogTest); | 150 DISALLOW_COPY_AND_ASSIGN(DialogTest); |
| 140 }; | 151 }; |
| 141 | 152 |
| 142 } // namespace | 153 } // namespace |
| 143 | 154 |
| 144 TEST_F(DialogTest, AcceptAndCancel) { | 155 TEST_F(DialogTest, AcceptAndCancel) { |
| 145 DialogClientView* client_view = dialog()->GetDialogClientView(); | 156 DialogClientView* client_view = dialog()->GetDialogClientView(); |
| 146 LabelButton* ok_button = client_view->ok_button(); | 157 LabelButton* ok_button = client_view->ok_button(); |
| 147 LabelButton* cancel_button = client_view->cancel_button(); | 158 LabelButton* cancel_button = client_view->cancel_button(); |
| 148 | 159 |
| 149 // Check that return/escape accelerators accept/cancel dialogs. | 160 // Check that return/escape accelerators accept/close dialogs. |
| 150 EXPECT_EQ(dialog()->input(), dialog()->GetFocusManager()->GetFocusedView()); | 161 EXPECT_EQ(dialog()->input(), dialog()->GetFocusManager()->GetFocusedView()); |
| 151 const ui::KeyEvent return_event( | 162 const ui::KeyEvent return_event( |
| 152 ui::ET_KEY_PRESSED, ui::VKEY_RETURN, ui::EF_NONE); | 163 ui::ET_KEY_PRESSED, ui::VKEY_RETURN, ui::EF_NONE); |
| 153 SimulateKeyEvent(return_event); | 164 SimulateKeyEvent(return_event); |
| 154 dialog()->CheckAndResetStates(false, true, nullptr); | 165 dialog()->CheckAndResetStates(false, true, false, nullptr); |
| 155 const ui::KeyEvent escape_event( | 166 const ui::KeyEvent escape_event( |
| 156 ui::ET_KEY_PRESSED, ui::VKEY_ESCAPE, ui::EF_NONE); | 167 ui::ET_KEY_PRESSED, ui::VKEY_ESCAPE, ui::EF_NONE); |
| 157 SimulateKeyEvent(escape_event); | 168 SimulateKeyEvent(escape_event); |
| 158 dialog()->CheckAndResetStates(true, false, nullptr); | 169 dialog()->CheckAndResetStates(false, false, true, nullptr); |
| 159 | 170 |
| 160 // Check ok and cancel button behavior on a directed return key events. | 171 // Check ok and cancel button behavior on a directed return key events. |
| 161 ok_button->OnKeyPressed(return_event); | 172 ok_button->OnKeyPressed(return_event); |
| 162 dialog()->CheckAndResetStates(false, true, nullptr); | 173 dialog()->CheckAndResetStates(false, true, false, nullptr); |
| 163 cancel_button->OnKeyPressed(return_event); | 174 cancel_button->OnKeyPressed(return_event); |
| 164 dialog()->CheckAndResetStates(true, false, nullptr); | 175 dialog()->CheckAndResetStates(true, false, false, nullptr); |
| 165 | 176 |
| 166 // Check that return accelerators cancel dialogs if cancel is focused. | 177 // Check that return accelerators cancel dialogs if cancel is focused. |
| 167 cancel_button->RequestFocus(); | 178 cancel_button->RequestFocus(); |
| 168 EXPECT_EQ(cancel_button, dialog()->GetFocusManager()->GetFocusedView()); | 179 EXPECT_EQ(cancel_button, dialog()->GetFocusManager()->GetFocusedView()); |
| 169 SimulateKeyEvent(return_event); | 180 SimulateKeyEvent(return_event); |
| 170 dialog()->CheckAndResetStates(true, false, nullptr); | 181 dialog()->CheckAndResetStates(true, false, false, nullptr); |
| 171 | 182 |
| 172 // Check that escape can be overridden. | 183 // Check that escape can be overridden. |
| 173 dialog()->set_should_handle_escape(true); | 184 dialog()->set_should_handle_escape(true); |
| 174 SimulateKeyEvent(escape_event); | 185 SimulateKeyEvent(escape_event); |
| 175 dialog()->CheckAndResetStates(false, false, nullptr); | 186 dialog()->CheckAndResetStates(false, false, false, nullptr); |
| 176 } | 187 } |
| 177 | 188 |
| 178 TEST_F(DialogTest, RemoveDefaultButton) { | 189 TEST_F(DialogTest, RemoveDefaultButton) { |
| 179 // Removing buttons from the dialog here should not cause a crash on close. | 190 // Removing buttons from the dialog here should not cause a crash on close. |
| 180 delete dialog()->GetDialogClientView()->ok_button(); | 191 delete dialog()->GetDialogClientView()->ok_button(); |
| 181 delete dialog()->GetDialogClientView()->cancel_button(); | 192 delete dialog()->GetDialogClientView()->cancel_button(); |
| 182 } | 193 } |
| 183 | 194 |
| 184 TEST_F(DialogTest, HitTest_HiddenTitle) { | 195 TEST_F(DialogTest, HitTest_HiddenTitle) { |
| 185 // Ensure that BubbleFrameView hit-tests as expected when the title is hidden. | 196 // Ensure that BubbleFrameView hit-tests as expected when the title is hidden. |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 254 dialog2->TearDown(); | 265 dialog2->TearDown(); |
| 255 } | 266 } |
| 256 | 267 |
| 257 // Tests default focus is assigned correctly when showing a new dialog. | 268 // Tests default focus is assigned correctly when showing a new dialog. |
| 258 TEST_F(DialogTest, InitialFocus) { | 269 TEST_F(DialogTest, InitialFocus) { |
| 259 EXPECT_TRUE(dialog()->input()->HasFocus()); | 270 EXPECT_TRUE(dialog()->input()->HasFocus()); |
| 260 EXPECT_EQ(dialog()->input(), dialog()->GetFocusManager()->GetFocusedView()); | 271 EXPECT_EQ(dialog()->input(), dialog()->GetFocusManager()->GetFocusedView()); |
| 261 } | 272 } |
| 262 | 273 |
| 263 } // namespace views | 274 } // namespace views |
| OLD | NEW |