| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "ui/views/focus/focus_manager.h" | 5 #include "ui/views/focus/focus_manager.h" |
| 6 | 6 |
| 7 #include "base/run_loop.h" |
| 7 #include "base/string_number_conversions.h" | 8 #include "base/string_number_conversions.h" |
| 8 #include "base/utf_string_conversions.h" | 9 #include "base/utf_string_conversions.h" |
| 9 #include "ui/base/models/combobox_model.h" | 10 #include "ui/base/models/combobox_model.h" |
| 10 #include "ui/views/controls/button/checkbox.h" | 11 #include "ui/views/controls/button/checkbox.h" |
| 12 #include "ui/views/controls/button/label_button.h" |
| 11 #include "ui/views/controls/button/radio_button.h" | 13 #include "ui/views/controls/button/radio_button.h" |
| 12 #include "ui/views/controls/button/text_button.h" | |
| 13 #include "ui/views/controls/combobox/combobox.h" | 14 #include "ui/views/controls/combobox/combobox.h" |
| 14 #include "ui/views/controls/label.h" | 15 #include "ui/views/controls/label.h" |
| 15 #include "ui/views/controls/link.h" | 16 #include "ui/views/controls/link.h" |
| 16 #include "ui/views/controls/native/native_view_host.h" | 17 #include "ui/views/controls/native/native_view_host.h" |
| 17 #include "ui/views/controls/scroll_view.h" | 18 #include "ui/views/controls/scroll_view.h" |
| 18 #include "ui/views/controls/tabbed_pane/tabbed_pane.h" | 19 #include "ui/views/controls/tabbed_pane/tabbed_pane.h" |
| 19 #include "ui/views/controls/textfield/textfield.h" | 20 #include "ui/views/controls/textfield/textfield.h" |
| 21 #include "ui/views/focus/accelerator_handler.h" |
| 20 #include "ui/views/focus/focus_manager_test.h" | 22 #include "ui/views/focus/focus_manager_test.h" |
| 21 #include "ui/views/widget/root_view.h" | 23 #include "ui/views/widget/root_view.h" |
| 22 #include "ui/views/widget/widget.h" | 24 #include "ui/views/widget/widget.h" |
| 23 | 25 |
| 24 namespace views { | 26 namespace views { |
| 25 | 27 |
| 26 namespace { | 28 namespace { |
| 27 | 29 |
| 28 int count = 1; | 30 int count = 1; |
| 29 | 31 |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 95 public: | 97 public: |
| 96 PaneView() : focus_search_(NULL) {} | 98 PaneView() : focus_search_(NULL) {} |
| 97 | 99 |
| 98 // If this method is called, this view will use GetPaneFocusTraversable to | 100 // If this method is called, this view will use GetPaneFocusTraversable to |
| 99 // have this provided FocusSearch used instead of the default one, allowing | 101 // have this provided FocusSearch used instead of the default one, allowing |
| 100 // you to trap focus within the pane. | 102 // you to trap focus within the pane. |
| 101 void EnablePaneFocus(FocusSearch* focus_search) { | 103 void EnablePaneFocus(FocusSearch* focus_search) { |
| 102 focus_search_ = focus_search; | 104 focus_search_ = focus_search; |
| 103 } | 105 } |
| 104 | 106 |
| 105 // Overridden from views::View: | 107 // Overridden from View: |
| 106 virtual FocusTraversable* GetPaneFocusTraversable() OVERRIDE { | 108 virtual FocusTraversable* GetPaneFocusTraversable() OVERRIDE { |
| 107 if (focus_search_) | 109 if (focus_search_) |
| 108 return this; | 110 return this; |
| 109 else | 111 else |
| 110 return NULL; | 112 return NULL; |
| 111 } | 113 } |
| 112 | 114 |
| 113 // Overridden from views::FocusTraversable: | 115 // Overridden from FocusTraversable: |
| 114 virtual views::FocusSearch* GetFocusSearch() OVERRIDE { | 116 virtual FocusSearch* GetFocusSearch() OVERRIDE { |
| 115 return focus_search_; | 117 return focus_search_; |
| 116 } | 118 } |
| 117 virtual FocusTraversable* GetFocusTraversableParent() OVERRIDE { | 119 virtual FocusTraversable* GetFocusTraversableParent() OVERRIDE { |
| 118 return NULL; | 120 return NULL; |
| 119 } | 121 } |
| 120 virtual View* GetFocusTraversableParentView() OVERRIDE { | 122 virtual View* GetFocusTraversableParentView() OVERRIDE { |
| 121 return NULL; | 123 return NULL; |
| 122 } | 124 } |
| 123 | 125 |
| 124 private: | 126 private: |
| (...skipping 225 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 350 label->SetBounds(label_x, y, label_width, label_height); | 352 label->SetBounds(label_x, y, label_width, label_height); |
| 351 | 353 |
| 352 text_field = new Textfield(); | 354 text_field = new Textfield(); |
| 353 text_field->set_id(kKiwiTextfieldID); | 355 text_field->set_id(kKiwiTextfieldID); |
| 354 left_container_->AddChildView(text_field); | 356 left_container_->AddChildView(text_field); |
| 355 text_field->SetBounds(label_x + label_width + 5, y, | 357 text_field->SetBounds(label_x + label_width + 5, y, |
| 356 text_field_width, label_height); | 358 text_field_width, label_height); |
| 357 | 359 |
| 358 y += label_height + gap_between_labels; | 360 y += label_height + gap_between_labels; |
| 359 | 361 |
| 360 NativeTextButton* button = new NativeTextButton(NULL, | 362 LabelButton* button = new LabelButton(NULL, ASCIIToUTF16("Click me")); |
| 361 ASCIIToUTF16("Click me")); | 363 button->SetStyle(Button::STYLE_NATIVE_TEXTBUTTON); |
| 362 button->SetBounds(label_x, y + 10, 80, 30); | 364 button->SetBounds(label_x, y + 10, 80, 30); |
| 363 button->set_id(kFruitButtonID); | 365 button->set_id(kFruitButtonID); |
| 364 left_container_->AddChildView(button); | 366 left_container_->AddChildView(button); |
| 365 y += 40; | 367 y += 40; |
| 366 | 368 |
| 367 cb = new Checkbox(ASCIIToUTF16("This is another check box")); | 369 cb = new Checkbox(ASCIIToUTF16("This is another check box")); |
| 368 cb->SetBounds(label_x + label_width + 5, y, 180, 20); | 370 cb->SetBounds(label_x + label_width + 5, y, 180, 20); |
| 369 cb->set_id(kFruitCheckBoxID); | 371 cb->set_id(kFruitCheckBoxID); |
| 370 left_container_->AddChildView(cb); | 372 left_container_->AddChildView(cb); |
| 371 y += 20; | 373 y += 20; |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 446 Link* link = new Link(ASCIIToUTF16(kTitles[i])); | 448 Link* link = new Link(ASCIIToUTF16(kTitles[i])); |
| 447 link->SetHorizontalAlignment(gfx::ALIGN_LEFT); | 449 link->SetHorizontalAlignment(gfx::ALIGN_LEFT); |
| 448 link->set_id(kIDs[i]); | 450 link->set_id(kIDs[i]); |
| 449 scroll_content->AddChildView(link); | 451 scroll_content->AddChildView(link); |
| 450 link->SetBounds(5, y, 300, 15); | 452 link->SetBounds(5, y, 300, 15); |
| 451 y += 15; | 453 y += 15; |
| 452 } | 454 } |
| 453 | 455 |
| 454 y = 250; | 456 y = 250; |
| 455 int width = 60; | 457 int width = 60; |
| 456 button = new NativeTextButton(NULL, ASCIIToUTF16("OK")); | 458 button = new LabelButton(NULL, ASCIIToUTF16("OK")); |
| 459 button->SetStyle(Button::STYLE_NATIVE_TEXTBUTTON); |
| 457 button->set_id(kOKButtonID); | 460 button->set_id(kOKButtonID); |
| 458 button->SetIsDefault(true); | 461 button->SetIsDefault(true); |
| 459 | 462 |
| 460 GetContentsView()->AddChildView(button); | 463 GetContentsView()->AddChildView(button); |
| 461 button->SetBounds(150, y, width, 30); | 464 button->SetBounds(150, y, width, 30); |
| 462 | 465 |
| 463 button = new NativeTextButton(NULL, ASCIIToUTF16("Cancel")); | 466 button = new LabelButton(NULL, ASCIIToUTF16("Cancel")); |
| 467 button->SetStyle(Button::STYLE_NATIVE_TEXTBUTTON); |
| 464 button->set_id(kCancelButtonID); | 468 button->set_id(kCancelButtonID); |
| 465 GetContentsView()->AddChildView(button); | 469 GetContentsView()->AddChildView(button); |
| 466 button->SetBounds(220, y, width, 30); | 470 button->SetBounds(220, y, width, 30); |
| 467 | 471 |
| 468 button = new NativeTextButton(NULL, ASCIIToUTF16("Help")); | 472 button = new LabelButton(NULL, ASCIIToUTF16("Help")); |
| 473 button->SetStyle(Button::STYLE_NATIVE_TEXTBUTTON); |
| 469 button->set_id(kHelpButtonID); | 474 button->set_id(kHelpButtonID); |
| 470 GetContentsView()->AddChildView(button); | 475 GetContentsView()->AddChildView(button); |
| 471 button->SetBounds(290, y, width, 30); | 476 button->SetBounds(290, y, width, 30); |
| 472 | 477 |
| 473 y += 40; | 478 y += 40; |
| 474 | 479 |
| 475 View* contents = NULL; | 480 View* contents = NULL; |
| 476 Link* link = NULL; | 481 Link* link = NULL; |
| 477 | 482 |
| 478 // Left bottom box with style checkboxes. | 483 // Left bottom box with style checkboxes. |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 511 style_tab_->AddTab(ASCIIToUTF16("Other"), new View()); | 516 style_tab_->AddTab(ASCIIToUTF16("Other"), new View()); |
| 512 | 517 |
| 513 // Right bottom box with search. | 518 // Right bottom box with search. |
| 514 contents = new View(); | 519 contents = new View(); |
| 515 contents->set_background(Background::CreateSolidBackground(SK_ColorWHITE)); | 520 contents->set_background(Background::CreateSolidBackground(SK_ColorWHITE)); |
| 516 text_field = new Textfield(); | 521 text_field = new Textfield(); |
| 517 contents->AddChildView(text_field); | 522 contents->AddChildView(text_field); |
| 518 text_field->SetBounds(10, 10, 100, 20); | 523 text_field->SetBounds(10, 10, 100, 20); |
| 519 text_field->set_id(kSearchTextfieldID); | 524 text_field->set_id(kSearchTextfieldID); |
| 520 | 525 |
| 521 button = new NativeTextButton(NULL, ASCIIToUTF16("Search")); | 526 button = new LabelButton(NULL, ASCIIToUTF16("Search")); |
| 527 button->SetStyle(Button::STYLE_NATIVE_TEXTBUTTON); |
| 522 contents->AddChildView(button); | 528 contents->AddChildView(button); |
| 523 button->SetBounds(112, 5, 60, 30); | 529 button->SetBounds(112, 5, 60, 30); |
| 524 button->set_id(kSearchButtonID); | 530 button->set_id(kSearchButtonID); |
| 525 | 531 |
| 526 link = new Link(ASCIIToUTF16("Help")); | 532 link = new Link(ASCIIToUTF16("Help")); |
| 527 link->SetHorizontalAlignment(gfx::ALIGN_LEFT); | 533 link->SetHorizontalAlignment(gfx::ALIGN_LEFT); |
| 528 link->set_id(kHelpLinkID); | 534 link->set_id(kHelpLinkID); |
| 529 contents->AddChildView(link); | 535 contents->AddChildView(link); |
| 530 link->SetBounds(175, 10, 30, 20); | 536 link->SetBounds(175, 10, 30, 20); |
| 531 | 537 |
| 532 search_border_view_ = new BorderView(contents); | 538 search_border_view_ = new BorderView(contents); |
| 533 search_border_view_->set_id(kSearchContainerID); | 539 search_border_view_->set_id(kSearchContainerID); |
| 534 | 540 |
| 535 GetContentsView()->AddChildView(search_border_view_); | 541 GetContentsView()->AddChildView(search_border_view_); |
| 536 search_border_view_->SetBounds(300, y, 240, 50); | 542 search_border_view_->SetBounds(300, y, 240, 50); |
| 537 | 543 |
| 538 y += 60; | 544 y += 60; |
| 539 | 545 |
| 540 contents = new View(); | 546 contents = new View(); |
| 541 contents->set_focusable(true); | 547 contents->set_focusable(true); |
| 542 contents->set_background(Background::CreateSolidBackground(SK_ColorBLUE)); | 548 contents->set_background(Background::CreateSolidBackground(SK_ColorBLUE)); |
| 543 contents->set_id(kThumbnailContainerID); | 549 contents->set_id(kThumbnailContainerID); |
| 544 button = new NativeTextButton(NULL, ASCIIToUTF16("Star")); | 550 button = new LabelButton(NULL, ASCIIToUTF16("Star")); |
| 551 button->SetStyle(Button::STYLE_NATIVE_TEXTBUTTON); |
| 545 contents->AddChildView(button); | 552 contents->AddChildView(button); |
| 546 button->SetBounds(5, 5, 50, 30); | 553 button->SetBounds(5, 5, 50, 30); |
| 547 button->set_id(kThumbnailStarID); | 554 button->set_id(kThumbnailStarID); |
| 548 button = new NativeTextButton(NULL, ASCIIToUTF16("SuperStar")); | 555 button = new LabelButton(NULL, ASCIIToUTF16("SuperStar")); |
| 556 button->SetStyle(Button::STYLE_NATIVE_TEXTBUTTON); |
| 549 contents->AddChildView(button); | 557 contents->AddChildView(button); |
| 550 button->SetBounds(60, 5, 100, 30); | 558 button->SetBounds(60, 5, 100, 30); |
| 551 button->set_id(kThumbnailSuperStarID); | 559 button->set_id(kThumbnailSuperStarID); |
| 552 | 560 |
| 553 GetContentsView()->AddChildView(contents); | 561 GetContentsView()->AddChildView(contents); |
| 554 contents->SetBounds(250, y, 200, 50); | 562 contents->SetBounds(250, y, 200, 50); |
| 555 // We can only call RadioButton::SetChecked() on the radio-button is part of | 563 // We can only call RadioButton::SetChecked() on the radio-button is part of |
| 556 // the view hierarchy. | 564 // the view hierarchy. |
| 557 radio_button_to_check->SetChecked(true); | 565 radio_button_to_check->SetChecked(true); |
| 558 } | 566 } |
| 559 | 567 |
| 560 TEST_F(FocusTraversalTest, NormalTraversal) { | 568 TEST_F(FocusTraversalTest, NormalTraversal) { |
| 561 const int kTraversalIDs[] = { kTopCheckBoxID, kAppleTextfieldID, | 569 const int kTraversalIDs[] = { kTopCheckBoxID, kAppleTextfieldID, |
| 562 kOrangeTextfieldID, kBananaTextfieldID, kKiwiTextfieldID, | 570 kOrangeTextfieldID, kBananaTextfieldID, kKiwiTextfieldID, |
| 563 kFruitButtonID, kFruitCheckBoxID, kComboboxID, kBroccoliButtonID, | 571 kFruitButtonID, kFruitCheckBoxID, kComboboxID, kBroccoliButtonID, |
| 564 kRosettaLinkID, kStupeurEtTremblementLinkID, | 572 kRosettaLinkID, kStupeurEtTremblementLinkID, |
| 565 kDinerGameLinkID, kRidiculeLinkID, kClosetLinkID, kVisitingLinkID, | 573 kDinerGameLinkID, kRidiculeLinkID, kClosetLinkID, kVisitingLinkID, |
| 566 kAmelieLinkID, kJoyeuxNoelLinkID, kCampingLinkID, kBriceDeNiceLinkID, | 574 kAmelieLinkID, kJoyeuxNoelLinkID, kCampingLinkID, kBriceDeNiceLinkID, |
| 567 kTaxiLinkID, kAsterixLinkID, kOKButtonID, kCancelButtonID, kHelpButtonID, | 575 kTaxiLinkID, kAsterixLinkID, kOKButtonID, kCancelButtonID, kHelpButtonID, |
| 568 kStyleContainerID, kBoldCheckBoxID, kItalicCheckBoxID, | 576 kStyleContainerID, kBoldCheckBoxID, kItalicCheckBoxID, |
| 569 kUnderlinedCheckBoxID, kStyleHelpLinkID, kStyleTextEditID, | 577 kUnderlinedCheckBoxID, kStyleHelpLinkID, kStyleTextEditID, |
| 570 kSearchTextfieldID, kSearchButtonID, kHelpLinkID, | 578 kSearchTextfieldID, kSearchButtonID, kHelpLinkID, |
| 571 kThumbnailContainerID, kThumbnailStarID, kThumbnailSuperStarID }; | 579 kThumbnailContainerID, kThumbnailStarID, kThumbnailSuperStarID }; |
| 572 | 580 |
| 573 // Uncomment the following line if you want to test manually the UI of this | 581 // Uncomment the following line to manually test the UI of this test. |
| 574 // test. | 582 // base::RunLoop(new AcceleratorHandler()).Run(); |
| 575 // MessageLoopForUI::current()->RunWithDispatcher(new AcceleratorHandler()); | |
| 576 | 583 |
| 577 // Let's traverse the whole focus hierarchy (several times, to make sure it | 584 // Let's traverse the whole focus hierarchy (several times, to make sure it |
| 578 // loops OK). | 585 // loops OK). |
| 579 GetFocusManager()->ClearFocus(); | 586 GetFocusManager()->ClearFocus(); |
| 580 for (int i = 0; i < 3; ++i) { | 587 for (int i = 0; i < 3; ++i) { |
| 581 for (size_t j = 0; j < arraysize(kTraversalIDs); j++) { | 588 for (size_t j = 0; j < arraysize(kTraversalIDs); j++) { |
| 582 GetFocusManager()->AdvanceFocus(false); | 589 GetFocusManager()->AdvanceFocus(false); |
| 583 View* focused_view = GetFocusManager()->GetFocusedView(); | 590 View* focused_view = GetFocusManager()->GetFocusedView(); |
| 584 EXPECT_TRUE(focused_view != NULL); | 591 EXPECT_TRUE(focused_view != NULL); |
| 585 if (focused_view) | 592 if (focused_view) |
| (...skipping 30 matching lines...) Expand all Loading... |
| 616 kSearchButtonID, kThumbnailContainerID, kThumbnailStarID, | 623 kSearchButtonID, kThumbnailContainerID, kThumbnailStarID, |
| 617 kThumbnailSuperStarID }; | 624 kThumbnailSuperStarID }; |
| 618 | 625 |
| 619 // Let's disable some views. | 626 // Let's disable some views. |
| 620 for (size_t i = 0; i < arraysize(kDisabledIDs); i++) { | 627 for (size_t i = 0; i < arraysize(kDisabledIDs); i++) { |
| 621 View* v = FindViewByID(kDisabledIDs[i]); | 628 View* v = FindViewByID(kDisabledIDs[i]); |
| 622 ASSERT_TRUE(v != NULL); | 629 ASSERT_TRUE(v != NULL); |
| 623 v->SetEnabled(false); | 630 v->SetEnabled(false); |
| 624 } | 631 } |
| 625 | 632 |
| 626 // Uncomment the following line if you want to test manually the UI of this | 633 // Uncomment the following line to manually test the UI of this test. |
| 627 // test. | 634 // base::RunLoop(new AcceleratorHandler()).Run(); |
| 628 // MessageLoopForUI::current()->RunWithDispatcher(new AcceleratorHandler()); | |
| 629 | 635 |
| 630 View* focused_view; | 636 View* focused_view; |
| 631 // Let's do one traversal (several times, to make sure it loops ok). | 637 // Let's do one traversal (several times, to make sure it loops ok). |
| 632 GetFocusManager()->ClearFocus(); | 638 GetFocusManager()->ClearFocus(); |
| 633 for (int i = 0; i < 3; ++i) { | 639 for (int i = 0; i < 3; ++i) { |
| 634 for (size_t j = 0; j < arraysize(kTraversalIDs); j++) { | 640 for (size_t j = 0; j < arraysize(kTraversalIDs); j++) { |
| 635 GetFocusManager()->AdvanceFocus(false); | 641 GetFocusManager()->AdvanceFocus(false); |
| 636 focused_view = GetFocusManager()->GetFocusedView(); | 642 focused_view = GetFocusManager()->GetFocusedView(); |
| 637 EXPECT_TRUE(focused_view != NULL); | 643 EXPECT_TRUE(focused_view != NULL); |
| 638 if (focused_view) | 644 if (focused_view) |
| (...skipping 29 matching lines...) Expand all Loading... |
| 668 kStyleTextEditID, kSearchTextfieldID, kSearchButtonID, kHelpLinkID }; | 674 kStyleTextEditID, kSearchTextfieldID, kSearchButtonID, kHelpLinkID }; |
| 669 | 675 |
| 670 | 676 |
| 671 // Let's make some views invisible. | 677 // Let's make some views invisible. |
| 672 for (size_t i = 0; i < arraysize(kInvisibleIDs); i++) { | 678 for (size_t i = 0; i < arraysize(kInvisibleIDs); i++) { |
| 673 View* v = FindViewByID(kInvisibleIDs[i]); | 679 View* v = FindViewByID(kInvisibleIDs[i]); |
| 674 ASSERT_TRUE(v != NULL); | 680 ASSERT_TRUE(v != NULL); |
| 675 v->SetVisible(false); | 681 v->SetVisible(false); |
| 676 } | 682 } |
| 677 | 683 |
| 678 // Uncomment the following line if you want to test manually the UI of this | 684 // Uncomment the following line to manually test the UI of this test. |
| 679 // test. | 685 // base::RunLoop(new AcceleratorHandler()).Run(); |
| 680 // MessageLoopForUI::current()->RunWithDispatcher(new AcceleratorHandler()); | |
| 681 | 686 |
| 682 View* focused_view; | 687 View* focused_view; |
| 683 // Let's do one traversal (several times, to make sure it loops ok). | 688 // Let's do one traversal (several times, to make sure it loops ok). |
| 684 GetFocusManager()->ClearFocus(); | 689 GetFocusManager()->ClearFocus(); |
| 685 for (int i = 0; i < 3; ++i) { | 690 for (int i = 0; i < 3; ++i) { |
| 686 for (size_t j = 0; j < arraysize(kTraversalIDs); j++) { | 691 for (size_t j = 0; j < arraysize(kTraversalIDs); j++) { |
| 687 GetFocusManager()->AdvanceFocus(false); | 692 GetFocusManager()->AdvanceFocus(false); |
| 688 focused_view = GetFocusManager()->GetFocusedView(); | 693 focused_view = GetFocusManager()->GetFocusedView(); |
| 689 EXPECT_TRUE(focused_view != NULL); | 694 EXPECT_TRUE(focused_view != NULL); |
| 690 if (focused_view) | 695 if (focused_view) |
| (...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 776 GetFocusManager()->AdvanceFocus(true); | 781 GetFocusManager()->AdvanceFocus(true); |
| 777 View* focused_view = GetFocusManager()->GetFocusedView(); | 782 View* focused_view = GetFocusManager()->GetFocusedView(); |
| 778 EXPECT_TRUE(focused_view != NULL); | 783 EXPECT_TRUE(focused_view != NULL); |
| 779 if (focused_view) | 784 if (focused_view) |
| 780 EXPECT_EQ(kRightTraversalIDs[j], focused_view->id()); | 785 EXPECT_EQ(kRightTraversalIDs[j], focused_view->id()); |
| 781 } | 786 } |
| 782 } | 787 } |
| 783 } | 788 } |
| 784 | 789 |
| 785 } // namespace views | 790 } // namespace views |
| OLD | NEW |