Android自动化大讲堂30--Instrumentation自动化条件
本文摘自《深入理解Android自动化测试》第四章第4.9.2节
我们发现项目文件结构变为,如图4-26所示。
增加的文件如下:
src下多了一个名为com.xuben.change的包,其下包括一个Change.java文件;
在原有的com.xuben.hellobugben下也增加了一个ChangeActivity.java文件;
在res下面,layout增加一个display.xml文件。
先来看看ChangeActivity.java这个文件,如代码清单4-35所示。
代码清单4-35 ChangeActivity.java
package com.xuben.hellobugben;
import com.xuben.change.Change;
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;
import android.widget.RadioButton;
public class ChangeActivity extends Activity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button subButton = (Button)findViewById(R.id.myButton01);
subButton.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
// xuben:获取提交数据
EditText txt1 = (EditText)findViewById(R.id.txt1);
EditText txt2 = (EditText)findViewById(R.id.txt2);
RadioButton bold = (RadioButton)findViewById(R.id.bold);
RadioButton small =
(RadioButton)findViewById(R.id.small);
// xuben:获取用户输入
String text1 = txt1.getText().toString();
String text2 = txt2.getText().toString();
String isBold = bold.isChecked() ? "bold" : "notbold";
String wordSize = small.isChecked() ? "small" : "big";
// xuben:将用户输入创建为可序列化对象change
Change change = new Change(text1, text2, isBold,
wordSize);
// xuben:通过Bundle将可序列化对象change传
// 递给HelloBugbenActivity
Bundle data = new Bundle();
data.putSerializable("change", change);
Intent intent = new Intent(ChangeActivity.this
, HelloBugbenActivity.class);
intent.putExtras(data);
startActivity(intent);
}
});
}
}
该界面由两个文字输入框:txt1和txt2,两个粗细选项框:bold和notbold,两个字号选项框:small和big,还有一个提交按钮subButton组成。
当点击提交按钮时,将触发点击事件,将用户输入传给可序列化对象change,并通过Bundle将可序列化对象change传递给显示界面HelloBugbenActivity进行显示,而HelloBugbenActivity显示的内容即为之前的项目所展示的效果。
这样一来,我们大体就明白,新增的ChangeActivity作为提交界面而存在,用户输入后需要将数据打包一下传给HelloBugbenActivity进行显示,而Change则提供了用以将数据打包的对象。
理解了这点,再来看看Change.java,就一目了然了,如代码清单4-36所示。
代码清单4-36 Change.java
package com.xuben.change;
import java.io.Serializable;
public class Change implements Serializable
{
private static final long serialVersionUID = 1L;
private Integer id;
private String txt1;
private String txt2;
private String isBold;
private String wordSize;
public Change()
{
}
// xuben:Change对象就是一个包,
//用以设置和获取文本框文字和选择的属性
public Change(String txt1, String txt2, String isBold, String wordSize)
{
this.txt1 = txt1;
this.txt2 = txt2;
this.isBold = isBold;
this.wordSize = wordSize;
}
public Integer getId()
{
return id;
}
public void setId(Integer id)
{
this.id = id;
}
public String getText1()
{
return txt1;
}
public void setText1(String txt)
{
this.txt1 = txt;
}
public String getText2()
{
return txt2;
}
public void setText2(String txt)
{
this.txt2 = txt;
}
public String getBold()
{
return isBold;
}
public void setBold(String isBold)
{
this.isBold = isBold;
}
public String getwordSize()
{
return wordSize;
}
public void setwordSize(String wordSize)
{
this.wordSize = wordSize;
}
}
HelloBugbenActivity.java,如代码清单4-37所示。
代码清单4-37 HelloBugbenActivity.java
package com.xuben.hellobugben;
import com.xuben.change.Change;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextPaint;
import android.widget.TextView;
public class HelloBugbenActivity extends Activity {
private TextView textview1;
private TextView textview2;
// xuben:对应选项
Boolean bugben_bold = true;
Boolean bugben_notbold = false;
Float bugben_small_size = (float) 60.0;
Float bugben_big_size = (float) 180.0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.display);
textview1 = (TextView)findViewById(R.id.myTextView01);
textview2 = (TextView)findViewById(R.id.myTextView02);
// xuben:取出提交的文本和属性信息
Intent intent = getIntent();
Bundle data = intent.getExtras();
Change change = (Change)data.getSerializable("change");
// xuben:设置文本框1和文本框2的文字
textview1.setText(change.getText1());
textview2.setText(change.getText2());
// xuben:设置文本框1加粗与否
if(change.getBold().equalsIgnoreCase("bold")){
TextPaint tp = textview1.getPaint();
tp.setFakeBoldText(bugben_bold);
}
else if(change.getBold().equalsIgnoreCase("notbold") ){
TextPaint tp = textview1.getPaint();
tp.setFakeBoldText(bugben_notbold);
}
// xuben:设置文本框2的大小
if(change.getwordSize().equalsIgnoreCase("small")){
TextPaint tp = textview2.getPaint();
tp.setTextSize(bugben_small_size);
}
else if(change.getwordSize().equalsIgnoreCase("big")){
TextPaint tp = textview2.getPaint();
tp.setTextSize(bugben_big_size);
}
}
}
不知道大家注意到没有,当HelloBugbenActivity需要设置两个文本框的文字、属性和大小等,都是通过change.getText1(),change.getText2(),change.getBold(),change.getwordSize()等进行获取的,这就是设计这个对象类的目的所在。
当项目代码清晰后,那对应的资源文件也就清晰了:main.xml对应的不再是项目的显示界面,而是提交界面,如代码清单4-38所示。
代码清单4-38 main.xml
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="请在下框中输入希望修改的文字:"
android:textSize="20sp"
/>
<TableRow>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="文本框1文字 :"
android:textSize="16sp"
/>
<!-- 文本框1文字输入 -->
<EditText
android:id="@+id/txt1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="Bugben微信:巴哥奔"
android:selectAllOnFocus="true"
/>
</TableRow>
<TableRow>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="文本框2文字: "
android:textSize="16sp"
/>
<!-- 文本框2文字输入 -->
<EditText
android:id="@+id/txt2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="BugbenQQ:1971629467"
android:selectAllOnFocus="true"
/>
</TableRow>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="请选择文字属性:"
android:textSize="20sp"
/>
<TableRow>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="文本框1粗细:"
android:textSize="16sp"
/>
<!-- 文本框1大小选择 -->
<RadioGroup
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<RadioButton
android:id="@+id/bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="加粗"
android:textSize="16sp"
/>
<RadioButton
android:id="@+id/notbold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="不加粗"
android:textSize="16sp"
/>
</RadioGroup>
</TableRow>
<TableRow>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="文本框2大小:"
android:textSize="16sp"
/>
<!-- 文本框2大小选择 -->
<RadioGroup
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<RadioButton
android:id="@+id/small"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="小号"
android:textSize="16sp"
/>
<RadioButton
android:id="@+id/big"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="大号"
android:textSize="16sp"
/>
</RadioGroup>
</TableRow>
<Button
android:id="@+id/myButton01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="提交"
android:textSize="16sp"
/>
</TableLayout>
更多内容,请点击“阅读原文”,参考《深入理解Android自动化测试》一书,谢谢!