package com.phicomm.hu;
import android.app.Application;
public class FxAndroidApplication extends Application
{
@Override
public void onCreate()
{
// TODO Auto-generated method stub
super.onCreate();
}
@Override
public void onTerminate()
{
// TODO Auto-generated method stub
super.onTerminate();
}
public String getFavourite()
{
return "I Love Java";
}
}
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.phicomm.hu.test" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="10" /> <instrumentation android:name="android.test.InstrumentationTestRunner" android:targetPackage="com.phicomm.hu" /> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" > <uses-library android:name="android.test.runner" /> </application> </manifest>
package com.phicomm.hu.test;
import com.phicomm.hu.FxAndroidApplication;
import android.app.Application;
import android.test.ApplicationTestCase;
public class FxApplicationTest extends ApplicationTestCase<FxAndroidApplication>
{
private FxAndroidApplication AppTest;
public FxApplicationTest()
{
//调用父类构造函数,且构造函中传递的参数为被测试的类
super(FxAndroidApplication.class);
}
@Override
protected void setUp() throws Exception
{
// TODO Auto-generated method stub
super.setUp();
//获取application之前必须调用的方法
createApplication();
//获取待测试的FxAndroidApplication
AppTest = getApplication();
}
//测试FxAndroidApplication的getFavourite方法
public void testGetFavourite()
{
/*验证预测值"I Love C++"是否等于实际值,
由于实际值为"I love Java",所以此处测试结果为Failure*/
assertEquals("I Love C++", AppTest.getFavourite());
}
}
package com.phicomm.hu;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class FxLoginActivity extends Activity
{
private EditText userName;
private EditText passWord;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
userName = (EditText)findViewById(R.id.name);
passWord = (EditText)findViewById(R.id.psd);
Button login = (Button)findViewById(R.id.login);
Button reset = (Button)findViewById(R.id.reset);
//监听登录按钮
login.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v)
{
// TODO Auto-generated method stub
Intent intent = new Intent(FxLoginActivity.this, FxResultActivity.class);
//通过intent传递登录信息到ResultActivity的界面中显示
intent.putExtra("userName", userName.getText().toString());
intent.putExtra("passWord", passWord.getText().toString());
//启动ResultActivity显示登录界面信息
startActivity(intent);
}
});
//监听重置按钮
reset.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
// TODO Auto-generated method stub
resetInput();
}
});
}
public void resetInput()
{
userName.setText("");
passWord.setText("");
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<EditText
android:id="@+id/name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="@string/name"/>
<EditText
android:id="@+id/psd"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="@string/psd"/>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<Button
android:id="@+id/login"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/login"/>
<Button
android:id="@+id/reset"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/reset"/>
</LinearLayout>
</LinearLayout>
package com.phicomm.hu;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.EditText;
import android.widget.TextView;
public class FxResultActivity extends Activity
{
private static final String TAG = "ResultActivity";
@Override
protected void onCreate(Bundle savedInstanceState)
{
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.result);
TextView result = (TextView)findViewById(R.id.result);
//通过得到intent获取登录界面传来的信息
Intent intent = getIntent();
String userName = intent.getStringExtra("userName");
String passWord = intent.getStringExtra("passWord");
//将登录信息在页面中显示
result.setText("用户名:" + userName + "\n" + "密码:" + passWord);
}
}
package com.phicomm.hu.test;
import android.app.Instrumentation;
import android.test.ActivityInstrumentationTestCase2;
import android.view.KeyEvent;
import android.widget.Button;
import android.widget.EditText;
import com.phicomm.hu.FxLoginActivity;
public class FxLoginActivityTest extends ActivityInstrumentationTestCase2<FxLoginActivity>
{
private Instrumentation mInstrumentation;
private FxLoginActivity mLoginTest;
private EditText userName;
private EditText passWord;
private Button login;
private Button reset;
public FxLoginActivityTest()
{
super(FxLoginActivity.class);
}
//重写setUp方法,在该方法中进行相关的初始化操作
@Override
protected void setUp() throws Exception
{
// TODO Auto-generated method stub
super.setUp();
/**这个程序中需要输入用户信息和密码,也就是说需要发送key事件,
* 所以,必须在调用getActivity之前,调用下面的方法来关闭
* touch模式,否则key事件会被忽略
*/
//关闭touch模式
setActivityInitialTouchMode(false);
mInstrumentation = getInstrumentation();
//获取被测试的FxLoginActivity
mLoginTest = getActivity();
//获取FxLoginActivity相关的UI组件
userName = (EditText)mLoginTest.findViewById(com.phicomm.hu.R.id.name);
passWord = (EditText)mLoginTest.findViewById(com.phicomm.hu.R.id.psd);
login = (Button)mLoginTest.findViewById(com.phicomm.hu.R.id.login);
reset = (Button)mLoginTest.findViewById(com.phicomm.hu.R.id.reset);
}
//该测试用例实现在测试其他用例之前,测试确保获取的组件不为空
public void testPreConditions()
{
assertNotNull(mLoginTest);
assertNotNull(userName);
assertNotNull(passWord);
assertNotNull(login);
assertNotNull(reset);
}
/**该方法实现在登录界面上输入相关的登录信息。由于UI组件的
* 相关处理(如此处的请求聚焦)需要在UI线程上实现,
* 所以需调用Activity的runOnUiThread方法实现。
*/
public void input()
{
mLoginTest.runOnUiThread(new Runnable()
{
@Override
public void run()
{
// TODO Auto-generated method stub
userName.requestFocus();
userName.performClick();
}
});
/*由于测试用例在单独的线程上执行,所以此处需要同步application,
* 调用waitForIdleSync等待测试线程和UI线程同步,才能进行输入操作。
* waitForIdleSync和sendKeys不允许在UI线程里运行
*/
mInstrumentation.waitForIdleSync();
//调用sendKeys方法,输入用户名
sendKeys(KeyEvent.KEYCODE_P, KeyEvent.KEYCODE_H,
KeyEvent.KEYCODE_I, KeyEvent.KEYCODE_C,
KeyEvent.KEYCODE_O, KeyEvent.KEYCODE_M,
KeyEvent.KEYCODE_M);
mLoginTest.runOnUiThread(new Runnable()
{
@Override
public void run()
{
// TODO Auto-generated method stub
passWord.requestFocus();
passWord.performClick();
}
});
//调用sendKeys方法,输入密码
sendKeys(KeyEvent.KEYCODE_1, KeyEvent.KEYCODE_2,
KeyEvent.KEYCODE_3, KeyEvent.KEYCODE_4);
}
//测试输入的用户信息
public void testInput()
{
//调用测试类的input方法,实现输入用户信息(sendKeys实现输入)
input();
//测试验证用户信息的预期值是否等于实际值
assertEquals("phicomm", userName.getText().toString());
//密码的预期值123与实际值1234不符,Failure;
assertEquals("123", passWord.getText().toString());
}
//测试登录按钮
public void testLogin()
{
input();
//开新线程,并通过该线程在实现在UI线程上执行操作
mInstrumentation.runOnMainSync(new Runnable()
{
@Override
public void run()
{
// TODO Auto-generated method stub
login.requestFocus();
login.performClick();
}
});
}
//测试重置按钮
public void testReset()
{
input();
mInstrumentation.runOnMainSync(new Runnable()
{
@Override
public void run()
{
// TODO Auto-generated method stub
reset.requestFocus();
//点击按钮
reset.performClick();
}
});
//验证重置按钮的实现功能,是否点击后内容为空
assertEquals("", userName.getText().toString());
assertEquals("", passWord.getText().toString());
}
}
package com.phicomm.hu.test;
import android.content.Intent;
import android.test.ActivityInstrumentationTestCase2;
import android.widget.TextView;
import com.phicomm.hu.FxResultActivity;
public class FxResultActivityTest extends ActivityInstrumentationTestCase2<FxResultActivity>
{
private static final String LOGIN_INFO = "用户名:feixun\n密码:123";
private FxResultActivity mResultActivity;
private TextView result;
public FxResultActivityTest()
{
super(FxResultActivity.class);
}
@Override
protected void setUp() throws Exception
{
// TODO Auto-generated method stub
super.setUp();
//创建Intent,通过Intent传递用户的登录信息
Intent intent = new Intent();
intent.putExtra("userName", "feixun");
intent.putExtra("passWord", "123");
//通过携带用户登录信息的intent启动FxResultActivity
mResultActivity = launchActivityWithIntent("com.phicomm.hu",
FxResultActivity.class, intent);
//获取UI组件
result = (TextView)mResultActivity.findViewById(com.phicomm.hu.R.id.result);
}
//测试验证用户的登录信息
public void testLoginInfo()
{
//验证预期值是否等于实际值
assertEquals(LOGIN_INFO, result.getText().toString());
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有