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 259 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
270 | 270 |
271 dialog2->TearDown(); | 271 dialog2->TearDown(); |
272 } | 272 } |
273 | 273 |
274 // Tests default focus is assigned correctly when showing a new dialog. | 274 // Tests default focus is assigned correctly when showing a new dialog. |
275 TEST_F(DialogTest, InitialFocus) { | 275 TEST_F(DialogTest, InitialFocus) { |
276 EXPECT_TRUE(dialog()->input()->HasFocus()); | 276 EXPECT_TRUE(dialog()->input()->HasFocus()); |
277 EXPECT_EQ(dialog()->input(), dialog()->GetFocusManager()->GetFocusedView()); | 277 EXPECT_EQ(dialog()->input(), dialog()->GetFocusManager()->GetFocusedView()); |
278 } | 278 } |
279 | 279 |
| 280 // A dialog for testing initial focus with only an OK button. |
| 281 class InitialFocusTestDialog : public DialogDelegateView { |
| 282 public: |
| 283 InitialFocusTestDialog() {} |
| 284 ~InitialFocusTestDialog() override {} |
| 285 |
| 286 views::View* OkButton() { return GetDialogClientView()->ok_button(); } |
| 287 |
| 288 // DialogDelegateView overrides: |
| 289 int GetDialogButtons() const override { return ui::DIALOG_BUTTON_OK; } |
| 290 |
| 291 private: |
| 292 DISALLOW_COPY_AND_ASSIGN(InitialFocusTestDialog); |
| 293 }; |
| 294 |
| 295 // If the Widget can't be activated while the initial focus View is requesting |
| 296 // focus, test it is still able to receive focus once the Widget is activated. |
| 297 TEST_F(DialogTest, InitialFocusWithDeactivatedWidget) { |
| 298 InitialFocusTestDialog* dialog = new InitialFocusTestDialog(); |
| 299 Widget* dialog_widget = |
| 300 DialogDelegate::CreateDialogWidget(dialog, GetContext(), nullptr); |
| 301 // Set the initial focus while the Widget is unactivated to prevent the |
| 302 // initially focused View from receiving focus. Use a minimised state here to |
| 303 // prevent the Widget from being activated while this happens. |
| 304 dialog_widget->SetInitialFocus(ui::WindowShowState::SHOW_STATE_MINIMIZED); |
| 305 |
| 306 // Nothing should be focused, because the Widget is still deactivated. |
| 307 EXPECT_EQ(nullptr, dialog_widget->GetFocusManager()->GetFocusedView()); |
| 308 EXPECT_EQ(dialog->OkButton(), |
| 309 dialog_widget->GetFocusManager()->GetStoredFocusView()); |
| 310 dialog_widget->Show(); |
| 311 // After activation, the initially focused View should have focus as intended. |
| 312 EXPECT_EQ(dialog->OkButton(), |
| 313 dialog_widget->GetFocusManager()->GetFocusedView()); |
| 314 EXPECT_TRUE(dialog->OkButton()->HasFocus()); |
| 315 dialog_widget->CloseNow(); |
| 316 } |
| 317 |
280 // If the initially focused View provided is unfocusable, check the next | 318 // If the initially focused View provided is unfocusable, check the next |
281 // available focusable View is focused. | 319 // available focusable View is focused. |
282 TEST_F(DialogTest, UnfocusableInitialFocus) { | 320 TEST_F(DialogTest, UnfocusableInitialFocus) { |
283 #if defined(OS_MACOSX) | 321 #if defined(OS_MACOSX) |
284 // On Mac, make all buttons unfocusable by turning off full keyboard access. | 322 // On Mac, make all buttons unfocusable by turning off full keyboard access. |
285 // This is the more common configuration, and if a dialog has a focusable | 323 // This is the more common configuration, and if a dialog has a focusable |
286 // textfield, tree or table, that should obtain focus instead. | 324 // textfield, tree or table, that should obtain focus instead. |
287 ui::test::ScopedFakeFullKeyboardAccess::GetInstance() | 325 ui::test::ScopedFakeFullKeyboardAccess::GetInstance() |
288 ->set_full_keyboard_access_state(false); | 326 ->set_full_keyboard_access_state(false); |
289 #endif | 327 #endif |
(...skipping 11 matching lines...) Expand all Loading... |
301 dcv->ok_button()->SetFocusBehavior(View::FocusBehavior::NEVER); | 339 dcv->ok_button()->SetFocusBehavior(View::FocusBehavior::NEVER); |
302 dcv->cancel_button()->SetFocusBehavior(View::FocusBehavior::NEVER); | 340 dcv->cancel_button()->SetFocusBehavior(View::FocusBehavior::NEVER); |
303 #endif | 341 #endif |
304 | 342 |
305 // 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. |
306 // 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 |
307 // View, which is |textfield|. | 345 // View, which is |textfield|. |
308 dialog_widget->Show(); | 346 dialog_widget->Show(); |
309 EXPECT_TRUE(textfield->HasFocus()); | 347 EXPECT_TRUE(textfield->HasFocus()); |
310 EXPECT_EQ(textfield, dialog->GetFocusManager()->GetFocusedView()); | 348 EXPECT_EQ(textfield, dialog->GetFocusManager()->GetFocusedView()); |
311 dialog_widget->Close(); | 349 dialog_widget->CloseNow(); |
312 } | 350 } |
313 | 351 |
314 } // namespace views | 352 } // namespace views |
OLD | NEW |