Android測試教程(14):ActivityInstrumentationTestCase2樣本

來源:互聯網
上載者:User

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

ActivityInstrumentationTestCase2 也支援:

可以在UI線程中運行測試方法.

可以注入Intent對象到被測試的Activity中

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

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()); }     }

查看全套教程:http://www.bianceng.cn/OS/extra/201301/35252.htm

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.