package test.utils;

 import android.support.test.espresso.PerformException;
import android.support.test.espresso.ViewAction;
import android.support.test.espresso.action.CoordinatesProvider;
import android.support.test.espresso.action.GeneralSwipeAction;
import android.support.test.espresso.action.Press;
import android.support.test.espresso.action.Swipe;
import android.support.test.espresso.util.HumanReadables;
import android.view.View;
import android.widget.SeekBar; import static android.support.test.espresso.action.ViewActions.actionWithAssertions; /**
* Created by qiantao on 17-10-19.
*/ public class SeekBarActions {
public static ViewAction scrubSeekBarAction(int progress) {
return actionWithAssertions(new GeneralSwipeAction(
Swipe.SLOW,
new SeekBarThumbCoordinatesProvider(-1),
new SeekBarThumbCoordinatesProvider(progress),
Press.PINPOINT));
} /**
* -1 表示起始位置
*/
private static class SeekBarThumbCoordinatesProvider implements CoordinatesProvider {
int mProgress; public SeekBarThumbCoordinatesProvider(int progress) {
mProgress = progress;
} private static float[] getVisibleLeftTop(View view) {
final int[] xy = new int[2];
view.getLocationOnScreen(xy);
return new float[]{(float) xy[0], (float) xy[1]};
} @Override
public float[] calculateCoordinates(View view) {
if (!(view instanceof SeekBar)) {
throw new PerformException.Builder()
.withViewDescription(HumanReadables.describe(view))
.withCause(new RuntimeException(String.format("SeekBar expected"))).build();
}
SeekBar seekBar = (SeekBar) view;
int width = seekBar.getWidth() - seekBar.getPaddingLeft() - seekBar.getPaddingRight();
LogUtils.i("seekBar.getProgress():" + seekBar.getProgress());
double progress = mProgress == -1 ? seekBar.getProgress() : (mProgress * seekBar.getMax() / 100);
int xPosition = (int) (seekBar.getPaddingLeft() + (width * progress / seekBar.getMax()));
float[] xy = getVisibleLeftTop(seekBar);
return new float[]{xy[0] + xPosition, xy[1]};
}
}
}

检查Mtacher

    public static Matcher<View> withSeekBarProgress(final int expectedProgress) {
return new BoundedMatcher<View, SeekBar>(SeekBar.class) {
@Override
public void describeTo(Description description) {
description.appendText("expected: ");
description.appendText("" + expectedProgress);
} @Override
public boolean matchesSafely(SeekBar seekBar) {
LogUtils.i("seekBar.getProgress():" + seekBar.getProgress());
return seekBar.getProgress()*100/seekBar.getMax() == expectedProgress;
}
};
}

用法

for (int i = 0; i <= 100; i += 50) {    
  onView(seekBarViewMatcher).perform(SeekBarActions.scrubSeekBarAction(i));
  onView(seekBarViewMatcher).check(matches(withSeekBarProgress(i)));
}

