Home | History | Annotate | Download | only in wm
      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 "ash/wm/ash_activation_controller.h"
      6 
      7 #include "ash/launcher/launcher.h"
      8 #include "ash/root_window_controller.h"
      9 #include "ash/shelf/shelf_widget.h"
     10 #include "ash/shell_delegate.h"
     11 #include "ash/test/ash_test_base.h"
     12 #include "ash/wm/property_util.h"
     13 #include "ash/wm/window_util.h"
     14 #include "ui/aura/window.h"
     15 #include "ui/views/corewm/corewm_switches.h"
     16 
     17 namespace ash {
     18 
     19 namespace wm {
     20 
     21 namespace {
     22 
     23 class AshActivationControllerTest : public test::AshTestBase {
     24  public:
     25   AshActivationControllerTest()
     26       : launcher_(NULL), launcher_widget_(NULL), launcher_window_(NULL) {}
     27   virtual ~AshActivationControllerTest() {}
     28 
     29   virtual void SetUp() OVERRIDE {
     30     test::AshTestBase::SetUp();
     31     ash_activation_controller_.reset(new internal::AshActivationController());
     32     launcher_ = Launcher::ForPrimaryDisplay();
     33     ASSERT_TRUE(launcher_);
     34     launcher_widget_ = launcher_->shelf_widget();
     35     ASSERT_TRUE(launcher_widget_);
     36     launcher_window_ = launcher_widget_->GetNativeWindow();
     37     ASSERT_TRUE(launcher_window_);
     38   }
     39 
     40   void SetSpokenFeedbackState(bool enabled) {
     41     if (Shell::GetInstance()->delegate()->IsSpokenFeedbackEnabled() !=
     42         enabled) {
     43       Shell::GetInstance()->delegate()->ToggleSpokenFeedback(
     44           A11Y_NOTIFICATION_NONE);
     45     }
     46   }
     47 
     48  protected:
     49   scoped_ptr<internal::ActivationControllerDelegate> ash_activation_controller_;
     50   ash::Launcher* launcher_;
     51   views::Widget* launcher_widget_;
     52   aura::Window* launcher_window_;
     53 
     54   DISALLOW_COPY_AND_ASSIGN(AshActivationControllerTest);
     55 };
     56 
     57 TEST_F(AshActivationControllerTest, LauncherFallback) {
     58   // When spoken feedback is disabled, then fallback should not occur.
     59   {
     60     SetSpokenFeedbackState(false);
     61     aura::Window* result = ash_activation_controller_->WillActivateWindow(NULL);
     62     EXPECT_EQ(NULL, result);
     63   }
     64 
     65   // When spoken feedback is enabled, then fallback should occur.
     66   {
     67     SetSpokenFeedbackState(true);
     68     aura::Window* result = ash_activation_controller_->WillActivateWindow(NULL);
     69     EXPECT_EQ(launcher_window_, result);
     70   }
     71 
     72   // No fallback when activating another window.
     73   {
     74     aura::Window* test_window = CreateTestWindowInShellWithId(0);
     75     aura::Window* result = ash_activation_controller_->
     76         WillActivateWindow(test_window);
     77     EXPECT_EQ(test_window, result);
     78   }
     79 }
     80 
     81 TEST_F(AshActivationControllerTest, LauncherFallbackOnShutdown) {
     82   SetSpokenFeedbackState(true);
     83   // While shutting down a root window controller, activation controller
     84   // is notified about destroyed windows and therefore will try to activate
     85   // a launcher as fallback, which would result in segmentation faults since
     86   // the launcher's window or the workspace's controller may be already
     87   // destroyed.
     88   GetRootWindowController(Shell::GetActiveRootWindow())->CloseChildWindows();
     89 
     90   aura::Window* result = ash_activation_controller_->WillActivateWindow(NULL);
     91   EXPECT_EQ(NULL, result);
     92 }
     93 
     94 TEST_F(AshActivationControllerTest, LauncherEndToEndFallbackOnDestroyTest) {
     95   // TODO(mtomasz): make this test work with the FocusController.
     96   if (views::corewm::UseFocusController())
     97     return;
     98 
     99   // This test checks the whole fallback activation flow.
    100   SetSpokenFeedbackState(true);
    101 
    102   scoped_ptr<aura::Window> test_window(CreateTestWindowInShellWithId(0));
    103   ActivateWindow(test_window.get());
    104   ASSERT_EQ(test_window.get(), GetActiveWindow());
    105 
    106   // Close the window.
    107   test_window.reset();
    108 
    109   // Verify if the launcher got activated as fallback.
    110   ASSERT_EQ(launcher_window_, GetActiveWindow());
    111 }
    112 
    113 TEST_F(AshActivationControllerTest, LauncherEndToEndFallbackOnMinimizeTest) {
    114   // TODO(mtomasz): make this test work with the FocusController.
    115   if (views::corewm::UseFocusController())
    116     return;
    117 
    118   // This test checks the whole fallback activation flow.
    119   SetSpokenFeedbackState(true);
    120 
    121   scoped_ptr<aura::Window> test_window(CreateTestWindowInShellWithId(0));
    122   ActivateWindow(test_window.get());
    123   ASSERT_EQ(test_window.get(), GetActiveWindow());
    124 
    125   // Minimize the window.
    126   MinimizeWindow(test_window.get());
    127 
    128   // Verify if the launcher got activated as fallback.
    129   ASSERT_EQ(launcher_window_, GetActiveWindow());
    130 }
    131 
    132 TEST_F(AshActivationControllerTest, LauncherEndToEndNoFallbackOnDestroyTest) {
    133   // This test checks the whole fallback activation flow when spoken feedback
    134   // is disabled.
    135   SetSpokenFeedbackState(false);
    136 
    137   scoped_ptr<aura::Window> test_window(CreateTestWindowInShellWithId(0));
    138   ActivateWindow(test_window.get());
    139   ASSERT_EQ(test_window.get(), GetActiveWindow());
    140 
    141   // Close the window.
    142   test_window.reset();
    143 
    144   // Verify if the launcher didn't get activated as fallback.
    145   ASSERT_NE(launcher_window_, GetActiveWindow());
    146 }
    147 
    148 TEST_F(AshActivationControllerTest, LauncherEndToEndNoFallbackOnMinimizeTest) {
    149   // This test checks the whole fallback activation flow when spoken feedback
    150   // is disabled.
    151   SetSpokenFeedbackState(false);
    152 
    153   scoped_ptr<aura::Window> test_window(CreateTestWindowInShellWithId(0));
    154   ActivateWindow(test_window.get());
    155   ASSERT_EQ(test_window.get(), GetActiveWindow());
    156 
    157   // Minimize the window.
    158   MinimizeWindow(test_window.get());
    159 
    160   // Verify if the launcher didn't get activated as fallback.
    161   ASSERT_NE(launcher_window_, GetActiveWindow());
    162 }
    163 
    164 }  // namespace
    165 
    166 }  // namespace wm
    167 
    168 }  // namespace ash
    169