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

Commit 1bad7323 authored by Lais Andrade's avatar Lais Andrade
Browse files

Create benchmark perf tests for View haptics APIs

Use android.perftests.utils.* (older version of go/androidx-bench-dev)
to create apct-tests/core/perftests tests for
View#performHapticFeedback.

Trends should be available at https://android-master-perf.skia.org.

More at go/toolkitty/benchmarks/apct-perf-tests.

Fix: 156901308
Test: ViewPerfTest
Change-Id: Ic265b42b66a66e3388efd6fb30227d11c4474d35
parent a8261988
Loading
Loading
Loading
Loading
+43 −7
Original line number Diff line number Diff line
@@ -16,30 +16,45 @@

package android.view;

import static junit.framework.Assert.assertTrue;

import android.content.Context;
import android.perftests.utils.BenchmarkState;
import android.perftests.utils.PerfStatusReporter;
import android.perftests.utils.PerfTestActivity;
import android.widget.FrameLayout;

import androidx.test.InstrumentationRegistry;
import androidx.test.filters.LargeTest;
import androidx.test.rule.ActivityTestRule;

import com.android.perftests.core.R;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;

@LargeTest
public class ViewPerfTest {
    @Rule
    public PerfStatusReporter mPerfStatusReporter = new PerfStatusReporter();
    public final PerfStatusReporter mPerfStatusReporter = new PerfStatusReporter();

    @Rule
    public final ActivityTestRule<PerfTestActivity> mActivityRule =
            new ActivityTestRule<>(PerfTestActivity.class);

    private Context mContext;

    @Before
    public void setUp() {
        mContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
    }

    @Test
    public void testSimpleViewInflate() {
        final BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
        final Context context = InstrumentationRegistry.getInstrumentation().getTargetContext();
        LayoutInflater inflater = LayoutInflater.from(context);
        FrameLayout root = new FrameLayout(context);
        LayoutInflater inflater = LayoutInflater.from(mContext);
        FrameLayout root = new FrameLayout(mContext);
        while (state.keepRunning()) {
            inflater.inflate(R.layout.test_simple_view, root, false);
        }
@@ -48,11 +63,32 @@ public class ViewPerfTest {
    @Test
    public void testTwelveKeyInflate() {
        final BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
        final Context context = InstrumentationRegistry.getInstrumentation().getTargetContext();
        LayoutInflater inflater = LayoutInflater.from(context);
        FrameLayout root = new FrameLayout(context);
        LayoutInflater inflater = LayoutInflater.from(mContext);
        FrameLayout root = new FrameLayout(mContext);
        while (state.keepRunning()) {
            inflater.inflate(R.layout.twelve_key_entry, root, false);
        }
    }

    @Test
    public void testPerformHapticFeedback() throws Throwable {
        final BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
        mActivityRule.runOnUiThread(() -> {
            state.pauseTiming();
            View view = new View(mContext);
            mActivityRule.getActivity().setContentView(view);
            assertTrue("View needs to be attached to Window to perform haptic feedback",
                    view.isAttachedToWindow());
            state.resumeTiming();

            // Disable settings so perform will never be ignored.
            int flags = HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING
                    | HapticFeedbackConstants.FLAG_IGNORE_GLOBAL_SETTING;

            while (state.keepRunning()) {
                assertTrue("Call to performHapticFeedback was ignored",
                        view.performHapticFeedback(HapticFeedbackConstants.KEYBOARD_PRESS, flags));
            }
        });
    }
}