Robolectric 3.0 testing Vibrator service

ε祈祈猫儿з 提交于 2019-12-25 07:39:30

问题


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:


Have a look at RoboVibrator

RoboVibrator vibrator = (RoboVibrator) RuntimeEnvironment.application.getSystemService(Context.VIBRATOR_SERVICE);


来源:https://stackoverflow.com/questions/32688359/robolectric-3-0-testing-vibrator-service

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!