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 #ifndef UI_VIEWS_TEST_FOCUS_MANAGER_TEST_H_ 6 #define UI_VIEWS_TEST_FOCUS_MANAGER_TEST_H_ 7 8 #include "ui/views/focus/focus_manager.h" 9 #include "ui/views/focus/widget_focus_manager.h" 10 #include "ui/views/test/views_test_base.h" 11 #include "ui/views/widget/widget_delegate.h" 12 13 namespace views { 14 15 class FocusChangeListener; 16 17 class FocusManagerTest : public ViewsTestBase, public WidgetDelegate { 18 public: 19 FocusManagerTest(); 20 virtual ~FocusManagerTest(); 21 22 // Convenience to obtain the focus manager for the test's hosting widget. 23 FocusManager* GetFocusManager(); 24 25 // Overridden from ViewsTestBase: 26 virtual void SetUp() OVERRIDE; 27 virtual void TearDown() OVERRIDE; 28 29 // Overridden from WidgetDelegate: 30 virtual View* GetContentsView() OVERRIDE; 31 virtual Widget* GetWidget() OVERRIDE; 32 virtual const Widget* GetWidget() const OVERRIDE; 33 virtual void GetAccessiblePanes(std::vector<View*>* panes) OVERRIDE; 34 35 protected: 36 // Called after the Widget is initialized and the content view is added. 37 // Override to add controls to the layout. 38 virtual void InitContentView(); 39 40 void AddFocusChangeListener(FocusChangeListener* listener); 41 void AddWidgetFocusChangeListener(WidgetFocusChangeListener* listener); 42 43 // For testing FocusManager::RotatePaneFocus(). 44 void SetAccessiblePanes(const std::vector<View*>& panes); 45 46 private: 47 View* contents_view_; 48 FocusChangeListener* focus_change_listener_; 49 WidgetFocusChangeListener* widget_focus_change_listener_; 50 std::vector<View*> accessible_panes_; 51 52 DISALLOW_COPY_AND_ASSIGN(FocusManagerTest); 53 }; 54 55 typedef std::pair<View*, View*> ViewPair; 56 57 // Use to record focus change notifications. 58 class TestFocusChangeListener : public FocusChangeListener { 59 public: 60 TestFocusChangeListener(); 61 virtual ~TestFocusChangeListener(); 62 focus_changes()63 const std::vector<ViewPair>& focus_changes() const { return focus_changes_; } 64 void ClearFocusChanges(); 65 66 // Overridden from FocusChangeListener: 67 virtual void OnWillChangeFocus(View* focused_before, 68 View* focused_now) OVERRIDE; 69 virtual void OnDidChangeFocus(View* focused_before, 70 View* focused_now) OVERRIDE; 71 72 private: 73 // A vector of which views lost/gained focus. 74 std::vector<ViewPair> focus_changes_; 75 76 DISALLOW_COPY_AND_ASSIGN(TestFocusChangeListener); 77 }; 78 79 typedef std::pair<gfx::NativeView, gfx::NativeView> NativeViewPair; 80 81 // Use to record widget focus change notifications. 82 class TestWidgetFocusChangeListener : public WidgetFocusChangeListener { 83 public: 84 TestWidgetFocusChangeListener(); 85 virtual ~TestWidgetFocusChangeListener(); 86 focus_changes()87 const std::vector<NativeViewPair>& focus_changes() const { 88 return focus_changes_; 89 } 90 void ClearFocusChanges(); 91 92 // Overridden from WidgetFocusChangeListener: 93 virtual void OnNativeFocusChange(gfx::NativeView focused_before, 94 gfx::NativeView focused_now) OVERRIDE; 95 96 private: 97 // Pairs of (focused_before, focused_now) parameters we've received via calls 98 // to OnNativeFocusChange(), in oldest-to-newest-received order. 99 std::vector<NativeViewPair> focus_changes_; 100 101 DISALLOW_COPY_AND_ASSIGN(TestWidgetFocusChangeListener); 102 }; 103 104 } // namespace views 105 106 #endif // UI_VIEWS_TEST_FOCUS_MANAGER_TEST_H_ 107