Android测试教程(15):AndroidTestCase示例

jerry Android 2015年08月24日 收藏

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 的下個可以獲取焦點的控制項是否符合事先的設計。