編輯:Android開發教程
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
iOS 7與Android 4.3的功能對比
一直以來,關於蘋果iOS和谷歌Android誰更好用的爭論從來沒有間斷過,它們不僅代表了世界上兩個最先進、最受歡迎的移動平台,同時也是蘋果和谷歌兩家科技巨頭品味、風格的不
Android版Outlook.com增強了服務器端搜索功能
微軟已經推送了Android版Outlook.com軟件的更新。v7.8.2版Outlook.com此次帶來最大的變化就是加入了全新的服務器端搜索功能。換句話說,用戶即便
Android Fragment完全解析,關於碎片你所需知道的一切
我們都知道,Android上的界面展示都是通過Activity實現的,Activity實在是太常用了,我相信大家都 已經非常熟悉了,這裡就不再贅述。但是Activity也
Android系統修改版:CyanogenMod 10穩定版現已推出
CyanogenMod 10(CM10),最新基於Android 4.1果凍豆版本的ROM,在今天對少數設備推出。版本10已經在過去的數月經歷了無數個夜晚的beta開發,