> 編寫:[huanglizhuo](https://github.com/huanglizhuo) - 原文:[http://developer.android.com/training/](http://developer.android.com/training/activity-testing/activity-ui-testing.html)[activity](# "An activity represents a single screen with a user interface.")-testing/[activity](# "An activity represents a single screen with a user interface.")-ui-testing.html
通常情況下,[Activity](# "An activity represents a single screen with a user interface."),包括用戶界面組件(如按鈕,復選框,可編輯的文本域,和選框)允許用戶與Android應用程序交互。本節介紹如何對一個簡單的帶有按鈕的界面交互測試。我們可以使用相同的步驟來測試其他更復雜的UI組件。
> **注意**: 這一節的測試方法叫做白盒測試,因為我們擁有要測試應用程序的源碼。Android Instrumentation框架適用于創建應用程序中UI部件的白盒測試。用戶界面測試的另一種類型是黑盒測試,即無法得知應用程序源代碼的類型。這種類型的測試可以用來測試應用程序如何與其他應用程序,或與系統進行交互。黑盒測試不包括在本節中。了解更多關于如何在你的Android應用程序進行黑盒測試,請閱讀[UI Testing guide](http://developer.android.com/tools/testing/testing_ui.html)。
要參看完整的測試案例,可以查看本節示例代碼中的`ClickFunActivityTest.java`文件。
### 使用 Instrumentation 建立UI測試
當測試擁有UI的[Activity](# "An activity represents a single screen with a user interface.")時,被測試的[Activity](# "An activity represents a single screen with a user interface.")在UI線程中運行。然而,測試程序會在程序自己的進程中,單獨的一個線程內運行。這意味著,我們的測試程序可以獲得UI線程的對象,但是如果它嘗試改變UI線程對象的值,會得到`WrongThreadException`錯誤。
為了安全地將`Intent`注入到`Activity`,或是在UI線程中執行測試方法,我們可以讓測試類繼承于[ActivityInstrumentationTestCase2](http://developer.android.com/reference/android/test/ActivityInstrumentationTestCase2.html)。要學習如何在UI線程運行測試方法,請看[在UI線程測試](http://developer.android.com/tools/testing/activity_testing.html#RunOnUIThread)。
### 建立測試數據集(Fixture)
當為UI測試建立測試數據集時,我們應該在[setUp()](http://developer.android.com/reference/junit/framework/TestCase.html#setUp())方法中指定[touch mode](http://developer.android.com/guide/topics/ui/ui-events.html#TouchMode)。把touch mode設置為真可以防止在執行編寫的測試方法時,人為的UI操作獲取到控件的焦點(比如,一個按鈕會觸發它的點擊監聽器)。確保在調用[getActivity()](http://developer.android.com/reference/android/test/ActivityInstrumentationTestCase2.html#getActivity())方法前調用了[setActivityInitialTouchMode](http://developer.android.com/reference/android/test/ActivityInstrumentationTestCase2.html#setActivityInitialTouchMode(boolean))。
比如:
~~~
public class ClickFunActivityTest
extends ActivityInstrumentationTestCase2 {
...
@Override
protected void setUp() throws Exception {
super.setUp();
setActivityInitialTouchMode(true);
mClickFunActivity = getActivity();
mClickMeButton = (Button)
mClickFunActivity
.findViewById(R.id.launch_next_activity_button);
mInfoTextView = (TextView)
mClickFunActivity.findViewById(R.id.info_text_view);
}
}
~~~
### 添加測試方法確認UI響應表現
UI測試目標應包括:
_. 檢驗[Activity](# "An activity represents a single screen with a user interface.")啟動時[Button](http://developer.android.com/reference/android/widget/Button.html)在正確布局位置顯示。_. 檢驗[TextView](http://developer.android.com/reference/android/widget/TextView.html)初始化時是隱藏的。*. 檢驗[TextView](http://developer.android.com/reference/android/widget/TextView.html)在[Button](http://developer.android.com/reference/android/widget/Button.html)點擊時顯示預期的字符串
接下來的部分會演示怎樣實現上述驗證方法
### 驗證Button布局參數
我們應該像如下添加的測試方法那樣。驗證[Activity](# "An activity represents a single screen with a user interface.")中的按鈕是否正確顯示:
~~~
@MediumTest
public void testClickMeButton_layout() {
final View decorView = mClickFunActivity.getWindow().getDecorView();
ViewAsserts.assertOnScreen(decorView, mClickMeButton);
final ViewGroup.LayoutParams layoutParams =
mClickMeButton.getLayoutParams();
assertNotNull(layoutParams);
assertEquals(layoutParams.width, WindowManager.LayoutParams.MATCH_PARENT);
assertEquals(layoutParams.height, WindowManager.LayoutParams.WRAP_CONTENT);
}
~~~
在調用[assertOnScreen()](http://developer.android.com/reference/android/test/ViewAsserts.html#assertOnScreen(android.view.View, android.view.View))方法時,傳遞根視圖以及期望呈現在屏幕上的視圖作為參數。如果想呈現的視圖沒有在根視圖中,該方法會拋出一個[AssertionFailedError](http://developer.android.com/reference/junit/framework/AssertionFailedError.html)異常,否則測試通過。
我們也可以通過獲取一個[ViewGroup.LayoutParams](http://developer.android.com/reference/android/view/ViewGroup.LayoutParams.html)對象的引用驗證[Button](http://developer.android.com/reference/android/widget/Button.html)布局是否正確,然后調用`assert`方法驗證[Button](http://developer.android.com/reference/android/widget/Button.html)對象的寬高屬性值是否與預期值一致。
`@MediumTest`注解指定測試是如何歸類的(和它的執行時間相關)。要了解更多有關測試的注解,見本節示例。
### 驗證TextView的布局參數
可以像這樣添加一個測試方法來驗證[TextView](http://developer.android.com/reference/android/widget/TextView.html)最初是隱藏在[Activity](# "An activity represents a single screen with a user interface.")中的:
~~~
@MediumTest
public void testInfoTextView_layout() {
final View decorView = mClickFunActivity.getWindow().getDecorView();
ViewAsserts.assertOnScreen(decorView, mInfoTextView);
assertTrue(View.GONE == mInfoTextView.getVisibility());
}
~~~
我們可以調用`getDecorView()`方法得到一個[Activity](# "An activity represents a single screen with a user interface.")中修飾試圖(Decor View)的引用。要修飾的View在布局層次視圖中是最上層的ViewGroup([FrameLayout](http://developer.android.com/reference/android/widget/FrameLayout.html))
### 驗證按鈕的行為
可以使用如下測試方法來驗證當按下按鈕時[TextView](http://developer.android.com/reference/android/widget/TextView.html)變得可見:
~~~
@MediumTest
public void testClickMeButton_clickButtonAndExpectInfoText() {
String expectedInfoText = mClickFunActivity.getString(R.string.info_text);
TouchUtils.clickView(this, mClickMeButton);
assertTrue(View.VISIBLE == mInfoTextView.getVisibility());
assertEquals(expectedInfoText, mInfoTextView.getText());
}
~~~
在測試中調用[clickView()](http://developer.android.com/reference/android/test/TouchUtils.html#clickView(android.test.InstrumentationTestCase, android.view.View))可以讓我們用編程方式點擊一個按鈕。我們必須傳遞正在運行的測試用例的一個引用和要操作按鈕的引用。
> **注意**:[TouchUtils](http://developer.android.com/reference/android/test/TouchUtils.html)輔助類提供與應用程序交互的方法可以方便進行模擬觸摸操作。我們可以使用這些方法來模擬點擊,輕敲,或應用程序屏幕拖動View。
> **警告**[TouchUtils](http://developer.android.com/reference/android/test/TouchUtils.html)方法的目的是將事件安全地從測試線程發送到UI線程。我們不可以直接在UI線程或任何標注@UIThread的測試方法中使用[TouchUtils](http://developer.android.com/reference/android/test/TouchUtils.html)這樣做可能會增加錯誤線程異常。
### 應用測試注解
[@SmallTest](http://developer.android.com/reference/android/test/suitebuilder/annotation/SmallTest.html)
~~~
標志該測試方法是小型測試的一部分。
~~~
[@MediumTest](http://developer.android.com/reference/android/test/suitebuilder/annotation/MediumTest.html)
~~~
標志該測試方法是中等測試的一部分。
~~~
[@LargeTest](http://developer.android.com/reference/android/test/suitebuilder/annotation/LargeTest.html)
~~~
標志該測試方法是大型測試的一部分。
~~~
通常情況下,如果測試方法只需要幾毫秒的時間,那么它應該被標記為[@SmallTest](http://developer.android.com/reference/android/test/suitebuilder/annotation/SmallTest.html),長時間運行的測試(100毫秒或更多)通常被標記為[@MediumTest](http://developer.android.com/reference/android/test/suitebuilder/annotation/MediumTest.html)或[@LargeTest](http://developer.android.com/reference/android/test/suitebuilder/annotation/LargeTest.html),這主要取決于測試訪問資源在網絡上或在本地系統。 可以參看[Android Tools Protip](https://plus.google.com/+AndroidDevelopers/posts/TPy1EeSaSg8),它可以更好地指導我們使用測試注釋
我們可以創建其它的測試注釋來控制測試的組織和運行。要了解更多關于其他注釋的信息,見[Annotation](http://developer.android.com/reference/java/lang/annotation/Annotation.html)類參考。
本節示例代碼[AndroidTestingFun.zip](http://developer.android.com/shareables/training/AndroidTestingFun.zip)
- 序言
- Android入門基礎:從這里開始
- 建立第一個App
- 創建Android項目
- 執行Android程序
- 建立簡單的用戶界面
- 啟動其他的Activity
- 添加ActionBar
- 建立ActionBar
- 添加Action按鈕
- 自定義ActionBar的風格
- ActionBar的覆蓋層疊
- 兼容不同的設備
- 適配不同的語言
- 適配不同的屏幕
- 適配不同的系統版本
- 管理Activity的生命周期
- 啟動與銷毀Activity
- 暫停與恢復Activity
- 停止與重啟Activity
- 重新創建Activity
- 使用Fragment建立動態的UI
- 創建一個Fragment
- 建立靈活動態的UI
- Fragments之間的交互
- 數據保存
- 保存到Preference
- 保存到文件
- 保存到數據庫
- 與其他應用的交互
- Intent的發送
- 接收Activity返回的結果
- Intent過濾
- Android分享操作
- 分享簡單的數據
- 給其他App發送簡單的數據
- 接收從其他App返回的數據
- 給ActionBar增加分享功能
- 分享文件
- 建立文件分享
- 分享文件
- 請求分享一個文件
- 獲取文件信息
- 使用NFC分享文件
- 發送文件給其他設備
- 接收其他設備的文件
- Android多媒體
- 管理音頻播放
- 控制音量與音頻播放
- 管理音頻焦點
- 兼容音頻輸出設備
- 拍照
- 簡單的拍照
- 簡單的錄像
- 控制相機硬件
- 打印
- 打印照片
- 打印HTML文檔
- 打印自定義文檔
- Android圖像與動畫
- 高效顯示Bitmap
- 高效加載大圖
- 非UI線程處理Bitmap
- 緩存Bitmap
- 管理Bitmap的內存
- 在UI上顯示Bitmap
- 使用OpenGL ES顯示圖像
- 建立OpenGL ES的環境
- 定義Shapes
- 繪制Shapes
- 運用投影與相機視圖
- 添加移動
- 響應觸摸事件
- 添加動畫
- View間漸變
- 使用ViewPager實現屏幕側滑
- 展示卡片翻轉動畫
- 縮放View
- 布局變更動畫
- Android網絡連接與云服務
- 無線連接設備
- 使得網絡服務可發現
- 使用WiFi建立P2P連接
- 使用WiFi P2P服務
- 執行網絡操作
- 連接到網絡
- 管理網絡
- 解析XML數據
- 高效下載
- 為網絡訪問更加高效而優化下載
- 最小化更新操作的影響
- 避免下載多余的數據
- 根據網絡類型改變下載模式
- 云同步
- 使用備份API
- 使用Google Cloud Messaging
- 解決云同步的保存沖突
- 使用Sync Adapter傳輸數據
- 創建Stub授權器
- 創建Stub Content Provider
- 創建Sync Adpater
- 執行Sync Adpater
- 使用Volley執行網絡數據傳輸
- 發送簡單的網絡請求
- 建立請求隊列
- 創建標準的網絡請求
- 實現自定義的網絡請求
- Android聯系人與位置信息
- Android聯系人信息
- 獲取聯系人列表
- 獲取聯系人詳情
- 使用Intents修改聯系人信息
- 顯示聯系人頭像
- Android位置信息
- 獲取最后可知位置
- 獲取位置更新
- 顯示位置地址
- 創建和監視地理圍欄
- Android可穿戴應用
- 賦予Notification可穿戴特性
- 創建Notification
- 在Notifcation中接收語音輸入
- 為Notification添加顯示頁面
- 以Stack的方式顯示Notifications
- 創建可穿戴的應用
- 創建并運行可穿戴應用
- 創建自定義的布局
- 添加語音功能
- 打包可穿戴應用
- 通過藍牙進行調試
- 創建自定義的UI
- 定義Layouts
- 創建Cards
- 創建Lists
- 創建2D-Picker
- 創建確認界面
- 退出全屏的Activity
- 發送并同步數據
- 訪問可穿戴數據層
- 同步數據單元
- 傳輸資源
- 發送與接收消息
- 處理數據層的事件
- Android TV應用
- 創建TV應用
- 創建TV應用的第一步
- 處理TV硬件部分
- 創建TV的布局文件
- 創建TV的導航欄
- 創建TV播放應用
- 創建目錄瀏覽器
- 提供一個Card視圖
- 創建詳情頁
- 顯示正在播放卡片
- 幫助用戶在TV上探索內容
- TV上的推薦內容
- 使得TV App能夠被搜索
- 使用TV應用進行搜索
- 創建TV游戲應用
- 創建TV直播應用
- TV Apps Checklist
- Android企業級應用
- Ensuring Compatibility with Managed Profiles
- Implementing App Restrictions
- Building a Work Policy Controller
- Android交互設計
- 設計高效的導航
- 規劃屏幕界面與他們之間的關系
- 為多種大小的屏幕進行規劃
- 提供向下和橫向導航
- 提供向上和歷史導航
- 綜合:設計樣例 App
- 實現高效的導航
- 使用Tabs創建Swipe視圖
- 創建抽屜導航
- 提供向上的導航
- 提供向后的導航
- 實現向下的導航
- 通知提示用戶
- 建立Notification
- 當啟動Activity時保留導航
- 更新Notification
- 使用BigView風格
- 顯示Notification進度
- 增加搜索功能
- 建立搜索界面
- 保存并搜索數據
- 保持向下兼容
- 使得你的App內容可被Google搜索
- 為App內容開啟深度鏈接
- 為索引指定App內容
- Android界面設計
- 為多屏幕設計
- 兼容不同的屏幕大小
- 兼容不同的屏幕密度
- 實現可適應的UI
- 創建自定義View
- 創建自定義的View類
- 實現自定義View的繪制
- 使得View可交互
- 優化自定義View
- 創建向后兼容的UI
- 抽象新的APIs
- 代理至新的APIs
- 使用舊的APIs實現新API的效果
- 使用版本敏感的組件
- 實現輔助功能
- 開發輔助程序
- 開發輔助服務
- 管理系統UI
- 淡化系統Bar
- 隱藏系統Bar
- 隱藏導航Bar
- 全屏沉浸式應用
- 響應UI可見性的變化
- 創建使用Material Design的應用
- 開始使用Material Design
- 使用Material的主題
- 創建Lists與Cards
- 定義Shadows與Clipping視圖
- 使用Drawables
- 自定義動畫
- 維護兼容性
- Android用戶輸入
- 使用觸摸手勢
- 檢測常用的手勢
- 跟蹤手勢移動
- Scroll手勢動畫
- 處理多觸摸手勢
- 拖拽與縮放
- 管理ViewGroup中的觸摸事件
- 處理鍵盤輸入
- 指定輸入法類型
- 處理輸入法可見性
- 兼容鍵盤導航
- 處理按鍵動作
- 兼容游戲控制器
- 處理控制器輸入動作
- 支持不同的Android系統版本
- 支持多個控制器
- Android后臺任務
- 在IntentService中執行后臺任務
- 創建IntentService
- 發送工作任務到IntentService
- 報告后臺任務執行狀態
- 使用CursorLoader在后臺加載數據
- 使用CursorLoader執行查詢任務
- 處理查詢的結果
- 管理設備的喚醒狀態
- 保持設備的喚醒
- 制定重復定時的任務
- Android性能優化
- 管理應用的內存
- 代碼性能優化建議
- 提升Layout的性能
- 優化layout的層級
- 使用include標簽重用layouts
- 按需加載視圖
- 使得ListView滑動順暢
- 優化電池壽命
- 監測電量與充電狀態
- 判斷與監測Docking狀態
- 判斷與監測網絡連接狀態
- 根據需要操作Broadcast接受者
- 多線程操作
- 在一個線程中執行一段特定的代碼
- 為多線程創建線程池
- 啟動與停止線程池中的線程
- 與UI線程通信
- 避免出現程序無響應ANR
- JNI使用指南
- 優化多核處理器(SMP)下的Android程序
- Android安全與隱私
- Security Tips
- 使用HTTPS與SSL
- 為防止SSL漏洞而更新Security
- 使用設備管理條例增強安全性
- Android測試程序
- 測試你的Activity
- 建立測試環境
- 創建與執行測試用例
- 測試UI組件
- 創建單元測試
- 創建功能測試
- 術語表