1/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of 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,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License
15 */
16
17package android.widget;
18
19import android.app.Activity;
20import android.os.Looper;
21import android.os.Bundle;
22import android.perftests.utils.PerfStatusReporter;
23import android.util.Log;
24import android.view.View;
25
26import android.perftests.utils.BenchmarkState;
27import android.perftests.utils.StubActivity;
28import android.support.test.filters.LargeTest;
29import android.support.test.runner.AndroidJUnit4;
30import android.support.test.rule.ActivityTestRule;
31import android.support.test.InstrumentationRegistry;
32
33import com.android.perftests.core.R;
34
35import java.util.Locale;
36import java.util.Collection;
37import java.util.Arrays;
38
39import org.junit.Test;
40import org.junit.Rule;
41import org.junit.runners.Parameterized;
42import org.junit.runners.Parameterized.Parameters;
43import org.junit.runner.RunWith;
44
45import static org.junit.Assert.assertTrue;
46
47@LargeTest
48@RunWith(Parameterized.class)
49public class TextViewAutoSizeLayoutPerfTest {
50    @Parameters(name = "{0}")
51    public static Collection layouts() {
52        return Arrays.asList(new Object[][] {
53                { "Basic TextView - no autosize", R.layout.test_basic_textview_layout},
54                { "Autosize TextView 5 sizes", R.layout.test_autosize_textview_5},
55                { "Autosize TextView 10 sizes", R.layout.test_autosize_textview_10},
56                { "Autosize TextView 50 sizes", R.layout.test_autosize_textview_50},
57                { "Autosize TextView 100 sizes", R.layout.test_autosize_textview_100},
58                { "Autosize TextView 300 sizes", R.layout.test_autosize_textview_300},
59                { "Autosize TextView 500 sizes", R.layout.test_autosize_textview_500},
60                { "Autosize TextView 1000 sizes", R.layout.test_autosize_textview_1000},
61                { "Autosize TextView 10000 sizes", R.layout.test_autosize_textview_10000},
62                { "Autosize TextView 100000 sizes", R.layout.test_autosize_textview_100000}
63        });
64    }
65
66    private int mLayoutId;
67
68    public TextViewAutoSizeLayoutPerfTest(String key, int layoutId) {
69        mLayoutId = layoutId;
70    }
71
72    @Rule
73    public ActivityTestRule<StubActivity> mActivityRule =
74            new ActivityTestRule(StubActivity.class);
75
76    @Rule
77    public PerfStatusReporter mPerfStatusReporter = new PerfStatusReporter();
78
79    @Test
80    public void testConstruction() throws Throwable {
81        mActivityRule.runOnUiThread(() -> {
82            assertTrue("We should be running on the main thread",
83                    Looper.getMainLooper().getThread() == Thread.currentThread());
84            assertTrue("We should be running on the main thread",
85                    Looper.myLooper() == Looper.getMainLooper());
86            BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
87            Activity activity = mActivityRule.getActivity();
88            activity.setContentView(mLayoutId);
89
90            while (state.keepRunning()) {
91                TextView textView = new TextView(activity);
92                // TextView#onLayout() gets called, which triggers TextView#autoSizeText()
93                // which is the method we want to benchmark.
94                textView.requestLayout();
95            }
96        });
97    }
98}
99