Android测试框架初步-程序员宅基地

技术标签: 移动开发  

一、实验目的

1.掌握android测试项目的建立

2.掌握android测试框架的基本内容

3.编写运行android测试

二、实验内容与步骤

 建立android项目MyProject,运行截图如下:

l  点击ok按钮,EditText内字母变大写

l  点击超链接,打开浏览器上网

请用知识对本项目进行测试,要求:

1、对组件进行对齐测试(assertOnScreen和assertRightAligned方法)

2、对EditText进行传值测试(使用sendKeys 和 sendRepeatedKeys两种方法)

3、对Button进行功能测试(performClick和sendKeys方法)

4、对超链接进行测试(ActivityMonitor内部类)

5、为了保证测试的完整性和准确性,请适当添加必要的功能(如先决条件,多种方法等)

注:建立android测试项目过程如下

1、新建android测试项目

2、建立好测试项目之后,在测试项目中的src目录下,右键点击你创建的包,依次选择新建—>JUnit Test Case,弹出如下界面:

 

 
//代码
package com.sise.zhw;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class MyProjectActivity extends Activity {
    /** Called when the activity is first created. */
	private EditText mMessage;
	private Button mok;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mMessage=(EditText)findViewById(R.id.message);
        mok=(Button)findViewById(R.id.ok);
        mok.setOnClickListener(new OnClickListener()
		{
			
			public void onClick(View v)
			{
				// TODO Auto-generated method stub
				mMessage.setText(mMessage.getText().
						toString().toUpperCase());
			}
		});
    }
}

//布局文件

<?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" >
<ImageView 
    android:layout_width="wrap_content"
    android:id="@+id/imageView1"
    android:layout_height="wrap_content"
    android:src="@drawable/ic_launcher"
    android:layout_marginBottom="6dip"
    android:layout_gravity="center_horizontal"
    android:layout_marginTop="20dip"
    />
<TextView 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="MyFirstProjectTest"
    android:layout_gravity="center"
    />
<TextView 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="www.sise.com.cn"
    android:layout_gravity="center"
    android:autoLink="web" 
    android:id="@+id/link"
    android:textSize="18sp"
    />
<EditText 
    android:layout_height="wrap_content"
    android:layout_width="fill_parent"
    android:layout_marginBottom="0dip"
    android:layout_marginLeft="6dip"
    android:layout_marginRight="6dip"
    android:layout_marginTop="24dip"
    android:hint="sise"
    android:id="@+id/message"
    android:maxLines="1"
    />
<Button 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="right"
    android:layout_margin="6dip"
    android:paddingLeft="24dip"
    android:paddingRight="24dip"
    android:text="ok"
    android:id="@+id/ok"
    />
</LinearLayout>

  

//测试代码
package com.sise.zhw.test;

import static android.test.MoreAsserts.assertNotContainsRegex;
import static android.test.ViewAsserts.assertOnScreen;
import static android.test.ViewAsserts.assertRightAligned;

import com.sise.zhw.MyProjectActivity;


import android.app.Instrumentation;
import android.app.Instrumentation.ActivityMonitor;
import android.content.Intent;
import android.content.IntentFilter;
import android.test.ActivityInstrumentationTestCase2;
import android.test.TouchUtils;
import android.test.UiThreadTest;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;


