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

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

Merge "Create benchmark perf tests for View haptics APIs"

parents f0c2688a 1bad7323
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));
            }
        });
    }
}