appium-espresso-driver 9.3.10 → 9.3.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (154) hide show
  1. package/CHANGELOG.md +12 -0
  2. package/espresso-server/app/build/outputs/apk/androidTest/debug/app-debug-androidTest.apk +0 -0
  3. package/espresso-server/buildSrc/.gradle/9.7.1/executionHistory/executionHistory.bin +0 -0
  4. package/espresso-server/buildSrc/.gradle/9.7.1/executionHistory/executionHistory.lock +0 -0
  5. package/espresso-server/buildSrc/.gradle/buildOutputCleanup/buildOutputCleanup.lock +0 -0
  6. package/espresso-server/buildSrc/.gradle/buildOutputCleanup/cache.properties +1 -1
  7. package/espresso-server/buildSrc/build/kotlin/compileKotlin/cacheable/last-build.bin +0 -0
  8. package/espresso-server/gradle/libs.versions.toml +2 -0
  9. package/espresso-server/library/build.gradle.kts +6 -0
  10. package/espresso-server/library/src/composeOff/java/io/appium/espressoserver/lib/http/Server.kt +8 -1
  11. package/espresso-server/library/src/main/java/io/appium/espressoserver/lib/handlers/Keys.kt +5 -2
  12. package/espresso-server/library/src/main/java/io/appium/espressoserver/lib/handlers/PerformAction.kt +9 -2
  13. package/espresso-server/library/src/main/java/io/appium/espressoserver/lib/helpers/Version.kt +1 -1
  14. package/espresso-server/library/src/main/java/io/appium/espressoserver/lib/helpers/w3c/adapter/espresso/AndroidKeyEvent.kt +77 -9
  15. package/espresso-server/library/src/main/java/io/appium/espressoserver/lib/helpers/w3c/adapter/espresso/EspressoW3CActionAdapter.kt +2 -0
  16. package/espresso-server/library/src/main/java/io/appium/espressoserver/lib/helpers/w3c/adapter/espresso/NonAsciiTextInserter.kt +112 -0
  17. package/espresso-server/library/src/main/java/io/appium/espressoserver/lib/helpers/w3c/processor/KeyProcessor.kt +1 -1
  18. package/espresso-server/library/src/test/java/io/appium/espressoserver/test/helpers/w3c/ProcessorTest.kt +20 -0
  19. package/espresso-server/library/src/test/java/io/appium/espressoserver/test/helpers/w3c/adapter/AndroidKeyEventTest.kt +4 -0
  20. package/espresso-server/library/src/test/java/io/appium/espressoserver/test/helpers/w3c/adapter/NonAsciiTextInserterTest.kt +109 -0
  21. package/espresso-server/library/src/test/java/io/appium/espressoserver/test/http/ServerTest.kt +14 -0
  22. package/node_modules/@appium/support/node_modules/semver/LICENSE +15 -0
  23. package/node_modules/@appium/support/node_modules/semver/README.md +680 -0
  24. package/node_modules/@appium/support/node_modules/semver/bin/semver.js +195 -0
  25. package/node_modules/@appium/support/node_modules/semver/classes/comparator.js +143 -0
  26. package/node_modules/@appium/support/node_modules/semver/classes/index.js +7 -0
  27. package/node_modules/@appium/support/node_modules/semver/classes/range.js +577 -0
  28. package/node_modules/@appium/support/node_modules/semver/classes/semver.js +350 -0
  29. package/node_modules/@appium/support/node_modules/semver/functions/clean.js +8 -0
  30. package/node_modules/@appium/support/node_modules/semver/functions/cmp.js +54 -0
  31. package/node_modules/@appium/support/node_modules/semver/functions/coerce.js +62 -0
  32. package/node_modules/@appium/support/node_modules/semver/functions/compare-build.js +9 -0
  33. package/node_modules/@appium/support/node_modules/semver/functions/compare-loose.js +5 -0
  34. package/node_modules/@appium/support/node_modules/semver/functions/compare.js +7 -0
  35. package/node_modules/@appium/support/node_modules/semver/functions/diff.js +60 -0
  36. package/node_modules/@appium/support/node_modules/semver/functions/eq.js +5 -0
  37. package/node_modules/@appium/support/node_modules/semver/functions/gt.js +5 -0
  38. package/node_modules/@appium/support/node_modules/semver/functions/gte.js +5 -0
  39. package/node_modules/@appium/support/node_modules/semver/functions/inc.js +21 -0
  40. package/node_modules/@appium/support/node_modules/semver/functions/lt.js +5 -0
  41. package/node_modules/@appium/support/node_modules/semver/functions/lte.js +5 -0
  42. package/node_modules/@appium/support/node_modules/semver/functions/major.js +5 -0
  43. package/node_modules/@appium/support/node_modules/semver/functions/minor.js +5 -0
  44. package/node_modules/@appium/support/node_modules/semver/functions/neq.js +5 -0
  45. package/node_modules/@appium/support/node_modules/semver/functions/parse.js +18 -0
  46. package/node_modules/@appium/support/node_modules/semver/functions/patch.js +5 -0
  47. package/node_modules/@appium/support/node_modules/semver/functions/prerelease.js +8 -0
  48. package/node_modules/@appium/support/node_modules/semver/functions/rcompare.js +5 -0
  49. package/node_modules/@appium/support/node_modules/semver/functions/rsort.js +5 -0
  50. package/node_modules/@appium/support/node_modules/semver/functions/satisfies.js +12 -0
  51. package/node_modules/@appium/support/node_modules/semver/functions/sort.js +5 -0
  52. package/node_modules/@appium/support/node_modules/semver/functions/truncate.js +48 -0
  53. package/node_modules/@appium/support/node_modules/semver/functions/valid.js +8 -0
  54. package/node_modules/@appium/support/node_modules/semver/index.js +93 -0
  55. package/node_modules/@appium/support/node_modules/semver/internal/constants.js +37 -0
  56. package/node_modules/@appium/support/node_modules/semver/internal/debug.js +11 -0
  57. package/node_modules/@appium/support/node_modules/semver/internal/identifiers.js +29 -0
  58. package/node_modules/@appium/support/node_modules/semver/internal/lrucache.js +42 -0
  59. package/node_modules/@appium/support/node_modules/semver/internal/parse-options.js +17 -0
  60. package/node_modules/@appium/support/node_modules/semver/internal/re.js +223 -0
  61. package/node_modules/@appium/support/node_modules/semver/package.json +78 -0
  62. package/node_modules/@appium/support/node_modules/semver/preload.js +4 -0
  63. package/node_modules/@appium/support/node_modules/semver/range.bnf +17 -0
  64. package/node_modules/@appium/support/node_modules/semver/ranges/gtr.js +6 -0
  65. package/node_modules/@appium/support/node_modules/semver/ranges/intersects.js +9 -0
  66. package/node_modules/@appium/support/node_modules/semver/ranges/ltr.js +6 -0
  67. package/node_modules/@appium/support/node_modules/semver/ranges/max-satisfying.js +27 -0
  68. package/node_modules/@appium/support/node_modules/semver/ranges/min-satisfying.js +26 -0
  69. package/node_modules/@appium/support/node_modules/semver/ranges/min-version.js +63 -0
  70. package/node_modules/@appium/support/node_modules/semver/ranges/outside.js +82 -0
  71. package/node_modules/@appium/support/node_modules/semver/ranges/simplify.js +49 -0
  72. package/node_modules/@appium/support/node_modules/semver/ranges/subset.js +249 -0
  73. package/node_modules/@appium/support/node_modules/semver/ranges/to-comparators.js +10 -0
  74. package/node_modules/@appium/support/node_modules/semver/ranges/valid.js +13 -0
  75. package/node_modules/appium-adb/CHANGELOG.md +22 -0
  76. package/node_modules/appium-adb/README.md +5 -2
  77. package/node_modules/appium-adb/build/lib/adb.d.ts +9 -0
  78. package/node_modules/appium-adb/build/lib/adb.d.ts.map +1 -1
  79. package/node_modules/appium-adb/build/lib/adb.js +12 -1
  80. package/node_modules/appium-adb/build/lib/adb.js.map +1 -1
  81. package/node_modules/appium-adb/build/lib/tools/android-manifest.js +1 -1
  82. package/node_modules/appium-adb/build/lib/tools/android-manifest.js.map +1 -1
  83. package/node_modules/appium-adb/build/lib/tools/apk-utils.js +3 -3
  84. package/node_modules/appium-adb/build/lib/tools/apk-utils.js.map +1 -1
  85. package/node_modules/appium-adb/build/lib/tools/app-commands.d.ts +4 -4
  86. package/node_modules/appium-adb/build/lib/tools/app-commands.d.ts.map +1 -1
  87. package/node_modules/appium-adb/build/lib/tools/app-commands.js +35 -36
  88. package/node_modules/appium-adb/build/lib/tools/app-commands.js.map +1 -1
  89. package/node_modules/appium-adb/build/lib/tools/device-settings.js +2 -2
  90. package/node_modules/appium-adb/build/lib/tools/device-settings.js.map +1 -1
  91. package/node_modules/appium-adb/build/lib/tools/fs-commands.js +2 -2
  92. package/node_modules/appium-adb/build/lib/tools/fs-commands.js.map +1 -1
  93. package/node_modules/appium-adb/build/lib/tools/lockmgmt.d.ts.map +1 -1
  94. package/node_modules/appium-adb/build/lib/tools/lockmgmt.js +6 -2
  95. package/node_modules/appium-adb/build/lib/tools/lockmgmt.js.map +1 -1
  96. package/node_modules/appium-adb/build/lib/tools/system-calls.d.ts +14 -3
  97. package/node_modules/appium-adb/build/lib/tools/system-calls.d.ts.map +1 -1
  98. package/node_modules/appium-adb/build/lib/tools/system-calls.js +12 -6
  99. package/node_modules/appium-adb/build/lib/tools/system-calls.js.map +1 -1
  100. package/node_modules/appium-adb/build/lib/tools/types.d.ts +8 -0
  101. package/node_modules/appium-adb/build/lib/tools/types.d.ts.map +1 -1
  102. package/node_modules/appium-adb/build/lib/types.d.ts +20 -0
  103. package/node_modules/appium-adb/build/lib/types.d.ts.map +1 -1
  104. package/node_modules/appium-adb/lib/adb.ts +14 -1
  105. package/node_modules/appium-adb/lib/tools/android-manifest.ts +1 -1
  106. package/node_modules/appium-adb/lib/tools/apk-utils.ts +3 -3
  107. package/node_modules/appium-adb/lib/tools/app-commands.ts +52 -50
  108. package/node_modules/appium-adb/lib/tools/device-settings.ts +2 -2
  109. package/node_modules/appium-adb/lib/tools/fs-commands.ts +2 -2
  110. package/node_modules/appium-adb/lib/tools/lockmgmt.ts +6 -2
  111. package/node_modules/appium-adb/lib/tools/system-calls.ts +29 -9
  112. package/node_modules/appium-adb/lib/tools/types.ts +8 -0
  113. package/node_modules/appium-adb/lib/types.ts +21 -0
  114. package/node_modules/appium-adb/package.json +3 -3
  115. package/node_modules/appium-android-driver/CHANGELOG.md +6 -0
  116. package/node_modules/appium-android-driver/build/lib/commands/permissions.js +2 -2
  117. package/node_modules/appium-android-driver/build/lib/commands/permissions.js.map +1 -1
  118. package/node_modules/appium-android-driver/lib/commands/permissions.ts +2 -2
  119. package/node_modules/appium-android-driver/package.json +1 -1
  120. package/node_modules/appium-chromedriver/CHANGELOG.md +38 -0
  121. package/node_modules/appium-chromedriver/config/mapping.json +1 -0
  122. package/node_modules/appium-chromedriver/package.json +5 -5
  123. package/node_modules/encodeurl/README.md +14 -0
  124. package/node_modules/encodeurl/index.js +5 -10
  125. package/node_modules/encodeurl/package.json +10 -6
  126. package/node_modules/io.appium.settings/CHANGELOG.md +23 -0
  127. package/node_modules/io.appium.settings/apks/settings_apk-debug.apk +0 -0
  128. package/node_modules/io.appium.settings/build/lib/client.d.ts.map +1 -1
  129. package/node_modules/io.appium.settings/build/lib/client.js +2 -1
  130. package/node_modules/io.appium.settings/build/lib/client.js.map +1 -1
  131. package/node_modules/io.appium.settings/build/lib/commands/media-projection.js +1 -1
  132. package/node_modules/io.appium.settings/build/lib/commands/media-projection.js.map +1 -1
  133. package/node_modules/io.appium.settings/build/lib/commands/typing.d.ts.map +1 -1
  134. package/node_modules/io.appium.settings/build/lib/commands/typing.js +1 -1
  135. package/node_modules/io.appium.settings/build/lib/commands/typing.js.map +1 -1
  136. package/node_modules/io.appium.settings/build/lib/commands/video-stream.js +1 -1
  137. package/node_modules/io.appium.settings/build/lib/commands/video-stream.js.map +1 -1
  138. package/node_modules/io.appium.settings/lib/client.ts +2 -1
  139. package/node_modules/io.appium.settings/lib/commands/media-projection.ts +1 -1
  140. package/node_modules/io.appium.settings/lib/commands/typing.ts +4 -1
  141. package/node_modules/io.appium.settings/lib/commands/video-stream.ts +1 -1
  142. package/node_modules/io.appium.settings/package.json +4 -4
  143. package/node_modules/qs/CHANGELOG.md +42 -0
  144. package/node_modules/qs/README.md +38 -10
  145. package/node_modules/qs/dist/qs.js +18 -18
  146. package/node_modules/qs/lib/parse.js +16 -4
  147. package/node_modules/qs/lib/utils.js +13 -2
  148. package/node_modules/qs/package.json +1 -1
  149. package/node_modules/qs/test/parse.js +69 -0
  150. package/node_modules/semver/classes/range.js +2 -2
  151. package/node_modules/semver/internal/constants.js +30 -0
  152. package/node_modules/semver/internal/trim-comparator-whitespace.js +438 -0
  153. package/node_modules/semver/package.json +1 -1
  154. package/package.json +5 -5
