Home | History | Annotate | Download | only in test
      1 // Copyright 2014 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/test/focus_manager_test.h"
      6 
      7 #include <algorithm>
      8 
      9 #include "ui/views/focus/focus_manager.h"
     10 #include "ui/views/widget/widget.h"
     11 
     12 namespace views {
     13 
     14 ////////////////////////////////////////////////////////////////////////////////
     15 // FocusManagerTest, public:
     16 
     17 FocusManagerTest::FocusManagerTest()
     18     : contents_view_(new View),
     19       focus_change_listener_(NULL),
     20       widget_focus_change_listener_(NULL) {
     21 }
     22 
     23 FocusManagerTest::~FocusManagerTest() {
     24 }
     25 
     26 FocusManager* FocusManagerTest::GetFocusManager() {
     27   return GetWidget()->GetFocusManager();
     28 }
     29 
     30 ////////////////////////////////////////////////////////////////////////////////
     31 // FocusManagerTest, ViewTestBase overrides:
     32 
     33 void FocusManagerTest::SetUp() {
     34   ViewsTestBase::SetUp();
     35 
     36   Widget* widget = new Widget;
     37   Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_WINDOW);
     38   params.delegate = this;
     39   params.bounds = gfx::Rect(0, 0, 1024, 768);
     40   widget->Init(params);
     41 
     42   InitContentView();
     43   widget->Show();
     44 }
     45 
     46 void FocusManagerTest::TearDown() {
     47   if (focus_change_listener_)
     48     GetFocusManager()->RemoveFocusChangeListener(focus_change_listener_);
     49   if (widget_focus_change_listener_) {
     50     WidgetFocusManager::GetInstance()->RemoveFocusChangeListener(
     51         widget_focus_change_listener_);
     52   }
     53   GetWidget()->Close();
     54 
     55   // Flush the message loop to make application verifiers happy.
     56   RunPendingMessages();
     57   ViewsTestBase::TearDown();
     58 }
     59 
     60 ////////////////////////////////////////////////////////////////////////////////
     61 // FocusManagerTest, WidgetDelegate implementation:
     62 
     63 View* FocusManagerTest::GetContentsView() {
     64   return contents_view_;
     65 }
     66 
     67 Widget* FocusManagerTest::GetWidget() {
     68   return contents_view_->GetWidget();
     69 }
     70 
     71 const Widget* FocusManagerTest::GetWidget() const {
     72   return contents_view_->GetWidget();
     73 }
     74 
     75 void FocusManagerTest::GetAccessiblePanes(std::vector<View*>* panes) {
     76   std::copy(accessible_panes_.begin(), accessible_panes_.end(),
     77             std::back_inserter(*panes));
     78 }
     79 
     80 ////////////////////////////////////////////////////////////////////////////////
     81 // FocusManagerTest, protected:
     82 
     83 void FocusManagerTest::InitContentView() {
     84 }
     85 
     86 void FocusManagerTest::AddFocusChangeListener(FocusChangeListener* listener) {
     87   ASSERT_FALSE(focus_change_listener_);
     88   focus_change_listener_ = listener;
     89   GetFocusManager()->AddFocusChangeListener(listener);
     90 }
     91 
     92 void FocusManagerTest::AddWidgetFocusChangeListener(
     93     WidgetFocusChangeListener* listener) {
     94   ASSERT_FALSE(widget_focus_change_listener_);
     95   widget_focus_change_listener_ = listener;
     96   WidgetFocusManager::GetInstance()->AddFocusChangeListener(listener);
     97 }
     98 
     99 void FocusManagerTest::SetAccessiblePanes(const std::vector<View*>& panes) {
    100   accessible_panes_ = panes;
    101 }
    102 
    103 ////////////////////////////////////////////////////////////////////////////////
    104 // TestFocusChangeListener
    105 
    106 TestFocusChangeListener::TestFocusChangeListener() {
    107 }
    108 
    109 TestFocusChangeListener::~TestFocusChangeListener() {
    110 }
    111 
    112 void TestFocusChangeListener::OnWillChangeFocus(View* focused_before,
    113                                                 View* focused_now) {
    114   focus_changes_.push_back(ViewPair(focused_before, focused_now));
    115 }
    116 void TestFocusChangeListener::OnDidChangeFocus(View* focused_before,
    117                                                View* focused_now) {
    118 }
    119 
    120 void TestFocusChangeListener::ClearFocusChanges() {
    121   focus_changes_.clear();
    122 }
    123 
    124 ////////////////////////////////////////////////////////////////////////////////
    125 // TestWidgetFocusChangeListener
    126 
    127 TestWidgetFocusChangeListener::TestWidgetFocusChangeListener() {
    128 }
    129 
    130 TestWidgetFocusChangeListener::~TestWidgetFocusChangeListener() {
    131 }
    132 
    133 void TestWidgetFocusChangeListener::ClearFocusChanges() {
    134   focus_changes_.clear();
    135 }
    136 
    137 void TestWidgetFocusChangeListener::OnNativeFocusChange(
    138     gfx::NativeView focused_before,
    139     gfx::NativeView focused_now) {
    140   focus_changes_.push_back(NativeViewPair(focused_before, focused_now));
    141 }
    142 
    143 }  // namespace views
    144