在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 教程/ Android/ Android 測試教程(14):ActivityInstrumentationTestCase2 示例
monkeyrunner 簡介
Android 測試教程(14):ActivityInstrumentationTestCase2 示例
ApplicationTestCase 示例
Android 測試教程(13):TestCase 示例
ActivityInstrumentationTestCase2 示例
測試 Activity
測試項(xiàng)目
Android 測試教程(12):ServiceTestCase 示例
測試相關(guān) API
Monkey 命令行工具
第一個測試項(xiàng)目 HelloWorldTest
測試 Content Provider
Android 測試教程(11):ActivityUnitTestCase 示例
測試基礎(chǔ)
AndroidTestCase 示例
測試 Service

Android 測試教程(14):ActivityInstrumentationTestCase2 示例

ActivityInstrumentationTestCase2 用來測試單個的 Activity,被測試的 Activity 可以使用 InstrumentationTestCase.launchActivity 來啟動,然後你能夠直接操作被測試的 Activity。

ActivityInstrumentationTestCase2 也支持:

  • 可以在 UI 線程中運(yùn)行測試方法.
  • 可以注入 Intent 對象到被測試的 Activity 中

ActivityInstrumentationTestCase2 取代之前的 ActivityInstrumentationTestCase ,新的測試應(yīng)該使用 ActivityInstrumentationTestCase2 作為基類。

http://wiki.jikexueyuan.com/project/android-test-course/images/14.1.jpg" alt="picture14.1" />

Focus2ActivityTest 的代碼如下,用於測試 Android ApiDemos 示例解析(116):Views->Focus->2. Horizontal


    public class Focus2ActivityTest
     extends ActivityInstrumentationTestCase2<Focus2> {

     private Button mLeftButton;
     private Button mCenterButton;
     private Button mRightButton;

     public Focus2ActivityTest() {
     super("com.example.android.apis", Focus2.class);
     }

     @Override
     protected void setUp() throws Exception {
     super.setUp();
     final Focus2 a = getActivity();
     mLeftButton = (Button) a.findViewById(R.id.leftButton);
     mCenterButton = (Button) a.findViewById(R.id.centerButton);
     mRightButton = (Button) a.findViewById(R.id.rightButton);
     }

     @MediumTest
     public void testPreconditions() {
     assertTrue("center button should be right of left button",
     mLeftButton.getRight() < mCenterButton.getLeft());
     assertTrue("right button should be right of center button",
     mCenterButton.getRight() < mRightButton.getLeft());
     assertTrue("left button should be focused", mLeftButton.isFocused());
     }

     @MediumTest
     public void testGoingRightFromLeftButtonJumpsOverCenterToRight() {
     sendKeys(KeyEvent.KEYCODE_DPAD_RIGHT);
     assertTrue("right button should be focused", mRightButton.isFocused());
     }

     @MediumTest
     public void testGoingLeftFromRightButtonGoesToCenter()  {

     getActivity().runOnUiThread(new Runnable() {
     public void run() {
     mRightButton.requestFocus();
     }
     });
     // wait for the request to go through
     getInstrumentation().waitForIdleSync();

     assertTrue(mRightButton.isFocused());

     sendKeys(KeyEvent.KEYCODE_DPAD_LEFT);
     assertTrue("center button should be focused",
     mCenterButton.isFocused());
     }
    }

etUp 中初始化 mLeftButton,mCenterButton 和 mRightButton,調(diào)用每個測試方法之前,setUp 都會被調(diào)用。

testPreconditions 通常為第一個測試方法,用來檢測後續(xù)的測試環(huán)境是否符合條件。

testGoingRightFromLeftButtonJumpsOverCenterToRight 中調(diào)用 sendKeys 可以模擬按鍵消息。

testGoingLeftFromRightButtonGoesToCenter 中 ,使用 runOnUiThread 來為mRightButton 請求 focus ,使用 runOnUiThread 的原因是因?yàn)楸緶y試方法不在 UI 線程中運(yùn)行。 getInstrumentation 可以取得 Instrumentation 對象,有了 Instrumentation 對象就可以對 Activity 進(jìn)行大部分的操作,waitForIdleSync() 等待 application 回到 idle 狀態(tài),之後就可以檢測 mRightButton 是否獲得了焦點(diǎn)。

Tags: Android 測試