Skip to content

Commit ff4aa7d

Browse files
committed
feat(android): add AndroidKeyMetastate class for keyboard metastate constants
1 parent 8466c27 commit ff4aa7d

File tree

3 files changed

+63
-3
lines changed

3 files changed

+63
-3
lines changed

appium/webdriver/extensions/android/nativekey.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,49 @@
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+
"""
23+
24+
# No modifiers.
25+
NONE = 0
26+
27+
# This mask is used to check whether one of the SHIFT meta keys is pressed.
28+
META_SHIFT_ON = 0x01
29+
META_SHIFT_LEFT_ON = 0x40
30+
META_SHIFT_RIGHT_ON = 0x80
31+
32+
# This mask is used to check whether one of the ALT meta keys is pressed.
33+
META_ALT_ON = 0x02
34+
META_ALT_LEFT_ON = 0x10
35+
META_ALT_RIGHT_ON = 0x20
36+
37+
# This mask is used to check whether one of the SYM meta keys is pressed.
38+
META_SYM_ON = 0x04
39+
40+
# This mask is used to check whether the FUNCTION meta key is pressed.
41+
META_FUNCTION_ON = 0x08
42+
43+
# This mask is used to check whether one of the CTRL meta keys is pressed.
44+
META_CTRL_ON = 0x1000
45+
META_CTRL_LEFT_ON = 0x2000
46+
META_CTRL_RIGHT_ON = 0x4000
47+
48+
# This mask is used to check whether one of the META meta keys is pressed.
49+
META_META_ON = 0x10000
50+
META_META_LEFT_ON = 0x20000
51+
META_META_RIGHT_ON = 0x40000
52+
53+
# Lock key states.
54+
META_CAPS_LOCK_ON = 0x100000
55+
META_NUM_LOCK_ON = 0x200000
56+
META_SCROLL_LOCK_ON = 0x400000
57+
58+
1659
class AndroidKey:
1760
# Key code constant: Unknown key code.
1861
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: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
# limitations under the License.
1414

1515

16-
from appium.webdriver.extensions.android.nativekey import AndroidKey
16+
from appium.webdriver.extensions.android.nativekey import AndroidKey, AndroidKeyMetastate
1717

1818

1919
class TestAndroidKey:
@@ -23,6 +23,14 @@ def test_has_some_codes(self):
2323
assert AndroidKey.CAMERA == 27
2424
assert AndroidKey.SPACE == 62
2525

26+
def test_has_some_metastates(self):
27+
assert AndroidKeyMetastate.NONE == 0
28+
assert AndroidKeyMetastate.META_SHIFT_ON == 0x00000001
29+
assert AndroidKeyMetastate.META_NUM_LOCK_ON == 0x00200000
30+
assert (
31+
AndroidKeyMetastate.META_SHIFT_ON | AndroidKeyMetastate.META_NUM_LOCK_ON
32+
) == 0x00000001 | 0x00200000
33+
2634
def test_is_gamepad_key(self):
2735
assert AndroidKey.is_gamepad_button(AndroidKey.BUTTON_8)
2836
assert not AndroidKey.is_gamepad_button(250)

0 commit comments

Comments
 (0)