package/CHANGELOG.md CHANGED
@@ -1,3 +1,15 @@
1
+ ## [9.3.12](https://github.com/appium/appium-espresso-driver/compare/v9.3.11...v9.3.12) (2026-11-03)
2
+
3
+ ### Bug Fixes
4
+
5
+ * **server:** make the compose-off server build start on a device ([#1242](https://github.com/appium/appium-espresso-driver/issues/1242)) ([1c40633](https://github.com/appium/appium-espresso-driver/commit/1c406335fc696024d6183ef505253196b1e5f2ea))
6
+
7
+ ## [9.3.11](https://github.com/appium/appium-espresso-driver/compare/v9.3.10...v9.3.11) (2026-11-02)
8
+
9
+ ### Bug Fixes
10
+
11
+ * **server:** support non-ASCII characters in W3C key actions ([#1240](https://github.com/appium/appium-espresso-driver/issues/1240)) ([d7af08c](https://github.com/appium/appium-espresso-driver/commit/d7af08c1bc71ed1a0c95d41dd27535d277989b1b))
12
+
1
13
  ## [9.3.10](https://github.com/appium/appium-espresso-driver/compare/v9.3.9...v9.3.10) (2026-10-28)
2
14
 
3
15
  ### Bug Fixes
@@ -1,2 +1,2 @@
1
- #Wed Oct 28 09:12:15 UTC 2026
1
+ #Tue Nov 03 17:53:57 UTC 2026
2
2
  gradle.version=9.7.1
@@ -5,6 +5,7 @@ androidxTest = "1.7.0"
5
5
  annotation = "1.10.0"
6
6
  composeUiTest = "1.12.1"
7
7
  gson = "2.14.0"
8
+ lifecycle = "2.9.4"
8
9
  espresso = "3.7.0"
9
10
  nanohttpd = "2.3.1"
10
11
  robolectric = "4.17"
@@ -21,6 +22,7 @@ androidx-test-espresso-web = { group = "androidx.test.espresso", name = "espress
21
22
  androidx-test-uiautomator = { group = "androidx.test.uiautomator", name = "uiautomator", version.ref = "uiautomator" }
22
23
  androidx-compose-ui-test = { group = "androidx.compose.ui", name = "ui-test", version.ref = "composeUiTest" }
23
24
  androidx-compose-ui-test-junit4 = { group = "androidx.compose.ui", name = "ui-test-junit4", version.ref = "composeUiTest" }
25
+ androidx-lifecycle-common = { group = "androidx.lifecycle", name = "lifecycle-common", version.ref = "lifecycle" }
24
26
  gson = { group = "com.google.code.gson", name = "gson", version.ref = "gson" }
25
27
  nanohttpd-webserver = { group = "org.nanohttpd", name = "nanohttpd-webserver", version.ref = "nanohttpd" }
26
28
  kotlin-gradle-plugin = { group = "org.jetbrains.kotlin", name = "kotlin-gradle-plugin", version.ref = "kotlin" }
@@ -158,6 +158,12 @@ dependencies {
158
158
  }
159
159
 
160
160
  constraints {
161
+ api(libs.androidx.lifecycle.common) {
162
+ because(
163
+ "androidx.test:core brings an old version without Compose, and the server classes " +
164
+ "shadow the AUT ones, so apps with a modern lifecycle crash on startup",
165
+ )
166
+ }
161
167
  api(libs.kotlin.stdlib.jdk8) {
162
168
  version {
163
169
  require(resolveCapabilityVersion("appiumKotlin", libs.versions.kotlin.get()))
@@ -16,6 +16,13 @@
16
16
 
17
17
  package io.appium.espressoserver.lib.http
18
18
 
19
- class Server : ServerBase() {
19
+ import org.junit.rules.TestRule
20
+ import org.junit.runner.Description
21
+ import org.junit.runners.model.Statement
22
+
23
+ /** Must be a [TestRule] since the instrumentation test declares it as a JUnit rule. */
24
+ class Server : ServerBase(), TestRule {
20
25
  override fun advanceComposeMainClockIfNeeded() = Unit
26
+
27
+ override fun apply(base: Statement, description: Description): Statement = base
21
28
  }
@@ -67,8 +67,11 @@ class Keys : RequestHandler<TextValueParams, Unit> {
67
67
  .withAdapter(EspressoW3CActionAdapter(uiController))
68
68
  .build()
69
69
 
70
- actions.perform(params.sessionId!!)
71
- actions.release(params.sessionId!!)
70
+ try {
71
+ actions.perform(params.sessionId!!)
72
+ } finally {
73
+ actions.release(params.sessionId!!)
74
+ }
72
75
 
73
76
  return null
74
77
  }
@@ -30,8 +30,15 @@ class PerformAction : RequestHandler<Actions, Void?> {
30
30
 
31
31
  val runnable = object: UiControllerRunnable<Void?> {
32
32
  override fun run(uiController: UiController): Void? {
33
- params.adapter = EspressoW3CActionAdapter(uiController)
34
- params.perform(params.sessionId!!)
33
+ val adapter = EspressoW3CActionAdapter(uiController)
34
+ params.adapter = adapter
35
+ try {
36
+ params.perform(params.sessionId!!)
37
+ } catch (e: AppiumException) {
38
+ // The next request has no record of injected keys, so release them now
39
+ adapter.releasePressedKeys()
40
+ throw e
41
+ }
35
42
  return null
36
43
  }
37
44
  }
@@ -2,6 +2,6 @@ package io.appium.espressoserver.lib.helpers
2
2
 
3
3
  // This value is updated automatically by the NPM versioning script
4
4
  // It should be in sync with the NPM module version from package.json
5
- private const val VERSION = "9.3.10"
5
+ private const val VERSION = "9.3.12"
6
6
 
7
7
  fun getEspressoServerVersion() = VERSION
@@ -123,7 +123,7 @@ fun keyCodeToEvent(keyValue: String?, @Suppress("UNUSED_PARAMETER") location: In
123
123
  END -> KeyEvent.KEYCODE_MOVE_END
124
124
  HOME -> KeyEvent.KEYCODE_HOME
125
125
  ARROW_LEFT -> KeyEvent.KEYCODE_DPAD_LEFT
126
- ARROW_UP -> KeyEvent.KEYCODE_DPAD_DOWN
126
+ ARROW_UP -> KeyEvent.KEYCODE_DPAD_UP
127
127
  ARROW_RIGHT -> KeyEvent.KEYCODE_DPAD_RIGHT
128
128
  ARROW_DOWN -> KeyEvent.KEYCODE_DPAD_DOWN
129
129
  INSERT -> KeyEvent.KEYCODE_INSERT
@@ -132,9 +132,36 @@ fun keyCodeToEvent(keyValue: String?, @Suppress("UNUSED_PARAMETER") location: In
132
132
  }
133
133
  }
134
134
 
135
- class AndroidKeyEvent(private val uiController: UiController) {
136
- private val keyCharacterMap = KeyCharacterMap.load(KeyCharacterMap.VIRTUAL_KEYBOARD)
135
+ private const val IME_SETTLE_MS = 150L
136
+
137
+ // Shared across instances: the legacy /keys handler creates a new AndroidKeyEvent per request
138
+ private object ImeSettle {
139
+ private var lastWasInsertion = false
140
+ private var lastInputAt = 0L
141
+
142
+ /** Waits until the IME had time to process the previous input if it was of the other kind. */
143
+ fun await(uiController: UiController, isInsertion: Boolean) {
144
+ val remaining = IME_SETTLE_MS - (SystemClock.uptimeMillis() - lastInputAt)
145
+ if (lastInputAt > 0 && lastWasInsertion != isInsertion && remaining > 0) {
146
+ uiController.loopMainThreadForAtLeast(remaining)
147
+ }
148
+ }
149
+
150
+ fun mark(isInsertion: Boolean) {
151
+ lastWasInsertion = isInsertion
152
+ lastInputAt = SystemClock.uptimeMillis()
153
+ }
154
+ }
155
+
156
+ class AndroidKeyEvent @JvmOverloads constructor(
157
+ private val uiController: UiController,
158
+ // Overridable for tests; returns null for chars without key events
159
+ private val charsToEvents: (CharArray) -> Array<KeyEvent>? =
160
+ KeyCharacterMap.load(KeyCharacterMap.VIRTUAL_KEYBOARD)::getEvents
161
+ ) {
137
162
  private val keyDownTimes: MutableMap<String?, List<KeyEvent>> = HashMap()
163
+ private val insertedKeys: MutableSet<String?> = HashSet()
164
+ private val textInserter by lazy { NonAsciiTextInserter(uiController) }
138
165
 
139
166
  @Throws(AppiumException::class)
140
167
  fun keyDown(w3cKeyEvent: W3CKeyEvent) {
@@ -152,14 +179,11 @@ class AndroidKeyEvent(private val uiController: UiController) {
152
179
  * @param isDown Returns true if this is a key down events
153
180
  * @param isRepeat Returns true if this is a repeat key down
154
181
  * @param metaState The current global meta state of Android key events
155
- * @return
156
- * @throws InvalidArgumentException
182
+ * @return The events or null if the key has no Android key event representation
157
183
  */
158
- @Throws(InvalidArgumentException::class)
159
184
  private fun convertStringToAndroidKeyEvents(key: String?, isDown: Boolean,
160
- isRepeat: Boolean, metaState: Int): List<KeyEvent> {
161
- val keyEventsFromChar = keyCharacterMap.getEvents(key!!.toCharArray())
162
- ?: throw InvalidArgumentException(String.format("Could not find matching keycode for character %s", key))
185
+ isRepeat: Boolean, metaState: Int): List<KeyEvent>? {
186
+ val keyEventsFromChar = charsToEvents(key!!.toCharArray()) ?: return null
163
187
  val now = SystemClock.uptimeMillis()
164
188
  val keyEvents: MutableList<KeyEvent> = ArrayList()
165
189
  var keyEventIndex = 0
@@ -193,6 +217,10 @@ class AndroidKeyEvent(private val uiController: UiController) {
193
217
  val key = w3cKeyEvent.key
194
218
  val keyCode = w3cKeyEvent.keyCode
195
219
 
220
+ // Characters without key codes are typed on keyDown, so their keyUp is a no-op
221
+ if (!isDown && insertedKeys.remove(key)) {
222
+ return
223
+ }
196
224
  // No-op if releasing a key that isn't down
197
225
  if (action == KeyEvent.ACTION_UP && !keyDownTimes.containsKey(key)) {
198
226
  return
@@ -214,10 +242,47 @@ class AndroidKeyEvent(private val uiController: UiController) {
214
242
  } else {
215
243
  // If there's no keyCode, map the characters to Android keys
216
244
  convertStringToAndroidKeyEvents(key, isDown, w3cKeyEvent.isRepeat, getMetaState(w3cKeyEvent))
245
+ ?: run {
246
+ insertText(w3cKeyEvent)
247
+ return
248
+ }
217
249
  }
218
250
  injectKeyEvents(isDown, keyEvents, key)
219
251
  }
220
252
 
253
+ @Throws(AppiumException::class)
254
+ private fun insertText(w3cKeyEvent: W3CKeyEvent) {
255
+ if (w3cKeyEvent.isAltKey || w3cKeyEvent.isCtrlKey || w3cKeyEvent.isShiftKey || w3cKeyEvent.isMetaKey) {
256
+ throw InvalidArgumentException(
257
+ "Cannot type a character that has no key code while modifier keys are pressed")
258
+ }
259
+ // The IME handles key events and direct edits asynchronously, so switching needs a pause
260
+ ImeSettle.await(uiController, true)
261
+ textInserter.insert(w3cKeyEvent.key ?: "")
262
+ ImeSettle.mark(true)
263
+ insertedKeys.add(w3cKeyEvent.key)
264
+ }
265
+
266
+ /**
267
+ * Release the keys injected as pressed by this instance, e.g. after a failed request,
268
+ * because the next request starts with no knowledge of them.
269
+ */
270
+ fun releasePressedKeys() {
271
+ val pressed = keyDownTimes.values.toList()
272
+ keyDownTimes.clear()
273
+ for (keyEvents in pressed) {
274
+ for (down in keyEvents) {
275
+ val up = KeyEvent(down.downTime, SystemClock.uptimeMillis(), KeyEvent.ACTION_UP, down.keyCode,
276
+ 0, down.metaState, KeyCharacterMap.VIRTUAL_KEYBOARD, 0, 0)
277
+ try {
278
+ uiController.injectKeyEvent(up)
279
+ } catch (e: InjectEventSecurityException) {
280
+ AndroidLogger.info("Could not release the key ${down.keyCode}: ${e.message}")
281
+ }
282
+ }
283
+ }
284
+ }
285
+
221
286
  /**
222
287
  * Get the Android key meta state from a W3C key event
223
288
  * @param keyEvent W3C key event
@@ -254,6 +319,8 @@ class AndroidKeyEvent(private val uiController: UiController) {
254
319
  keyDownTimes.remove(key)
255
320
  }
256
321
 
322
+ ImeSettle.await(uiController, false)
323
+
257
324
  // Inject all of the key events, in order
258
325
  for (androidKeyEvent in keyEvents) {
259
326
  AndroidLogger.info(String.format("Calling key event: %s", androidKeyEvent))
@@ -267,5 +334,6 @@ class AndroidKeyEvent(private val uiController: UiController) {
267
334
  throw AppiumException(String.format("Could not inject key event %s", key))
268
335
  }
269
336
  }
337
+ ImeSettle.mark(false)
270
338
  }
271
339
  }
@@ -48,6 +48,8 @@ class EspressoW3CActionAdapter(private val uiController: UiController) : BaseW3C
48
48
  androidKeyEvent.keyUp(keyUpEvent)
49
49
  }
50
50
 
51
+ fun releasePressedKeys() = androidKeyEvent.releasePressedKeys()
52
+
51
53
  @Throws(AppiumException::class)
52
54
  override fun pointerDown(button: Int, sourceId: String, pointerType: PointerType?,
53
55
  x: Float, y: Float, depressedButtons: Set<Int>,
@@ -0,0 +1,112 @@
1
+ package io.appium.espressoserver.lib.helpers.w3c.adapter.espresso
2
+
3
+ import android.os.SystemClock
4
+ import android.text.Editable
5
+ import android.text.Selection
6
+ import android.view.View
7
+ import android.widget.TextView
8
+ import androidx.test.espresso.UiController
9
+ import androidx.test.runner.lifecycle.ActivityLifecycleMonitorRegistry
10
+ import androidx.test.runner.lifecycle.Stage
11
+ import io.appium.espressoserver.lib.handlers.exceptions.InvalidArgumentException
12
+ import io.appium.espressoserver.lib.handlers.exceptions.InvalidElementStateException
13
+ import io.appium.espressoserver.lib.helpers.AndroidLogger
14
+
15
+ private const val FOCUS_TIMEOUT_MS = 1000L
16
+ private const val FOCUS_POLL_MS = 50L
17
+
18
+ /**
19
+ * Types characters that have no key code (e.g. Cyrillic, CJK, emoji) by editing
20
+ * the focused text view directly. Must be used on the main thread.
21
+ */
22
+ class NonAsciiTextInserter(private val uiController: UiController) {
23
+
24
+ fun insert(text: String) {
25
+ val codePoints = text.codePoints().toArray()
26
+ if (codePoints.isEmpty()) {
27
+ throw InvalidArgumentException("Cannot type an empty key value")
28
+ }
29
+ codePoints.firstOrNull { !isTypeableCodePoint(it) }?.let {
30
+ throw InvalidArgumentException(
31
+ "The code point $it is neither a known W3C special key nor a printable character that could be typed")
32
+ }
33
+ uiController.loopMainThreadUntilIdle()
34
+ val textView = waitForFocusedEditor()
35
+ val editable = textView.text as? Editable
36
+ ?: throw InvalidElementStateException("The focused element does not expose editable text")
37
+ val inserted = insertIntoEditable(editable, text)
38
+ AndroidLogger.info("Typed $inserted of ${text.length} chars into the focused element")
39
+ uiController.loopMainThreadUntilIdle()
40
+ }
41
+
42
+ private fun waitForFocusedEditor(): TextView {
43
+ val deadline = SystemClock.uptimeMillis() + FOCUS_TIMEOUT_MS
44
+ var focused: View?
45
+ while (true) {
46
+ focused = findFocusedView()
47
+ if (focused is TextView && focused.onCheckIsTextEditor()) {
48
+ return focused
49
+ }
50
+ if (SystemClock.uptimeMillis() >= deadline) {
51
+ break
52
+ }
53
+ uiController.loopMainThreadForAtLeast(FOCUS_POLL_MS)
54
+ }
55
+ throw InvalidElementStateException(
56
+ if (focused == null) "Cannot type the character: no element is focused"
57
+ else "Cannot type the character: the focused element is not editable")
58
+ }
59
+
60
+ // Dialogs and popups have their own windows, so look at all app roots, the focused window first
61
+ private fun findFocusedView(): View? {
62
+ val roots = getRootViews()
63
+ val ordered = roots.filter { it.hasWindowFocus() } + roots.filterNot { it.hasWindowFocus() }.asReversed()
64
+ return ordered.firstNotNullOfOrNull { it.findFocus() }
65
+ }
66
+
67
+ @Suppress("UNCHECKED_CAST")
68
+ private fun getRootViews(): List<View> {
69
+ try {
70
+ val wmgClass = Class.forName("android.view.WindowManagerGlobal")
71
+ val wmg = wmgClass.getMethod("getInstance").invoke(null)
72
+ val views = wmgClass.getDeclaredField("mViews").apply { isAccessible = true }.get(wmg)
73
+ return (views as List<View>).filter { it.isShown }
74
+ } catch (e: ReflectiveOperationException) {
75
+ AndroidLogger.info("Cannot enumerate root views, using resumed activities only: ${e.message}")
76
+ }
77
+ return ActivityLifecycleMonitorRegistry.getInstance().getActivitiesInStage(Stage.RESUMED)
78
+ .mapNotNull { it.window?.decorView?.rootView }
79
+ }
80
+
81
+ companion object {
82
+ fun isTypeableCodePoint(codePoint: Int): Boolean {
83
+ if (!Character.isValidCodePoint(codePoint) || Character.isISOControl(codePoint)) {
84
+ return false
85
+ }
86
+ val type = Character.getType(codePoint)
87
+ return type != Character.PRIVATE_USE.toInt()
88
+ && type != Character.UNASSIGNED.toInt()
89
+ && type != Character.SURROGATE.toInt()
90
+ }
91
+
92
+ /**
93
+ * Replaces the selection (or appends if there is none) and puts the cursor after the new text.
94
+ * Input filters apply, so fewer chars (or none) may be inserted.
95
+ * @return the number of chars actually inserted
96
+ */
97
+ fun insertIntoEditable(editable: Editable, text: String): Int {
98
+ val selStart = Selection.getSelectionStart(editable)
99
+ val selEnd = Selection.getSelectionEnd(editable)
100
+ val (start, end) = if (selStart < 0 || selEnd < 0) {
101
+ editable.length to editable.length
102
+ } else {
103
+ minOf(selStart, selEnd) to maxOf(selStart, selEnd)
104
+ }
105
+ val lengthBefore = editable.length
106
+ editable.replace(start, end, text)
107
+ val inserted = editable.length - (lengthBefore - (end - start))
108
+ Selection.setSelection(editable, (start + inserted).coerceIn(0, editable.length))
109
+ return inserted
110
+ }
111
+ }
112
+ }
@@ -47,7 +47,7 @@ object KeyProcessor {
47
47
 
48
48
  // 5-7 get the Unicode value of the keystroke (verify that it's a single character)
49
49
  val key = action.value
50
- if (key?.length != 1) {
50
+ if (key == null || key.codePointCount(0, key.length) != 1) {
51
51
  throwArgException(index, id, String.format("has invalid 'value' %s. Must be a unicode point", key))
52
52
  }
53
53
  val actionObject = ActionObject(id, inputSourceType, subType, index)
@@ -189,6 +189,26 @@ class ProcessorTest {
189
189
 
190
190
  }
191
191
 
192
+ @Test
193
+ @Throws(InvalidArgumentException::class)
194
+ fun shouldPassKeyIfSurrogatePair() {
195
+ val action = Action()
196
+ action.type = ActionType.KEY_DOWN
197
+ action.value = "\uD83D\uDE00"
198
+ val actionObject = processKeyAction(action, InputSourceType.KEY, "any", 0)
199
+ assertEquals(actionObject.value, action.value)
200
+ }
201
+
202
+ @Test
203
+ fun shouldRejectKeyIfTwoSurrogatePairs() {
204
+ val action = Action()
205
+ action.type = ActionType.KEY_DOWN
206
+ action.value = "\uD83D\uDE00\uD83D\uDE00"
207
+ assertThrows(InvalidArgumentException::class.java) {
208
+ processKeyAction(action, InputSourceType.KEY, "any", 0)
209
+ }
210
+ }
211
+
192
212
  @Test
193
213
  @Throws(InvalidArgumentException::class)
194
214
  fun shouldPassKeyIfUnicode() {
@@ -9,6 +9,8 @@ import io.appium.espressoserver.lib.helpers.w3c.adapter.espresso.AndroidKeyEvent
9
9
  import io.appium.espressoserver.lib.helpers.w3c.adapter.espresso.EspressoW3CActionAdapter
10
10
  import io.appium.espressoserver.lib.helpers.w3c.adapter.espresso.keyCodeToEvent
11
11
  import io.appium.espressoserver.lib.helpers.w3c.dispatcher.W3CKeyEvent
12
+ import io.appium.espressoserver.lib.helpers.w3c.dispatcher.constants.NormalizedKeys.ARROW_DOWN
13
+ import io.appium.espressoserver.lib.helpers.w3c.dispatcher.constants.NormalizedKeys.ARROW_UP
12
14
  import io.appium.espressoserver.lib.helpers.w3c.dispatcher.constants.NormalizedKeys.BACKSPACE
13
15
  import io.appium.espressoserver.lib.helpers.w3c.dispatcher.getKeyEvent
14
16
  import org.junit.Assert
@@ -110,5 +112,7 @@ class AndroidKeyEventTest {
110
112
  fun keyCode() {
111
113
  Assert.assertEquals(keyCodeToEvent(BACKSPACE, 0).toLong(), KeyEvent.KEYCODE_DEL.toLong())
112
114
  Assert.assertEquals(keyCodeToEvent("A", 0).toLong(), -1)
115
+ Assert.assertEquals(keyCodeToEvent(ARROW_UP, 0).toLong(), KeyEvent.KEYCODE_DPAD_UP.toLong())
116
+ Assert.assertEquals(keyCodeToEvent(ARROW_DOWN, 0).toLong(), KeyEvent.KEYCODE_DPAD_DOWN.toLong())
113
117
  }
114
118
  }
@@ -0,0 +1,109 @@
1
+ package io.appium.espressoserver.test.helpers.w3c.adapter
2
+
3
+ import android.text.InputFilter
4
+ import android.view.KeyEvent
5
+ import android.text.Selection
6
+ import android.text.SpannableStringBuilder
7
+ import io.appium.espressoserver.lib.handlers.exceptions.InvalidArgumentException
8
+ import io.appium.espressoserver.lib.helpers.w3c.adapter.espresso.AndroidKeyEvent
9
+ import io.appium.espressoserver.lib.helpers.w3c.adapter.espresso.NonAsciiTextInserter.Companion.insertIntoEditable
10
+ import io.appium.espressoserver.lib.helpers.w3c.adapter.espresso.NonAsciiTextInserter.Companion.isTypeableCodePoint
11
+ import io.appium.espressoserver.lib.helpers.w3c.dispatcher.W3CKeyEvent
12
+ import org.junit.Assert
13
+ import org.junit.Test
14
+ import org.junit.runner.RunWith
15
+ import org.robolectric.RobolectricTestRunner
16
+
17
+ @RunWith(RobolectricTestRunner::class)
18
+ class NonAsciiTextInserterTest {
19
+ private fun editable(text: String, start: Int = -1, end: Int = start) =
20
+ SpannableStringBuilder(text).also { if (start >= 0) Selection.setSelection(it, start, end) }
21
+
22
+ private fun keyEvent(value: String) = W3CKeyEvent().also { it.key = value; it.keyCode = -1 }
23
+
24
+ @Test
25
+ fun typeableCodePoints() {
26
+ listOf("П", "日", "ी", "😀", " ").forEach {
27
+ Assert.assertTrue(it, isTypeableCodePoint(it.codePointAt(0)))
28
+ }
29
+ listOf(0x0, 0x0A, 0x7F, 0xE000, 0xE017, 0xD800, 0x378, 0x110000).forEach {
30
+ Assert.assertFalse(it.toString(), isTypeableCodePoint(it))
31
+ }
32
+ }
33
+
34
+ @Test
35
+ fun appendsWithoutSelection() {
36
+ val e = editable("ab")
37
+ Assert.assertEquals(1, insertIntoEditable(e, "Ж"))
38
+ Assert.assertEquals("abЖ", e.toString())
39
+ Assert.assertEquals(3, Selection.getSelectionEnd(e))
40
+ }
41
+
42
+ @Test
43
+ fun insertsAtCursor() {
44
+ val e = editable("abc", 1)
45
+ insertIntoEditable(e, "Ж")
46
+ Assert.assertEquals("aЖbc", e.toString())
47
+ Assert.assertEquals(2, Selection.getSelectionStart(e))
48
+ Assert.assertEquals(2, Selection.getSelectionEnd(e))
49
+ }
50
+
51
+ @Test
52
+ fun replacesReversedSelection() {
53
+ val e = editable("abcd", 3, 1)
54
+ insertIntoEditable(e, "Ж")
55
+ Assert.assertEquals("aЖd", e.toString())
56
+ Assert.assertEquals(2, Selection.getSelectionEnd(e))
57
+ }
58
+
59
+ @Test
60
+ fun insertsSurrogatePairAndCombiningSequence() {
61
+ val e = editable("")
62
+ "😀".let { Assert.assertEquals(2, insertIntoEditable(e, it)) }
63
+ "री".codePoints().toArray().forEach { insertIntoEditable(e, String(Character.toChars(it))) }
64
+ Assert.assertEquals("😀री", e.toString())
65
+ Assert.assertEquals(e.length, Selection.getSelectionEnd(e))
66
+ }
67
+
68
+ @Test
69
+ fun respectsInputFilters() {
70
+ val e = editable("ab", 1)
71
+ e.filters = arrayOf(InputFilter.LengthFilter(2))
72
+ Assert.assertEquals(0, insertIntoEditable(e, "Ж"))
73
+ Assert.assertEquals("ab", e.toString())
74
+ Assert.assertEquals(1, Selection.getSelectionEnd(e))
75
+ }
76
+
77
+ @Test
78
+ fun rejectsUnknownSpecialKey() {
79
+ val androidKeyEvent = AndroidKeyEvent(AndroidKeyEventTest.MockUiController()) { null }
80
+ Assert.assertThrows(InvalidArgumentException::class.java) { androidKeyEvent.keyDown(keyEvent("")) }
81
+ }
82
+
83
+ @Test
84
+ fun rejectsModifiedNonAsciiCharacter() {
85
+ val androidKeyEvent = AndroidKeyEvent(AndroidKeyEventTest.MockUiController()) { null }
86
+ val event = keyEvent("Ж").also { it.isCtrlKey = true }
87
+ Assert.assertThrows(InvalidArgumentException::class.java) { androidKeyEvent.keyDown(event) }
88
+ }
89
+
90
+ @Test
91
+ fun keyUpOfNeverTypedNonAsciiIsNoop() {
92
+ AndroidKeyEvent(AndroidKeyEventTest.MockUiController()) { null }.keyUp(keyEvent("Ж"))
93
+ }
94
+
95
+ @Test
96
+ fun releasesPressedKeys() {
97
+ val uiController = AndroidKeyEventTest.MockUiController()
98
+ val androidKeyEvent = AndroidKeyEvent(uiController) { null }
99
+ val shift = W3CKeyEvent().also { it.key = "\uE008"; it.keyCode = KeyEvent.KEYCODE_SHIFT_LEFT }
100
+ androidKeyEvent.keyDown(shift)
101
+ androidKeyEvent.releasePressedKeys()
102
+ val last = uiController.keyEvents.last()
103
+ Assert.assertEquals(KeyEvent.ACTION_UP, last.action)
104
+ Assert.assertEquals(KeyEvent.KEYCODE_SHIFT_LEFT, last.keyCode)
105
+ val count = uiController.keyEvents.size
106
+ androidKeyEvent.releasePressedKeys()
107
+ Assert.assertEquals(count, uiController.keyEvents.size)
108
+ }
109
+ }
@@ -0,0 +1,14 @@
1
+ package io.appium.espressoserver.test.http
2
+
3
+ import io.appium.espressoserver.lib.http.Server
4
+ import org.junit.Assert.assertTrue
5
+ import org.junit.Test
6
+ import org.junit.rules.TestRule
7
+
8
+ class ServerTest {
9
+ @Test
10
+ fun serverIsUsableAsJUnitRule() {
11
+ // The instrumentation test declares the server as a JUnit rule in every flavor
12
+ assertTrue(TestRule::class.java.isAssignableFrom(Server::class.java))
13
+ }
14
+ }
@@ -0,0 +1,15 @@
1
+ The ISC License
2
+
3
+ Copyright (c) Isaac Z. Schlueter and Contributors
4
+
5
+ Permission to use, copy, modify, and/or distribute this software for any
6
+ purpose with or without fee is hereby granted, provided that the above
7
+ copyright notice and this permission notice appear in all copies.
8
+
9
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10
+ WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11
+ MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12
+ ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13
+ WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14
+ ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
15
+ IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.