Skip to content

Commit

Permalink
Make ShdadowVibrator compatible with SDK 28+
Browse files Browse the repository at this point in the history
Signed-off-by: utzcoz <utzcoz@outlook.com>
  • Loading branch information
utzcoz authored and hoisie committed May 2, 2023
1 parent 866dacc commit 5127cef
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 60 deletions.
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -47,4 +49,9 @@ class NormalCompatibilityTest {
fun `Create speech recognizer succeed`() {
assertThat(SpeechRecognizer.createSpeechRecognizer(application)).isNotNull()
}

@Test
fun `Get default Vibrator succeed`() {
assertThat(application.getSystemService(VIBRATOR_SERVICE) as Vibrator).isNotNull()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -130,48 +130,6 @@ public void getPrimitiveEffects_composeTwice_shouldReturnTheLastComposition() {
new PrimitiveEffect(EFFECT_CLICK, /* scale= */ 1f, /* delay= */ 2150)));
}

@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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,17 @@
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;
import org.robolectric.annotation.Implementation;
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());
Expand Down Expand Up @@ -133,11 +133,14 @@ protected void vibrate(

@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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -25,10 +23,10 @@ public class ShadowVibrator {
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;
Expand Down Expand Up @@ -84,11 +82,6 @@ public int getRepeat() {
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() {
Expand Down Expand Up @@ -125,9 +118,9 @@ public void setSupportedPrimitives(Collection<Integer> primitives) {
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;
}

Expand Down

0 comments on commit 5127cef

Please sign in to comment.