12#include "flutter/shell/platform/fuchsia/flutter/keyboard.h"
14#include <fuchsia/input/cpp/fidl.h>
15#include <fuchsia/ui/input/cpp/fidl.h>
16#include <fuchsia/ui/input3/cpp/fidl.h>
18#include <gtest/gtest.h>
19#include <zircon/time.h>
26using fuchsia::ui::input::kModifierCapsLock;
27using fuchsia::ui::input::kModifierLeftAlt;
28using fuchsia::ui::input::kModifierLeftControl;
29using fuchsia::ui::input::kModifierLeftShift;
30using fuchsia::ui::input::kModifierNone;
31using fuchsia::ui::input::kModifierRightAlt;
32using fuchsia::ui::input::kModifierRightControl;
33using fuchsia::ui::input::kModifierRightShift;
34using fuchsia::ui::input3::KeyEvent;
36using fuchsia::ui::input3::KeyMeaning;
38class KeyboardTest :
public testing::Test {
40 static void SetUpTestCase() { testing::Test::SetUpTestCase(); }
46 event.set_timestamp(++timestamp_);
53 KeyMeaning key_meaning) {
56 event.set_timestamp(++timestamp_);
58 event.set_key_meaning(std::move(key_meaning));
66 bool ConsumeEvents(Keyboard* keyboard,
const std::vector<KeyEvent>& events) {
67 for (
const auto&
event : events) {
70 if (keyboard->ConsumeEvent(std::move(
e)) ==
false) {
78 uint32_t ToUsage(
Key key) {
return static_cast<uint64_t
>(
key) & 0xFFFFFFFF; }
81 zx_time_t timestamp_ = 0;
85TEST_F(KeyboardTest, UsageValues) {
86 std::vector<KeyEvent> keys;
87 keys.emplace_back(NewKeyEvent(KeyEventType::SYNC, Key::CAPS_LOCK));
89 ASSERT_TRUE(ConsumeEvents(&keyboard, keys));
94 EXPECT_EQ(0x07u, keyboard.LastHIDUsagePage());
95 EXPECT_EQ(0x39u, keyboard.LastHIDUsageID());
96 EXPECT_EQ(0x70039u, keyboard.LastHIDUsage());
105 keys.emplace_back(NewKeyEvent(KeyEventType::SYNC, Key::MEDIA_MUTE));
106 ASSERT_TRUE(ConsumeEvents(&keyboard, keys));
107 EXPECT_EQ(0x0Cu, keyboard.LastHIDUsagePage());
108 EXPECT_EQ(0xE2u, keyboard.LastHIDUsageID());
109 EXPECT_EQ(0xC00E2u, keyboard.LastHIDUsage());
113 keys.emplace_back(NewKeyEventWithMeaning(KeyEventType::SYNC,
114 KeyMeaning::WithCodepoint(32)));
115 ASSERT_TRUE(ConsumeEvents(&keyboard, keys));
116 EXPECT_EQ(0x0u, keyboard.LastHIDUsagePage());
117 EXPECT_EQ(0x0u, keyboard.LastHIDUsageID());
118 EXPECT_EQ(0x0u, keyboard.LastHIDUsage());
119 EXPECT_EQ(0x20u, keyboard.LastCodePoint());
123 NewKeyEventWithMeaning(KeyEventType::SYNC, KeyMeaning::WithCodepoint(65));
125 keys.emplace_back(std::move(
key));
126 ASSERT_TRUE(ConsumeEvents(&keyboard, keys));
127 EXPECT_EQ(0x07u, keyboard.LastHIDUsagePage());
128 EXPECT_EQ(0x04u, keyboard.LastHIDUsageID());
129 EXPECT_EQ(0x70004u, keyboard.LastHIDUsage());
130 EXPECT_EQ(65u, keyboard.LastCodePoint());
136TEST_F(KeyboardTest, CapsLockSync) {
138 std::vector<KeyEvent> keys;
139 keys.emplace_back(NewKeyEvent(KeyEventType::SYNC, Key::CAPS_LOCK));
143 ASSERT_TRUE(ConsumeEvents(&keyboard, keys));
150 EXPECT_EQ(0u, keyboard.LastCodePoint());
151 EXPECT_EQ(ToUsage(Key::CAPS_LOCK), keyboard.LastHIDUsage());
152 EXPECT_EQ(kModifierCapsLock, keyboard.Modifiers());
155TEST_F(KeyboardTest, CapsLockPress) {
156 std::vector<KeyEvent> keys;
157 keys.emplace_back(NewKeyEvent(KeyEventType::PRESSED, Key::CAPS_LOCK));
160 ASSERT_TRUE(ConsumeEvents(&keyboard, keys));
162 EXPECT_EQ(0u, keyboard.LastCodePoint());
163 EXPECT_EQ(ToUsage(Key::CAPS_LOCK), keyboard.LastHIDUsage());
164 EXPECT_EQ(kModifierCapsLock, keyboard.Modifiers());
167TEST_F(KeyboardTest, CapsLockPressRelease) {
168 std::vector<KeyEvent> keys;
169 keys.emplace_back(NewKeyEvent(KeyEventType::PRESSED, Key::CAPS_LOCK));
170 keys.emplace_back(NewKeyEvent(KeyEventType::RELEASED, Key::CAPS_LOCK));
173 ASSERT_TRUE(ConsumeEvents(&keyboard, keys));
175 EXPECT_EQ(0u, keyboard.LastCodePoint());
176 EXPECT_EQ(ToUsage(Key::CAPS_LOCK), keyboard.LastHIDUsage());
177 EXPECT_EQ(kModifierCapsLock, keyboard.Modifiers());
180TEST_F(KeyboardTest, ShiftA) {
181 std::vector<KeyEvent> keys;
182 keys.emplace_back(NewKeyEvent(KeyEventType::PRESSED, Key::LEFT_SHIFT));
183 keys.emplace_back(NewKeyEvent(KeyEventType::PRESSED,
Key::A));
186 ASSERT_TRUE(ConsumeEvents(&keyboard, keys));
188 EXPECT_EQ(
static_cast<uint32_t
>(
'A'), keyboard.LastCodePoint());
189 EXPECT_EQ(ToUsage(
Key::A), keyboard.LastHIDUsage());
190 EXPECT_EQ(kModifierLeftShift, keyboard.Modifiers());
193TEST_F(KeyboardTest, ShiftAWithRelease) {
194 std::vector<KeyEvent> keys;
195 keys.emplace_back(NewKeyEvent(KeyEventType::PRESSED, Key::LEFT_SHIFT));
196 keys.emplace_back(NewKeyEvent(KeyEventType::PRESSED,
Key::A));
197 keys.emplace_back(NewKeyEvent(KeyEventType::RELEASED,
Key::A));
200 ASSERT_TRUE(ConsumeEvents(&keyboard, keys));
202 EXPECT_EQ(
static_cast<uint32_t
>(
'A'), keyboard.LastCodePoint());
203 EXPECT_EQ(ToUsage(
Key::A), keyboard.LastHIDUsage());
204 EXPECT_EQ(kModifierLeftShift, keyboard.Modifiers());
207TEST_F(KeyboardTest, ShiftAWithReleaseShift) {
208 std::vector<KeyEvent> keys;
209 keys.emplace_back(NewKeyEvent(KeyEventType::PRESSED, Key::LEFT_SHIFT));
210 keys.emplace_back(NewKeyEvent(KeyEventType::PRESSED,
Key::A));
211 keys.emplace_back(NewKeyEvent(KeyEventType::RELEASED, Key::LEFT_SHIFT));
212 keys.emplace_back(NewKeyEvent(KeyEventType::RELEASED,
Key::A));
215 ASSERT_TRUE(ConsumeEvents(&keyboard, keys));
217 EXPECT_EQ(
static_cast<uint32_t
>(
'a'), keyboard.LastCodePoint());
218 EXPECT_EQ(ToUsage(
Key::A), keyboard.LastHIDUsage());
219 EXPECT_EQ(kModifierNone, keyboard.Modifiers());
222TEST_F(KeyboardTest, LowcaseA) {
223 std::vector<KeyEvent> keys;
224 keys.emplace_back(NewKeyEvent(KeyEventType::PRESSED,
Key::A));
225 keys.emplace_back(NewKeyEvent(KeyEventType::RELEASED,
Key::A));
228 ASSERT_TRUE(ConsumeEvents(&keyboard, keys));
230 EXPECT_EQ(
static_cast<uint32_t
>(
'a'), keyboard.LastCodePoint());
231 EXPECT_EQ(ToUsage(
Key::A), keyboard.LastHIDUsage());
232 EXPECT_EQ(kModifierNone, keyboard.Modifiers());
ax::mojom::Event event_type
TEST_F(DisplayListTest, Defaults)