Home | History | Annotate | Download | only in focus
      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 #ifndef UI_VIEWS_FOCUS_FOCUS_MANAGER_TEST_H_
      6 #define UI_VIEWS_FOCUS_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 #if defined(OS_WIN) && !defined(USE_AURA)
     47   // Mocks activating/deactivating the window.
     48   void SimulateActivateWindow();
     49   void SimulateDeactivateWindow();
     50 
     51   void PostKeyDown(ui::KeyboardCode key_code);
     52   void PostKeyUp(ui::KeyboardCode key_code);
     53 #endif
     54 
     55  private:
     56   View* contents_view_;
     57   FocusChangeListener* focus_change_listener_;
     58   WidgetFocusChangeListener* widget_focus_change_listener_;
     59   std::vector<View*> accessible_panes_;
     60 
     61   DISALLOW_COPY_AND_ASSIGN(FocusManagerTest);
     62 };
     63 
     64 typedef std::pair<View*, View*> ViewPair;
     65 
     66 // Use to record focus change notifications.
     67 class TestFocusChangeListener : public FocusChangeListener {
     68  public:
     69   TestFocusChangeListener();
     70   virtual ~TestFocusChangeListener();
     71 
     72   const std::vector<ViewPair>& focus_changes() const { return focus_changes_; }
     73   void ClearFocusChanges();
     74 
     75   // Overridden from FocusChangeListener:
     76   virtual void OnWillChangeFocus(View* focused_before,
     77                                  View* focused_now) OVERRIDE;
     78   virtual void OnDidChangeFocus(View* focused_before,
     79                                 View* focused_now) OVERRIDE;
     80 
     81  private:
     82   // A vector of which views lost/gained focus.
     83   std::vector<ViewPair> focus_changes_;
     84 
     85   DISALLOW_COPY_AND_ASSIGN(TestFocusChangeListener);
     86 };
     87 
     88 typedef std::pair<gfx::NativeView, gfx::NativeView> NativeViewPair;
     89 
     90 // Use to record widget focus change notifications.
     91 class TestWidgetFocusChangeListener : public WidgetFocusChangeListener {
     92  public:
     93   TestWidgetFocusChangeListener();
     94   virtual ~TestWidgetFocusChangeListener();
     95 
     96   const std::vector<NativeViewPair>& focus_changes() const {
     97     return focus_changes_;
     98   }
     99   void ClearFocusChanges();
    100 
    101   // Overridden from WidgetFocusChangeListener:
    102   virtual void OnNativeFocusChange(gfx::NativeView focused_before,
    103                                    gfx::NativeView focused_now) OVERRIDE;
    104 
    105  private:
    106   // Pairs of (focused_before, focused_now) parameters we've received via calls
    107   // to OnNativeFocusChange(), in oldest-to-newest-received order.
    108   std::vector<NativeViewPair> focus_changes_;
    109 
    110   DISALLOW_COPY_AND_ASSIGN(TestWidgetFocusChangeListener);
    111 };
    112 
    113 }  // namespace views
    114 
    115 #endif  // UI_VIEWS_FOCUS_FOCUS_MANAGER_TEST_H_
    116