问题
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