我正在研究一个使用SeekBar的简单示例,并且在使Espresso测试工作时遇到了问题。当我运行测试时,我可以在模拟器屏幕上看到SeekBar移动,但它显然没有调用OnSeekBarChangeListener,因为TextView没有改变值(' Hello World!'是最初的值)。如果我运行程序并手动测试它,TextView会按照我的预期更新。
主程序:
package edu.eku.styere.seekbar;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
public class MainActivity extends Activity {
private int cur_progress = 0;
boolean running = false;
final int SEEK_MAX = 100;
final int DELAY_MILLIS = 200;
private TextView mProgressLabel;
private SeekBar mProgressBar;
// message handler
private Handler mHandler = new Handler();
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// fetch items
mProgressLabel = (TextView) findViewById( R.id.progress_text );
mProgressBar = (SeekBar) findViewById( R.id.progress_seekbar );
Button startButton = (Button) findViewById( R.id.btn_start );
startButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// start the timer/progresss bar
cur_progress = 0;
running = true;
// start messages
mHandler.postDelayed( mUpdateTimeTask, DELAY_MILLIS );
}
});
// respond to changes in the seek bar by the user
mProgressBar.setOnSeekBarChangeListener( new OnSeekBarChangeListener() {
public void onProgressChanged( SeekBar sb, int progress, boolean fromUser ) {
// user change or from us?
if ( fromUser ) {
// user changed the bar, so get a new value
cur_progress = progress;
// update the text box
mProgressLabel.setText("progress: " + cur_progress );
mHandler.removeCallbacks(mUpdateTimeTask);
if ( running )
mHandler.postDelayed(mUpdateTimeTask, DELAY_MILLIS);
}
}
// need to have these, but not using them here
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
});
}
// the runnable object that corresponds to our timer
private Runnable mUpdateTimeTask = new Runnable() {
public void run() {
// done?
if ( cur_progress >= SEEK_MAX ) {
// stop everything
mHandler.removeCallbacks( mUpdateTimeTask );
running = false;
return;
}
// update the progress
cur_progress++;
// update the text box
mProgressLabel.setText("progress: " + cur_progress );
// update the seek bar (does not cause onChange event)
mProgressBar.setProgress( cur_progress );
//do this again
mHandler.postDelayed( this, DELAY_MILLIS );
}
}; // mUpdateTimeTask
}
测试代码:
package edu.eku.styere.seekbar;
import android.app.Instrumentation;
import android.os.SystemClock;
import android.support.test.InstrumentationRegistry;
import android.support.test.espresso.UiController;
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.matcher.ViewMatchers;
import android.support.test.rule.ActivityTestRule;
import android.support.test.runner.AndroidJUnit4;
import android.test.InstrumentationTestCase;
import android.view.MotionEvent;
import android.view.View;
import android.widget.SeekBar;
import org.hamcrest.Matcher;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import java.util.Random;
import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.action.ViewActions.click;
import static android.support.test.espresso.assertion.ViewAssertions.matches;
import static android.support.test.espresso.matcher.ViewMatchers.withId;
import static android.support.test.espresso.matcher.ViewMatchers.withText;
@RunWith(AndroidJUnit4.class)
public class MainActivityTest {
//Random rng = new Random();
@Rule
public ActivityTestRule<MainActivity> mActivityRule = new ActivityTestRule<>(MainActivity.class);
@Test
public void moveSeekBar() {
onView( withId( R.id.progress_seekbar )).perform(setProgress(35));
// Check that the text was changed
onView(withId(R.id.progress_text))
.check(matches(withText("progress: " + 35)));
}
public static ViewAction setProgress(final int progress) {
return new ViewAction() {
@Override
public void perform(UiController uiController, View view) {
SeekBar seekBar = (SeekBar) view;
seekBar.setProgress(progress);
}
@Override
public String getDescription() {
return "Set a progress on a SeekBar";
}
@Override
public Matcher<View> getConstraints() {
return ViewMatchers.isAssignableFrom(SeekBar.class);
}
};
}
}
测试失败的结果:
android.support.test.espresso.base.DefaultFailureHandler$AssertionFailedWithCauseError: 'with text: is "progress: 35"' doesn't match the selected view.
Expected: with text: is "progress: 35"
Got: "TextView{id=2131165185, res-name=progress_text, visibility=VISIBLE, width=480, height=29,
has-focus=false, has-focusable=false, has-window-focus=true, is-clickable=false, is-enabled=true,
is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false,
root-is-layout-requested=false, has-input-connection=false, x=0.0, y=72.0,
text=Hello world!, input-type=0, ime-target=false, has-links=false}"
...
答案 0 :(得分:4)
经过一些工作,我设法根据GenericClickAction为我的问题创建了一个解决方案:
package edu.eku.styere.seekbar;
import android.support.test.espresso.ViewAction;
import android.support.test.espresso.action.CoordinatesProvider;
import android.support.test.espresso.action.GeneralClickAction;
import android.support.test.espresso.action.Press;
import android.support.test.espresso.action.Tap;
import android.support.test.rule.ActivityTestRule;
import android.support.test.runner.AndroidJUnit4;
import android.view.View;
import android.widget.SeekBar;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import java.util.Random;
import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.assertion.ViewAssertions.matches;
import static android.support.test.espresso.matcher.ViewMatchers.withId;
import static android.support.test.espresso.matcher.ViewMatchers.withText;
@RunWith(AndroidJUnit4.class)
public class MainActivityTest {
Random rng = new Random();
@Rule
public ActivityTestRule<MainActivity> mActivityRule = new ActivityTestRule<>(MainActivity.class);
@Test
public void moveSeekBar() {
int cur_progress;
// do an initial move in case the first random number is 0
// -- if it didn't move, the OnSeekBarChangeListener isn't called
onView( withId( R.id.progress_seekbar )).perform(clickSeekBar(25));
// try 10 random locations
for( int i=0; i<10; i++ ) {
cur_progress = rng.nextInt(101); // 0..100
// move it to a random location
onView(withId(R.id.progress_seekbar)).perform(clickSeekBar(cur_progress));
try {
Thread.sleep(1000);
} catch ( Exception e ) {
// do nothing
}
// Check that the text was changed
onView(withId(R.id.progress_text))
.check(matches(withText("progress: " + cur_progress)));
}
}
public static ViewAction clickSeekBar(final int pos){
return new GeneralClickAction(
Tap.SINGLE,
new CoordinatesProvider() {
@Override
public float[] calculateCoordinates(View view) {
SeekBar seekBar = (SeekBar) view;
final int[] screenPos = new int[2];
seekBar.getLocationOnScreen(screenPos);
// get the width of the actual bar area
// by removing padding
int trueWidth = seekBar.getWidth()
- seekBar.getPaddingLeft() - seekBar.getPaddingRight();
// what is the position on a 0-1 scale
// add 0.3f to avoid roundoff to the next smaller position
float relativePos = (0.3f + pos)/(float) seekBar.getMax();
if ( relativePos > 1.0f )
relativePos = 1.0f;
// determine where to click
final float screenX = trueWidth*relativePos + screenPos[0]
+ seekBar.getPaddingLeft();
final float screenY = seekBar.getHeight()/2f + screenPos[1];
float[] coordinates = {screenX, screenY};
return coordinates;
}
},
Press.FINGER);
}
}
答案 1 :(得分:0)
看起来断言失败似乎输出相同(“progress:35”)。然而,它可能无法通过&lt; 4&gt;来断言。不等于&lt; 4&gt;当一个是字符串而一个是int。这可能是进展/评估进展的可能性。
编辑:(对不起评论的声誉太低)。它是“fromUser”布尔值吗?我正在使用一些非常相似的代码,但是我没有检查那个布尔值......