Athrun框架自定义控件的使用

转自:http://huangqinqin.iteye.com/blog/1798376


无论Android还是iOS都支持自定义控件,而原始Athrun框架只支持系统控件,所以在使用Athrun框架进行功能自动化测试时需要根据项目的实际情况,自行扩展Athrun框架并添加项目中自定义控件的映射。如以下示例所示。

 

1 带有自定义控件的小应用:TestCustom。

 

 Athrun框架自定义控件的使用_第1张图片 其中

CustomTextView.java

 
 package com.example.testcustom;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.widget.TextView;
public class CustomTextView extends TextView{
 public CustomTextView(Context context, AttributeSet attrs) {
  super(context, attrs);
  setText("Custom");
  Drawable draw = getResources().getDrawable(R.drawable.huangqinbg);
  this.setCompoundDrawablesWithIntrinsicBounds(draw, draw, draw, draw);
  setBackgroundResource(R.drawable.huangqinbg);
 }
 public CustomTextView(Context context) {
  super(context);
  setText("Custom");
  Drawable draw = getResources().getDrawable(R.drawable.huangqinbg);
  this.setCompoundDrawablesWithIntrinsicBounds(draw, draw, draw, draw);
  setBackgroundResource(R.drawable.huangqinbg);
 }
}



DrawGraphics.java

 package com.example.testcustom;
import android.graphics.Canvas;
public interface DrawGraphics {
 public void  draw(Canvas canvas);
} 


GameView.java

 package com.example.testcustom;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;
public class GameView extends View implements Runnable {
 private Paint mPaint = null;
 private DrawGraphics drawGraphics = null;
 private static int mCircleColor = Color.GREEN;
 private static int mLineColor = Color.GREEN;
 private static int mRectColor = Color.GREEN;
 public GameView(Context context) {
  super(context);
  mPaint = new Paint();
  new Thread(this).start();
 }
 
 public GameView(Context context, AttributeSet attrs) {
  super(context, attrs);
  mPaint = new Paint();
  new Thread(this).start();
 }
 
 public void onDraw(Canvas canvas) {
  super.onDraw(canvas);
  // 设置画布为黑色背景
  // canvas.drawColor(Color.BLACK);
  // 消除锯齿
  mPaint.setAntiAlias(true);
  // 设置图形为空心
  mPaint.setStyle(Paint.Style.STROKE);
  // 绘制空心几何图形
  drawGraphics = new DrawCircle();
  drawGraphics.draw(canvas);
  drawGraphics = new DrawLine();
  drawGraphics.draw(canvas);
  drawGraphics = new DrawRect();
  drawGraphics.draw(canvas);
 }
 @Override
 public void run() {
  while (!Thread.currentThread().isInterrupted()) {
   try {
    Thread.sleep(1000);
   } catch (InterruptedException e) {
    Thread.currentThread().interrupt();
   }
   // 使用postInvalidate 可以直接在线程中更新界面
   postInvalidate();
  }
 }
 
 public void setCircleColor(int circleColor) {
  mCircleColor = circleColor;
 }
 
 public int getCircleColor() {
  return mCircleColor;
 }
 
 public void setLineColor(int lineColor) {
  mLineColor = lineColor;
 }
 
 public int getLineColor() {
  return mLineColor;
 }
 
 public void setRectColor (int rectColor) {
  mRectColor = rectColor;
 }
 
 public int getRectColor() {
  return mRectColor;
 }
 
 static class DrawCircle implements DrawGraphics {
  private Paint paint = null;
  private Paint paint_eye = null;
  public DrawCircle() {
   paint = new Paint();
   paint_eye = new Paint();
  }
  @Override
  public void draw(Canvas canvas) {
   // 绘制圆形(圆心x,圆心y,半径r,画笔p)
   paint_eye.setAntiAlias(true);
   paint.setAntiAlias(true);
   RectF rectF = new RectF(120/2, 60/2, 370/2, 240/2);
   paint_eye.setColor(Color.BLACK);
   paint.setColor(mCircleColor);
   canvas.drawCircle(190/2, 110/2, 18/2, paint_eye);
   canvas.drawCircle(300/2, 110/2, 18/2, paint_eye);
   canvas.drawArc(rectF, 180, 180, true, paint);
  }
 }
 
 static class DrawLine implements DrawGraphics {
  private Paint paint = null;
  public DrawLine() {
   paint = new Paint();
  }
  @Override
  public void draw(Canvas canvas) {
   paint.setAntiAlias(true);
   // 绘制直线
   paint.setColor(mLineColor);
   // 设置线条粗细
   paint.setStrokeWidth(12);
   canvas.drawLine(120/2, 40/2, 170/2, 90/2, paint);
   canvas.drawLine(320/2, 90/2, 370/2, 40/2, paint);
  }
 }
 static class DrawRect implements DrawGraphics {
  private Paint paint = null;
  public DrawRect() {
   paint = new Paint();
  }
  @Override
  public void draw(Canvas canvas) {
   RectF rectF1 = new RectF(120/2, 170/2, 370/2, 500/2);
   RectF rectF2 = new RectF(40/2, 150/2, 90/2, 400/2);
   RectF rectF3 = new RectF(390/2, 150/2, 440/2, 400/2);
   RectF rectF4 = new RectF(140/2, 520/2, 200/2, 650/2);
   RectF rectF5 = new RectF(290/2, 520/2, 350/2, 650/2);
   
   paint.setAntiAlias(true);
   // 设置画笔颜色为BLUE
   paint.setColor(mRectColor);
   // 在画布上绘制圆角矩形/圆弧/直线
   canvas.drawRoundRect(rectF1, 20/2, 20/2, paint);
   canvas.drawRoundRect(rectF2, 20/2, 20/2, paint);
   canvas.drawRoundRect(rectF3, 20/2, 20/2, paint);
   canvas.drawRoundRect(rectF4, 20/2, 20/2, paint);
   canvas.drawRoundRect(rectF5, 20/2, 20/2, paint);
  }
 }
}


 

