Skip to content

Commit ad8c5c9

Browse files
Dor-blKazuCocoa
andauthored
feat: Add AndroidKeyMetastate class for keyboard metastate constants (#1211)
* feat(android): add AndroidKeyMetastate class for keyboard metastate constants * Reformat import statement for AndroidKey and Metastate * add space * fix(test): simplify assertion for AndroidKeyMetastate in test_has_some_metastates * Update appium/webdriver/extensions/android/nativekey.py --------- Co-authored-by: Kazuaki Matsuo <fly.49.89.over@gmail.com>
1 parent 8466c27 commit ad8c5c9

3 files changed

Lines changed: 65 additions & 3 deletions

File tree

appium/webdriver/extensions/android/nativekey.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,50 @@
1313
# limitations under the License.
1414

1515

16+
class AndroidKeyMetastate:
17+
"""Keyboard metastate constants for Android key events.
18+
19+
These constants can be combined with bitwise OR and passed to
20+
Keyboard.press_keycode or Keyboard.long_press_keycode as metastate.
21+
Values are based on android.view.KeyEvent.
22+
https://developer.android.com/reference/android/view/KeyEvent
23+
"""
24+
25+
# No modifiers.
26+
NONE = 0
27+
28+
# This mask is used to check whether one of the SHIFT meta keys is pressed.
29+
META_SHIFT_ON = 0x01
30+
META_SHIFT_LEFT_ON = 0x40
31+
META_SHIFT_RIGHT_ON = 0x80
32+
33+
# This mask is used to check whether one of the ALT meta keys is pressed.
34+
META_ALT_ON = 0x02
35+
META_ALT_LEFT_ON = 0x10
36+
META_ALT_RIGHT_ON = 0x20
37+
38+
# This mask is used to check whether one of the SYM meta keys is pressed.
39+
META_SYM_ON = 0x04
40+
41+
# This mask is used to check whether the FUNCTION meta key is pressed.
42+
META_FUNCTION_ON = 0x08
43+
44+
# This mask is used to check whether one of the CTRL meta keys is pressed.
45+
META_CTRL_ON = 0x1000
46+
META_CTRL_LEFT_ON = 0x2000
47+
META_CTRL_RIGHT_ON = 0x4000
48+
49+
# This mask is used to check whether one of the META meta keys is pressed.
50+
META_META_ON = 0x10000
51+
META_META_LEFT_ON = 0x20000
52+
META_META_RIGHT_ON = 0x40000
53+
54+
# Lock key states.
55+
META_CAPS_LOCK_ON = 0x100000
56+
META_NUM_LOCK_ON = 0x200000
57+
META_SCROLL_LOCK_ON = 0x400000
58+
59+
1660
class AndroidKey:
1761
# Key code constant: Unknown key code.
1862
UNKNOWN = 0

test/unit/webdriver/device/keyboard_test.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
import httpretty
1616

17+
from appium.webdriver.extensions.android.nativekey import AndroidKeyMetastate
1718
from appium.webdriver.webdriver import WebDriver
1819
from test.unit.helper.test_helper import android_w3c_driver, appium_command, get_httpretty_request_body, ios_w3c_driver
1920

@@ -71,7 +72,11 @@ def test_press_keycode_with_flags(self):
7172
# metastate is META_SHIFT_ON and META_NUM_LOCK_ON
7273
# flags is CANCELFLAG_CANCELEDED, FLAG_KEEP_TOUCH_MODE, FLAG_FROM_SYSTEM
7374
assert isinstance(
74-
driver.press_keycode(86, metastate=0x00000001 | 0x00200000, flags=0x20 | 0x00000004 | 0x00000008),
75+
driver.press_keycode(
76+
86,
77+
metastate=AndroidKeyMetastate.META_SHIFT_ON | AndroidKeyMetastate.META_NUM_LOCK_ON,
78+
flags=0x20 | 0x00000004 | 0x00000008,
79+
),
7580
WebDriver,
7681
)
7782

@@ -87,7 +92,11 @@ def test_long_press_keycode_with_flags(self):
8792
# metastate is META_SHIFT_ON and META_NUM_LOCK_ON
8893
# flags is CANCELFLAG_CANCELEDED, FLAG_KEEP_TOUCH_MODE, FLAG_FROM_SYSTEM
8994
assert isinstance(
90-
driver.long_press_keycode(86, metastate=0x00000001 | 0x00200000, flags=0x20 | 0x00000004 | 0x00000008),
95+
driver.long_press_keycode(
96+
86,
97+
metastate=AndroidKeyMetastate.META_SHIFT_ON | AndroidKeyMetastate.META_NUM_LOCK_ON,
98+
flags=0x20 | 0x00000004 | 0x00000008,
99+
),
91100
WebDriver,
92101
)
93102

test/unit/webdriver/nativekey_test.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@
1313
# limitations under the License.
1414

1515

16-
from appium.webdriver.extensions.android.nativekey import AndroidKey
16+
from appium.webdriver.extensions.android.nativekey import (
17+
AndroidKey,
18+
AndroidKeyMetastate,
19+
)
1720

1821

1922
class TestAndroidKey:
@@ -23,6 +26,12 @@ def test_has_some_codes(self):
2326
assert AndroidKey.CAMERA == 27
2427
assert AndroidKey.SPACE == 62
2528

29+
def test_has_some_metastates(self):
30+
assert AndroidKeyMetastate.NONE == 0
31+
assert AndroidKeyMetastate.META_SHIFT_ON == 0x00000001
32+
assert AndroidKeyMetastate.META_NUM_LOCK_ON == 0x00200000
33+
assert (AndroidKeyMetastate.META_SHIFT_ON | AndroidKeyMetastate.META_NUM_LOCK_ON) == 0x00000001 | 0x00200000
34+
2635
def test_is_gamepad_key(self):
2736
assert AndroidKey.is_gamepad_button(AndroidKey.BUTTON_8)
2837
assert not AndroidKey.is_gamepad_button(250)

0 commit comments

Comments
 (0)