File: nested_accelerator_controller_unittest.cc

package info (click to toggle)
chromium-browser 41.0.2272.118-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 2,189,132 kB
  • sloc: cpp: 9,691,462; ansic: 3,341,451; python: 712,689; asm: 518,779; xml: 208,926; java: 169,820; sh: 119,353; perl: 68,907; makefile: 28,311; yacc: 13,305; objc: 11,385; tcl: 3,186; cs: 2,225; sql: 2,217; lex: 2,215; lisp: 1,349; pascal: 1,256; awk: 407; ruby: 155; sed: 53; php: 14; exp: 11
file content (206 lines) | stat: -rw-r--r-- 7,128 bytes parent folder | download
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
// Copyright 2014 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 "ui/wm/core/nested_accelerator_controller.h"

#include "base/bind.h"
#include "base/event_types.h"
#include "base/message_loop/message_loop.h"
#include "ui/aura/test/aura_test_base.h"
#include "ui/aura/test/test_windows.h"
#include "ui/aura/window.h"
#include "ui/aura/window_event_dispatcher.h"
#include "ui/base/accelerators/accelerator.h"
#include "ui/base/accelerators/accelerator.h"
#include "ui/base/accelerators/accelerator_manager.h"
#include "ui/events/event_constants.h"
#include "ui/events/event_utils.h"
#include "ui/events/platform/platform_event_dispatcher.h"
#include "ui/events/platform/platform_event_source.h"
#include "ui/events/platform/scoped_event_dispatcher.h"
#include "ui/wm/core/nested_accelerator_delegate.h"
#include "ui/wm/public/dispatcher_client.h"

#if defined(USE_X11)
#include <X11/Xlib.h>
#include "ui/aura/test/x11_event_sender.h"
#include "ui/events/test/events_test_utils_x11.h"
#endif  // USE_X11

namespace wm {
namespace test {

namespace {

class MockDispatcher : public ui::PlatformEventDispatcher {
 public:
  MockDispatcher() : num_key_events_dispatched_(0) {}

  int num_key_events_dispatched() { return num_key_events_dispatched_; }

 private:
  // ui::PlatformEventDispatcher:
  bool CanDispatchEvent(const ui::PlatformEvent& event) override {
    return true;
  }
  uint32_t DispatchEvent(const ui::PlatformEvent& event) override {
    if (ui::EventTypeFromNative(event) == ui::ET_KEY_RELEASED)
      num_key_events_dispatched_++;
    return ui::POST_DISPATCH_NONE;
  }

  int num_key_events_dispatched_;

  DISALLOW_COPY_AND_ASSIGN(MockDispatcher);
};

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

  int accelerator_pressed_count() const { return accelerator_pressed_count_; }

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

 private:
  int accelerator_pressed_count_;

  DISALLOW_COPY_AND_ASSIGN(TestTarget);
};

void DispatchKeyReleaseA(aura::Window* root_window) {
// 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)
  aura::WindowTreeHost* host = root_window->GetHost();
  HWND hwnd = host->GetAcceleratedWidget();
  ::PostMessage(hwnd, WM_KEYDOWN, ui::VKEY_A, 0);
  ::PostMessage(hwnd, WM_KEYUP, ui::VKEY_A, 0);
#elif defined(USE_X11)
  ui::ScopedXI2Event native_event;
  native_event.InitKeyEvent(ui::ET_KEY_PRESSED, ui::VKEY_A, 0);
  aura::WindowTreeHost* host = root_window->GetHost();
  aura::test::PostEventToWindowTreeHost(*native_event, host);
  native_event.InitKeyEvent(ui::ET_KEY_RELEASED, ui::VKEY_A, 0);
  aura::test::PostEventToWindowTreeHost(*native_event, host);
#endif
  // Make sure the inner message-loop terminates after dispatching the events.
  base::MessageLoop::current()->PostTask(
      FROM_HERE, base::MessageLoop::current()->QuitClosure());
}

class MockNestedAcceleratorDelegate : public NestedAcceleratorDelegate {
 public:
  MockNestedAcceleratorDelegate()
      : accelerator_manager_(new ui::AcceleratorManager) {}
  ~MockNestedAcceleratorDelegate() override {}

