Make ShdadowVibrator compatible with SDK 28+
Signed-off-by: utzcoz <utzcoz@outlook.com>
diff --git a/integration_tests/compat-target28/src/test/java/org/robolectric/integration/compat/target28/NormalCompatibilityTest.kt b/integration_tests/compat-target28/src/test/java/org/robolectric/integration/compat/target28/NormalCompatibilityTest.kt
index ee56fc6..69bbf73 100644
--- a/integration_tests/compat-target28/src/test/java/org/robolectric/integration/compat/target28/NormalCompatibilityTest.kt
+++ b/integration_tests/compat-target28/src/test/java/org/robolectric/integration/compat/target28/NormalCompatibilityTest.kt
@@ -1,7 +1,9 @@
package org.robolectric.integration.compat.target28
import android.content.Context
+import android.content.Context.VIBRATOR_SERVICE
import android.os.Build
+import android.os.Vibrator
import android.speech.SpeechRecognizer
import com.google.common.truth.Truth.assertThat
import org.junit.Test
@@ -47,4 +49,9 @@
fun `Create speech recognizer succeed`() {
assertThat(SpeechRecognizer.createSpeechRecognizer(application)).isNotNull()
}
+
+ @Test
+ fun `Get default Vibrator succeed`() {
+ assertThat(application.getSystemService(VIBRATOR_SERVICE) as Vibrator).isNotNull()
+ }
}
diff --git a/robolectric/src/test/java/org/robolectric/shadows/ShadowVibratorTest.java b/robolectric/src/test/java/org/robolectric/shadows/ShadowVibratorTest.java
index bc93daa..b72231e 100644
--- a/robolectric/src/test/java/org/robolectric/shadows/ShadowVibratorTest.java
+++ b/robolectric/src/test/java/org/robolectric/shadows/ShadowVibratorTest.java
@@ -132,48 +132,6 @@
@Config(minSdk = S)
@Test
- public void getVibrationEffectSegments_composeOnce_shouldReturnSameFragment() {
- vibrator.vibrate(
- VibrationEffect.startComposition()
- .addPrimitive(EFFECT_CLICK, /* scale= */ 0.5f, /* delay= */ 20)
- .addPrimitive(EFFECT_CLICK, /* scale= */ 0.7f, /* delay= */ 50)
- .addPrimitive(EFFECT_CLICK, /* scale= */ 0.9f, /* delay= */ 150)
- .compose());
-
- assertThat(shadowOf(vibrator).getVibrationEffectSegments())
- .isEqualTo(
- ImmutableList.of(
- new PrimitiveSegment(EFFECT_CLICK, /* scale= */ 0.5f, /* delay= */ 20),
- new PrimitiveSegment(EFFECT_CLICK, /* scale= */ 0.7f, /* delay= */ 50),
- new PrimitiveSegment(EFFECT_CLICK, /* scale= */ 0.9f, /* delay= */ 150)));
- }
-
- @Config(minSdk = S)
- @Test
- public void getVibrationEffectSegments_composeTwice_shouldReturnTheLastComposition() {
- vibrator.vibrate(
- VibrationEffect.startComposition()
- .addPrimitive(EFFECT_CLICK, /* scale= */ 0.5f, /* delay= */ 20)
- .addPrimitive(EFFECT_CLICK, /* scale= */ 0.7f, /* delay= */ 50)
- .addPrimitive(EFFECT_CLICK, /* scale= */ 0.9f, /* delay= */ 150)
- .compose());
- vibrator.vibrate(
- VibrationEffect.startComposition()
- .addPrimitive(EFFECT_CLICK, /* scale= */ 0.4f, /* delay= */ 120)
- .addPrimitive(EFFECT_CLICK, /* scale= */ 0.9f, /* delay= */ 150)
- .addPrimitive(EFFECT_CLICK, /* scale= */ 1f, /* delay= */ 2150)
- .compose());
-
- assertThat(shadowOf(vibrator).getVibrationEffectSegments())
- .isEqualTo(
- ImmutableList.of(
- new PrimitiveSegment(EFFECT_CLICK, /* scale= */ 0.4f, /* delay= */ 120),
- new PrimitiveSegment(EFFECT_CLICK, /* scale= */ 0.9f, /* delay= */ 150),
- new PrimitiveSegment(EFFECT_CLICK, /* scale= */ 1f, /* delay= */ 2150)));
- }
-
- @Config(minSdk = S)
- @Test
public void getPrimitiveSegmentsInPrimitiveEffects_composeOnce_shouldReturnSameFragment() {
vibrator.vibrate(
VibrationEffect.startComposition()
diff --git a/shadows/framework/src/main/java/org/robolectric/shadows/ShadowSystemVibrator.java b/shadows/framework/src/main/java/org/robolectric/shadows/ShadowSystemVibrator.java
index cce1990..ee0d481 100644
--- a/shadows/framework/src/main/java/org/robolectric/shadows/ShadowSystemVibrator.java
+++ b/shadows/framework/src/main/java/org/robolectric/shadows/ShadowSystemVibrator.java
@@ -15,9 +15,9 @@
import android.os.Handler;
import android.os.Looper;
import android.os.SystemVibrator;
-import android.os.VibrationAttributes;
import android.os.VibrationEffect;
import android.os.vibrator.VibrationEffectSegment;
+import com.google.common.base.Preconditions;
import java.util.List;
import java.util.Optional;
import org.robolectric.RuntimeEnvironment;
@@ -25,7 +25,7 @@
import org.robolectric.annotation.Implements;
import org.robolectric.util.ReflectionHelpers;
-@Implements(value = SystemVibrator.class, isInAndroidSdk = false)
+@Implements(value = SystemVibrator.class, isInAndroidSdk = false, looseSignatures = true)
public class ShadowSystemVibrator extends ShadowVibrator {
private final Handler handler = new Handler(Looper.getMainLooper());
@@ -133,11 +133,14 @@
@Implementation(minSdk = S)
protected void vibrate(
- int uid,
- String opPkg,
- VibrationEffect effect,
- String reason,
- VibrationAttributes attributes) {
+ Object uid, Object opPkg, Object effect, Object reason, Object attributes) {
+ Preconditions.checkArgument(uid instanceof Integer);
+ Preconditions.checkArgument(opPkg == null || opPkg instanceof String);
+ // The SystemVibrator#vibrate needs effect NonNull.
+ Preconditions.checkArgument(effect instanceof VibrationEffect);
+ Preconditions.checkArgument(reason == null || reason instanceof String);
+ // The SystemVibrator#vibrate needs attributes NonNull.
+ Preconditions.checkArgument(attributes instanceof android.os.VibrationAttributes);
if (effect instanceof VibrationEffect.Composed) {
VibrationEffect.Composed composedEffect = (VibrationEffect.Composed) effect;
vibrationAttributesFromLastVibration = attributes;
diff --git a/shadows/framework/src/main/java/org/robolectric/shadows/ShadowVibrator.java b/shadows/framework/src/main/java/org/robolectric/shadows/ShadowVibrator.java
index df299c7..276a31d 100644
--- a/shadows/framework/src/main/java/org/robolectric/shadows/ShadowVibrator.java
+++ b/shadows/framework/src/main/java/org/robolectric/shadows/ShadowVibrator.java
@@ -3,11 +3,9 @@
import static android.os.Build.VERSION_CODES.R;
import android.media.AudioAttributes;
-import android.os.VibrationAttributes;
import android.os.VibrationEffect;
import android.os.Vibrator;
import android.os.vibrator.PrimitiveSegment;
-import android.os.vibrator.VibrationEffectSegment;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
@@ -25,10 +23,10 @@
static boolean cancelled;
static long milliseconds;
protected static long[] pattern;
- protected static final List<VibrationEffectSegment> vibrationEffectSegments = new ArrayList<>();
+ protected static final List<Object> vibrationEffectSegments = new ArrayList<>();
protected static final List<PrimitiveEffect> primitiveEffects = new ArrayList<>();
protected static final List<Integer> supportedPrimitives = new ArrayList<>();
- @Nullable protected static VibrationAttributes vibrationAttributesFromLastVibration;
+ @Nullable protected static Object vibrationAttributesFromLastVibration;
@Nullable protected static AudioAttributes audioAttributesFromLastVibration;
static int repeat;
static boolean hasVibrator = true;
@@ -84,11 +82,6 @@
return repeat;
}
- /** Returns the last list of {@link VibrationEffectSegment}. */
- public List<VibrationEffectSegment> getVibrationEffectSegments() {
- return vibrationEffectSegments;
- }
-
/** Returns the last list of {@link PrimitiveSegment} vibrations in {@link PrimitiveEffect}. */
@SuppressWarnings("JdkCollectors") // toImmutableList is only supported in Java 8+.
public List<PrimitiveEffect> getPrimitiveSegmentsInPrimitiveEffects() {
@@ -125,9 +118,9 @@
supportedPrimitives.addAll(primitives);
}
- /** Returns the {@link VibrationAttributes} from the last vibration. */
+ /** Returns the {@link android.os.VibrationAttributes} from the last vibration. */
@Nullable
- public VibrationAttributes getVibrationAttributesFromLastVibration() {
+ public Object getVibrationAttributesFromLastVibration() {
return vibrationAttributesFromLastVibration;
}