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,弹出如下界面:

 

 
//代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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());
            }
        });
    }
}

//布局文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
<?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>

  

//测试代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
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);
         
    }
 
 
}

  

posted @   钟汉文  阅读(642)  评论(0编辑  收藏  举报
编辑推荐:
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
点击右上角即可分享
微信分享提示