Robolectric 3.0 testing Vibrator service

2019-09-08 08:12发布

I am in process of migrating my test cases to latest Robolectric 3.0. To test the viberator service in my app, earlier I used

org.robolectric.shadows.ShadowVibrator

but now I am not able to test it, even using custom shadow class.

Even the Robolectric wesite is not updated and it shows the use Robolectric.shadowOf_() which does not exist.

This is the link of the website, which is not updated version. Kindly guide.

Following is code for custom implementation:--

The custom class:--

@Implements(Vibrator.class)
public class ShadowVibrator {
    private boolean vibrating;
    private boolean cancelled;
    private long milliseconds;
    private long[] pattern;
    private int repeat;

    @Implementation
    public void vibrate(long milliseconds) {
        vibrating = true;
        this.milliseconds = milliseconds;
    }

    @Implementation
    public void vibrate(long[] pattern, int repeat) {
        vibrating = true;
        this.pattern = pattern;
        this.repeat = repeat;
    }

    @Implementation
    public void cancel() {
        cancelled = true;
        vibrating = false;
    }

    public boolean isVibrating() {
        return vibrating;
    }

    public boolean isCancelled() {
        return cancelled;
    }

    public long getMilliseconds() {
        return milliseconds;
    }

    public long[] getPattern() {
        return pattern;
    }

    public int getRepeat() {
        return repeat;
    }
}

And I want use in my code something like this , can someone point me correct API:--

ShadowVibrator shadowVibrator = Shadows.shadowOf((Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE));

1条回答
干净又极端
2楼-- · 2019-09-08 09:05

Have a look at RoboVibrator

RoboVibrator vibrator = (RoboVibrator) RuntimeEnvironment.application.getSystemService(Context.VIBRATOR_SERVICE);
查看更多
登录 后发表回答