public class MyFirstProjectActivityTests extends
		ActivityInstrumentationTestCase2<MyProjectActivity> {
	private MyProjectActivity mActivity;
	private EditText mMessage;
	private Button mCapitalize;
	private TextView mLink;
	private Instrumentation mInstrumentation;
	public MyFirstProjectActivityTests() {
		this("MyFirstProjectActivityTests");
	}
	public MyFirstProjectActivityTests(String name) {
		super(MyProjectActivity.class);
		setName(name);
	}
	protected void setUp() throws Exception {
		super.setUp();
		setActivityInitialTouchMode(false);		
		mActivity = getActivity();
		mInstrumentation = getInstrumentation();
		mLink = (TextView)mActivity.findViewById(com.sise.zhw.R.id.link);
		mMessage = (EditText)mActivity.findViewById(com.sise.zhw.R.id.message);
		mCapitalize = (Button)mActivity.findViewById(com.sise.zhw.R.id.ok);
	}
	protected void tearDown() throws Exception {
		super.tearDown();
	}
	public void testPreConditions() {
		assertNotNull(mActivity);
		assertNotNull(mInstrumentation);
		assertNotNull(mLink);
		assertNotNull(mMessage);
		assertNotNull(mCapitalize);
	}
	public void testAlignment() {
		final int margin = 0;
		assertRightAligned(mMessage, mCapitalize, margin);
	}
	public void testUserInterfaceLayout() {
		final int margin = 0;
		final View origin = mActivity.getWindow().getDecorView();
		assertOnScreen(origin, mMessage);
		assertOnScreen(origin, mCapitalize);
		assertRightAligned(mMessage, mCapitalize, margin);
	}
	public void testUserInterfaceLayoutWithOtherOrigin() {
		final int margin = 0;
		View origin = mMessage.getRootView();
		assertOnScreen(origin, mMessage);
		origin = mCapitalize.getRootView();
		assertOnScreen(origin, mCapitalize);
		assertRightAligned(mMessage, mCapitalize, margin);
	}
	@UiThreadTest
	public void testNoErrorInCapitalization() {
		final String msg = "this is a sample";
		mMessage.setText(msg);
		mCapitalize.performClick();
		final String actual = mMessage.getText().toString();
		final String notExpectedRegexp = "(?i:ERROR)";
		assertNotContainsRegex("Capitalization found error:",
                notExpectedRegexp, actual);
	}
	public void testFollowLink() {
		final Instrumentation inst = getInstrumentation();
		IntentFilter intentFilter = new IntentFilter(Intent.ACTION_VIEW);
		intentFilter.addDataScheme("http");
		intentFilter.addCategory(Intent.CATEGORY_BROWSABLE);
		ActivityMonitor monitor = inst.addMonitor(intentFilter, null, false);
		assertEquals(0, monitor.getHits());
		TouchUtils.clickView(this, mLink);
		monitor.waitForActivityWithTimeout(5000);
		assertEquals(1, monitor.getHits());
		inst.removeMonitor(monitor);
	}
	
	private void requestMessageFocus() {
		try {
			runTestOnUiThread(new Runnable() {
				public void run() {
					mMessage.requestFocus();
				}
			});
		} catch (Throwable e) {
			fail("Couldn't set focus");
		}
		mInstrumentation.waitForIdleSync();
	}
	
	public void testSendKeyInts() {
		requestMessageFocus();
		sendKeys(KeyEvent.KEYCODE_H,
				KeyEvent.KEYCODE_E,
				KeyEvent.KEYCODE_E,
				KeyEvent.KEYCODE_E,
				KeyEvent.KEYCODE_Y,
				KeyEvent.KEYCODE_ALT_LEFT,
				KeyEvent.KEYCODE_1,
				KeyEvent.KEYCODE_DPAD_DOWN,
				KeyEvent.KEYCODE_ENTER);
		final String expected = "HEEEY!";
		final String actual = mMessage.getText().toString();
		assertEquals(expected, actual);
	}
	
	public void testSendKeyString() {
		requestMessageFocus();
		sendKeys("H 3*E Y ALT_LEFT 1 DPAD_DOWN ENTER");
		final String expected = "HEEEY!";
		final String actual = mMessage.getText().toString();
		assertEquals(expected, actual);
	}
	
	public void testSendRepeatedKeys() {
		requestMessageFocus();
		sendRepeatedKeys(1, KeyEvent.KEYCODE_H,
				3, KeyEvent.KEYCODE_E,
				1, KeyEvent.KEYCODE_Y,
				1, KeyEvent.KEYCODE_ALT_LEFT,
				1, KeyEvent.KEYCODE_1,
				1, KeyEvent.KEYCODE_DPAD_DOWN,
				1, KeyEvent.KEYCODE_ENTER);
		
		final String expected = "HEEEY!";
		final String actual = mMessage.getText().toString();
		assertEquals(expected, actual);
	}
	
	public void testCapitalizationSendingKeys() {
		final String keysSequence = "T E S T SPACE M E";
		requestMessageFocus();
		sendKeys(keysSequence);
		TouchUtils.clickView(this, mCapitalize);
		final String expected = "test me".toUpperCase();
		final String actual = mMessage.getText().toString();
		assertEquals(expected, actual);
	}
	
	public void testActivityPermission(){
		final String PKG="com.sise.zhw";
		final String ACTIVITY=PKG+".MyContactsActivity";
		final String PERMISSION="android.MainFest.permission.CALL_PHONE";
		//assertActivityRequiresPermission(PKG,ACTIVITY,PERMISSION);
		
	}


}

  