2 提取TestCustom中自定义控件CustomTextView、GameView文件,并以CustomView.jar形式导出。

右击TestCustom——选中Export,将弹出下图:

Athrun框架自定义控件的使用_第2张图片

 

如图选中JAR file,单击Next,如下图所示:

Athrun框架自定义控件的使用_第3张图片

 

注意:右框中的最好都处于不选状态,左框中的视实际情况而定,有时自定义控件中需要用到一些资源文件,则res目录中相应的资源文件必须选中。JAR file框中应填写该jar文件的保存完整路径及jar文件名,这里为G:\huangqin\CustomView.jar 。

一直单击Next,知道Finish。

 

3 将CustomView.jar拷贝到Athrun framework中的libs目录下。

 Athrun框架自定义控件的使用_第4张图片

4 根据TestCustom中自定义控件CustomTextView、GameView的定义,在framework添加相应的映射文件CustomViewElement、GameViewElement。

Athrun框架自定义控件的使用_第5张图片

 

 其中

CustomViewElement.java

package org.athrun.android.framework.viewelement;
import android.app.Instrumentation;
import android.view.View;
import com.example.testcustom.*;
public class CustomViewElement extends ViewElement{ 
 
 private CustomTextView mCustomView;
 protected CustomViewElement(Instrumentation inst, CustomTextView view) {
  super(inst, view);
  mCustomView = view;
 }
 
 public void clearText() {
  setText("");
 }
 public void setText(final String newText) {
  inst.waitForIdleSync();
  ViewUtils.scrollInToScreenIfNeeded(inst, mCustomView);
  inst.waitForIdleSync();
  inst.runOnMainSync(new Runnable() {
   @Override
   public void run() {
    mCustomView.setText(newText);
   }
  });
  inst.waitForIdleSync();
 }
 
 public String getText() {
  inst.waitForIdleSync();
  ViewUtils.scrollInToScreenIfNeeded(inst, mCustomView);
  inst.waitForIdleSync();
  return mCustomView.getText().toString();
 }
}


GameViewElement.java

package org.athrun.android.framework.viewelement;
import android.app.Instrumentation;
import com.example.testcustom.*;
public class GameViewElement extends ViewElement{
 private GameView mGameView = null;
 protected GameViewElement(Instrumentation inst, GameView view) {
  super(inst, view);
  mGameView = view;
 }
 
 public void setCircleColor(final int circleColor) {
  inst.waitForIdleSync();
  inst.runOnMainSync(new Runnable() {
   @Override
   public void run() {
    mGameView.setCircleColor(circleColor);
   }   
  });  
 }
 
 public void setLineColor (final int lineColor) {
  inst.waitForIdleSync();
  inst.runOnMainSync(new Runnable() {
   @Override
   public void run() {
    mGameView.setLineColor(lineColor);
   }   
  });  
 }
 
 public void setRectColor (final int rectColor){
  inst.waitForIdleSync();
  inst.runOnMainSync(new Runnable() {
   @Override
   public void run() {
    mGameView.setRectColor(rectColor);
   }   
  });  
 }
}


通过ant jar重新编译framework.jar 。

 Athrun框架自定义控件的使用_第6张图片

将编译好的framework.jar文件拷贝到测试程序TestAthrunCustomView的libs目录下。然后引用自定义控件的映射文件CustomViewElement、GameViewElement进行功能自动化测试。

Athrun框架自定义控件的使用_第7张图片

 

 

其中

MainActivityTest.java

package com.dragon.android.pandaspacetest;
import org.athrun.android.framework.AthrunTestCase;
import org.athrun.android.framework.Test;
import org.athrun.android.framework.ViewOperation.Direction;
import org.athrun.android.framework.viewelement.AbsListViewElement;
import org.athrun.android.framework.viewelement.CustomViewElement;
import org.athrun.android.framework.viewelement.GameViewElement;
import org.athrun.android.framework.viewelement.ViewGroupElement;
import android.graphics.Color;
public class MainActivityTest extends AthrunTestCase{
 public MainActivityTest() throws Exception {
  super("com.example.testcustom", "com.example.testcustom.MainActivity");
  AthrunTestCase.setMaxTimeToFindView(10000);
 }
 
 @Test
 public void testWaitForActivity() throws Exception{
  assertEquals(true, this.getDevice().waitForActivity("MainActivity", 3000));   
 }
 
 @Test
 public void testSetText() throws Exception {
  
  
  CustomViewElement customView = this.findElementById("custome_view", CustomViewElement.class);
  customView.setText("Custom Successfully!");  
  String result = customView.getText();
  assertEquals(result, "Custom Successfully!");
  assertNotSame(result, "Custom Failed!");
 }
 
 @Test
 public void testSetGameViewLineColor() throws Exception {
  GameViewElement gameView = this.findElementById("game_view", GameViewElement.class);
  gameView.setCircleColor(Color.YELLOW);
  gameView.setRectColor(Color.RED);
    
 }
}


  • 查看图片附件


你可能感兴趣的:(android,自动化测试)