espresso Seekbar的更多相关文章

  1. 安卓SeekBar

    public class Speak extends Fragment implements OnSeekBarChangeListener { private SeekBar bar1; priva ...

  2. SeekBar进度条简单案例

    SeekBar是进度条.我们使用进度条时,可以使用系统默认的进度条:也可以自定义进度条的图片和滑块图片等 向右拉进度条让图片显示出来 向右拉五角星加载有色进度条 baseSeekBar package ...

  3. 【Android测试】【第十九节】Espresso——API详解

    ◆版权声明:本文出自胖喵~的博客,转载必须注明出处. 转载请注明出处:http://www.cnblogs.com/by-dream/p/5997557.html 前言 Espresso的提供了不少A ...

  4. 安卓自动化测试:Android studio 自带的 Record Espresso Test || [ Appium & (Android studio || Python|| Eclipse ) ]

    1.Android studio 自带的 Record Espresso Test  https://developer.android.com/studio/test/espresso-test-r ...

  5. 通过seekBar改变图片的透明度

    作者:堕落的天使 对应的图片 activity_main.xml(代码) <RelativeLayout xmlns:android="http://schemas.android.c ...

  6. SeekBar 圆角问题

    用图片做背景色,最后处理成.9.png的.用普通png图片做背景,则两边会有圆角出现,原因是图片不适合SeekBar尺寸,因而被拉伸或压缩,从而产生圆角. <?xml version=" ...

  7. Android之SeekBar定制

    1.SeekBar样式定制 xml文件中:  <SeekBar             android:id="@+id/seekbar_voice"             ...

  8. 自定义SeekBar的使用

    一.seekbar是进度条,可以使用系统的,也可以自己定义,下面我们将自己定义一个seekbar. 1.自定义滑条,包括对背景,第一进度,第二进度的设置,通过一个xml来实现,在drawable下创建 ...

  9. listview+seekbar问题的解决

    最近做了个项目,其中有录音播放功能.每次录音结束都会在listView中显示,在listView中能播放每次的录音,也可以每条录音之间的切换播放.随之就引发了许多的问题,比如当我播放第一条录音的时所有 ...

随机推荐

  1. String类型的方法使用

    String.equals()方法源代码: public boolean equals(Object anObject) { if (this == anObject) { return true; ...

  2. 【DNS】- 域名解析中A记录、CNAME、MX记录、NS记录的区别和联系

    1. A记录 又称IP指向,用户可以在此设置子域名并指向到自己的目标主机地址上,从而实现通过域名找到服务器.说明:·指向的目标主机地址类型只能使用IP地址: 附加说明: 1) 泛域名解析即将该域名所有 ...

  3. Python中的单元测试模块Unittest快速入门

    前言 为什么需要单元测试? 如果没有单元测试,我们会遇到这种情况:已有的健康运行的代码在经过改动之后,我们无法得知改动之后是否引入了Bug.如果有单元测试的话,只要单元测试全部通过,我们就可以保证没有 ...

  4. 【bzoj1976】[BeiJing2010组队]能量魔方 Cube 网络流最小割

    题目描述 一个n*n*n的立方体,每个位置为0或1.有些位置已经确定,还有一些需要待填入.问最后可以得到的 相邻且填入的数不同的点对 的数目最大. 输入 第一行包含一个数N,表示魔方的大小. 接下来 ...

  5. bzoj 3111 蚂蚁 动态规划

    题目描述 在一个 n*m 的棋盘上,每个格子有一个权值,初始时,在某个格子的顶点处一只面朝北的蚂蚁,我们只知道它的行走路线是如何转弯,却不知道每次转弯前走了多长. 蚂蚁转弯是有一定特点的,即它的转弯序 ...

  6. HDU1263 map二维运用

    #include <iostream> #include <cstdio> #include <cstring> #include <map> #inc ...

  7. 行为型设计模式之备忘录模式(Memento)

    结构 意图 在不破坏封装性的前提下,捕获一个对象的内部状态,并在该对象之外保存这个状态.这样以后就可将该对象恢复到原先保存的状态. 适用性 必须保存一个对象在某一个时刻的(部分)状态, 这样以后需要时 ...

  8. Windows8 上用Ubuntu-Ubuntu启动SSH

    公司刚给配了一个电脑,华硕的超级本8个G的内存,很强大的了,但是系统是64位的windows 8,我用wubi.exe直接安装到系统上,但是开机启动的时候总是下面这个错误,去Ubuntu社区请教,结论 ...

  9. os.path.isfile的错误

    今天写了一个程序,读取子目录(source)下的所有文件,然后转换. 程序一部分代码如下: def DtoTab(dsrc, dtarget): try: for item in os.listdir ...

  10. leetcode-Min Cost Climbing Stairs

    题目: On a staircase, the i-th step has some non-negative cost cost[i] assigned (0 indexed). Once you ...