Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit ddd00b92 authored by Treehugger Robot's avatar Treehugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Introduce HalVibratorHelper for tests" into main

parents eb17f780 429760be
Loading
Loading
Loading
Loading
+43 −40
Original line number Diff line number Diff line
@@ -85,6 +85,7 @@ public class DeviceAdapterTest {
            /* 50Hz= */ 0.08f, 0.16f, 0.32f, 0.64f, /* 150Hz= */ 0.8f, 0.72f, /* 200Hz= */ 0.64f};
    private static final int TEST_MAX_ENVELOPE_EFFECT_SIZE = 10;
    private static final int TEST_MIN_ENVELOPE_EFFECT_CONTROL_POINT_DURATION_MILLIS = 20;
    private static final int TEST_MAX_ENVELOPE_EFFECT_CONTROL_POINT_DURATION_MILLIS = 100;
    private static final float[] TEST_FREQUENCIES_HZ = new float[]{30f, 50f, 100f, 120f, 150f};
    private static final float[] TEST_OUTPUT_ACCELERATIONS_GS =
            new float[]{0.0f, 3.0f, 4.0f, 2.0f, 1.0f};
@@ -102,6 +103,7 @@ public class DeviceAdapterTest {
    @Rule public final SetFlagsRule mSetFlagsRule = new SetFlagsRule();

    @Mock private PackageManagerInternal mPackageManagerInternalMock;
    @Mock private HalVibrator.Callbacks mHalCallbacks;

    private final SparseArray<VibrationEffect> mFallbackEffects = new SparseArray<>();

@@ -143,9 +145,9 @@ public class DeviceAdapterTest {
        mFallbackEffects.put(EFFECT_THUD, VibrationEffect.createOneShot(10, 100));
        VibrationEffect.Composed effect = new VibrationEffect.Composed(Arrays.asList(
                new PrebakedSegment(EFFECT_CLICK, false, VibrationEffect.EFFECT_STRENGTH_LIGHT),
                new PrimitiveSegment(VibrationEffect.Composition.PRIMITIVE_TICK, 1, 10),
                new PrebakedSegment(EFFECT_TICK, true, VibrationEffect.EFFECT_STRENGTH_STRONG),
                new PrimitiveSegment(VibrationEffect.Composition.PRIMITIVE_SPIN, 0.5f, 100)),
                new PrimitiveSegment(PRIMITIVE_TICK, 1, 10),
                new PrebakedSegment(EFFECT_THUD, true, VibrationEffect.EFFECT_STRENGTH_STRONG),
                new PrimitiveSegment(PRIMITIVE_SPIN, 0.5f, 100)),
                /* repeatIndex= */ -1);

        assertThat(mAdapter.adaptToVibrator(BASIC_VIBRATOR_ID, effect)).isEqualTo(effect);
@@ -236,7 +238,7 @@ public class DeviceAdapterTest {
                // Individual step without frequency control, will not use PWLE composition
                // Step(amplitude, frequencyHz, duration)
                new StepSegment(1, 0, 10),
                new PrimitiveSegment(VibrationEffect.Composition.PRIMITIVE_TICK, 1, 10),
                new PrimitiveSegment(PRIMITIVE_TICK, 1, 10),
                // Step with frequency control and followed by ramps, will use PWLE composition
                // Step(amplitude, frequencyHz, duration)
                new StepSegment(0.5f, 0, 10),
@@ -249,7 +251,7 @@ public class DeviceAdapterTest {
        VibrationEffect.Composed expected = new VibrationEffect.Composed(Arrays.asList(
                // Step(amplitude, frequencyHz, duration)
                new StepSegment(1, 0, 10),
                new PrimitiveSegment(VibrationEffect.Composition.PRIMITIVE_TICK, 1, 10),
                new PrimitiveSegment(PRIMITIVE_TICK, 1, 10),
                // Ramp(startAmplitude, endAmplitude, startFrequencyHz, endFrequencyHz, duration)
                new RampSegment(0.5f, 0.5f, 150, 150, 10),
                new RampSegment(0.64f, 0.64f, 125, 125, 100),
@@ -263,9 +265,11 @@ public class DeviceAdapterTest {
    @Test
    public void testMonoCombinedVibration_returnsSameVibrationWhenEffectsUnchanged() {
        VibrationEffect.Composed effect = new VibrationEffect.Composed(Arrays.asList(
                new StepSegment(DEFAULT_AMPLITUDE, 0, 10),
                new StepSegment(0, 0, 10),
                new StepSegment(1, 0, 10)), /* repeatIndex= */ -1);
                new PrebakedSegment(EFFECT_CLICK, false, VibrationEffect.EFFECT_STRENGTH_LIGHT),
                new StepSegment(1, 0, 10),
                new PrebakedSegment(EFFECT_THUD, true, VibrationEffect.EFFECT_STRENGTH_STRONG),
                new StepSegment(1, 0, 10)),
                /* repeatIndex= */ -1);

        CombinedVibration expected = CombinedVibration.createParallel(effect);

@@ -356,7 +360,7 @@ public class DeviceAdapterTest {
    @EnableFlags(Flags.FLAG_NORMALIZED_PWLE_EFFECTS)
    public void testPwleSegment_withoutPwleV2Capability_returnsNull() {
        VibrationEffect.Composed effect = new VibrationEffect.Composed(Arrays.asList(
                new PrimitiveSegment(VibrationEffect.Composition.PRIMITIVE_SPIN, 0.5f, 100),
                new PrimitiveSegment(PRIMITIVE_SPIN, 0.5f, 100),
                new PwleSegment(1, 0.2f, 30, 60, 20),
                new PwleSegment(0.8f, 0.2f, 60, 100, 100),
                new PwleSegment(0.65f, 0.65f, 100, 50, 50)),
@@ -415,7 +419,7 @@ public class DeviceAdapterTest {
    @EnableFlags(Flags.FLAG_NORMALIZED_PWLE_EFFECTS)
    public void testBasicPwleSegment_withoutPwleV2Capability_returnsNull() {
        VibrationEffect.Composed effect = new VibrationEffect.Composed(Arrays.asList(
                new PrimitiveSegment(VibrationEffect.Composition.PRIMITIVE_SPIN, 0.5f, 100),
                new PrimitiveSegment(PRIMITIVE_SPIN, 0.5f, 100),
                new BasicPwleSegment(0.2f, 0.8f, 0.2f, 0.4f, 20),
                new BasicPwleSegment(0.8f, 0.2f, 0.4f, 0.5f, 100),
                new BasicPwleSegment(0.2f, 0.65f, 0.5f, 0.5f, 50)),
@@ -564,59 +568,58 @@ public class DeviceAdapterTest {
    }

    private HalVibrator createEmptyVibrator(int vibratorId) {
        return new FakeVibratorControllerProvider(mTestLooper.getLooper())
                .newVibratorController(vibratorId);
        return new HalVibratorHelper(mTestLooper.getLooper())
                .newInitializedHalVibrator(vibratorId, mHalCallbacks);
    }

    private HalVibrator createBasicVibrator(int vibratorId) {
        FakeVibratorControllerProvider provider = createVibratorProviderWithEffects(
                IVibrator.CAP_COMPOSE_EFFECTS);
        return provider.newVibratorController(vibratorId);
        return createVibratorHelperWithEffects(IVibrator.CAP_COMPOSE_EFFECTS)
                .newInitializedHalVibrator(vibratorId, mHalCallbacks);
    }

    private HalVibrator createPwleWithoutFrequenciesVibrator(int vibratorId) {
        FakeVibratorControllerProvider provider = createVibratorProviderWithEffects(
        HalVibratorHelper helper = createVibratorHelperWithEffects(
                IVibrator.CAP_COMPOSE_EFFECTS, IVibrator.CAP_COMPOSE_PWLE_EFFECTS);
        return provider.newVibratorController(vibratorId);
        return helper.newInitializedHalVibrator(vibratorId, mHalCallbacks);
    }

    private HalVibrator createPwleVibrator(int vibratorId) {
        FakeVibratorControllerProvider provider = createVibratorProviderWithEffects(
        HalVibratorHelper helper = createVibratorHelperWithEffects(
                IVibrator.CAP_COMPOSE_EFFECTS, IVibrator.CAP_COMPOSE_PWLE_EFFECTS);
        provider.setResonantFrequency(TEST_RESONANT_FREQUENCY);
        provider.setMinFrequency(TEST_MIN_FREQUENCY);
        provider.setFrequencyResolution(TEST_FREQUENCY_RESOLUTION);
        provider.setMaxAmplitudes(TEST_AMPLITUDE_MAP);
        return provider.newVibratorController(vibratorId);
        helper.setResonantFrequency(TEST_RESONANT_FREQUENCY);
        helper.setMinFrequency(TEST_MIN_FREQUENCY);
        helper.setFrequencyResolution(TEST_FREQUENCY_RESOLUTION);
        helper.setMaxAmplitudes(TEST_AMPLITUDE_MAP);
        return helper.newHalVibrator(vibratorId);
    }

    private HalVibrator createPwleV2Vibrator(int vibratorId) {
        return createPwleV2Vibrator(vibratorId, TEST_FREQUENCIES_HZ,
                TEST_OUTPUT_ACCELERATIONS_GS);
        return createPwleV2Vibrator(vibratorId, TEST_FREQUENCIES_HZ, TEST_OUTPUT_ACCELERATIONS_GS);
    }

    private HalVibrator createPwleV2Vibrator(int vibratorId, float[] frequencies,
            float[] accelerations) {
        FakeVibratorControllerProvider provider = createVibratorProviderWithEffects(
        HalVibratorHelper helper = createVibratorHelperWithEffects(
                IVibrator.CAP_COMPOSE_EFFECTS, IVibrator.CAP_COMPOSE_PWLE_EFFECTS_V2);
        provider.setResonantFrequency(TEST_RESONANT_FREQUENCY);
        provider.setFrequenciesHz(frequencies);
        provider.setOutputAccelerationsGs(accelerations);
        provider.setMaxEnvelopeEffectSize(TEST_MAX_ENVELOPE_EFFECT_SIZE);
        provider.setMinEnvelopeEffectControlPointDurationMillis(
        helper.setResonantFrequency(TEST_RESONANT_FREQUENCY);
        helper.setFrequenciesHz(frequencies);
        helper.setOutputAccelerationsGs(accelerations);
        helper.setMaxEnvelopeEffectSize(TEST_MAX_ENVELOPE_EFFECT_SIZE);
        helper.setMinEnvelopeEffectControlPointDurationMillis(
                TEST_MIN_ENVELOPE_EFFECT_CONTROL_POINT_DURATION_MILLIS);
        helper.setMaxEnvelopeEffectControlPointDurationMillis(
                TEST_MAX_ENVELOPE_EFFECT_CONTROL_POINT_DURATION_MILLIS);

        return provider.newVibratorController(vibratorId);
        return helper.newHalVibrator(vibratorId);
    }

    private FakeVibratorControllerProvider createVibratorProviderWithEffects(int... capabilities) {
        FakeVibratorControllerProvider provider = new FakeVibratorControllerProvider(
                mTestLooper.getLooper());
        provider.setCapabilities(capabilities);
        provider.setSupportedPrimitives(PRIMITIVE_CLICK, PRIMITIVE_TICK, PRIMITIVE_THUD,
    private HalVibratorHelper createVibratorHelperWithEffects(int... capabilities) {
        HalVibratorHelper helper = new HalVibratorHelper(mTestLooper.getLooper());
        helper.setCapabilities(capabilities);
        helper.setSupportedPrimitives(PRIMITIVE_CLICK, PRIMITIVE_TICK, PRIMITIVE_THUD,
                PRIMITIVE_SPIN, PRIMITIVE_QUICK_RISE, PRIMITIVE_QUICK_FALL, PRIMITIVE_SLOW_RISE);
        provider.setSupportedEffects(EFFECT_CLICK, VibrationEffect.EFFECT_TICK);
        provider.setPrimitiveDuration(TEST_PRIMITIVE_DURATION);
        return provider;
        helper.setSupportedEffects(EFFECT_CLICK, EFFECT_TICK);
        helper.setPrimitiveDuration(TEST_PRIMITIVE_DURATION);
        return helper;
    }
}
+340 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2020 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.server.vibrator;

import static android.os.VibrationEffect.Composition.PRIMITIVE_CLICK;
import static android.os.VibrationEffect.Composition.PRIMITIVE_TICK;
import static android.os.VibrationEffect.EFFECT_CLICK;
import static android.os.VibrationEffect.EFFECT_STRENGTH_MEDIUM;

import static com.google.common.truth.Truth.assertThat;

import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.inOrder;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.mockito.Mockito.when;

import android.hardware.vibrator.IVibrator;
import android.os.IBinder;
import android.os.IVibratorStateListener;
import android.os.VibrationEffect;
import android.os.test.TestLooper;
import android.os.vibrator.PrebakedSegment;
import android.os.vibrator.PrimitiveSegment;
import android.os.vibrator.PwlePoint;
import android.os.vibrator.RampSegment;
import android.os.vibrator.StepSegment;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.mockito.InOrder;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnit;
import org.mockito.junit.MockitoRule;

/** Base test class for {@link HalVibrator} implementations. */
public abstract class HalVibratorTestCase {
    static final int VIBRATOR_ID = 0;

    @Rule public MockitoRule rule = MockitoJUnit.rule();

    @Mock private HalVibrator.Callbacks mCallbacksMock;
    @Mock private IVibratorStateListener mVibratorStateListenerMock;
    @Mock private IBinder mVibratorStateListenerBinderMock;

    private final TestLooper mTestLooper = new TestLooper();
    final HalVibratorHelper mHelper = new HalVibratorHelper(mTestLooper.getLooper());

    abstract HalVibrator newVibrator(int vibratorId);

    HalVibrator newInitializedVibrator(int vibratorId) {
        HalVibrator vibrator = newVibrator(vibratorId);
        vibrator.init(mCallbacksMock);
        vibrator.onSystemReady();
        return vibrator;
    }

    @Before
    public void setUp() throws Exception {
        when(mVibratorStateListenerMock.asBinder()).thenReturn(mVibratorStateListenerBinderMock);
    }

    @Test
    public void init_initializesVibratorAndSetsId() {
        HalVibrator vibrator = newVibrator(VIBRATOR_ID);
        vibrator.init(mCallbacksMock);

        assertThat(mHelper.isInitialized()).isTrue();
        assertThat(vibrator.getInfo().getId()).isEqualTo(VIBRATOR_ID);
    }

    @Test
    public void init_turnsOffVibratorAndDisablesExternalControl() {
        mHelper.setCapabilities(IVibrator.CAP_EXTERNAL_CONTROL);
        HalVibrator vibrator = newVibrator(VIBRATOR_ID);
        vibrator.init(mCallbacksMock);

        assertThat(vibrator.isVibrating()).isFalse();
        assertThat(mHelper.getOffCount()).isEqualTo(1);
        assertThat(mHelper.getExternalControlStates()).containsExactly(false).inOrder();
    }

    @Test
    public void onSystemReady_loadSucceeded_doesNotReload() {
        mHelper.setCapabilities(IVibrator.CAP_EXTERNAL_CONTROL);
        HalVibrator vibrator = newVibrator(VIBRATOR_ID);

        vibrator.init(mCallbacksMock);
        assertThat(vibrator.getInfo().getCapabilities()).isEqualTo(IVibrator.CAP_EXTERNAL_CONTROL);

        mHelper.setCapabilities(IVibrator.CAP_AMPLITUDE_CONTROL); // will be ignored
        vibrator.onSystemReady();
        assertThat(vibrator.getInfo().getCapabilities()).isEqualTo(IVibrator.CAP_EXTERNAL_CONTROL);
    }

    @Test
    public void onSystemReady_loadFailed_reloadsVibratorInfo() {
        mHelper.setCapabilities(IVibrator.CAP_EXTERNAL_CONTROL);
        mHelper.setLoadInfoToFail();
        HalVibrator vibrator = newVibrator(VIBRATOR_ID);

        vibrator.init(mCallbacksMock);
        assertThat(vibrator.getInfo().getCapabilities()).isEqualTo(IVibrator.CAP_EXTERNAL_CONTROL);

        mHelper.setCapabilities(IVibrator.CAP_AMPLITUDE_CONTROL); // will be loaded
        vibrator.onSystemReady();
        assertThat(vibrator.getInfo().getCapabilities()).isEqualTo(IVibrator.CAP_AMPLITUDE_CONTROL);
    }

    @Test
    public void setExternalControl_withCapability_enablesExternalControl() {
        mHelper.setCapabilities(IVibrator.CAP_EXTERNAL_CONTROL);
        HalVibrator vibrator = newInitializedVibrator(VIBRATOR_ID);
        assertThat(vibrator.isVibrating()).isFalse();

        vibrator.setExternalControl(true);
        assertThat(vibrator.isVibrating()).isTrue();

        vibrator.setExternalControl(false);
        assertThat(vibrator.isVibrating()).isFalse();

        assertThat(mHelper.getExternalControlStates())
                .containsExactly(false, true, false).inOrder();
    }

    @Test
    public void setExternalControl_withNoCapability_ignoresExternalControl() {
        HalVibrator vibrator = newInitializedVibrator(VIBRATOR_ID);
        assertThat(vibrator.isVibrating()).isFalse();

        vibrator.setExternalControl(true);
        assertThat(vibrator.isVibrating()).isFalse();

        assertThat(mHelper.getExternalControlStates()).isEmpty();
    }

    @Test
    public void setAlwaysOn_withCapability_enablesAndDisablesAlwaysOnEffect() {
        mHelper.setCapabilities(IVibrator.CAP_ALWAYS_ON_CONTROL);
        mHelper.setSupportedEffects(EFFECT_CLICK);
        HalVibrator vibrator = newInitializedVibrator(VIBRATOR_ID);

        PrebakedSegment prebaked = createPrebaked(EFFECT_CLICK, EFFECT_STRENGTH_MEDIUM);

        vibrator.setAlwaysOn(1, prebaked);
        assertThat(mHelper.getAlwaysOnEffect(1)).isEqualTo(prebaked);

        vibrator.setAlwaysOn(1, null);
        assertThat(mHelper.getAlwaysOnEffect(1)).isNull();
    }

    @Test
    public void setAlwaysOn_withoutCapability_ignoresEffect() {
        mHelper.setSupportedEffects(EFFECT_CLICK);
        HalVibrator vibrator = newInitializedVibrator(VIBRATOR_ID);
        PrebakedSegment prebaked = createPrebaked(EFFECT_CLICK, EFFECT_STRENGTH_MEDIUM);

        vibrator.setAlwaysOn(1, prebaked);
        assertThat(mHelper.getAlwaysOnEffect(1)).isNull();
    }

    @Test
    public void setAmplitude_vibratorIdle_ignoresAmplitude() {
        mHelper.setCapabilities(IVibrator.CAP_AMPLITUDE_CONTROL);
        HalVibrator vibrator = newInitializedVibrator(VIBRATOR_ID);
        assertThat(vibrator.isVibrating()).isFalse();

        vibrator.setAmplitude(1);
        assertThat(vibrator.getCurrentAmplitude()).isEqualTo(0f);
        assertThat(mHelper.getAmplitudes()).containsExactly(1f).inOrder();
    }

    @Test
    public void setAmplitude_vibratorUnderExternalControl_ignoresAmplitude() {
        mHelper.setCapabilities(IVibrator.CAP_AMPLITUDE_CONTROL, IVibrator.CAP_EXTERNAL_CONTROL);
        HalVibrator vibrator = newInitializedVibrator(VIBRATOR_ID);

        vibrator.setExternalControl(true);
        assertThat(vibrator.isVibrating()).isTrue();
        assertThat(vibrator.getCurrentAmplitude()).isEqualTo(-1f);

        vibrator.setAmplitude(1);
        assertThat(vibrator.getCurrentAmplitude()).isEqualTo(-1f);
        assertThat(mHelper.getAmplitudes()).containsExactly(1f).inOrder();
    }

    @Test
    public void setAmplitude_vibratorVibrating_setsAmplitude() {
        mHelper.setCapabilities(IVibrator.CAP_AMPLITUDE_CONTROL);
        HalVibrator vibrator = newInitializedVibrator(VIBRATOR_ID);
        vibrator.on(1, 1, /* milliseconds= */ 100);
        assertThat(vibrator.isVibrating()).isTrue();
        assertThat(vibrator.getCurrentAmplitude()).isEqualTo(-1f);

        vibrator.setAmplitude(1);
        assertThat(vibrator.getCurrentAmplitude()).isEqualTo(1f);
        assertThat(mHelper.getAmplitudes()).containsExactly(1f).inOrder();
    }

    @Test
    public void on_withDuration_turnsVibratorOn() {
        HalVibrator vibrator = newInitializedVibrator(VIBRATOR_ID);

        assertThat(vibrator.on(1, 1, /* milliseconds= */ 100)).isEqualTo(100L);
        assertThat(vibrator.isVibrating()).isTrue();
        assertThat(vibrator.getCurrentAmplitude()).isEqualTo(-1f);
        assertThat(mHelper.getEffectSegments(1)).containsExactly(createStep(100)).inOrder();
    }

    @Test
    public void on_withPrebaked_performsEffect() {
        mHelper.setSupportedEffects(EFFECT_CLICK);
        HalVibrator vibrator = newInitializedVibrator(VIBRATOR_ID);
        PrebakedSegment prebaked = createPrebaked(EFFECT_CLICK, EFFECT_STRENGTH_MEDIUM);

        assertThat(vibrator.on(1, 1, prebaked)).isEqualTo(HalVibratorHelper.EFFECT_DURATION);
        assertThat(vibrator.isVibrating()).isTrue();
        assertThat(vibrator.getCurrentAmplitude()).isEqualTo(-1f);
        assertThat(mHelper.getEffectSegments(1)).containsExactly(prebaked).inOrder();
    }

    @Test
    public void on_withComposed_performsEffect() {
        mHelper.setCapabilities(IVibrator.CAP_COMPOSE_EFFECTS);
        mHelper.setSupportedPrimitives(PRIMITIVE_CLICK, PRIMITIVE_TICK);
        HalVibrator vibrator = newInitializedVibrator(VIBRATOR_ID);
        PrimitiveSegment[] primitives = new PrimitiveSegment[]{
                new PrimitiveSegment(PRIMITIVE_CLICK, 0.5f, 10),
                new PrimitiveSegment(PRIMITIVE_TICK, 0.7f, 100),
        };

        long expectedDuration = 2 * HalVibratorHelper.EFFECT_DURATION + 10 + 100;
        assertThat(vibrator.on(1, 1, primitives)).isEqualTo(expectedDuration);
        assertThat(vibrator.isVibrating()).isTrue();
        assertThat(vibrator.getCurrentAmplitude()).isEqualTo(-1f);
        assertThat(mHelper.getEffectSegments(1)).containsExactlyElementsIn(primitives).inOrder();
    }

    @Test
    public void on_withComposedPwle_performsEffect() {
        mHelper.setCapabilities(IVibrator.CAP_COMPOSE_PWLE_EFFECTS);
        HalVibrator vibrator = newInitializedVibrator(VIBRATOR_ID);
        RampSegment[] ramps = new RampSegment[]{
                new RampSegment(/* startAmplitude= */ 0, /* endAmplitude= */ 1,
                        /* startFrequencyHz= */ 100, /* endFrequencyHz= */ 200, /* duration= */ 10)
        };

        assertThat(vibrator.on(1, 1, ramps)).isEqualTo(10L);
        assertThat(vibrator.isVibrating()).isTrue();
        assertThat(vibrator.getCurrentAmplitude()).isEqualTo(-1f);
        assertThat(mHelper.getEffectSegments(1)).containsExactlyElementsIn(ramps).inOrder();
    }

    @Test
    public void on_withComposedPwleV2_performsEffect() {
        mHelper.setCapabilities(IVibrator.CAP_COMPOSE_PWLE_EFFECTS_V2);
        HalVibrator vibrator = newInitializedVibrator(VIBRATOR_ID);
        PwlePoint[] points = new PwlePoint[]{
                new PwlePoint(/*amplitude=*/ 0, /*frequencyHz=*/ 100, /*timeMillis=*/ 0),
                new PwlePoint(/*amplitude=*/ 1, /*frequencyHz=*/ 200, /*timeMillis=*/ 30)
        };

        assertThat(vibrator.on(1, 1, points)).isEqualTo(30L);
        assertThat(vibrator.isVibrating()).isTrue();
        assertThat(vibrator.getCurrentAmplitude()).isEqualTo(-1f);
        assertThat(mHelper.getEffectPwlePoints(1)).containsExactlyElementsIn(points).inOrder();
    }

    @Test
    public void off_turnsOffVibrator() {
        HalVibrator vibrator = newInitializedVibrator(VIBRATOR_ID);

        vibrator.on(1, 1, /* milliseconds= */ 100);
        assertThat(vibrator.isVibrating()).isTrue();

        vibrator.off();
        vibrator.off();
        assertThat(vibrator.isVibrating()).isFalse();
        assertThat(mHelper.getOffCount()).isEqualTo(3); // one extra call from system ready.
    }

    @Test
    public void registerVibratorStateListener_callbacksAreTriggered() throws Exception {
        HalVibrator vibrator = newInitializedVibrator(VIBRATOR_ID);

        vibrator.registerVibratorStateListener(mVibratorStateListenerMock);
        vibrator.on(1, 1, /* milliseconds= */ 10);
        vibrator.on(2, 1, /* milliseconds= */ 100);
        vibrator.off();
        vibrator.off();

        InOrder inOrderVerifier = inOrder(mVibratorStateListenerMock);
        // First notification done when listener is registered.
        inOrderVerifier.verify(mVibratorStateListenerMock).onVibrating(false);
        inOrderVerifier.verify(mVibratorStateListenerMock).onVibrating(eq(true));
        inOrderVerifier.verify(mVibratorStateListenerMock).onVibrating(eq(false));
        inOrderVerifier.verifyNoMoreInteractions();
    }

    @Test
    public void unregisterVibratorStateListener_callbackNotTriggeredAfter() throws Exception {
        HalVibrator vibrator = newInitializedVibrator(VIBRATOR_ID);

        vibrator.registerVibratorStateListener(mVibratorStateListenerMock);
        verify(mVibratorStateListenerMock).onVibrating(false);

        vibrator.on(1, 1, /* milliseconds= */ 10);
        verify(mVibratorStateListenerMock).onVibrating(true);

        vibrator.unregisterVibratorStateListener(mVibratorStateListenerMock);
        Mockito.clearInvocations(mVibratorStateListenerMock);

        vibrator.on(2, 1, /* milliseconds= */ 100);
        verifyNoMoreInteractions(mVibratorStateListenerMock);
    }

    private PrebakedSegment createPrebaked(int effectId, int effectStrength) {
        return new PrebakedSegment(effectId, /* shouldFallback= */ false, effectStrength);
    }

    private StepSegment createStep(int millis) {
        return new StepSegment(VibrationEffect.DEFAULT_AMPLITUDE, 0f, millis);
    }
}
+330 −365

File changed.

Preview size limit exceeded, changes collapsed.

+4 −372

File changed.

Preview size limit exceeded, changes collapsed.

+264 −276

File changed.

Preview size limit exceeded, changes collapsed.

Loading