转载于:https://www.cnblogs.com/common1140/p/4034114.html

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/weixin_34110749/article/details/94202073

智能推荐

python 数据图像修正_在OpenCV中修正失真的裁剪图像(Python)-程序员宅基地

文章浏览阅读352次。由于空白部分在图像中间具有最大值/最小值,因此您可以查找中间线并查看颜色何时发生变化.下图可能会澄清这一点: 您只需要找到图像中显示的x1,y1,x2和y2.这可以按如下方式完成:import cv2# Reads the image in grayscaleimg = cv2.imread('Undistorted.jpg', 0)h, w = img.shapex1, x2, y1, y2 =..._opencv 截取图片一部分后失真

TORCH MAXIMUM_module 'torch' has no attribute 'maximum-程序员宅基地

文章浏览阅读3.5k次。import torcha = torch.tensor((1, 2, -1))b = torch.tensor((3, 0, 4))torch.maximum(a, b)结果应该是:tensor([3, 2, 4])AttributeError: module 'torch' has no attribute 'maximum'torch版本:1.6.0+cu101_module 'torch' has no attribute 'maximum

Windows上WinRAR.exe命令行参数说明(转载)_winrar.exe 参数-程序员宅基地

文章浏览阅读1.9k次。 winrar.exe 命令行参数[语法]RAR [ - ] [ <@列表文件...> ]RAR [ ] [ ]一.简单的例子和说明:压缩文件夹winrar.exe a -ag -k -r -s -ibck c:/bak.rar c:/dat/压缩多个文件winrar a -ag -ibck bak.rar filen_winrar.exe 参数

三元:将对20万名贫困家庭儿童进行健康扶贫-程序员宅基地

文章浏览阅读104次。法制晚报2018-12-2821:13:44法制晚报讯 (记者 张鑫)27日晚,由中国社会责任百人论坛主办的“第七届分享责任年会-责任之夜”在京召开。做为乳业中多年来践行着社会责任的领导者,尤其在精准扶贫领域所作出的杰出贡献,三元被授予“精准扶贫”奖。未来,三元食品还计划未来几年,在全国选择100个贫困地区,对至少20万名贫困家庭的儿童进行健康扶贫。现场,中国社科院、责任云研究院联合发布《中国社会...

免费的短信验证码接口_国内永久免费接收验证码-程序员宅基地

文章浏览阅读1.2w次,点赞3次,收藏14次。免费的短信验证码接口改了唯ID的东西,希望能够大家用来玩玩,希望大家能文明使用!不说废话了,直接上接口。1、发送短信验证码接口POST http://sms.usts.top/sms/sendCode?phone=手机号2、验证接口 POST http://sms.usts.top/sms/verify?phone=手机号&amp;amp;amp;amp;验证码..._国内永久免费接收验证码

Unity UGUI 字体加粗特效_ugui字体加粗-程序员宅基地

