Android測試教程(15):AndroidTestCase樣本

來源:互聯網
上載者:User

AndroidTestCase 為一Android平台下通用的測試類別,它支援所有JUnit的Assert方法和標準的setUp 和tearDown 方法。如果 你的測試需要訪問應用的資源或者測試方法依賴於Context,可以使用AndroidTestCase 作為基類。

它的類繼承關係如下 圖所示:

Focus2AndroidTest 測試也是Android  ApiDemos樣本解析(116):Views->Focus->2. Horizontal

但測試的側重點不一樣,Focus2AndroidTest 測試的內容無需啟動Activity,而是測試R.layout.focus_2 的布局(資源)中 Focus 的順序是否符合預先設計(可以看作是Activity的一些靜態效能),可以通過FocusFinder的方法來測試Focus的一些靜態 屬性,它的代碼如下:

public class Focus2AndroidTest      extends AndroidTestCase {      private FocusFinder mFocusFinder;                private ViewGroup mRoot;                private Button mLeftButton;      private Button mCenterButton;      private Button mRightButton;                @Override  protected void setUp() throws Exception {      super.setUp();                mFocusFinder = FocusFinder.getInstance();                // inflate the layout      final Context context = getContext();      final LayoutInflater inflater = LayoutInflater.from(context);      mRoot = (ViewGroup) inflater.inflate(R.layout.focus_2, null);                // manually measure it, and lay it out      mRoot.measure(500, 500);      mRoot.layout(0, 0, 500, 500);                mLeftButton = (Button) mRoot.findViewById(R.id.leftButton);      mCenterButton = (Button) mRoot.findViewById(R.id.centerButton);      mRightButton = (Button) mRoot.findViewById(R.id.rightButton);      }                @SmallTest  public void testPreconditions() {      assertNotNull(mLeftButton);      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());      }                @SmallTest  public void testGoingRightFromLeftButtonJumpsOverCenterToRight() {      assertEquals("right should be next focus from left",      mRightButton,      mFocusFinder.findNextFocus(mRoot, mLeftButton,      View.FOCUS_RIGHT));      }                @SmallTest  public void testGoingLeftFromRightButtonGoesToCenter() {      assertEquals("center should be next focus from right",      mCenterButton,      mFocusFinder.findNextFocus(mRoot, mRightButton,      View.FOCUS_LEFT));      }     }

testGoingRightFromLeftButtonJumpsOverCenterToRight 和 testGoingLeftFromRightButtonGoesToCenter

通過mFocusFinder 的 findNextFocus 來測試 mLeftButton,mRightButton 的下個可以擷取焦點的控制項是否符合事先的設計。

查看全套教程: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.