Seekbar测试未调用OnSeekBarChangeListener

Eug*_*yer 5 android android-espresso

我正在研究一个使用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
}
Run Code Online (Sandbox Code Playgroud)

测试代码:

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);
            }
        };
    }
}
Run Code Online (Sandbox Code Playgroud)

测试失败的结果:

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}"

...
Run Code Online (Sandbox Code Playgroud)

Sco*_*tyD 0

看起来像是断言失败,输出似乎是相同的(“进度:35”)。但是,当一个是字符串而一个是整数时,<4> 不等于 <4> 的断言可能会失败。随着进展的安排/评估方式的出现,这可能是一种可能性。

编辑:(抱歉声誉太低,无法发表评论)。是“fromUser”布尔值吗?我正在使用一些非常相似的代码,但我没有检查该布尔值...