Home | History | Annotate | Download | only in views
      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
      3 // found in the LICENSE file.
      4 
      5 #include "ui/views/accessible_pane_view.h"
      6 
      7 #include "ui/base/accelerators/accelerator.h"
      8 #include "ui/views/controls/button/label_button.h"
      9 #include "ui/views/layout/fill_layout.h"
     10 #include "ui/views/test/views_test_base.h"
     11 #include "ui/views/widget/widget.h"
     12 
     13 namespace views {
     14 
     15 // TODO(alicet): bring pane rotation into views and add tests.
     16 //               See browser_view.cc for details.
     17 
     18 typedef ViewsTestBase AccessiblePaneViewTest;
     19 
     20 class TestBarView : public AccessiblePaneView,
     21                     public ButtonListener {
     22  public:
     23   TestBarView();
     24   virtual ~TestBarView();
     25 
     26   virtual void ButtonPressed(Button* sender,
     27                              const ui::Event& event) OVERRIDE;
     28   LabelButton* child_button() const { return child_button_.get(); }
     29   LabelButton* second_child_button() const {
     30     return second_child_button_.get();
     31   }
     32   LabelButton* third_child_button() const { return third_child_button_.get(); }
     33   LabelButton* not_child_button() const { return not_child_button_.get(); }
     34 
     35   virtual View* GetDefaultFocusableChild() OVERRIDE;
     36 
     37  private:
     38   void Init();
     39 
     40   scoped_ptr<LabelButton> child_button_;
     41   scoped_ptr<LabelButton> second_child_button_;
     42   scoped_ptr<LabelButton> third_child_button_;
     43   scoped_ptr<LabelButton> not_child_button_;
     44 
     45   DISALLOW_COPY_AND_ASSIGN(TestBarView);
     46 };
     47 
     48 TestBarView::TestBarView() {
     49   Init();
     50   set_allow_deactivate_on_esc(true);
     51 }
     52 
     53 TestBarView::~TestBarView() {}
     54 
     55 void TestBarView::ButtonPressed(Button* sender, const ui::Event& event) {
     56 }
     57 
     58 void TestBarView::Init() {
     59   SetLayoutManager(new FillLayout());
     60   base::string16 label;
     61   child_button_.reset(new LabelButton(this, label));
     62   AddChildView(child_button_.get());
     63   second_child_button_.reset(new LabelButton(this, label));
     64   AddChildView(second_child_button_.get());
     65   third_child_button_.reset(new LabelButton(this, label));
     66   AddChildView(third_child_button_.get());
     67   not_child_button_.reset(new LabelButton(this, label));
     68 }
     69 
     70 View* TestBarView::GetDefaultFocusableChild() {
     71   return child_button_.get();
     72 }
     73 
     74 TEST_F(AccessiblePaneViewTest, SimpleSetPaneFocus) {
     75   TestBarView* test_view = new TestBarView();
     76   scoped_ptr<Widget> widget(new Widget());
     77   Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP);
     78   params.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
     79   params.bounds = gfx::Rect(50, 50, 650, 650);
     80   widget->Init(params);
     81   View* root = widget->GetRootView();
     82   root->AddChildView(test_view);
     83   widget->Show();
     84   widget->Activate();
     85 
     86   // Set pane focus succeeds, focus on child.
     87   EXPECT_TRUE(test_view->SetPaneFocusAndFocusDefault());
     88   EXPECT_EQ(test_view, test_view->GetPaneFocusTraversable());
     89   EXPECT_EQ(test_view->child_button(),
     90             test_view->GetWidget()->GetFocusManager()->GetFocusedView());
     91 
     92   // Set focus on non child view, focus failed, stays on pane.
     93   EXPECT_TRUE(test_view->SetPaneFocus(test_view->not_child_button()));
     94   EXPECT_FALSE(test_view->not_child_button() ==
     95                test_view->GetWidget()->GetFocusManager()->GetFocusedView());
     96   EXPECT_EQ(test_view->child_button(),
     97             test_view->GetWidget()->GetFocusManager()->GetFocusedView());
     98   widget->CloseNow();
     99   widget.reset();
    100 }
    101 
    102 // This test will not work properly in Windows because it uses ::GetNextWindow
    103 // on deactivate which is rather unpredictable where the focus will land.
    104 TEST_F(AccessiblePaneViewTest, SetPaneFocusAndRestore) {
    105   View* test_view_main = new View();
    106   scoped_ptr<Widget> widget_main(new Widget());
    107   Widget::InitParams params_main = CreateParams(Widget::InitParams::TYPE_POPUP);
    108   params_main.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
    109   params_main.bounds = gfx::Rect(0, 0, 20, 20);
    110   widget_main->Init(params_main);
    111   View* root_main = widget_main->GetRootView();
    112   root_main->AddChildView(test_view_main);
    113   widget_main->Activate();
    114   test_view_main->GetFocusManager()->SetFocusedView(test_view_main);
    115   EXPECT_TRUE(widget_main->IsActive());
    116   EXPECT_TRUE(test_view_main->HasFocus());
    117 
    118   TestBarView* test_view_bar = new TestBarView();
    119   scoped_ptr<Widget> widget_bar(new Widget());
    120   Widget::InitParams params_bar = CreateParams(Widget::InitParams::TYPE_POPUP);
    121   params_bar.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
    122   params_bar.bounds = gfx::Rect(50, 50, 650, 650);
    123   widget_bar->Init(params_bar);
    124   View* root_bar = widget_bar->GetRootView();
    125   root_bar->AddChildView(test_view_bar);
    126   widget_bar->Show();
    127   widget_bar->Activate();
    128 
    129   // Set pane focus succeeds, focus on child.
    130   EXPECT_TRUE(test_view_bar->SetPaneFocusAndFocusDefault());
    131   EXPECT_FALSE(test_view_main->HasFocus());
    132   EXPECT_FALSE(widget_main->IsActive());
    133   EXPECT_EQ(test_view_bar, test_view_bar->GetPaneFocusTraversable());
    134   EXPECT_EQ(test_view_bar->child_button(),
    135             test_view_bar->GetWidget()->GetFocusManager()->GetFocusedView());
    136 
    137   test_view_bar->AcceleratorPressed(test_view_bar->escape_key());
    138   EXPECT_TRUE(widget_main->IsActive());
    139   EXPECT_FALSE(widget_bar->IsActive());
    140 
    141   widget_bar->CloseNow();
    142   widget_bar.reset();
    143 
    144   widget_main->CloseNow();
    145   widget_main.reset();
    146 }
    147 
    148 TEST_F(AccessiblePaneViewTest, TwoSetPaneFocus) {
    149   TestBarView* test_view = new TestBarView();
    150   TestBarView* test_view_2 = new TestBarView();
    151   scoped_ptr<Widget> widget(new Widget());
    152   Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP);
    153   params.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
    154   params.bounds = gfx::Rect(50, 50, 650, 650);
    155   widget->Init(params);
    156   View* root = widget->GetRootView();
    157   root->AddChildView(test_view);
    158   root->AddChildView(test_view_2);
    159   widget->Show();
    160   widget->Activate();
    161 
    162   // Set pane focus succeeds, focus on child.
    163   EXPECT_TRUE(test_view->SetPaneFocusAndFocusDefault());
    164   EXPECT_EQ(test_view, test_view->GetPaneFocusTraversable());
    165   EXPECT_EQ(test_view->child_button(),
    166             test_view->GetWidget()->GetFocusManager()->GetFocusedView());
    167 
    168   // Set focus on another test_view, focus move to that pane.
    169   EXPECT_TRUE(test_view_2->SetPaneFocus(test_view_2->second_child_button()));
    170   EXPECT_FALSE(test_view->child_button() ==
    171                test_view->GetWidget()->GetFocusManager()->GetFocusedView());
    172   EXPECT_EQ(test_view_2->second_child_button(),
    173             test_view->GetWidget()->GetFocusManager()->GetFocusedView());
    174   widget->CloseNow();
    175   widget.reset();
    176 }
    177 
    178 TEST_F(AccessiblePaneViewTest, PaneFocusTraversal) {
    179   TestBarView* test_view = new TestBarView();
    180   TestBarView* original_test_view = new TestBarView();
    181   scoped_ptr<Widget> widget(new Widget());
    182   Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP);
    183   params.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
    184   params.bounds = gfx::Rect(50, 50, 650, 650);
    185   widget->Init(params);
    186   View* root = widget->GetRootView();
    187   root->AddChildView(original_test_view);
    188   root->AddChildView(test_view);
    189   widget->Show();
    190   widget->Activate();
    191 
    192   // Set pane focus on first view.
    193   EXPECT_TRUE(original_test_view->SetPaneFocus(
    194       original_test_view->third_child_button()));
    195 
    196   // Test travesal in second view.
    197   // Set pane focus on second child.
    198   EXPECT_TRUE(test_view->SetPaneFocus(test_view->second_child_button()));
    199   // home
    200   test_view->AcceleratorPressed(test_view->home_key());
    201   EXPECT_EQ(test_view->child_button(),
    202             test_view->GetWidget()->GetFocusManager()->GetFocusedView());
    203   // end
    204   test_view->AcceleratorPressed(test_view->end_key());
    205   EXPECT_EQ(test_view->third_child_button(),
    206             test_view->GetWidget()->GetFocusManager()->GetFocusedView());
    207   // left
    208   test_view->AcceleratorPressed(test_view->left_key());
    209   EXPECT_EQ(test_view->second_child_button(),
    210             test_view->GetWidget()->GetFocusManager()->GetFocusedView());
    211   // right, right
    212   test_view->AcceleratorPressed(test_view->right_key());
    213   test_view->AcceleratorPressed(test_view->right_key());
    214   EXPECT_EQ(test_view->child_button(),
    215             test_view->GetWidget()->GetFocusManager()->GetFocusedView());
    216 
    217   // ESC
    218   test_view->AcceleratorPressed(test_view->escape_key());
    219   EXPECT_EQ(original_test_view->third_child_button(),
    220             test_view->GetWidget()->GetFocusManager()->GetFocusedView());
    221   widget->CloseNow();
    222   widget.reset();
    223 }
    224 }  // namespace views
    225