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