Robolectric 3.0测试振动器服务

时间:2015-09-21 06:06:54

标签: android robolectric android-testing

我正在将我的测试用例迁移到最新的Robolectric 3.0。 要在我的应用程序中测试振动器服务,我之前使用过

org.robolectric.shadows.ShadowVibrator

但现在我无法测试它,即使使用自定义阴影类。

即使是Robolectric wesite也没有更新,它显示使用的Robolectric.shadowOf_()不存在。

This是网站的链接,不是更新版本。请指导。

以下是自定义实现的代码: -

自定义类: -

@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;
    }
}

我希望在我的代码中使用这样的东西,有人能指出我正确的API: -

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

1 个答案:

答案 0 :(得分:3)

看看RoboVibrator

RoboVibrator vibrator = (RoboVibrator) RuntimeEnvironment.application.getSystemService(Context.VIBRATOR_SERVICE);
相关问题