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/focus/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 #if defined(OS_WIN) && !defined(USE_AURA) 104 void FocusManagerTest::SimulateActivateWindow() { 105 SendMessage(GetWidget()->GetNativeWindow(), WM_ACTIVATE, WA_ACTIVE, NULL); 106 } 107 108 void FocusManagerTest::SimulateDeactivateWindow() { 109 SendMessage(GetWidget()->GetNativeWindow(), WM_ACTIVATE, WA_INACTIVE, NULL); 110 } 111 112 void FocusManagerTest::PostKeyDown(ui::KeyboardCode key_code) { 113 PostMessage(GetWidget()->GetNativeView(), WM_KEYDOWN, key_code, 0); 114 } 115 116 void FocusManagerTest::PostKeyUp(ui::KeyboardCode key_code) { 117 PostMessage(GetWidget()->GetNativeView(), WM_KEYUP, key_code, 0); 118 } 119 #endif 120 121 //////////////////////////////////////////////////////////////////////////////// 122 // TestFocusChangeListener 123 124 TestFocusChangeListener::TestFocusChangeListener() { 125 } 126 127 TestFocusChangeListener::~TestFocusChangeListener() { 128 } 129 130 void TestFocusChangeListener::OnWillChangeFocus(View* focused_before, 131 View* focused_now) { 132 focus_changes_.push_back(ViewPair(focused_before, focused_now)); 133 } 134 void TestFocusChangeListener::OnDidChangeFocus(View* focused_before, 135 View* focused_now) { 136 } 137 138 void TestFocusChangeListener::ClearFocusChanges() { 139 focus_changes_.clear(); 140 } 141 142 //////////////////////////////////////////////////////////////////////////////// 143 // TestWidgetFocusChangeListener 144 145 TestWidgetFocusChangeListener::TestWidgetFocusChangeListener() { 146 } 147 148 TestWidgetFocusChangeListener::~TestWidgetFocusChangeListener() { 149 } 150 151 void TestWidgetFocusChangeListener::ClearFocusChanges() { 152 focus_changes_.clear(); 153 } 154 155 void TestWidgetFocusChangeListener::OnNativeFocusChange( 156 gfx::NativeView focused_before, 157 gfx::NativeView focused_now) { 158 focus_changes_.push_back(NativeViewPair(focused_before, focused_now)); 159 } 160 161 } // namespace views 162