summaryrefslogtreecommitdiffstats
path: root/ash/accelerators/nested_dispatcher_controller_unittest.cc
blob: c0a2e4f417c1c0d9a07916e744751b0217c6ab20 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ash/accelerators/accelerator_controller.h"
#include "ash/shell.h"
#include "ash/shell_window_ids.h"
#include "ash/test/ash_test_base.h"
#include "base/bind.h"
#include "base/event_types.h"
#include "base/message_loop.h"
#include "ui/aura/client/dispatcher_client.h"
#include "ui/aura/root_window.h"
#include "ui/aura/test/test_windows.h"
#include "ui/aura/window.h"
#include "ui/base/accelerators/accelerator.h"

#if defined(USE_X11)
#include <X11/Xlib.h>
#include "ui/base/x/x11_util.h"
#endif  // USE_X11

namespace ash {
namespace test {

namespace {

class MockDispatcher : public MessageLoop::Dispatcher {
 public:
  MockDispatcher() : num_key_events_dispatched_(0) {
  }

  int num_key_events_dispatched() { return num_key_events_dispatched_; }

#if defined(OS_WIN)
  virtual bool Dispatch(const MSG& msg) OVERRIDE {
    if (msg.message == WM_KEYUP)
      num_key_events_dispatched_++;
    return !ui::IsNoopEvent(msg);
  }
#elif defined(USE_X11)
  virtual base::MessagePumpDispatcher::DispatchStatus Dispatch(
      XEvent* xev) OVERRIDE {
    if (xev->type == KeyRelease)
      num_key_events_dispatched_++;
    return ui::IsNoopEvent(xev) ? MessagePumpDispatcher::EVENT_QUIT :
        MessagePumpDispatcher::EVENT_IGNORED;
  }
#endif

 private:
  int num_key_events_dispatched_;
};

class TestTarget : public ui::AcceleratorTarget {
 public:
  TestTarget() : accelerator_pressed_count_(0) {
  }
  virtual ~TestTarget() {
  }

  int accelerator_pressed_count() const {
    return accelerator_pressed_count_;
  }

  // Overridden from ui::AcceleratorTarget:
  virtual bool AcceleratorPressed(const ui::Accelerator& accelerator) OVERRIDE {
    accelerator_pressed_count_++;
    return true;
  }
  virtual bool CanHandleAccelerators() const OVERRIDE {
    return true;
  }

 private:
  int accelerator_pressed_count_;

  DISALLOW_COPY_AND_ASSIGN(TestTarget);
};

void DispatchKeyReleaseA() {
  // Sending both keydown and keyup is necessary here because the accelerator
  // manager only checks a keyup event following a keydown event. See
  // ShouldHandle() in ui/base/accelerators/accelerator_manager.cc for details.
#if defined(OS_WIN)
  MSG native_event_down = { NULL, WM_KEYDOWN, ui::VKEY_A, 0 };
  ash::Shell::GetRootWindow()->PostNativeEvent(native_event_down);
  MSG native_event_up = { NULL, WM_KEYUP, ui::VKEY_A, 0 };
  ash::Shell::GetRootWindow()->PostNativeEvent(native_event_up);
#elif defined(USE_X11)
  XEvent native_event;
  ui::InitXKeyEventForTesting(ui::ET_KEY_PRESSED,
                              ui::VKEY_A,
                              0,
                              &native_event);
  ash::Shell::GetRootWindow()->PostNativeEvent(&native_event);
  ui::InitXKeyEventForTesting(ui::ET_KEY_RELEASED,
                              ui::VKEY_A,
                              0,
                              &native_event);
  ash::Shell::GetRootWindow()->PostNativeEvent(&native_event);
#endif

  // Send noop event to signal dispatcher to exit.
  ash::Shell::GetRootWindow()->PostNativeEvent(ui::CreateNoopEvent());
}

}  // namespace

typedef AshTestBase NestedDispatcherTest;

// Aura window below lock screen in z order.
TEST_F(NestedDispatcherTest, AssociatedWindowBelowLockScreen) {
  MockDispatcher inner_dispatcher;
  aura::Window* default_container = Shell::GetInstance()->GetContainer(
      internal::kShellWindowId_DefaultContainer);
  scoped_ptr<aura::Window>associated_window(aura::test::CreateTestWindowWithId(
      0, default_container));
  scoped_ptr<aura::Window>mock_lock_container(
      aura::test::CreateTestWindowWithId(0, default_container));
  mock_lock_container->set_stops_event_propagation(true);
  aura::test::CreateTestWindowWithId(0, mock_lock_container.get());
  EXPECT_TRUE(aura::test::WindowIsAbove(mock_lock_container.get(),
      associated_window.get()));
  DispatchKeyReleaseA();
  aura::RootWindow* root_window = ash::Shell::GetInstance()->GetRootWindow();
  aura::client::GetDispatcherClient(root_window)->RunWithDispatcher(
      &inner_dispatcher,
      associated_window.get(),
      true /* nestable_tasks_allowed */);
  EXPECT_EQ(0, inner_dispatcher.num_key_events_dispatched());
}

// Aura window above lock screen in z order.
TEST_F(NestedDispatcherTest, AssociatedWindowAboveLockScreen) {
  MockDispatcher inner_dispatcher;

  aura::Window* default_container = Shell::GetInstance()->GetContainer(
      internal::kShellWindowId_DefaultContainer);
  scoped_ptr<aura::Window>mock_lock_container(
      aura::test::CreateTestWindowWithId(0, default_container));
  mock_lock_container->set_stops_event_propagation(true);
  aura::test::CreateTestWindowWithId(0, mock_lock_container.get());
  scoped_ptr<aura::Window>associated_window(aura::test::CreateTestWindowWithId(
      0, default_container));
  EXPECT_TRUE(aura::test::WindowIsAbove(associated_window.get(),
      mock_lock_container.get()));

  DispatchKeyReleaseA();
  aura::RootWindow* root_window = ash::Shell::GetInstance()->GetRootWindow();
  aura::client::GetDispatcherClient(root_window)->RunWithDispatcher(
      &inner_dispatcher,
      associated_window.get(),
      true /* nestable_tasks_allowed */);
  EXPECT_EQ(1, inner_dispatcher.num_key_events_dispatched());
}

// Test that the nested dispatcher handles accelerators.
TEST_F(NestedDispatcherTest, AcceleratorsHandled) {
  MockDispatcher inner_dispatcher;
  aura::RootWindow* root_window = ash::Shell::GetInstance()->GetRootWindow();

  ui::Accelerator accelerator(ui::VKEY_A, false, false, false);
  accelerator.set_type(ui::ET_KEY_RELEASED);
  // TODO(yusukes): Add a test for a ui::ET_TRANSLATED_KEY_RELEASE accelerator.
  TestTarget target;
  Shell::GetInstance()->accelerator_controller()->Register(accelerator,
                                                           &target);

  DispatchKeyReleaseA();
  aura::client::GetDispatcherClient(root_window)->RunWithDispatcher(
      &inner_dispatcher,
      root_window,
      true /* nestable_tasks_allowed */);
  EXPECT_EQ(0, inner_dispatcher.num_key_events_dispatched());
  EXPECT_EQ(1, target.accelerator_pressed_count());
}

} //  namespace test
} //  namespace ash