## 對安卓輸入事件的淺見
### 什么是輸入事件
安卓操作系統使用事件概念來處理從不同輸入設備接收到的信號。
它支持各種不同的設備,例如觸摸屏,激光筆,鼠標,鍵盤,但其中大多數都使用從基本inputEvent類派生的MotionEvent或者KeyEvent API。這些API相當靈活,并支持各種不同的設置。我們對這些API中負責觸摸和鍵盤事件生成/模擬的部分特別感興趣。
### 輸入事件如何工作
事件是用于響應來自輸入設備的信號而生成的對象。這些對象之后會被傳遞到對應的內核子系統,由子系統處理這些對象,并通知所有監聽進程關于點擊,按鍵,滑動等操作。這就意味著,為了模擬由外部設備生成的信號(如觸摸屏幕),發送具有與真實設備生成的屬性與序列相同的事件對象是必要的。
### 讓我們模擬一次點擊
每個輸入設備都有一組動作,這些動作的屬性范圍和序列已經在操作系統中預定義好。我們稱這些動作為”點擊“,”滑動“或者”雙擊“等。每個動作的屬性都可以在安卓文檔或者操作系統源碼中找到。為了執行被識別為單擊的事件序列,必須生成以下運動事件:
- `ACTION_POINTER_DOWN`
- 等待 125毫秒 (525毫秒或更長的等待時間將改為合成長按動作)
- `ACTION_POINTER_UP`. 該 `downTime` 屬性應設置為與 `ACTION_POINTER_DOWN`相同的時間戳
同樣重要的是,開始事件和結束事件的坐標和其他屬性都應相等,但`eventTime` 必須始終等于當前系統時間戳(以毫秒為單位,`SystemClock.uptimeMillis()`).
`MotionEvent`對象本身可以通過[obtain](https://developer.android.com/reference/android/view/MotionEvent#obtain(long,%20long,%20int,%20float,%20float,%20int)) API來創建,其參數是相應的事件屬性。
事件創建后,必須將它們傳遞給系統去執行。這樣的動作并不安全,因此只有在通過`IUiAutomationConnection` 接口的[injectInputEvent](https://android.googlesource.com/platform/frameworks/base/+/master/core/java/android/app/IUiAutomationConnection.aidl) 方法進行儀器化測試才有可能執行。這是一種非常低級的方法,而且只能在自動化測試中通過映射來訪問。通常情況下,UiAutomator的 API有對應的修飾器模擬這些上文描述過的東西(如`touchDown`, `touchMove`等)
### 如何進行更復雜的動作
理論上,我們可以使用生成的時間序列來模擬任何輸入動作。雖然,某些動作(如多指滑動)確實非常復雜,并且需要在正確的時間和屬性下生成大量的事件。如果給定的事件不遵循內部操作要求,則操作系統會忽略這些事件。我們也能從UiAutomator框架中獲得一點點的幫助,因為谷歌只封裝了有限的簡單動作,如`tap`, `drag` 或者 `swipe`。因此,為了生成兩指滑動的動作,我們需要提供以下事件鏈:
- `ACTION_POINTER_DOWN` (手指1)
- `ACTION_POINTER_DOWN` (手指2)
- 啟動一個循環,每20ms為手指1和手指2生成`ACTION_POINTER_MOVE` 事件直到`ACTION_POINTER_UP` 被執行為止。`downTime` 的設置時間應該與`ACTION_POINTER_DOWN`一致。每個移動事件的坐標應該由相對起點和終點坐標之間點組成的。(x0 + sqrt(sqr(x0) + sqr(x1))) * k, y0 + sqrt(sqr(y0) + sqr(y1))) * k)
- `ACTION_POINTER_UP` (手指1) `downTime` 應該設置為與 `ACTION_POINTER_DOWN`相對應的時間戳
- `ACTION_POINTER_UP` (手指2) `downTime` 應該設置為與 `ACTION_POINTER_DOWN`相對應的時間戳
谷歌在UiAutomator代碼中使用5ms作為兩次移動時間之間的間隔時間,但是據我們的觀察,這個值太小了,會導致執行動作明顯延遲。
### 進一步閱讀
不幸的是,關于這個主題沒有太多詳細的信息。唯一可靠的信息來源是安卓系統源碼。可以考慮訪問以下資源:
- https://android.googlesource.com/platform/frameworks/base/+/master/core/java/android/view/ViewConfiguration.java
- https://android.googlesource.com/platform/frameworks/uiautomator/+/refs/heads/master
- https://github.com/appium/appium-uiautomator2-server/tree/master/app/src/main/java/io/appium/uiautomator2/utils/w3c
- https://github.com/appium/appium-uiautomator2-server/tree/master/app/src/test/java/io/appium/uiautomator2/utils/w3c
- https://github.com/appium/appium-espresso-driver/tree/master/espresso-server/app/src/androidTest/java/io/appium/espressoserver/lib/helpers/w3c
- 關于TesterHome和MTSC
- 關于Appium
- 簡介
- Appium 客戶端
- 入門指南
- 已支持的平臺
- API 文檔
- Appium驅動
- XCUITest (iOS)
- XCUITest Real Devices (iOS)
- UIAutomation (iOS)
- UIAutomation Safari Launcher (iOS)
- UIAutomator (Android)
- UIAutomator2 (Android)
- Espresso (Android)
- Windows
- Mac
- Appium命令
- Status
- Execute Mobile Command
- Session
- Create
- End
- Get Session Capabilities
- Go Back
- Screenshot
- Source
- Timeouts
- Timeouts
- Implicit Wait
- Async Script
- Orientation
- Get Orientation
- Set Orientation
- Geolocation
- Get Geolocation
- Set Geolocation
- Logs
- Get Log Types
- Get Logs
- Events
- Log event
- Get events
- Settings
- Update Settings
- Get Device Settings
- Settings
- Update Settings
- Get Device Settings
- Execute Driver Script
- Device
- Activity
- Start Activity
- Current Activity
- Current Package
- App
- Install App
- Is App Installed
- Launch App
- Background App
- Close App
- Reset App
- Remove App
- Activate App
- Terminate App
- Get App State
- Get App Strings
- End Test Coverage
- Clipboard
- Get Clipboard
- Set Clipboard
- Emulator
- Power AC
- Power Capacity
- Files
- Push File
- Pull File
- Pull Folder
- Interactions
- Shake
- Lock
- Unlock
- Is Locked
- Rotate
- Keys
- Press keycode
- Long press keycode
- Hide Keyboard
- Is Keyboard Shown
- Network
- Toggle Airplane Mode
- Toggle Data
- Toggle WiFi
- Toggle Location Services
- Send SMS
- GSM Call
- GSM Signal
- GSM Voice
- Network Speed
- Performance Data
- Get Performance Data
- Performance Data Types
- Screen Recording
- Start Screen Recording
- Stop Screen Recording
- Simulator
- Perform Touch ID
- Toggle Touch ID Enrollment
- System
- Open Notifications
- System Bars
- System Time
- Display density
- Authentication
- Finger Print
- Element
- Find Element
- Find Elements
- Actions
- Click
- Send Keys
- Clear
- Attributes
- Text
- Name
- Attribute
- Selected
- Enabled
- Displayed
- Location
- Size
- Rect
- CSS Property
- Location in View
- Other
- Submit
- Active Element
- Equals Element
- Context
- Get Context
- Get All Contexts
- Set Context
- Interactions
- Mouse
- Move To
- Click
- Double Click
- Button Down
- Button Up
- Touch
- Single Tap
- Double Tap
- Move
- Touch Down
- Touch Up
- Long Press
- Scroll
- Flick
- Multi Touch Perform
- Touch Perform
- W3C Actions
- Web
- Window
- Set Window
- Close Window
- Get Handle
- Get Handles
- Get Title
- Get Window Size
- Set Window Size
- Get Window Position
- Set Window Position
- Maximize Window
- Navigation
- Go to URL
- Get URL
- Back
- Forward
- Refresh
- Storage
- Get All Cookies
- Set Cookie
- Delete Cookie
- Delete All Cookies
- Frame
- Switch to Frame
- Switch to Parent Frame
- Execute Async
- Execute
- 編寫 & 運行Appium腳本
- Running Tests
- Desired Capabilities
- The --default-capabilities flag
- Finding Elements
- Touch Actions
- CLI Arguments
- Server Security
- Web/Web Views
- Mobile Web Testing
- Automating Hybrid Apps
- Using ios-webkit-debug-proxy
- Using Chromedriver
- Image Comparison
- iOS
- Low-Level Insights on iOS Input Events
- XCUITest Mobile Gestures
- XCUITest Mobile App Management
- iOS Pasteboard Guide
- iOS Predicate Guide
- iOS Touch ID Guide
- iOS Install Certificate
- tvOS support
- Pushing/Pulling files
- Audio Capture
- Android
- Low-Level Insights on Android Input Events
- UiSelector Guide
- Espresso Datamatcher Guide
- Android Code Coverage Guide
- Activities Startup Troubleshooting Guide
- How To Execute Shell Commands On The Remote Device
- Android Device Screen Streaming
- How To Emulate IME Actions Generation
- How To Test Android App Bundle
- Other
- Reset Strategies
- Network Connection Guide
- Using Unicode with Appium
- Troubleshooting
- Tutorial
- Swipe Tutorial
- Screen
- Element
- Partial screen
- Simple
- Multiple scroll views
- Add scroll layout
- Tricks and Tips
- Screen
- Element
- Element search
- Fast
- Slow
- Guide
- 進階概念
- 定位圖像中的元素
- 使用定位元素的插件
- 遷移到 XCUITest
- 在 Appium 中使用 Selenium Grid
- Appium Logs Filtering
- 跨域 iframes
- 使用自定義 WDA 服務器
- 使用不同版本的 Xcode 運行
- The Event Timings API
- 并行測試的設置
- The Settings API
- Memory Collection
- 向Appium項目做貢獻
- 從源代碼運行 Appium
- 開發者概述
- 標準開發命令
- Appium 風格指南
- 如何編寫文檔
- Appium 包結構
- 鳴謝