global-events.test.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. const platformInfo = process.env.uniTestPlatformInfo.toLocaleLowerCase()
  2. const isMP = platformInfo.startsWith('mp')
  3. const isAndroid = platformInfo.startsWith('android')
  4. const isIos = platformInfo.startsWith('ios')
  5. const isHarmony = platformInfo.startsWith('harmony')
  6. const isAppWebView = process.env.UNI_AUTOMATOR_APP_WEBVIEW == 'true'
  7. const PAGE_PATH = '/pages/component/global-events/global-events'
  8. describe('event trigger', () => {
  9. if (process.env.UNI_TEST_DEVICES_DIRECTION == 'landscape') {
  10. it('跳过横屏模式', () => {
  11. expect(1).toBe(1)
  12. })
  13. return
  14. }
  15. let page
  16. const tapParams = {
  17. x: 100,
  18. y: 380,
  19. duration: 1000
  20. }
  21. beforeAll(async () => {
  22. page = await program.navigateTo(PAGE_PATH)
  23. await page.waitFor('view')
  24. })
  25. it('touch', async () => {
  26. if (!isAppWebView) {
  27. const el = await page.$('#touch-target')
  28. await el.touchstart({
  29. touches: [{
  30. identifier: 1,
  31. pageX: 101,
  32. pageY: 101,
  33. clientX: 101,
  34. clientY: 101,
  35. screenX: 101,
  36. screenY: 101
  37. }, ],
  38. changedTouches: [{
  39. identifier: 1,
  40. pageX: 101,
  41. pageY: 101,
  42. clientX: 101,
  43. clientY: 101,
  44. screenX: 101,
  45. screenY: 101
  46. }, ],
  47. })
  48. await page.waitFor(100)
  49. const touchStartTouchTargetIdentifier = '1'
  50. const touchStartTouchTargetValue = '101'
  51. const touchStartTouchIdentifier = await page.$('#touch-start-touch-identifier')
  52. expect(await touchStartTouchIdentifier.text()).toBe(touchStartTouchTargetIdentifier)
  53. const touchStartTouchPageX = await page.$('#touch-start-touch-page-x')
  54. expect(await touchStartTouchPageX.text()).toBe(touchStartTouchTargetValue)
  55. const touchStartTouchPageY = await page.$('#touch-start-touch-page-y')
  56. expect(await touchStartTouchPageY.text()).toBe(touchStartTouchTargetValue)
  57. const touchStartTouchClientX = await page.$('#touch-start-touch-client-x')
  58. expect(await touchStartTouchClientX.text()).toBe(touchStartTouchTargetValue)
  59. const touchStartTouchClientY = await page.$('#touch-start-touch-client-y')
  60. expect(await touchStartTouchClientY.text()).toBe(touchStartTouchTargetValue)
  61. const touchStartTouchScreenX = await page.$('#touch-start-touch-screen-x')
  62. expect(await touchStartTouchScreenX.text()).toBe(touchStartTouchTargetValue)
  63. const touchStartTouchScreenY = await page.$('#touch-start-touch-screen-y')
  64. expect(await touchStartTouchScreenY.text()).toBe(touchStartTouchTargetValue)
  65. const touchStartChangedTouchIdentifier = await page.$('#touch-start-changed-touch-identifier')
  66. expect(await touchStartChangedTouchIdentifier.text()).toBe(touchStartTouchTargetIdentifier)
  67. const touchStartChangedTouchPageX = await page.$('#touch-start-changed-touch-page-x')
  68. expect(await touchStartChangedTouchPageX.text()).toBe(touchStartTouchTargetValue)
  69. const touchStartChangedTouchPageY = await page.$('#touch-start-changed-touch-page-y')
  70. expect(await touchStartChangedTouchPageY.text()).toBe(touchStartTouchTargetValue)
  71. const touchStartChangedTouchClientX = await page.$('#touch-start-changed-touch-client-x')
  72. expect(await touchStartChangedTouchClientX.text()).toBe(touchStartTouchTargetValue)
  73. const touchStartChangedTouchClientY = await page.$('#touch-start-changed-touch-client-y')
  74. expect(await touchStartChangedTouchClientY.text()).toBe(touchStartTouchTargetValue)
  75. const touchStartChangedTouchScreenX = await page.$('#touch-start-changed-touch-screen-x')
  76. expect(await touchStartChangedTouchScreenX.text()).toBe(touchStartTouchTargetValue)
  77. const touchStartChangedTouchScreenY = await page.$('#touch-start-changed-touch-screen-y')
  78. expect(await touchStartChangedTouchScreenY.text()).toBe(touchStartTouchTargetValue)
  79. await el.touchmove({
  80. touches: [{
  81. identifier: 1,
  82. pageX: 102,
  83. pageY: 102,
  84. clientX: 102,
  85. clientY: 102,
  86. screenX: 102,
  87. screenY: 102
  88. }],
  89. changedTouches: [{
  90. identifier: 1,
  91. pageX: 102,
  92. pageY: 102,
  93. clientX: 102,
  94. clientY: 102,
  95. screenX: 102,
  96. screenY: 102
  97. }, ],
  98. })
  99. await page.waitFor(100)
  100. const touchMoveTouchTargetIdentifier = '1'
  101. const touchMoveTouchTargetValue = '102'
  102. const touchMoveTouchIdentifier = await page.$('#touch-move-touch-identifier')
  103. expect(await touchMoveTouchIdentifier.text()).toBe(touchMoveTouchTargetIdentifier)
  104. const touchMoveTouchPageX = await page.$('#touch-move-touch-page-x')
  105. expect(await touchMoveTouchPageX.text()).toBe(touchMoveTouchTargetValue)
  106. const touchMoveTouchPageY = await page.$('#touch-move-touch-page-y')
  107. expect(await touchMoveTouchPageY.text()).toBe(touchMoveTouchTargetValue)
  108. const touchMoveTouchClientX = await page.$('#touch-move-touch-client-x')
  109. expect(await touchMoveTouchClientX.text()).toBe(touchMoveTouchTargetValue)
  110. const touchMoveTouchClientY = await page.$('#touch-move-touch-client-y')
  111. expect(await touchMoveTouchClientY.text()).toBe(touchMoveTouchTargetValue)
  112. const touchMoveTouchScreenX = await page.$('#touch-move-touch-screen-x')
  113. expect(await touchMoveTouchScreenX.text()).toBe(touchMoveTouchTargetValue)
  114. const touchMoveTouchScreenY = await page.$('#touch-move-touch-screen-y')
  115. expect(await touchMoveTouchScreenY.text()).toBe(touchMoveTouchTargetValue)
  116. const touchMoveChangedTouchIdentifier = await page.$('#touch-move-changed-touch-identifier')
  117. expect(await touchMoveChangedTouchIdentifier.text()).toBe(touchMoveTouchTargetIdentifier)
  118. const touchMoveChangedTouchPageX = await page.$('#touch-move-changed-touch-page-x')
  119. expect(await touchMoveChangedTouchPageX.text()).toBe(touchMoveTouchTargetValue)
  120. const touchMoveChangedTouchPageY = await page.$('#touch-move-changed-touch-page-y')
  121. expect(await touchMoveChangedTouchPageY.text()).toBe(touchMoveTouchTargetValue)
  122. const touchMoveChangedTouchClientX = await page.$('#touch-move-changed-touch-client-x')
  123. expect(await touchMoveChangedTouchClientX.text()).toBe(touchMoveTouchTargetValue)
  124. const touchMoveChangedTouchClientY = await page.$('#touch-move-changed-touch-client-y')
  125. expect(await touchMoveChangedTouchClientY.text()).toBe(touchMoveTouchTargetValue)
  126. const touchMoveChangedTouchScreenX = await page.$('#touch-move-changed-touch-screen-x')
  127. expect(await touchMoveChangedTouchScreenX.text()).toBe(touchMoveTouchTargetValue)
  128. const touchMoveChangedTouchScreenY = await page.$('#touch-move-changed-touch-screen-y')
  129. expect(await touchMoveChangedTouchScreenY.text()).toBe(touchMoveTouchTargetValue)
  130. await el.touchend({
  131. touches: [{
  132. identifier: 1,
  133. pageX: 103,
  134. pageY: 103,
  135. clientX: 103,
  136. clientY: 103,
  137. screenX: 103,
  138. screenY: 103
  139. }],
  140. changedTouches: [{
  141. identifier: 1,
  142. pageX: 103,
  143. pageY: 103,
  144. clientX: 103,
  145. clientY: 103,
  146. screenX: 103,
  147. screenY: 103
  148. }, ],
  149. })
  150. await page.waitFor(100)
  151. const touchEndTouchTargetIdentifier = '1'
  152. const touchEndTouchTargetValue = '103'
  153. const touchEndTouchIdentifier = await page.$('#touch-end-touch-identifier')
  154. expect(await touchEndTouchIdentifier.text()).toBe(touchEndTouchTargetIdentifier)
  155. const touchEndTouchPageX = await page.$('#touch-end-touch-page-x')
  156. expect(await touchEndTouchPageX.text()).toBe(touchEndTouchTargetValue)
  157. const touchEndTouchPageY = await page.$('#touch-end-touch-page-y')
  158. expect(await touchEndTouchPageY.text()).toBe(touchEndTouchTargetValue)
  159. const touchEndTouchClientX = await page.$('#touch-end-touch-client-x')
  160. expect(await touchEndTouchClientX.text()).toBe(touchEndTouchTargetValue)
  161. const touchEndTouchClientY = await page.$('#touch-end-touch-client-y')
  162. expect(await touchEndTouchClientY.text()).toBe(touchEndTouchTargetValue)
  163. const touchEndTouchScreenX = await page.$('#touch-end-touch-screen-x')
  164. expect(await touchEndTouchScreenX.text()).toBe(touchEndTouchTargetValue)
  165. const touchEndTouchScreenY = await page.$('#touch-end-touch-screen-y')
  166. expect(await touchEndTouchScreenY.text()).toBe(touchEndTouchTargetValue)
  167. const touchEndChangedTouchIdentifier = await page.$('#touch-end-changed-touch-identifier')
  168. expect(await touchEndChangedTouchIdentifier.text()).toBe(touchEndTouchTargetIdentifier)
  169. const touchEndChangedTouchPageX = await page.$('#touch-end-changed-touch-page-x')
  170. expect(await touchEndChangedTouchPageX.text()).toBe(touchEndTouchTargetValue)
  171. const touchEndChangedTouchPageY = await page.$('#touch-end-changed-touch-page-y')
  172. expect(await touchEndChangedTouchPageY.text()).toBe(touchEndTouchTargetValue)
  173. const touchEndChangedTouchClientX = await page.$('#touch-end-changed-touch-client-x')
  174. expect(await touchEndChangedTouchClientX.text()).toBe(touchEndTouchTargetValue)
  175. const touchEndChangedTouchClientY = await page.$('#touch-end-changed-touch-client-y')
  176. expect(await touchEndChangedTouchClientY.text()).toBe(touchEndTouchTargetValue)
  177. const touchEndChangedTouchScreenX = await page.$('#touch-end-changed-touch-screen-x')
  178. expect(await touchEndChangedTouchScreenX.text()).toBe(touchEndTouchTargetValue)
  179. const touchEndChangedTouchScreenY = await page.$('#touch-end-changed-touch-screen-y')
  180. expect(await touchEndChangedTouchScreenY.text()).toBe(touchEndTouchTargetValue)
  181. }
  182. })
  183. it('click', async () => {
  184. if (isMP) {
  185. page = await program.navigateTo(PAGE_PATH)
  186. await page.waitFor('view')
  187. const el = await page.$('#longpress-target')
  188. await el.tap()
  189. await page.waitFor(500)
  190. const clickEventX = await page.$('#click-event-x')
  191. expect(parseInt(await clickEventX.text())).toBeGreaterThan(0)
  192. const clickEventY = await page.$('#click-event-y')
  193. expect(parseInt(await clickEventY.text())).toBeGreaterThan(0)
  194. } else {
  195. const el = await page.$('#longpress-target')
  196. await el.tap()
  197. await page.waitFor(100)
  198. const targetX = '0'
  199. const targetY = '0'
  200. const tapEventX = await page.$('#tap-event-x')
  201. expect(await tapEventX.text()).toBe(targetX)
  202. const tapEventY = await page.$('#tap-event-y')
  203. expect(await tapEventY.text()).toBe(targetY)
  204. const clickEventX = await page.$('#click-event-x')
  205. expect(await clickEventX.text()).toBe(targetX)
  206. const clickEventY = await page.$('#click-event-y')
  207. expect(await clickEventY.text()).toBe(targetY)
  208. }
  209. })
  210. it('longPress', async () => {
  211. if (!isAppWebView) {
  212. const el = await page.$('#longpress-target')
  213. await el.longpress()
  214. await page.waitFor(100)
  215. if (isMP) {
  216. const longPressTouchIdentifier = await page.$('#long-press-touch-identifier')
  217. expect(await longPressTouchIdentifier.text()).toBe('0')
  218. const longPressTouchPageX = await page.$('#long-press-touch-page-x')
  219. expect(parseInt(await longPressTouchPageX.text())).toBeGreaterThan(0)
  220. const longPressTouchPageY = await page.$('#long-press-touch-page-y')
  221. expect(parseInt(await longPressTouchPageY.text())).toBeGreaterThan(0)
  222. const longPressTouchClientX = await page.$('#long-press-touch-client-x')
  223. expect(parseInt(await longPressTouchClientX.text())).toBeGreaterThan(0)
  224. const longPressTouchClientY = await page.$('#long-press-touch-client-y')
  225. expect(parseInt(await longPressTouchClientY.text())).toBeGreaterThan(0)
  226. const longPressChangedTouchIdentifier = await page.$('#long-press-changed-touch-identifier')
  227. expect(await longPressChangedTouchIdentifier.text()).toBe('0')
  228. const longPressChangedTouchPageX = await page.$('#long-press-changed-touch-page-x')
  229. expect(parseInt(await longPressChangedTouchPageX.text())).toBeGreaterThan(0)
  230. const longPressChangedTouchPageY = await page.$('#long-press-changed-touch-page-y')
  231. expect(parseInt(await longPressChangedTouchPageY.text())).toBeGreaterThan(0)
  232. const longPressChangedTouchClientX = await page.$('#long-press-changed-touch-client-x')
  233. expect(parseInt(await longPressChangedTouchClientX.text())).toBeGreaterThan(0)
  234. const longPressChangedTouchClientY = await page.$('#long-press-changed-touch-client-y')
  235. expect(parseInt(await longPressChangedTouchClientY.text())).toBeGreaterThan(0)
  236. } else {
  237. const longPressTouchTargetIdentifier = '1'
  238. const longPressTouchTargetValue = '0'
  239. const longPressTouchIdentifier = await page.$('#long-press-touch-identifier')
  240. expect(await longPressTouchIdentifier.text()).toBe(longPressTouchTargetIdentifier)
  241. const longPressTouchPageX = await page.$('#long-press-touch-page-x')
  242. expect(await longPressTouchPageX.text()).toBe(longPressTouchTargetValue)
  243. const longPressTouchPageY = await page.$('#long-press-touch-page-y')
  244. expect(await longPressTouchPageY.text()).toBe(longPressTouchTargetValue)
  245. const longPressTouchClientX = await page.$('#long-press-touch-client-x')
  246. expect(await longPressTouchClientX.text()).toBe(longPressTouchTargetValue)
  247. const longPressTouchClientY = await page.$('#long-press-touch-client-y')
  248. expect(await longPressTouchClientY.text()).toBe(longPressTouchTargetValue)
  249. const longPressTouchScreenX = await page.$('#long-press-touch-screen-x')
  250. expect(await longPressTouchScreenX.text()).toBe(longPressTouchTargetValue)
  251. const longPressTouchScreenY = await page.$('#long-press-touch-screen-y')
  252. expect(await longPressTouchScreenY.text()).toBe(longPressTouchTargetValue)
  253. const longPressChangedTouchIdentifier = await page.$('#long-press-changed-touch-identifier')
  254. expect(await longPressChangedTouchIdentifier.text()).toBe(longPressTouchTargetIdentifier)
  255. const longPressChangedTouchPageX = await page.$('#long-press-changed-touch-page-x')
  256. expect(await longPressChangedTouchPageX.text()).toBe(longPressTouchTargetValue)
  257. const longPressChangedTouchPageY = await page.$('#long-press-changed-touch-page-y')
  258. expect(await longPressChangedTouchPageY.text()).toBe(longPressTouchTargetValue)
  259. const longPressChangedTouchClientX = await page.$('#long-press-changed-touch-client-x')
  260. expect(await longPressChangedTouchClientX.text()).toBe(longPressTouchTargetValue)
  261. const longPressChangedTouchClientY = await page.$('#long-press-changed-touch-client-y')
  262. expect(await longPressChangedTouchClientY.text()).toBe(longPressTouchTargetValue)
  263. const longPressChangedTouchScreenX = await page.$('#long-press-changed-touch-screen-x')
  264. expect(await longPressChangedTouchScreenX.text()).toBe(longPressTouchTargetValue)
  265. const longPressChangedTouchScreenY = await page.$('#long-press-changed-touch-screen-y')
  266. expect(await longPressChangedTouchScreenY.text()).toBe(longPressTouchTargetValue)
  267. if (isAndroid || isIos || isHarmony) {
  268. if (isIos) {
  269. // 规避系统授权弹框
  270. await program.tap({
  271. x: 100,
  272. y: 500,
  273. })
  274. }
  275. await program.tap(tapParams)
  276. const longPressTouchIdentifierText = await longPressTouchIdentifier.text()
  277. expect(longPressTouchIdentifierText).not.toBe(longPressTouchTargetIdentifier)
  278. expect(longPressTouchIdentifierText).toBeTruthy()
  279. const longPressTouchPageXText = await longPressTouchPageX.text()
  280. expect(longPressTouchPageXText).not.toBe(longPressTouchTargetValue)
  281. expect(longPressTouchPageXText).toBeTruthy()
  282. const longPressTouchPageYText = await longPressTouchPageY.text()
  283. expect(longPressTouchPageYText).not.toBe(longPressTouchTargetValue)
  284. expect(longPressTouchPageYText).toBeTruthy()
  285. const longPressTouchClientXText = await longPressTouchClientX.text()
  286. expect(longPressTouchClientXText).not.toBe(longPressTouchTargetValue)
  287. expect(longPressTouchClientXText).toBeTruthy()
  288. const longPressTouchClientYText = await longPressTouchClientY.text()
  289. expect(longPressTouchClientYText).not.toBe(longPressTouchTargetValue)
  290. expect(longPressTouchClientYText).toBeTruthy()
  291. const longPressTouchScreenXText = await longPressTouchScreenX.text()
  292. expect(longPressTouchScreenXText).not.toBe(longPressTouchTargetValue)
  293. expect(longPressTouchScreenXText).toBeTruthy()
  294. const longPressTouchScreenYText = await longPressTouchScreenY.text()
  295. expect(longPressTouchScreenYText).not.toBe(longPressTouchTargetValue)
  296. expect(longPressTouchScreenYText).toBeTruthy()
  297. const longPressChangedTouchIdentifierText = await longPressChangedTouchIdentifier.text()
  298. expect(longPressChangedTouchIdentifierText).not.toBe(longPressTouchTargetIdentifier)
  299. expect(longPressChangedTouchIdentifierText).toBeTruthy()
  300. const longPressChangedTouchPageXText = await longPressChangedTouchPageX.text()
  301. expect(longPressChangedTouchPageXText).not.toBe(longPressTouchTargetValue)
  302. expect(longPressChangedTouchPageXText).toBeTruthy()
  303. const longPressChangedTouchPageYText = await longPressChangedTouchPageY.text()
  304. expect(longPressChangedTouchPageYText).not.toBe(longPressTouchTargetValue)
  305. expect(longPressChangedTouchPageYText).toBeTruthy()
  306. const longPressChangedTouchClientXText = await longPressChangedTouchClientX.text()
  307. expect(longPressChangedTouchClientXText).not.toBe(longPressTouchTargetValue)
  308. expect(longPressChangedTouchClientXText).toBeTruthy()
  309. const longPressChangedTouchClientYText = await longPressChangedTouchClientY.text()
  310. expect(longPressChangedTouchClientYText).not.toBe(longPressTouchTargetValue)
  311. expect(longPressChangedTouchClientYText).toBeTruthy()
  312. const longPressChangedTouchScreenXText = await longPressChangedTouchScreenX.text()
  313. expect(longPressChangedTouchScreenXText).not.toBe(longPressTouchTargetValue)
  314. expect(longPressChangedTouchScreenXText).toBeTruthy()
  315. const longPressChangedTouchScreenYText = await longPressChangedTouchScreenY.text()
  316. expect(longPressChangedTouchScreenYText).not.toBe(longPressTouchTargetValue)
  317. expect(longPressChangedTouchScreenYText).toBeTruthy()
  318. }
  319. }
  320. }
  321. })
  322. if (isIos || isHarmony) {
  323. it('mock tap event', async () => {
  324. await page.setData({
  325. clickEvent: null
  326. })
  327. tapParams.duration = 0
  328. await program.tap(tapParams)
  329. await page.waitFor(200)
  330. const clickEventX = await page.$('#click-event-x')
  331. const StringX = await clickEventX.text()
  332. expect(Number(StringX)).toBeGreaterThan(0)
  333. const clickEventY = await page.$('#click-event-y')
  334. const StringY = await clickEventY.text()
  335. expect(Number(StringY)).toBeGreaterThan(0)
  336. })
  337. }
  338. })