Home | History | Annotate | Download | only in core
      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/wm/core/focus_controller.h"
      6 
      7 #include <map>
      8 
      9 #include "ui/aura/client/aura_constants.h"
     10 #include "ui/aura/client/default_capture_client.h"
     11 #include "ui/aura/client/focus_change_observer.h"
     12 #include "ui/aura/test/aura_test_base.h"
     13 #include "ui/aura/test/event_generator.h"
     14 #include "ui/aura/test/test_window_delegate.h"
     15 #include "ui/aura/test/test_windows.h"
     16 #include "ui/aura/window.h"
     17 #include "ui/aura/window_event_dispatcher.h"
     18 #include "ui/aura/window_tracker.h"
     19 #include "ui/base/ime/dummy_text_input_client.h"
     20 #include "ui/base/ime/text_input_focus_manager.h"
     21 #include "ui/events/event.h"
     22 #include "ui/events/event_constants.h"
     23 #include "ui/events/event_handler.h"
     24 #include "ui/wm/core/base_focus_rules.h"
     25 #include "ui/wm/core/wm_state.h"
     26 #include "ui/wm/public/activation_change_observer.h"
     27 #include "ui/wm/public/activation_client.h"
     28 
     29 namespace wm {
     30 
     31 class FocusNotificationObserver : public aura::client::ActivationChangeObserver,
     32                                   public aura::client::FocusChangeObserver {
     33  public:
     34   FocusNotificationObserver()
     35       : activation_changed_count_(0),
     36         focus_changed_count_(0),
     37         reactivation_count_(0),
     38         reactivation_requested_window_(NULL),
     39         reactivation_actual_window_(NULL) {}
     40   virtual ~FocusNotificationObserver() {}
     41 
     42   void ExpectCounts(int activation_changed_count, int focus_changed_count) {
     43     EXPECT_EQ(activation_changed_count, activation_changed_count_);
     44     EXPECT_EQ(focus_changed_count, focus_changed_count_);
     45   }
     46   int reactivation_count() const {
     47     return reactivation_count_;
     48   }
     49   aura::Window* reactivation_requested_window() const {
     50     return reactivation_requested_window_;
     51   }
     52   aura::Window* reactivation_actual_window() const {
     53     return reactivation_actual_window_;
     54   }
     55 
     56  private:
     57   // Overridden from aura::client::ActivationChangeObserver:
     58   virtual void OnWindowActivated(aura::Window* gained_active,
     59                                  aura::Window* lost_active) OVERRIDE {
     60     ++activation_changed_count_;
     61   }
     62   virtual void OnAttemptToReactivateWindow(
     63       aura::Window* request_active,
     64       aura::Window* actual_active) OVERRIDE {
     65     ++reactivation_count_;
     66     reactivation_requested_window_ = request_active;
     67     reactivation_actual_window_ = actual_active;
     68   }
     69 
     70   // Overridden from aura::client::FocusChangeObserver:
     71   virtual void OnWindowFocused(aura::Window* gained_focus,
     72                                aura::Window* lost_focus) OVERRIDE {
     73     ++focus_changed_count_;
     74   }
     75 
     76   int activation_changed_count_;
     77   int focus_changed_count_;
     78   int reactivation_count_;
     79   aura::Window* reactivation_requested_window_;
     80   aura::Window* reactivation_actual_window_;
     81 
     82   DISALLOW_COPY_AND_ASSIGN(FocusNotificationObserver);
     83 };
     84 
     85 class WindowDeleter {
     86  public:
     87   virtual aura::Window* GetDeletedWindow() = 0;
     88 
     89  protected:
     90   virtual ~WindowDeleter() {}
     91 };
     92 
     93 // ActivationChangeObserver and FocusChangeObserver that keeps track of whether
     94 // it was notified about activation changes or focus changes with a deleted
     95 // window.
     96 class RecordingActivationAndFocusChangeObserver
     97     : public aura::client::ActivationChangeObserver,
     98       public aura::client::FocusChangeObserver {
     99  public:
    100   RecordingActivationAndFocusChangeObserver(aura::Window* root,
    101                                             WindowDeleter* deleter)
    102       : root_(root),
    103         deleter_(deleter),
    104         was_notified_with_deleted_window_(false) {
    105     aura::client::GetActivationClient(root_)->AddObserver(this);
    106     aura::client::GetFocusClient(root_)->AddObserver(this);
    107   }
    108   virtual ~RecordingActivationAndFocusChangeObserver() {
    109     aura::client::GetActivationClient(root_)->RemoveObserver(this);
    110     aura::client::GetFocusClient(root_)->RemoveObserver(this);
    111   }
    112 
    113   bool was_notified_with_deleted_window() const {
    114     return was_notified_with_deleted_window_;
    115   }
    116 
    117   // Overridden from aura::client::ActivationChangeObserver:
    118   virtual void OnWindowActivated(aura::Window* gained_active,
    119                                  aura::Window* lost_active) OVERRIDE {
    120     if (lost_active && lost_active == deleter_->GetDeletedWindow())
    121       was_notified_with_deleted_window_ = true;
    122   }
    123 
    124   // Overridden from aura::client::FocusChangeObserver:
    125   virtual void OnWindowFocused(aura::Window* gained_focus,
    126                                aura::Window* lost_focus) OVERRIDE {
    127     if (lost_focus && lost_focus == deleter_->GetDeletedWindow())
    128       was_notified_with_deleted_window_ = true;
    129   }
    130 
    131  private:
    132   aura::Window* root_;
    133 
    134   // Not owned.
    135   WindowDeleter* deleter_;
    136 
    137   // Whether the observer was notified about the loss of activation or the
    138   // loss of focus with a window already deleted by |deleter_| as the
    139   // |lost_active| or |lost_focus| parameter.
    140   bool was_notified_with_deleted_window_;
    141 
    142   DISALLOW_COPY_AND_ASSIGN(RecordingActivationAndFocusChangeObserver);
    143 };
    144 
    145 // ActivationChangeObserver that deletes the window losing activation.
    146 class DeleteOnLoseActivationChangeObserver :
    147     public aura::client::ActivationChangeObserver,
    148     public WindowDeleter {
    149  public:
    150   explicit DeleteOnLoseActivationChangeObserver(aura::Window* window)
    151       : root_(window->GetRootWindow()),
    152         window_(window),
    153         did_delete_(false) {
    154     aura::client::GetActivationClient(root_)->AddObserver(this);
    155   }
    156   virtual ~DeleteOnLoseActivationChangeObserver() {
    157     aura::client::GetActivationClient(root_)->RemoveObserver(this);
    158   }
    159 
    160   // Overridden from aura::client::ActivationChangeObserver:
    161   virtual void OnWindowActivated(aura::Window* gained_active,
    162                                  aura::Window* lost_active) OVERRIDE {
    163     if (window_ && lost_active == window_) {
    164       delete lost_active;
    165       did_delete_ = true;
    166     }
    167   }
    168 
    169   // Overridden from WindowDeleter:
    170   virtual aura::Window* GetDeletedWindow() OVERRIDE {
    171     return did_delete_ ? window_ : NULL;
    172   }
    173 
    174  private:
    175   aura::Window* root_;
    176   aura::Window* window_;
    177   bool did_delete_;
    178 
    179   DISALLOW_COPY_AND_ASSIGN(DeleteOnLoseActivationChangeObserver);
    180 };
    181 
    182 // FocusChangeObserver that deletes the window losing focus.
    183 class DeleteOnLoseFocusChangeObserver
    184     : public aura::client::FocusChangeObserver,
    185       public WindowDeleter {
    186  public:
    187   explicit DeleteOnLoseFocusChangeObserver(aura::Window* window)
    188       : root_(window->GetRootWindow()),
    189         window_(window),
    190         did_delete_(false) {
    191     aura::client::GetFocusClient(root_)->AddObserver(this);
    192   }
    193   virtual ~DeleteOnLoseFocusChangeObserver() {
    194     aura::client::GetFocusClient(root_)->RemoveObserver(this);
    195   }
    196 
    197   // Overridden from aura::client::FocusChangeObserver:
    198   virtual void OnWindowFocused(aura::Window* gained_focus,
    199                                aura::Window* lost_focus) OVERRIDE {
    200     if (window_ && lost_focus == window_) {
    201       delete lost_focus;
    202       did_delete_ = true;
    203     }
    204   }
    205 
    206   // Overridden from WindowDeleter:
    207   virtual aura::Window* GetDeletedWindow() OVERRIDE {
    208     return did_delete_ ? window_ : NULL;
    209   }
    210 
    211  private:
    212   aura::Window* root_;
    213   aura::Window* window_;
    214   bool did_delete_;
    215 
    216   DISALLOW_COPY_AND_ASSIGN(DeleteOnLoseFocusChangeObserver);
    217 };
    218 
    219 class ScopedFocusNotificationObserver : public FocusNotificationObserver {
    220  public:
    221   ScopedFocusNotificationObserver(aura::Window* root_window)
    222       : root_window_(root_window) {
    223     aura::client::GetActivationClient(root_window_)->AddObserver(this);
    224     aura::client::GetFocusClient(root_window_)->AddObserver(this);
    225   }
    226   virtual ~ScopedFocusNotificationObserver() {
    227     aura::client::GetActivationClient(root_window_)->RemoveObserver(this);
    228     aura::client::GetFocusClient(root_window_)->RemoveObserver(this);
    229   }
    230 
    231  private:
    232   aura::Window* root_window_;
    233 
    234   DISALLOW_COPY_AND_ASSIGN(ScopedFocusNotificationObserver);
    235 };
    236 
    237 class ScopedTargetFocusNotificationObserver : public FocusNotificationObserver {
    238  public:
    239   ScopedTargetFocusNotificationObserver(aura::Window* root_window, int id)
    240       : target_(root_window->GetChildById(id)) {
    241     aura::client::SetActivationChangeObserver(target_, this);
    242     aura::client::SetFocusChangeObserver(target_, this);
    243     tracker_.Add(target_);
    244   }
    245   virtual ~ScopedTargetFocusNotificationObserver() {
    246     if (tracker_.Contains(target_)) {
    247       aura::client::SetActivationChangeObserver(target_, NULL);
    248       aura::client::SetFocusChangeObserver(target_, NULL);
    249     }
    250   }
    251 
    252  private:
    253   aura::Window* target_;
    254   aura::WindowTracker tracker_;
    255 
    256   DISALLOW_COPY_AND_ASSIGN(ScopedTargetFocusNotificationObserver);
    257 };
    258 
    259 class ScopedFocusedTextInputClientChanger
    260     : public ScopedFocusNotificationObserver {
    261  public:
    262   ScopedFocusedTextInputClientChanger(aura::Window* root_window,
    263                                       ui::TextInputClient* text_input_client)
    264       : ScopedFocusNotificationObserver(root_window),
    265         text_input_client_(text_input_client) {}
    266 
    267  private:
    268   // Overridden from aura::client::FocusChangeObserver:
    269   virtual void OnWindowFocused(aura::Window* gained_focus,
    270                                aura::Window* lost_focus) OVERRIDE {
    271     ui::TextInputFocusManager::GetInstance()->FocusTextInputClient(
    272         text_input_client_);
    273   }
    274 
    275   ui::TextInputClient* text_input_client_;
    276 };
    277 
    278 // Used to fake the handling of events in the pre-target phase.
    279 class SimpleEventHandler : public ui::EventHandler {
    280  public:
    281   SimpleEventHandler() {}
    282   virtual ~SimpleEventHandler() {}
    283 
    284   // Overridden from ui::EventHandler:
    285   virtual void OnMouseEvent(ui::MouseEvent* event) OVERRIDE {
    286     event->SetHandled();
    287   }
    288   virtual void OnGestureEvent(ui::GestureEvent* event) OVERRIDE {
    289     event->SetHandled();
    290   }
    291 
    292  private:
    293   DISALLOW_COPY_AND_ASSIGN(SimpleEventHandler);
    294 };
    295 
    296 class FocusShiftingActivationObserver
    297     : public aura::client::ActivationChangeObserver {
    298  public:
    299   explicit FocusShiftingActivationObserver(aura::Window* activated_window)
    300       : activated_window_(activated_window),
    301         shift_focus_to_(NULL) {}
    302   virtual ~FocusShiftingActivationObserver() {}
    303 
    304   void set_shift_focus_to(aura::Window* shift_focus_to) {
    305     shift_focus_to_ = shift_focus_to;
    306   }
    307 
    308  private:
    309   // Overridden from aura::client::ActivationChangeObserver:
    310   virtual void OnWindowActivated(aura::Window* gained_active,
    311                                  aura::Window* lost_active) OVERRIDE {
    312     // Shift focus to a child. This should prevent the default focusing from
    313     // occurring in FocusController::FocusWindow().
    314     if (gained_active == activated_window_) {
    315       aura::client::FocusClient* client =
    316           aura::client::GetFocusClient(gained_active);
    317       client->FocusWindow(shift_focus_to_);
    318     }
    319   }
    320 
    321   aura::Window* activated_window_;
    322   aura::Window* shift_focus_to_;
    323 
    324   DISALLOW_COPY_AND_ASSIGN(FocusShiftingActivationObserver);
    325 };
    326 
    327 // BaseFocusRules subclass that allows basic overrides of focus/activation to
    328 // be tested. This is intended more as a test that the override system works at
    329 // all, rather than as an exhaustive set of use cases, those should be covered
    330 // in tests for those FocusRules implementations.
    331 class TestFocusRules : public BaseFocusRules {
    332  public:
    333   TestFocusRules() : focus_restriction_(NULL) {}
    334 
    335   // Restricts focus and activation to this window and its child hierarchy.
    336   void set_focus_restriction(aura::Window* focus_restriction) {
    337     focus_restriction_ = focus_restriction;
    338   }
    339 
    340   // Overridden from BaseFocusRules:
    341   virtual bool SupportsChildActivation(aura::Window* window) const OVERRIDE {
    342     // In FocusControllerTests, only the RootWindow has activatable children.
    343     return window->GetRootWindow() == window;
    344   }
    345   virtual bool CanActivateWindow(aura::Window* window) const OVERRIDE {
    346     // Restricting focus to a non-activatable child window means the activatable
    347     // parent outside the focus restriction is activatable.
    348     bool can_activate =
    349         CanFocusOrActivate(window) || window->Contains(focus_restriction_);
    350     return can_activate ? BaseFocusRules::CanActivateWindow(window) : false;
    351   }
    352   virtual bool CanFocusWindow(aura::Window* window) const OVERRIDE {
    353     return CanFocusOrActivate(window) ?
    354         BaseFocusRules::CanFocusWindow(window) : false;
    355   }
    356   virtual aura::Window* GetActivatableWindow(
    357       aura::Window* window) const OVERRIDE {
    358     return BaseFocusRules::GetActivatableWindow(
    359         CanFocusOrActivate(window) ? window : focus_restriction_);
    360   }
    361   virtual aura::Window* GetFocusableWindow(
    362       aura::Window* window) const OVERRIDE {
    363     return BaseFocusRules::GetFocusableWindow(
    364         CanFocusOrActivate(window) ? window : focus_restriction_);
    365   }
    366   virtual aura::Window* GetNextActivatableWindow(
    367       aura::Window* ignore) const OVERRIDE {
    368     aura::Window* next_activatable =
    369         BaseFocusRules::GetNextActivatableWindow(ignore);
    370     return CanFocusOrActivate(next_activatable) ?
    371         next_activatable : GetActivatableWindow(focus_restriction_);
    372   }
    373 
    374  private:
    375   bool CanFocusOrActivate(aura::Window* window) const {
    376     return !focus_restriction_ || focus_restriction_->Contains(window);
    377   }
    378 
    379   aura::Window* focus_restriction_;
    380 
    381   DISALLOW_COPY_AND_ASSIGN(TestFocusRules);
    382 };
    383 
    384 // Common infrastructure shared by all FocusController test types.
    385 class FocusControllerTestBase : public aura::test::AuraTestBase {
    386  protected:
    387   FocusControllerTestBase() {}
    388 
    389   // Overridden from aura::test::AuraTestBase:
    390   virtual void SetUp() OVERRIDE {
    391     wm_state_.reset(new wm::WMState);
    392     // FocusController registers itself as an Env observer so it can catch all
    393     // window initializations, including the root_window()'s, so we create it
    394     // before allowing the base setup.
    395     test_focus_rules_ = new TestFocusRules;
    396     focus_controller_.reset(new FocusController(test_focus_rules_));
    397     aura::test::AuraTestBase::SetUp();
    398     root_window()->AddPreTargetHandler(focus_controller_.get());
    399     aura::client::SetFocusClient(root_window(), focus_controller_.get());
    400     aura::client::SetActivationClient(root_window(), focus_controller_.get());
    401 
    402     // Hierarchy used by all tests:
    403     // root_window
    404     //       +-- w1
    405     //       |    +-- w11
    406     //       |    +-- w12
    407     //       +-- w2
    408     //       |    +-- w21
    409     //       |         +-- w211
    410     //       +-- w3
    411     aura::Window* w1 = aura::test::CreateTestWindowWithDelegate(
    412         aura::test::TestWindowDelegate::CreateSelfDestroyingDelegate(), 1,
    413         gfx::Rect(0, 0, 50, 50), root_window());
    414     aura::test::CreateTestWindowWithDelegate(
    415         aura::test::TestWindowDelegate::CreateSelfDestroyingDelegate(), 11,
    416         gfx::Rect(5, 5, 10, 10), w1);
    417     aura::test::CreateTestWindowWithDelegate(
    418         aura::test::TestWindowDelegate::CreateSelfDestroyingDelegate(), 12,
    419         gfx::Rect(15, 15, 10, 10), w1);
    420     aura::Window* w2 = aura::test::CreateTestWindowWithDelegate(
    421         aura::test::TestWindowDelegate::CreateSelfDestroyingDelegate(), 2,
    422         gfx::Rect(75, 75, 50, 50), root_window());
    423     aura::Window* w21 = aura::test::CreateTestWindowWithDelegate(
    424         aura::test::TestWindowDelegate::CreateSelfDestroyingDelegate(), 21,
    425         gfx::Rect(5, 5, 10, 10), w2);
    426     aura::test::CreateTestWindowWithDelegate(
    427         aura::test::TestWindowDelegate::CreateSelfDestroyingDelegate(), 211,
    428         gfx::Rect(1, 1, 5, 5), w21);
    429     aura::test::CreateTestWindowWithDelegate(
    430         aura::test::TestWindowDelegate::CreateSelfDestroyingDelegate(), 3,
    431         gfx::Rect(125, 125, 50, 50), root_window());
    432   }
    433   virtual void TearDown() OVERRIDE {
    434     root_window()->RemovePreTargetHandler(focus_controller_.get());
    435     aura::test::AuraTestBase::TearDown();
    436     test_focus_rules_ = NULL;  // Owned by FocusController.
    437     focus_controller_.reset();
    438     wm_state_.reset();
    439   }
    440 
    441   void FocusWindow(aura::Window* window) {
    442     aura::client::GetFocusClient(root_window())->FocusWindow(window);
    443   }
    444   aura::Window* GetFocusedWindow() {
    445     return aura::client::GetFocusClient(root_window())->GetFocusedWindow();
    446   }
    447   int GetFocusedWindowId() {
    448     aura::Window* focused_window = GetFocusedWindow();
    449     return focused_window ? focused_window->id() : -1;
    450   }
    451   void ActivateWindow(aura::Window* window) {
    452     aura::client::GetActivationClient(root_window())->ActivateWindow(window);
    453   }
    454   void DeactivateWindow(aura::Window* window) {
    455     aura::client::GetActivationClient(root_window())->DeactivateWindow(window);
    456   }
    457   aura::Window* GetActiveWindow() {
    458     return aura::client::GetActivationClient(root_window())->GetActiveWindow();
    459   }
    460   int GetActiveWindowId() {
    461     aura::Window* active_window = GetActiveWindow();
    462     return active_window ? active_window->id() : -1;
    463   }
    464 
    465   TestFocusRules* test_focus_rules() { return test_focus_rules_; }
    466 
    467   // Test functions.
    468   virtual void BasicFocus() = 0;
    469   virtual void BasicActivation() = 0;
    470   virtual void FocusEvents() = 0;
    471   virtual void DuplicateFocusEvents() {}
    472   virtual void ActivationEvents() = 0;
    473   virtual void ReactivationEvents() {}
    474   virtual void DuplicateActivationEvents() {}
    475   virtual void ShiftFocusWithinActiveWindow() {}
    476   virtual void ShiftFocusToChildOfInactiveWindow() {}
    477   virtual void ShiftFocusToParentOfFocusedWindow() {}
    478   virtual void FocusRulesOverride() = 0;
    479   virtual void ActivationRulesOverride() = 0;
    480   virtual void ShiftFocusOnActivation() {}
    481   virtual void ShiftFocusOnActivationDueToHide() {}
    482   virtual void NoShiftActiveOnActivation() {}
    483   virtual void NoFocusChangeOnClickOnCaptureWindow() {}
    484   virtual void ChangeFocusWhenNothingFocusedAndCaptured() {}
    485   virtual void DontPassDeletedWindow() {}
    486   virtual void FocusedTextInputClient() {}
    487 
    488  private:
    489   scoped_ptr<FocusController> focus_controller_;
    490   TestFocusRules* test_focus_rules_;
    491   scoped_ptr<wm::WMState> wm_state_;
    492 
    493   DISALLOW_COPY_AND_ASSIGN(FocusControllerTestBase);
    494 };
    495 
    496 // Test base for tests where focus is directly set to a target window.
    497 class FocusControllerDirectTestBase : public FocusControllerTestBase {
    498  protected:
    499   FocusControllerDirectTestBase() {}
    500 
    501   // Different test types shift focus in different ways.
    502   virtual void FocusWindowDirect(aura::Window* window) = 0;
    503   virtual void ActivateWindowDirect(aura::Window* window) = 0;
    504   virtual void DeactivateWindowDirect(aura::Window* window) = 0;
    505 
    506   // Input events do not change focus if the window can not be focused.
    507   virtual bool IsInputEvent() = 0;
    508 
    509   void FocusWindowById(int id) {
    510     aura::Window* window = root_window()->GetChildById(id);
    511     DCHECK(window);
    512     FocusWindowDirect(window);
    513   }
    514   void ActivateWindowById(int id) {
    515     aura::Window* window = root_window()->GetChildById(id);
    516     DCHECK(window);
    517     ActivateWindowDirect(window);
    518   }
    519 
    520   // Overridden from FocusControllerTestBase:
    521   virtual void BasicFocus() OVERRIDE {
    522     EXPECT_EQ(NULL, GetFocusedWindow());
    523     FocusWindowById(1);
    524     EXPECT_EQ(1, GetFocusedWindowId());
    525     FocusWindowById(2);
    526     EXPECT_EQ(2, GetFocusedWindowId());
    527   }
    528   virtual void BasicActivation() OVERRIDE {
    529     EXPECT_EQ(NULL, GetActiveWindow());
    530     ActivateWindowById(1);
    531     EXPECT_EQ(1, GetActiveWindowId());
    532     ActivateWindowById(2);
    533     EXPECT_EQ(2, GetActiveWindowId());
    534     // Verify that attempting to deactivate NULL does not crash and does not
    535     // change activation.
    536     DeactivateWindow(NULL);
    537     EXPECT_EQ(2, GetActiveWindowId());
    538     DeactivateWindow(GetActiveWindow());
    539     EXPECT_EQ(1, GetActiveWindowId());
    540   }
    541   virtual void FocusEvents() OVERRIDE {
    542     ScopedFocusNotificationObserver root_observer(root_window());
    543     ScopedTargetFocusNotificationObserver observer1(root_window(), 1);
    544     ScopedTargetFocusNotificationObserver observer2(root_window(), 2);
    545 
    546     root_observer.ExpectCounts(0, 0);
    547     observer1.ExpectCounts(0, 0);
    548     observer2.ExpectCounts(0, 0);
    549 
    550     FocusWindowById(1);
    551     root_observer.ExpectCounts(1, 1);
    552     observer1.ExpectCounts(1, 1);
    553     observer2.ExpectCounts(0, 0);
    554 
    555     FocusWindowById(2);
    556     root_observer.ExpectCounts(2, 2);
    557     observer1.ExpectCounts(2, 2);
    558     observer2.ExpectCounts(1, 1);
    559   }
    560   virtual void DuplicateFocusEvents() OVERRIDE {
    561     // Focusing an existing focused window should not resend focus events.
    562     ScopedFocusNotificationObserver root_observer(root_window());
    563     ScopedTargetFocusNotificationObserver observer1(root_window(), 1);
    564 
    565     root_observer.ExpectCounts(0, 0);
    566     observer1.ExpectCounts(0, 0);
    567 
    568     FocusWindowById(1);
    569     root_observer.ExpectCounts(1, 1);
    570     observer1.ExpectCounts(1, 1);
    571 
    572     FocusWindowById(1);
    573     root_observer.ExpectCounts(1, 1);
    574     observer1.ExpectCounts(1, 1);
    575   }
    576   virtual void ActivationEvents() OVERRIDE {
    577     ActivateWindowById(1);
    578 
    579     ScopedFocusNotificationObserver root_observer(root_window());
    580     ScopedTargetFocusNotificationObserver observer1(root_window(), 1);
    581     ScopedTargetFocusNotificationObserver observer2(root_window(), 2);
    582 
    583     root_observer.ExpectCounts(0, 0);
    584     observer1.ExpectCounts(0, 0);
    585     observer2.ExpectCounts(0, 0);
    586 
    587     ActivateWindowById(2);
    588     root_observer.ExpectCounts(1, 1);
    589     observer1.ExpectCounts(1, 1);
    590     observer2.ExpectCounts(1, 1);
    591   }
    592   virtual void ReactivationEvents() OVERRIDE {
    593     ActivateWindowById(1);
    594     ScopedFocusNotificationObserver root_observer(root_window());
    595     EXPECT_EQ(0, root_observer.reactivation_count());
    596     root_window()->GetChildById(2)->Hide();
    597     // When we attempt to activate "2", which cannot be activated because it
    598     // is not visible, "1" will be reactivated.
    599     ActivateWindowById(2);
    600     EXPECT_EQ(1, root_observer.reactivation_count());
    601     EXPECT_EQ(root_window()->GetChildById(2),
    602               root_observer.reactivation_requested_window());
    603     EXPECT_EQ(root_window()->GetChildById(1),
    604               root_observer.reactivation_actual_window());
    605   }
    606   virtual void DuplicateActivationEvents() OVERRIDE {
    607     // Activating an existing active window should not resend activation events.
    608     ActivateWindowById(1);
    609 
    610     ScopedFocusNotificationObserver root_observer(root_window());
    611     ScopedTargetFocusNotificationObserver observer1(root_window(), 1);
    612     ScopedTargetFocusNotificationObserver observer2(root_window(), 2);
    613 
    614     root_observer.ExpectCounts(0, 0);
    615     observer1.ExpectCounts(0, 0);
    616     observer2.ExpectCounts(0, 0);
    617 
    618     ActivateWindowById(2);
    619     root_observer.ExpectCounts(1, 1);
    620     observer1.ExpectCounts(1, 1);
    621     observer2.ExpectCounts(1, 1);
    622 
    623     ActivateWindowById(2);
    624     root_observer.ExpectCounts(1, 1);
    625     observer1.ExpectCounts(1, 1);
    626     observer2.ExpectCounts(1, 1);
    627   }
    628   virtual void ShiftFocusWithinActiveWindow() OVERRIDE {
    629     ActivateWindowById(1);
    630     EXPECT_EQ(1, GetActiveWindowId());
    631     EXPECT_EQ(1, GetFocusedWindowId());
    632     FocusWindowById(11);
    633     EXPECT_EQ(11, GetFocusedWindowId());
    634     FocusWindowById(12);
    635     EXPECT_EQ(12, GetFocusedWindowId());
    636   }
    637   virtual void ShiftFocusToChildOfInactiveWindow() OVERRIDE {
    638     ActivateWindowById(2);
    639     EXPECT_EQ(2, GetActiveWindowId());
    640     EXPECT_EQ(2, GetFocusedWindowId());
    641     FocusWindowById(11);
    642     EXPECT_EQ(1, GetActiveWindowId());
    643     EXPECT_EQ(11, GetFocusedWindowId());
    644   }
    645   virtual void ShiftFocusToParentOfFocusedWindow() OVERRIDE {
    646     ActivateWindowById(1);
    647     EXPECT_EQ(1, GetFocusedWindowId());
    648     FocusWindowById(11);
    649     EXPECT_EQ(11, GetFocusedWindowId());
    650     FocusWindowById(1);
    651     // Focus should _not_ shift to the parent of the already-focused window.
    652     EXPECT_EQ(11, GetFocusedWindowId());
    653   }
    654   virtual void FocusRulesOverride() OVERRIDE {
    655     EXPECT_EQ(NULL, GetFocusedWindow());
    656     FocusWindowById(11);
    657     EXPECT_EQ(11, GetFocusedWindowId());
    658 
    659     test_focus_rules()->set_focus_restriction(root_window()->GetChildById(211));
    660     FocusWindowById(12);
    661     // Input events leave focus unchanged; direct API calls will change focus
    662     // to the restricted window.
    663     int focused_window = IsInputEvent() ? 11 : 211;
    664     EXPECT_EQ(focused_window, GetFocusedWindowId());
    665 
    666     test_focus_rules()->set_focus_restriction(NULL);
    667     FocusWindowById(12);
    668     EXPECT_EQ(12, GetFocusedWindowId());
    669   }
    670   virtual void ActivationRulesOverride() OVERRIDE {
    671     ActivateWindowById(1);
    672     EXPECT_EQ(1, GetActiveWindowId());
    673     EXPECT_EQ(1, GetFocusedWindowId());
    674 
    675     aura::Window* w3 = root_window()->GetChildById(3);
    676     test_focus_rules()->set_focus_restriction(w3);
    677 
    678     ActivateWindowById(2);
    679     // Input events leave activation unchanged; direct API calls will activate
    680     // the restricted window.
    681     int active_window = IsInputEvent() ? 1 : 3;
    682     EXPECT_EQ(active_window, GetActiveWindowId());
    683     EXPECT_EQ(active_window, GetFocusedWindowId());
    684 
    685     test_focus_rules()->set_focus_restriction(NULL);
    686     ActivateWindowById(2);
    687     EXPECT_EQ(2, GetActiveWindowId());
    688     EXPECT_EQ(2, GetFocusedWindowId());
    689   }
    690   virtual void ShiftFocusOnActivation() OVERRIDE {
    691     // When a window is activated, by default that window is also focused.
    692     // An ActivationChangeObserver may shift focus to another window within the
    693     // same activatable window.
    694     ActivateWindowById(2);
    695     EXPECT_EQ(2, GetFocusedWindowId());
    696     ActivateWindowById(1);
    697     EXPECT_EQ(1, GetFocusedWindowId());
    698 
    699     ActivateWindowById(2);
    700 
    701     aura::Window* target = root_window()->GetChildById(1);
    702     aura::client::ActivationClient* client =
    703         aura::client::GetActivationClient(root_window());
    704 
    705     scoped_ptr<FocusShiftingActivationObserver> observer(
    706         new FocusShiftingActivationObserver(target));
    707     observer->set_shift_focus_to(target->GetChildById(11));
    708     client->AddObserver(observer.get());
    709 
    710     ActivateWindowById(1);
    711 
    712     // w1's ActivationChangeObserver shifted focus to this child, pre-empting
    713     // FocusController's default setting.
    714     EXPECT_EQ(11, GetFocusedWindowId());
    715 
    716     ActivateWindowById(2);
    717     EXPECT_EQ(2, GetFocusedWindowId());
    718 
    719     // Simulate a focus reset by the ActivationChangeObserver. This should
    720     // trigger the default setting in FocusController.
    721     observer->set_shift_focus_to(NULL);
    722     ActivateWindowById(1);
    723     EXPECT_EQ(1, GetFocusedWindowId());
    724 
    725     client->RemoveObserver(observer.get());
    726 
    727     ActivateWindowById(2);
    728     EXPECT_EQ(2, GetFocusedWindowId());
    729     ActivateWindowById(1);
    730     EXPECT_EQ(1, GetFocusedWindowId());
    731   }
    732   virtual void ShiftFocusOnActivationDueToHide() OVERRIDE {
    733     // Similar to ShiftFocusOnActivation except the activation change is
    734     // triggered by hiding the active window.
    735     ActivateWindowById(1);
    736     EXPECT_EQ(1, GetFocusedWindowId());
    737 
    738     // Removes window 3 as candidate for next activatable window.
    739     root_window()->GetChildById(3)->Hide();
    740     EXPECT_EQ(1, GetFocusedWindowId());
    741 
    742     aura::Window* target = root_window()->GetChildById(2);
    743     aura::client::ActivationClient* client =
    744         aura::client::GetActivationClient(root_window());
    745 
    746     scoped_ptr<FocusShiftingActivationObserver> observer(
    747         new FocusShiftingActivationObserver(target));
    748     observer->set_shift_focus_to(target->GetChildById(21));
    749     client->AddObserver(observer.get());
    750 
    751     // Hide the active window.
    752     root_window()->GetChildById(1)->Hide();
    753 
    754     EXPECT_EQ(21, GetFocusedWindowId());
    755 
    756     client->RemoveObserver(observer.get());
    757   }
    758   virtual void NoShiftActiveOnActivation() OVERRIDE {
    759     // When a window is activated, we need to prevent any change to activation
    760     // from being made in response to an activation change notification.
    761   }
    762 
    763   virtual void NoFocusChangeOnClickOnCaptureWindow() OVERRIDE {
    764     scoped_ptr<aura::client::DefaultCaptureClient> capture_client(
    765         new aura::client::DefaultCaptureClient(root_window()));
    766     // Clicking on a window which has capture should not cause a focus change
    767     // to the window. This test verifies whether that is indeed the case.
    768     ActivateWindowById(1);
    769 
    770     EXPECT_EQ(1, GetActiveWindowId());
    771     EXPECT_EQ(1, GetFocusedWindowId());
    772 
    773     aura::Window* w2 = root_window()->GetChildById(2);
    774     aura::client::GetCaptureClient(root_window())->SetCapture(w2);
    775     aura::test::EventGenerator generator(root_window(), w2);
    776     generator.ClickLeftButton();
    777 
    778     EXPECT_EQ(1, GetActiveWindowId());
    779     EXPECT_EQ(1, GetFocusedWindowId());
    780     aura::client::GetCaptureClient(root_window())->ReleaseCapture(w2);
    781   }
    782 
    783   // Verifies focus change is honored while capture held.
    784   virtual void ChangeFocusWhenNothingFocusedAndCaptured() OVERRIDE {
    785     scoped_ptr<aura::client::DefaultCaptureClient> capture_client(
    786         new aura::client::DefaultCaptureClient(root_window()));
    787     aura::Window* w1 = root_window()->GetChildById(1);
    788     aura::client::GetCaptureClient(root_window())->SetCapture(w1);
    789 
    790     EXPECT_EQ(-1, GetActiveWindowId());
    791     EXPECT_EQ(-1, GetFocusedWindowId());
    792 
    793     FocusWindowById(1);
    794 
    795     EXPECT_EQ(1, GetActiveWindowId());
    796     EXPECT_EQ(1, GetFocusedWindowId());
    797 
    798     aura::client::GetCaptureClient(root_window())->ReleaseCapture(w1);
    799   }
    800 
    801   // Verifies if a window that loses activation or focus is deleted during
    802   // observer notification we don't pass the deleted window to other observers.
    803   virtual void DontPassDeletedWindow() OVERRIDE {
    804     FocusWindowById(1);
    805 
    806     EXPECT_EQ(1, GetActiveWindowId());
    807     EXPECT_EQ(1, GetFocusedWindowId());
    808 
    809     {
    810       aura::Window* to_delete = root_window()->GetChildById(1);
    811       DeleteOnLoseActivationChangeObserver observer1(to_delete);
    812       RecordingActivationAndFocusChangeObserver observer2(root_window(),
    813                                                           &observer1);
    814 
    815       FocusWindowById(2);
    816 
    817       EXPECT_EQ(2, GetActiveWindowId());
    818       EXPECT_EQ(2, GetFocusedWindowId());
    819 
    820       EXPECT_EQ(to_delete, observer1.GetDeletedWindow());
    821       EXPECT_FALSE(observer2.was_notified_with_deleted_window());
    822     }
    823 
    824     {
    825       aura::Window* to_delete = root_window()->GetChildById(2);
    826       DeleteOnLoseFocusChangeObserver observer1(to_delete);
    827       RecordingActivationAndFocusChangeObserver observer2(root_window(),
    828                                                           &observer1);
    829 
    830       FocusWindowById(3);
    831 
    832       EXPECT_EQ(3, GetActiveWindowId());
    833       EXPECT_EQ(3, GetFocusedWindowId());
    834 
    835       EXPECT_EQ(to_delete, observer1.GetDeletedWindow());
    836       EXPECT_FALSE(observer2.was_notified_with_deleted_window());
    837     }
    838   }
    839 
    840   // Verifies if the focused text input client is cleared when a window gains
    841   // or loses the focus.
    842   virtual void FocusedTextInputClient() OVERRIDE {
    843     ui::TextInputFocusManager* text_input_focus_manager =
    844         ui::TextInputFocusManager::GetInstance();
    845     ui::DummyTextInputClient text_input_client;
    846     ui::TextInputClient* null_text_input_client = NULL;
    847 
    848     EXPECT_EQ(null_text_input_client,
    849               text_input_focus_manager->GetFocusedTextInputClient());
    850 
    851     text_input_focus_manager->FocusTextInputClient(&text_input_client);
    852     EXPECT_EQ(&text_input_client,
    853               text_input_focus_manager->GetFocusedTextInputClient());
    854     FocusWindowById(1);
    855     // The focused text input client gets cleared when a window gets focused
    856     // unless any of observers sets the focused text input client.
    857     EXPECT_EQ(null_text_input_client,
    858               text_input_focus_manager->GetFocusedTextInputClient());
    859 
    860     ScopedFocusedTextInputClientChanger text_input_focus_changer(
    861         root_window(), &text_input_client);
    862     EXPECT_EQ(null_text_input_client,
    863               text_input_focus_manager->GetFocusedTextInputClient());
    864     FocusWindowById(2);
    865     // |text_input_focus_changer| sets the focused text input client.
    866     EXPECT_EQ(&text_input_client,
    867               text_input_focus_manager->GetFocusedTextInputClient());
    868 
    869     FocusWindow(NULL);
    870     // The focused text input client gets cleared when a window loses the focus.
    871     EXPECT_EQ(null_text_input_client,
    872               text_input_focus_manager->GetFocusedTextInputClient());
    873   }
    874 
    875  private:
    876   DISALLOW_COPY_AND_ASSIGN(FocusControllerDirectTestBase);
    877 };
    878 
    879 // Focus and Activation changes via aura::client::ActivationClient API.
    880 class FocusControllerApiTest : public FocusControllerDirectTestBase {
    881  public:
    882   FocusControllerApiTest() {}
    883 
    884  private:
    885   // Overridden from FocusControllerTestBase:
    886   virtual void FocusWindowDirect(aura::Window* window) OVERRIDE {
    887     FocusWindow(window);
    888   }
    889   virtual void ActivateWindowDirect(aura::Window* window) OVERRIDE {
    890     ActivateWindow(window);
    891   }
    892   virtual void DeactivateWindowDirect(aura::Window* window) OVERRIDE {
    893     DeactivateWindow(window);
    894   }
    895   virtual bool IsInputEvent() OVERRIDE { return false; }
    896 
    897   DISALLOW_COPY_AND_ASSIGN(FocusControllerApiTest);
    898 };
    899 
    900 // Focus and Activation changes via input events.
    901 class FocusControllerMouseEventTest : public FocusControllerDirectTestBase {
    902  public:
    903   FocusControllerMouseEventTest() {}
    904 
    905   // Tests that a handled mouse or gesture event does not trigger a window
    906   // activation.
    907   void IgnoreHandledEvent() {
    908     EXPECT_EQ(NULL, GetActiveWindow());
    909     aura::Window* w1 = root_window()->GetChildById(1);
    910     SimpleEventHandler handler;
    911     root_window()->PrependPreTargetHandler(&handler);
    912     aura::test::EventGenerator generator(root_window(), w1);
    913     generator.ClickLeftButton();
    914     EXPECT_EQ(NULL, GetActiveWindow());
    915     generator.GestureTapAt(w1->bounds().CenterPoint());
    916     EXPECT_EQ(NULL, GetActiveWindow());
    917     root_window()->RemovePreTargetHandler(&handler);
    918     generator.ClickLeftButton();
    919     EXPECT_EQ(1, GetActiveWindowId());
    920   }
    921 
    922  private:
    923   // Overridden from FocusControllerTestBase:
    924   virtual void FocusWindowDirect(aura::Window* window) OVERRIDE {
    925     aura::test::EventGenerator generator(root_window(), window);
    926     generator.ClickLeftButton();
    927   }
    928   virtual void ActivateWindowDirect(aura::Window* window) OVERRIDE {
    929     aura::test::EventGenerator generator(root_window(), window);
    930     generator.ClickLeftButton();
    931   }
    932   virtual void DeactivateWindowDirect(aura::Window* window) OVERRIDE {
    933     aura::Window* next_activatable =
    934         test_focus_rules()->GetNextActivatableWindow(window);
    935     aura::test::EventGenerator generator(root_window(), next_activatable);
    936     generator.ClickLeftButton();
    937   }
    938   virtual bool IsInputEvent() OVERRIDE { return true; }
    939 
    940   DISALLOW_COPY_AND_ASSIGN(FocusControllerMouseEventTest);
    941 };
    942 
    943 class FocusControllerGestureEventTest : public FocusControllerDirectTestBase {
    944  public:
    945   FocusControllerGestureEventTest() {}
    946 
    947  private:
    948   // Overridden from FocusControllerTestBase:
    949   virtual void FocusWindowDirect(aura::Window* window) OVERRIDE {
    950     aura::test::EventGenerator generator(root_window(), window);
    951     generator.GestureTapAt(window->bounds().CenterPoint());
    952   }
    953   virtual void ActivateWindowDirect(aura::Window* window) OVERRIDE {
    954     aura::test::EventGenerator generator(root_window(), window);
    955     generator.GestureTapAt(window->bounds().CenterPoint());
    956   }
    957   virtual void DeactivateWindowDirect(aura::Window* window) OVERRIDE {
    958     aura::Window* next_activatable =
    959         test_focus_rules()->GetNextActivatableWindow(window);
    960     aura::test::EventGenerator generator(root_window(), next_activatable);
    961     generator.GestureTapAt(window->bounds().CenterPoint());
    962   }
    963   virtual bool IsInputEvent() OVERRIDE { return true; }
    964 
    965   DISALLOW_COPY_AND_ASSIGN(FocusControllerGestureEventTest);
    966 };
    967 
    968 // Test base for tests where focus is implicitly set to a window as the result
    969 // of a disposition change to the focused window or the hierarchy that contains
    970 // it.
    971 class FocusControllerImplicitTestBase : public FocusControllerTestBase {
    972  protected:
    973   explicit FocusControllerImplicitTestBase(bool parent) : parent_(parent) {}
    974 
    975   aura::Window* GetDispositionWindow(aura::Window* window) {
    976     return parent_ ? window->parent() : window;
    977   }
    978 
    979   // Change the disposition of |window| in such a way as it will lose focus.
    980   virtual void ChangeWindowDisposition(aura::Window* window) = 0;
    981 
    982   // Allow each disposition change test to add additional post-disposition
    983   // change expectations.
    984   virtual void PostDispostionChangeExpectations() {}
    985 
    986   // Overridden from FocusControllerTestBase:
    987   virtual void BasicFocus() OVERRIDE {
    988     EXPECT_EQ(NULL, GetFocusedWindow());
    989 
    990     aura::Window* w211 = root_window()->GetChildById(211);
    991     FocusWindow(w211);
    992     EXPECT_EQ(211, GetFocusedWindowId());
    993 
    994     ChangeWindowDisposition(w211);
    995     // BasicFocusRules passes focus to the parent.
    996     EXPECT_EQ(parent_ ? 2 : 21, GetFocusedWindowId());
    997   }
    998   virtual void BasicActivation() OVERRIDE {
    999     DCHECK(!parent_) << "Activation tests don't support parent changes.";
   1000 
   1001     EXPECT_EQ(NULL, GetActiveWindow());
   1002 
   1003     aura::Window* w2 = root_window()->GetChildById(2);
   1004     ActivateWindow(w2);
   1005     EXPECT_EQ(2, GetActiveWindowId());
   1006 
   1007     ChangeWindowDisposition(w2);
   1008     EXPECT_EQ(3, GetActiveWindowId());
   1009     PostDispostionChangeExpectations();
   1010   }
   1011   virtual void FocusEvents() OVERRIDE {
   1012     aura::Window* w211 = root_window()->GetChildById(211);
   1013     FocusWindow(w211);
   1014 
   1015     ScopedFocusNotificationObserver root_observer(root_window());
   1016     ScopedTargetFocusNotificationObserver observer211(root_window(), 211);
   1017     root_observer.ExpectCounts(0, 0);
   1018     observer211.ExpectCounts(0, 0);
   1019 
   1020     ChangeWindowDisposition(w211);
   1021     root_observer.ExpectCounts(0, 1);
   1022     observer211.ExpectCounts(0, 1);
   1023   }
   1024   virtual void ActivationEvents() OVERRIDE {
   1025     DCHECK(!parent_) << "Activation tests don't support parent changes.";
   1026 
   1027     aura::Window* w2 = root_window()->GetChildById(2);
   1028     ActivateWindow(w2);
   1029 
   1030     ScopedFocusNotificationObserver root_observer(root_window());
   1031     ScopedTargetFocusNotificationObserver observer2(root_window(), 2);
   1032     ScopedTargetFocusNotificationObserver observer3(root_window(), 3);
   1033     root_observer.ExpectCounts(0, 0);
   1034     observer2.ExpectCounts(0, 0);
   1035     observer3.ExpectCounts(0, 0);
   1036 
   1037     ChangeWindowDisposition(w2);
   1038     root_observer.ExpectCounts(1, 1);
   1039     observer2.ExpectCounts(1, 1);
   1040     observer3.ExpectCounts(1, 1);
   1041   }
   1042   virtual void FocusRulesOverride() OVERRIDE {
   1043     EXPECT_EQ(NULL, GetFocusedWindow());
   1044     aura::Window* w211 = root_window()->GetChildById(211);
   1045     FocusWindow(w211);
   1046     EXPECT_EQ(211, GetFocusedWindowId());
   1047 
   1048     test_focus_rules()->set_focus_restriction(root_window()->GetChildById(11));
   1049     ChangeWindowDisposition(w211);
   1050     // Normally, focus would shift to the parent (w21) but the override shifts
   1051     // it to 11.
   1052     EXPECT_EQ(11, GetFocusedWindowId());
   1053 
   1054     test_focus_rules()->set_focus_restriction(NULL);
   1055   }
   1056   virtual void ActivationRulesOverride() OVERRIDE {
   1057     DCHECK(!parent_) << "Activation tests don't support parent changes.";
   1058 
   1059     aura::Window* w1 = root_window()->GetChildById(1);
   1060     ActivateWindow(w1);
   1061 
   1062     EXPECT_EQ(1, GetActiveWindowId());
   1063     EXPECT_EQ(1, GetFocusedWindowId());
   1064 
   1065     aura::Window* w3 = root_window()->GetChildById(3);
   1066     test_focus_rules()->set_focus_restriction(w3);
   1067 
   1068     // Normally, activation/focus would move to w2, but since we have a focus
   1069     // restriction, it should move to w3 instead.
   1070     ChangeWindowDisposition(w1);
   1071     EXPECT_EQ(3, GetActiveWindowId());
   1072     EXPECT_EQ(3, GetFocusedWindowId());
   1073 
   1074     test_focus_rules()->set_focus_restriction(NULL);
   1075     ActivateWindow(root_window()->GetChildById(2));
   1076     EXPECT_EQ(2, GetActiveWindowId());
   1077     EXPECT_EQ(2, GetFocusedWindowId());
   1078   }
   1079 
   1080  private:
   1081   // When true, the disposition change occurs to the parent of the window
   1082   // instead of to the window. This verifies that changes occurring in the
   1083   // hierarchy that contains the window affect the window's focus.
   1084   bool parent_;
   1085 
   1086   DISALLOW_COPY_AND_ASSIGN(FocusControllerImplicitTestBase);
   1087 };
   1088 
   1089 // Focus and Activation changes in response to window visibility changes.
   1090 class FocusControllerHideTest : public FocusControllerImplicitTestBase {
   1091  public:
   1092   FocusControllerHideTest() : FocusControllerImplicitTestBase(false) {}
   1093 
   1094  protected:
   1095   FocusControllerHideTest(bool parent)
   1096       : FocusControllerImplicitTestBase(parent) {}
   1097 
   1098   // Overridden from FocusControllerImplicitTestBase:
   1099   virtual void ChangeWindowDisposition(aura::Window* window) OVERRIDE {
   1100     GetDispositionWindow(window)->Hide();
   1101   }
   1102 
   1103  private:
   1104   DISALLOW_COPY_AND_ASSIGN(FocusControllerHideTest);
   1105 };
   1106 
   1107 // Focus and Activation changes in response to window parent visibility
   1108 // changes.
   1109 class FocusControllerParentHideTest : public FocusControllerHideTest {
   1110  public:
   1111   FocusControllerParentHideTest() : FocusControllerHideTest(true) {}
   1112 
   1113  private:
   1114   DISALLOW_COPY_AND_ASSIGN(FocusControllerParentHideTest);
   1115 };
   1116 
   1117 // Focus and Activation changes in response to window destruction.
   1118 class FocusControllerDestructionTest : public FocusControllerImplicitTestBase {
   1119  public:
   1120   FocusControllerDestructionTest() : FocusControllerImplicitTestBase(false) {}
   1121 
   1122  protected:
   1123   FocusControllerDestructionTest(bool parent)
   1124       : FocusControllerImplicitTestBase(parent) {}
   1125 
   1126   // Overridden from FocusControllerImplicitTestBase:
   1127   virtual void ChangeWindowDisposition(aura::Window* window) OVERRIDE {
   1128     delete GetDispositionWindow(window);
   1129   }
   1130 
   1131  private:
   1132   DISALLOW_COPY_AND_ASSIGN(FocusControllerDestructionTest);
   1133 };
   1134 
   1135 // Focus and Activation changes in response to window parent destruction.
   1136 class FocusControllerParentDestructionTest
   1137     : public FocusControllerDestructionTest {
   1138  public:
   1139   FocusControllerParentDestructionTest()
   1140       : FocusControllerDestructionTest(true) {}
   1141 
   1142  private:
   1143   DISALLOW_COPY_AND_ASSIGN(FocusControllerParentDestructionTest);
   1144 };
   1145 
   1146 // Focus and Activation changes in response to window removal.
   1147 class FocusControllerRemovalTest : public FocusControllerImplicitTestBase {
   1148  public:
   1149   FocusControllerRemovalTest() : FocusControllerImplicitTestBase(false) {}
   1150 
   1151  protected:
   1152   FocusControllerRemovalTest(bool parent)
   1153       : FocusControllerImplicitTestBase(parent) {}
   1154 
   1155   // Overridden from FocusControllerImplicitTestBase:
   1156   virtual void ChangeWindowDisposition(aura::Window* window) OVERRIDE {
   1157     aura::Window* disposition_window = GetDispositionWindow(window);
   1158     disposition_window->parent()->RemoveChild(disposition_window);
   1159     window_owner_.reset(disposition_window);
   1160   }
   1161   virtual void TearDown() OVERRIDE {
   1162     window_owner_.reset();
   1163     FocusControllerImplicitTestBase::TearDown();
   1164   }
   1165 
   1166  private:
   1167   scoped_ptr<aura::Window> window_owner_;
   1168 
   1169   DISALLOW_COPY_AND_ASSIGN(FocusControllerRemovalTest);
   1170 };
   1171 
   1172 // Focus and Activation changes in response to window parent removal.
   1173 class FocusControllerParentRemovalTest : public FocusControllerRemovalTest {
   1174  public:
   1175   FocusControllerParentRemovalTest() : FocusControllerRemovalTest(true) {}
   1176 
   1177  private:
   1178   DISALLOW_COPY_AND_ASSIGN(FocusControllerParentRemovalTest);
   1179 };
   1180 
   1181 
   1182 #define FOCUS_CONTROLLER_TEST(TESTCLASS, TESTNAME) \
   1183     TEST_F(TESTCLASS, TESTNAME) { TESTNAME(); }
   1184 
   1185 // Runs direct focus change tests (input events and API calls).
   1186 #define DIRECT_FOCUS_CHANGE_TESTS(TESTNAME) \
   1187     FOCUS_CONTROLLER_TEST(FocusControllerApiTest, TESTNAME) \
   1188     FOCUS_CONTROLLER_TEST(FocusControllerMouseEventTest, TESTNAME) \
   1189     FOCUS_CONTROLLER_TEST(FocusControllerGestureEventTest, TESTNAME)
   1190 
   1191 // Runs implicit focus change tests for disposition changes to target.
   1192 #define IMPLICIT_FOCUS_CHANGE_TARGET_TESTS(TESTNAME) \
   1193     FOCUS_CONTROLLER_TEST(FocusControllerHideTest, TESTNAME) \
   1194     FOCUS_CONTROLLER_TEST(FocusControllerDestructionTest, TESTNAME) \
   1195     FOCUS_CONTROLLER_TEST(FocusControllerRemovalTest, TESTNAME)
   1196 
   1197 // Runs implicit focus change tests for disposition changes to target's parent
   1198 // hierarchy.
   1199 #define IMPLICIT_FOCUS_CHANGE_PARENT_TESTS(TESTNAME) \
   1200     /* TODO(beng): parent destruction tests are not supported at
   1201        present due to workspace manager issues. \
   1202     FOCUS_CONTROLLER_TEST(FocusControllerParentDestructionTest, TESTNAME) */ \
   1203     FOCUS_CONTROLLER_TEST(FocusControllerParentHideTest, TESTNAME) \
   1204     FOCUS_CONTROLLER_TEST(FocusControllerParentRemovalTest, TESTNAME)
   1205 
   1206 // Runs all implicit focus change tests (changes to the target and target's
   1207 // parent hierarchy)
   1208 #define IMPLICIT_FOCUS_CHANGE_TESTS(TESTNAME) \
   1209     IMPLICIT_FOCUS_CHANGE_TARGET_TESTS(TESTNAME) \
   1210     IMPLICIT_FOCUS_CHANGE_PARENT_TESTS(TESTNAME)
   1211 
   1212 // Runs all possible focus change tests.
   1213 #define ALL_FOCUS_TESTS(TESTNAME) \
   1214     DIRECT_FOCUS_CHANGE_TESTS(TESTNAME) \
   1215     IMPLICIT_FOCUS_CHANGE_TESTS(TESTNAME)
   1216 
   1217 // Runs focus change tests that apply only to the target. For example,
   1218 // implicit activation changes caused by window disposition changes do not
   1219 // occur when changes to the containing hierarchy happen.
   1220 #define TARGET_FOCUS_TESTS(TESTNAME) \
   1221     DIRECT_FOCUS_CHANGE_TESTS(TESTNAME) \
   1222     IMPLICIT_FOCUS_CHANGE_TARGET_TESTS(TESTNAME)
   1223 
   1224 // - Focuses a window, verifies that focus changed.
   1225 ALL_FOCUS_TESTS(BasicFocus);
   1226 
   1227 // - Activates a window, verifies that activation changed.
   1228 TARGET_FOCUS_TESTS(BasicActivation);
   1229 
   1230 // - Focuses a window, verifies that focus events were dispatched.
   1231 ALL_FOCUS_TESTS(FocusEvents);
   1232 
   1233 // - Focuses or activates a window multiple times, verifies that events are only
   1234 //   dispatched when focus/activation actually changes.
   1235 DIRECT_FOCUS_CHANGE_TESTS(DuplicateFocusEvents);
   1236 DIRECT_FOCUS_CHANGE_TESTS(DuplicateActivationEvents);
   1237 
   1238 // - Activates a window, verifies that activation events were dispatched.
   1239 TARGET_FOCUS_TESTS(ActivationEvents);
   1240 
   1241 // - Attempts to active a hidden window, verifies that current window is
   1242 //   attempted to be reactivated and the appropriate event dispatched.
   1243 FOCUS_CONTROLLER_TEST(FocusControllerApiTest, ReactivationEvents);
   1244 
   1245 // - Input events/API calls shift focus between focusable windows within the
   1246 //   active window.
   1247 DIRECT_FOCUS_CHANGE_TESTS(ShiftFocusWithinActiveWindow);
   1248 
   1249 // - Input events/API calls to a child window of an inactive window shifts
   1250 //   activation to the activatable parent and focuses the child.
   1251 DIRECT_FOCUS_CHANGE_TESTS(ShiftFocusToChildOfInactiveWindow);
   1252 
   1253 // - Input events/API calls to focus the parent of the focused window do not
   1254 //   shift focus away from the child.
   1255 DIRECT_FOCUS_CHANGE_TESTS(ShiftFocusToParentOfFocusedWindow);
   1256 
   1257 // - Verifies that FocusRules determine what can be focused.
   1258 ALL_FOCUS_TESTS(FocusRulesOverride);
   1259 
   1260 // - Verifies that FocusRules determine what can be activated.
   1261 TARGET_FOCUS_TESTS(ActivationRulesOverride);
   1262 
   1263 // - Verifies that attempts to change focus or activation from a focus or
   1264 //   activation change observer are ignored.
   1265 DIRECT_FOCUS_CHANGE_TESTS(ShiftFocusOnActivation);
   1266 DIRECT_FOCUS_CHANGE_TESTS(ShiftFocusOnActivationDueToHide);
   1267 DIRECT_FOCUS_CHANGE_TESTS(NoShiftActiveOnActivation);
   1268 
   1269 // Clicking on a window which has capture should not result in a focus change.
   1270 DIRECT_FOCUS_CHANGE_TESTS(NoFocusChangeOnClickOnCaptureWindow);
   1271 
   1272 FOCUS_CONTROLLER_TEST(FocusControllerApiTest,
   1273                       ChangeFocusWhenNothingFocusedAndCaptured);
   1274 
   1275 // See description above DontPassDeletedWindow() for details.
   1276 FOCUS_CONTROLLER_TEST(FocusControllerApiTest, DontPassDeletedWindow);
   1277 
   1278 // - Verifies that the focused text input client is cleard when the window focus
   1279 //   changes.
   1280 ALL_FOCUS_TESTS(FocusedTextInputClient);
   1281 
   1282 // If a mouse event was handled, it should not activate a window.
   1283 FOCUS_CONTROLLER_TEST(FocusControllerMouseEventTest, IgnoreHandledEvent);
   1284 
   1285 }  // namespace wm
   1286