0测试振动器服务

0测试振动器服务

本文介绍了Robolectric 3.0测试振动器服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的测试用例迁移到最新的3.0 Robolectric过程。
在我的应用程序测试viberator服务,早先我用

  org.robolectric.shadows.ShadowVibrator

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

即使Robolectric wesite不更新,它显示了使用Robolectric.shadowOf_()不存在

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

以下是code为自定义实现: -

自定义类: -

  @Implements(Vibrator.class)
公共类ShadowVibrator {
    私人布尔振动;
    私人布尔取消;
    私人长期毫秒;
    私人长期[]模式;
    私人诠释重复;    @implementation
    公共无效振动(长毫秒){
        振动= TRUE;
        this.milliseconds =毫秒;
    }    @implementation
    公共无效振动(长[]模式,INT重复){
        振动= TRUE;
        this.pattern =格局;
        this.repeat =重复;
    }    @implementation
    公共无效取消(){
        取消= TRUE;
        振动= FALSE;
    }    公共布尔isVibrating(){
        返回振动;
    }    公共布尔isCancelled(){
        返回取消;
    }    众长getMilliseconds(){
        返回毫秒;
    }    众长[] getPattern(){
        返回格局;
    }    公众诠释getRepeat(){
        返回重复;
    }
}

和我想在我的code像这样使用,可有人点我正确的API: -

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


解决方案

看一看RoboVibrator

  RoboVibrator振动器=(RoboVibrator)RuntimeEnvironment.application.getSystemService(Context.VIBRATOR_SERVICE);

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));
解决方案

Have a look at RoboVibrator

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

这篇关于Robolectric 3.0测试振动器服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 12:40