文章浏览阅读8.2k次,点赞4次,收藏13次。Unity UGUI 字体加粗特效1.前言2.优化(一)3.优化(二)1.前言在项目组无可厚非会在一些描述的文本中加入粗体,比如标题或者是重要文字,然而Unity本身UGUI提供的Text的Bold属性在某些字体达到的效果并不尽人意,可以先看下原本Unity的效果:2.优化(一)原本的效果肯定是不满足美术需求的,我们需要通过字体渲染方面重新实现字体加粗效果,在本文中核心算法其实就是将文本..._ugui字体加粗

随便推点

【Java EE】--Contexts and Dependency Injection(上下文和依赖注入) 03_hasinjectioncontext什么意思-程序员宅基地

文章浏览阅读730次。bean作为可注入对象注入的概念已经成为Java技术的一部分。 由于引入了Java EE 5平台,注释使得可以将资源和其他类型的对象注入到容器管理的对象中。 CDI使得可以注入更多种类的对象并将其注入到不是容器管理的对象中。可以注入以下几种对象:(几乎)任何Java类会话beanJava EE资源:数据源,Java消息服务主题,队列,连接工厂等持久性上下文(Java Persistence_hasinjectioncontext什么意思

vux里x-address清空时遇到的坑_x-address的hide-district-程序员宅基地

文章浏览阅读1.6k次。先说说遇到的坑吧!其实,此处是一个细节性的问题。用起来一切都是正常的。但是,当你选择的城市为北京的时候,你点击提交成功后,把值给清空了,当你再次选择默认的北京时,你会发现此时是赋不上值的。原因是因为x-address里面的@on-shadow-change没有触发。解决方案其实很简单,提前定义一个变量存储一下@on-shadow-change里面的index。然后在@on-hide里面重新赋值就行..._x-address的hide-district

hub设备_便捷传输,奥睿科群控HUB分线器的使用体验-程序员宅基地

文章浏览阅读287次。在玩存储之前,其实对群控和分线器的印象一般,之前用过一款很简单的USB一分四,那时候只是觉得好玩,能缓解接口的不足,而体验了奥睿科群控HUB分线器之后,从观念上有了彻底的改变,不知道还在对分线器有误区的朋友,是否也觉得群控分线器有存在的必要?一、开箱晒物近期对奥睿科的4款产品有了初步的印象,在包装的设计一致性上还是比较的认可,此款十口HUB分线器结构其实很简单,开关+3.0USB口组合,看着就很简..._orico-bt2us-10ab 10口分控集线器作用

深度学习实战篇之 ( 十五) -- TensorFlow之GoogLeNet-程序员宅基地

文章浏览阅读1k次。科普知识AAAI的英文全称是 the Association for the Advance of Artificial Intelligence,中文意思是国际先进人工智能协会。国际先进..._深度学习训练颜值评分 googlenet

webpack更新_npm uodate web-程序员宅基地

文章浏览阅读1.6k次。更新方法npm update npm update -D更新前配置更新后配置问题webpack大版本号不会更新原因: 更新模块只能更新到小版本号最新的那个版本,不能更新大版本号。一个模块的版本号由三部分组成:大版本号.小版本号.次版本号解决办法:强制改写package.json中的版本号并执行下载命令:npm install [email protected]..._npm uodate web

虚拟机可以正常安装Win Server系统,但是VMware Tools无法安装?解决方法_windows server 2008虚拟机不能安装vmwaretool-程序员宅基地

文章浏览阅读413次,点赞7次,收藏4次。经查询大量文献得出结论,2019年12月微软更新了Windows驱动程序算法,2020年1月14日微软终止对Windows7系统的技术支持,VMware Workstation 16是2021年发布的,VM未支持旧版的Windows系统,另外Windows Vista系统也无法正常安装在VMware Workstation 15.5以上版本中。打开VMware Workstation,鼠标右键win虚拟机项目标号,依次执行“可移动设备”→“CD/DVD(SATA)”→“设置”。_windows server 2008虚拟机不能安装vmwaretool