Home | History | Annotate | Download | only in accelerators
      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/accelerators/accelerator_controller.h"
      6 #include "ash/session_state_delegate.h"
      7 #include "ash/shell.h"
      8 #include "ash/shell_window_ids.h"
      9 #include "ash/test/ash_test_base.h"
     10 #include "base/bind.h"
     11 #include "base/event_types.h"
     12 #include "base/message_loop/message_loop.h"
     13 #include "ui/aura/client/dispatcher_client.h"
     14 #include "ui/aura/root_window.h"
     15 #include "ui/aura/test/test_windows.h"
     16 #include "ui/aura/window.h"
     17 #include "ui/base/accelerators/accelerator.h"
     18 #include "ui/events/event_constants.h"
     19 #include "ui/events/event_utils.h"
     20 
     21 #if defined(USE_X11)
     22 #include <X11/Xlib.h>
     23 #include "ui/events/test/events_test_utils_x11.h"
     24 #endif  // USE_X11
     25 
     26 namespace ash {
     27 namespace test {
     28 
     29 namespace {
     30 
     31 class MockDispatcher : public base::MessageLoop::Dispatcher {
     32  public:
     33   MockDispatcher() : num_key_events_dispatched_(0) {
     34   }
     35 
     36   int num_key_events_dispatched() { return num_key_events_dispatched_; }
     37 
     38 #if defined(OS_WIN) || defined(USE_X11) || defined(USE_OZONE)
     39   virtual bool Dispatch(const base::NativeEvent& event) OVERRIDE {
     40     if (ui::EventTypeFromNative(event) == ui::ET_KEY_RELEASED)
     41       num_key_events_dispatched_++;
     42     return !ui::IsNoopEvent(event);
     43   }
     44 #endif
     45 
     46  private:
     47   int num_key_events_dispatched_;
     48 };
     49 
     50 class TestTarget : public ui::AcceleratorTarget {
     51  public:
     52   TestTarget() : accelerator_pressed_count_(0) {
     53   }
     54   virtual ~TestTarget() {
     55   }
     56 
     57   int accelerator_pressed_count() const {
     58     return accelerator_pressed_count_;
     59   }
     60 
     61   // Overridden from ui::AcceleratorTarget:
     62   virtual bool AcceleratorPressed(const ui::Accelerator& accelerator) OVERRIDE {
     63     accelerator_pressed_count_++;
     64     return true;
     65   }
     66   virtual bool CanHandleAccelerators() const OVERRIDE {
     67     return true;
     68   }
     69 
     70  private:
     71   int accelerator_pressed_count_;
     72 
     73   DISALLOW_COPY_AND_ASSIGN(TestTarget);
     74 };
     75 
     76 void DispatchKeyReleaseA() {
     77   // Sending both keydown and keyup is necessary here because the accelerator
     78   // manager only checks a keyup event following a keydown event. See
     79   // ShouldHandle() in ui/base/accelerators/accelerator_manager.cc for details.
     80 #if defined(OS_WIN)
     81   MSG native_event_down = { NULL, WM_KEYDOWN, ui::VKEY_A, 0 };
     82   ash::Shell::GetPrimaryRootWindow()->host()->PostNativeEvent(
     83       native_event_down);
     84   MSG native_event_up = { NULL, WM_KEYUP, ui::VKEY_A, 0 };
     85   ash::Shell::GetPrimaryRootWindow()->host()->PostNativeEvent(native_event_up);
     86 #elif defined(USE_X11)
     87   ui::ScopedXI2Event native_event;
     88   native_event.InitKeyEvent(ui::ET_KEY_PRESSED, ui::VKEY_A, 0);
     89   aura::WindowEventDispatcher* dispatcher =
     90       ash::Shell::GetPrimaryRootWindow()->GetDispatcher();
     91   dispatcher->host()->PostNativeEvent(native_event);
     92   native_event.InitKeyEvent(ui::ET_KEY_RELEASED, ui::VKEY_A, 0);
     93   dispatcher->host()->PostNativeEvent(native_event);
     94 #endif
     95 
     96   // Send noop event to signal dispatcher to exit.
     97   dispatcher->host()->PostNativeEvent(ui::CreateNoopEvent());
     98 }
     99 
    100 }  // namespace
    101 
    102 typedef AshTestBase NestedDispatcherTest;
    103 
    104 // Aura window below lock screen in z order.
    105 TEST_F(NestedDispatcherTest, AssociatedWindowBelowLockScreen) {
    106   MockDispatcher inner_dispatcher;
    107   scoped_ptr<aura::Window> associated_window(CreateTestWindowInShellWithId(0));
    108 
    109   Shell::GetInstance()->session_state_delegate()->LockScreen();
    110   DispatchKeyReleaseA();
    111   aura::Window* root_window = ash::Shell::GetPrimaryRootWindow();
    112   aura::client::GetDispatcherClient(root_window)->RunWithDispatcher(
    113       &inner_dispatcher,
    114       associated_window.get(),
    115       true /* nestable_tasks_allowed */);
    116   EXPECT_EQ(0, inner_dispatcher.num_key_events_dispatched());
    117   Shell::GetInstance()->session_state_delegate()->UnlockScreen();
    118 }
    119 
    120 // Aura window above lock screen in z order.
    121 TEST_F(NestedDispatcherTest, AssociatedWindowAboveLockScreen) {
    122   MockDispatcher inner_dispatcher;
    123 
    124   scoped_ptr<aura::Window>mock_lock_container(
    125       CreateTestWindowInShellWithId(0));
    126   aura::test::CreateTestWindowWithId(0, mock_lock_container.get());
    127   scoped_ptr<aura::Window> associated_window(CreateTestWindowInShellWithId(0));
    128   EXPECT_TRUE(aura::test::WindowIsAbove(associated_window.get(),
    129       mock_lock_container.get()));
    130 
    131   DispatchKeyReleaseA();
    132   aura::Window* root_window = ash::Shell::GetPrimaryRootWindow();
    133   aura::client::GetDispatcherClient(root_window)->RunWithDispatcher(
    134       &inner_dispatcher,
    135       associated_window.get(),
    136       true /* nestable_tasks_allowed */);
    137   EXPECT_EQ(1, inner_dispatcher.num_key_events_dispatched());
    138 }
    139 
    140 // Test that the nested dispatcher handles accelerators.
    141 TEST_F(NestedDispatcherTest, AcceleratorsHandled) {
    142   MockDispatcher inner_dispatcher;
    143   aura::Window* root_window = ash::Shell::GetPrimaryRootWindow();
    144 
    145   ui::Accelerator accelerator(ui::VKEY_A, ui::EF_NONE);
    146   accelerator.set_type(ui::ET_KEY_RELEASED);
    147   TestTarget target;
    148   Shell::GetInstance()->accelerator_controller()->Register(accelerator,
    149                                                            &target);
    150 
    151   DispatchKeyReleaseA();
    152   aura::client::GetDispatcherClient(root_window)->RunWithDispatcher(
    153       &inner_dispatcher,
    154       root_window,
    155       true /* nestable_tasks_allowed */);
    156   EXPECT_EQ(0, inner_dispatcher.num_key_events_dispatched());
    157   EXPECT_EQ(1, target.accelerator_pressed_count());
    158 }
    159 
    160 } //  namespace test
    161 } //  namespace ash
    162