1/*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5 * use this file except in compliance with the License. You may obtain a copy of
6 * the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 * License for the specific language governing permissions and limitations under
14 * the License.
15 */
16package android.text;
17
18import android.graphics.Canvas;
19import android.perftests.utils.BenchmarkState;
20import android.perftests.utils.PerfStatusReporter;
21import android.support.test.filters.LargeTest;
22import android.text.NonEditableTextGenerator.TextType;
23
24import org.junit.Rule;
25import org.junit.Test;
26import org.junit.runner.RunWith;
27import org.junit.runners.Parameterized;
28
29import java.util.ArrayList;
30import java.util.Collection;
31import java.util.List;
32import java.util.Random;
33
34/**
35 * Performance test for {@link BoringLayout#isBoring(CharSequence, TextPaint)}.
36 */
37@LargeTest
38@RunWith(Parameterized.class)
39public class BoringLayoutIsBoringPerfTest {
40
41    private static final boolean[] BOOLEANS = new boolean[]{false, true};
42
43    @Parameterized.Parameters(name = "cached={4},{1}chars,{0}")
44    public static Collection cases() {
45        final List<Object[]> params = new ArrayList<>();
46        for (int length : new int[]{128}) {
47            for (boolean boring : BOOLEANS) {
48                for (boolean cached : BOOLEANS) {
49                    for (TextType textType : new TextType[]{TextType.STRING,
50                            TextType.SPANNABLE_BUILDER}) {
51                        params.add(new Object[]{
52                                (boring ? "Boring" : "NotBoring") + "," + textType.name(),
53                                length, boring, textType, cached});
54                    }
55                }
56            }
57        }
58        return params;
59    }
60
61    @Rule
62    public PerfStatusReporter mPerfStatusReporter = new PerfStatusReporter();
63
64    private final int mLength;
65    private final TextType mTextType;
66    private final boolean mCreateBoring;
67    private final boolean mCached;
68    private final TextPaint mTextPaint;
69
70    public BoringLayoutIsBoringPerfTest(String label, int length, boolean boring, TextType textType,
71            boolean cached) {
72        mLength = length;
73        mCreateBoring = boring;
74        mCached = cached;
75        mTextType = textType;
76        mTextPaint = new TextPaint();
77        mTextPaint.setTextSize(10);
78    }
79
80    /**
81     * Measure the time for the {@link BoringLayout#isBoring(CharSequence, TextPaint)}.
82     */
83    @Test
84    public void timeIsBoring() throws Exception {
85        final BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
86
87        state.pauseTiming();
88        Canvas.freeTextLayoutCaches();
89        final CharSequence text = createRandomText();
90        if (mCached) BoringLayout.isBoring(text, mTextPaint);
91        state.resumeTiming();
92
93        while (state.keepRunning()) {
94            state.pauseTiming();
95            if (!mCached) Canvas.freeTextLayoutCaches();
96            state.resumeTiming();
97
98            BoringLayout.isBoring(text, mTextPaint);
99        }
100    }
101
102    private CharSequence createRandomText() {
103        return new NonEditableTextGenerator(new Random(0))
104                .setSequenceLength(mLength)
105                .setCreateBoring(mCreateBoring)
106                .setTextType(mTextType)
107                .build();
108    }
109}
110