  // NestedAcceleratorDelegate:
  Result ProcessAccelerator(const ui::Accelerator& accelerator) override {
    return accelerator_manager_->Process(accelerator) ?
        RESULT_PROCESSED : RESULT_NOT_PROCESSED;
  }

  void Register(const ui::Accelerator& accelerator,
                ui::AcceleratorTarget* target) {
    accelerator_manager_->Register(
        accelerator, ui::AcceleratorManager::kNormalPriority, target);
  }

 private:
  scoped_ptr<ui::AcceleratorManager> accelerator_manager_;

  DISALLOW_COPY_AND_ASSIGN(MockNestedAcceleratorDelegate);
};

class NestedAcceleratorTest : public aura::test::AuraTestBase {
 public:
  NestedAcceleratorTest() {}
  ~NestedAcceleratorTest() override {}

  void SetUp() override {
    AuraTestBase::SetUp();
    delegate_ = new MockNestedAcceleratorDelegate();
    nested_accelerator_controller_.reset(
        new NestedAcceleratorController(delegate_));
    aura::client::SetDispatcherClient(root_window(),
                                      nested_accelerator_controller_.get());
  }

  void TearDown() override {
    aura::client::SetDispatcherClient(root_window(), NULL);
    AuraTestBase::TearDown();
    delegate_ = NULL;
    nested_accelerator_controller_.reset();
  }

  MockNestedAcceleratorDelegate* delegate() { return delegate_; }

 private:
  scoped_ptr<NestedAcceleratorController> nested_accelerator_controller_;
  MockNestedAcceleratorDelegate* delegate_;

  DISALLOW_COPY_AND_ASSIGN(NestedAcceleratorTest);
};

}  // namespace

// Aura window above lock screen in z order.
// http://crbug.com/396494
TEST_F(NestedAcceleratorTest, DISABLED_AssociatedWindowAboveLockScreen) {
  // TODO(oshima|sadrul): remove when Win implements PES.
  if (!ui::PlatformEventSource::GetInstance())
    return;
  MockDispatcher inner_dispatcher;
  scoped_ptr<aura::Window> mock_lock_container(
      CreateNormalWindow(0, root_window(), NULL));
  aura::test::CreateTestWindowWithId(1, mock_lock_container.get());

  scoped_ptr<aura::Window> associated_window(
      CreateNormalWindow(2, root_window(), NULL));
  EXPECT_TRUE(aura::test::WindowIsAbove(associated_window.get(),
                                        mock_lock_container.get()));

  DispatchKeyReleaseA(root_window());
  scoped_ptr<ui::ScopedEventDispatcher> override_dispatcher =
      ui::PlatformEventSource::GetInstance()->OverrideDispatcher(
          &inner_dispatcher);
  aura::client::DispatcherRunLoop run_loop(
      aura::client::GetDispatcherClient(root_window()), NULL);
  run_loop.Run();
  EXPECT_EQ(1, inner_dispatcher.num_key_events_dispatched());
}

// Test that the nested dispatcher handles accelerators.
// http://crbug.com/396494
TEST_F(NestedAcceleratorTest, DISABLED_AcceleratorsHandled) {
  // TODO(oshima|sadrul): remove when Win implements PES.
  if (!ui::PlatformEventSource::GetInstance())
    return;
  MockDispatcher inner_dispatcher;
  ui::Accelerator accelerator(ui::VKEY_A, ui::EF_NONE);
  accelerator.set_type(ui::ET_KEY_RELEASED);
  TestTarget target;
  delegate()->Register(accelerator, &target);

  DispatchKeyReleaseA(root_window());
  scoped_ptr<ui::ScopedEventDispatcher> override_dispatcher =
      ui::PlatformEventSource::GetInstance()->OverrideDispatcher(
          &inner_dispatcher);
  aura::client::DispatcherRunLoop run_loop(
      aura::client::GetDispatcherClient(root_window()), NULL);
  run_loop.Run();
  EXPECT_EQ(0, inner_dispatcher.num_key_events_dispatched());
  EXPECT_EQ(1, target.accelerator_pressed_count());
}

}  //  namespace test
}  //  namespace wm