Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / chromeos / keyboard_driven_event_rewriter_unittest.cc
blob151bbab5c477a36dc8553621c6e4b81ca0820f74
1 // Copyright 2013 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.
5 #include "base/basictypes.h"
6 #include "base/compiler_specific.h"
7 #include "base/strings/stringprintf.h"
8 #include "chrome/browser/chromeos/keyboard_driven_event_rewriter.h"
9 #include "testing/gtest/include/gtest/gtest.h"
10 #include "ui/events/event.h"
11 #include "ui/events/test/events_test_utils_x11.h"
13 #include <X11/keysym.h>
14 #include <X11/XF86keysym.h>
15 #include <X11/Xlib.h>
17 namespace chromeos {
19 class KeyboardDrivenEventRewriterTest : public testing::Test {
20 public:
21 KeyboardDrivenEventRewriterTest()
22 : display_(gfx::GetXDisplay()),
23 keycode_a_(XKeysymToKeycode(display_, XK_a)),
24 keycode_up_(XKeysymToKeycode(display_, XK_Up)),
25 keycode_down_(XKeysymToKeycode(display_, XK_Down)),
26 keycode_left_(XKeysymToKeycode(display_, XK_Left)),
27 keycode_right_(XKeysymToKeycode(display_, XK_Right)),
28 keycode_return_(XKeysymToKeycode(display_, XK_Return)),
29 keycode_f6_(XKeysymToKeycode(display_, XK_F6)) {
32 virtual ~KeyboardDrivenEventRewriterTest() {}
34 protected:
35 std::string GetRewrittenEventAsString(ui::KeyboardCode ui_keycode,
36 int ui_flags,
37 ui::EventType ui_type,
38 KeyCode x_keycode,
39 unsigned int x_state) {
40 ui::ScopedXI2Event xev;
41 xev.InitKeyEvent(ui_type, ui_keycode, ui_flags);
42 XEvent* xevent = xev;
43 xevent->xkey.keycode = x_keycode;
44 xevent->xkey.state = x_state;
45 ui::KeyEvent keyevent(xev, false /* is_char */);
46 bool changed = rewriter_.RewriteForTesting(&keyevent);
47 return base::StringPrintf("ui_flags=%d x_state=%u changed=%d",
48 keyevent.flags(),
49 xevent->xkey.state,
50 changed);
53 std::string GetExpectedResultAsString(int ui_flags,
54 unsigned int x_state,
55 bool changed) {
56 return base::StringPrintf(
57 "ui_flags=%d x_state=%u changed=%d", ui_flags, x_state, changed);
60 XDisplay* display_;
61 const KeyCode keycode_a_;
62 const KeyCode keycode_up_;
63 const KeyCode keycode_down_;
64 const KeyCode keycode_left_;
65 const KeyCode keycode_right_;
66 const KeyCode keycode_return_;
67 const KeyCode keycode_f6_;
69 KeyboardDrivenEventRewriter rewriter_;
71 private:
72 DISALLOW_COPY_AND_ASSIGN(KeyboardDrivenEventRewriterTest);
75 TEST_F(KeyboardDrivenEventRewriterTest, PassThrough) {
76 struct {
77 ui::KeyboardCode ui_keycode;
78 int ui_flags;
79 KeyCode x_keycode;
80 unsigned int x_state;
81 } kTests[] = {
82 { ui::VKEY_A, ui::EF_NONE, keycode_a_, 0 },
83 { ui::VKEY_A, ui::EF_CONTROL_DOWN, keycode_a_, ControlMask },
84 { ui::VKEY_A, ui::EF_ALT_DOWN, keycode_a_, Mod1Mask },
85 { ui::VKEY_A, ui::EF_SHIFT_DOWN, keycode_a_, ShiftMask },
86 { ui::VKEY_A, ui::EF_CONTROL_DOWN | ui::EF_ALT_DOWN,
87 keycode_a_, ControlMask | Mod1Mask },
88 { ui::VKEY_A, ui::EF_CONTROL_DOWN | ui::EF_ALT_DOWN | ui::EF_SHIFT_DOWN,
89 keycode_a_, ControlMask | Mod1Mask | ShiftMask },
91 { ui::VKEY_LEFT, ui::EF_NONE, keycode_left_, 0 },
92 { ui::VKEY_LEFT, ui::EF_CONTROL_DOWN, keycode_left_, ControlMask },
93 { ui::VKEY_LEFT, ui::EF_CONTROL_DOWN | ui::EF_ALT_DOWN,
94 keycode_left_, ControlMask | Mod1Mask },
96 { ui::VKEY_RIGHT, ui::EF_NONE, keycode_right_, 0 },
97 { ui::VKEY_RIGHT, ui::EF_CONTROL_DOWN, keycode_right_, ControlMask },
98 { ui::VKEY_RIGHT, ui::EF_CONTROL_DOWN | ui::EF_ALT_DOWN,
99 keycode_right_, ControlMask | Mod1Mask },
101 { ui::VKEY_UP, ui::EF_NONE, keycode_up_, 0 },
102 { ui::VKEY_UP, ui::EF_CONTROL_DOWN, keycode_up_, ControlMask },
103 { ui::VKEY_UP, ui::EF_CONTROL_DOWN | ui::EF_ALT_DOWN,
104 keycode_up_, ControlMask | Mod1Mask },
106 { ui::VKEY_DOWN, ui::EF_NONE, keycode_down_, 0 },
107 { ui::VKEY_DOWN, ui::EF_CONTROL_DOWN, keycode_down_, ControlMask },
108 { ui::VKEY_DOWN, ui::EF_CONTROL_DOWN | ui::EF_ALT_DOWN,
109 keycode_down_, ControlMask | Mod1Mask },
111 { ui::VKEY_RETURN, ui::EF_NONE, keycode_return_, 0 },
112 { ui::VKEY_RETURN, ui::EF_CONTROL_DOWN, keycode_return_, ControlMask },
113 { ui::VKEY_RETURN, ui::EF_CONTROL_DOWN | ui::EF_ALT_DOWN,
114 keycode_return_, ControlMask | Mod1Mask },
117 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kTests); ++i) {
118 EXPECT_EQ(GetExpectedResultAsString(kTests[i].ui_flags,
119 kTests[i].x_state,
120 false),
121 GetRewrittenEventAsString(kTests[i].ui_keycode,
122 kTests[i].ui_flags,
123 ui::ET_KEY_PRESSED,
124 kTests[i].x_keycode,
125 kTests[i].x_state))
126 << "Test case " << i;
130 TEST_F(KeyboardDrivenEventRewriterTest, Rewrite) {
131 const int kModifierMask = ui::EF_SHIFT_DOWN;
132 const unsigned int kXState = ShiftMask;
134 struct {
135 ui::KeyboardCode ui_keycode;
136 int ui_flags;
137 KeyCode x_keycode;
138 unsigned int x_state;
139 } kTests[] = {
140 { ui::VKEY_LEFT, kModifierMask, keycode_left_, kXState },
141 { ui::VKEY_RIGHT, kModifierMask, keycode_right_, kXState },
142 { ui::VKEY_UP, kModifierMask, keycode_up_, kXState },
143 { ui::VKEY_DOWN, kModifierMask, keycode_down_, kXState },
144 { ui::VKEY_RETURN, kModifierMask, keycode_return_, kXState },
145 { ui::VKEY_F6, kModifierMask, keycode_f6_, kXState },
148 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kTests); ++i) {
149 EXPECT_EQ(GetExpectedResultAsString(ui::EF_NONE, 0, true),
150 GetRewrittenEventAsString(kTests[i].ui_keycode,
151 kTests[i].ui_flags,
152 ui::ET_KEY_PRESSED,
153 kTests[i].x_keycode,
154 kTests[i].x_state))
155 << "Test case " << i;
159 } // namespace chromeos