summaryrefslogtreecommitdiff
path: root/android/view/ViewShowHidePerfTest.java
blob: 6159da4fc3f5d31cd1921810c3e5e8efb95227a3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
/*
 * Copyright (C) 2016 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 android.view;

import static org.junit.Assert.assertTrue;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.perftests.utils.BenchmarkState;
import android.perftests.utils.PerfStatusReporter;
import android.perftests.utils.StubActivity;
import android.support.test.InstrumentationRegistry;
import android.support.test.filters.LargeTest;
import android.support.test.rule.ActivityTestRule;
import android.view.View.MeasureSpec;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;

import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;

import java.util.ArrayList;
import java.util.List;

@RunWith(Parameterized.class)
@LargeTest
public class ViewShowHidePerfTest {

    @Rule
    public ActivityTestRule mActivityRule = new ActivityTestRule(StubActivity.class);

    @Rule
    public PerfStatusReporter mPerfStatusReporter = new PerfStatusReporter();

    public Context getContext() {
        return InstrumentationRegistry.getInstrumentation().getTargetContext();
    }

    static abstract class SubTreeFactory {
        String mName;
        SubTreeFactory(String name) { mName = name; }

        abstract View create(Context context, int depth);

        @Override
        public String toString() {
            return mName;
        }
    }

    private static SubTreeFactory[] sSubTreeFactories = new SubTreeFactory[] {
            new SubTreeFactory("NestedLinearLayoutTree") {
                private int mColorToggle = 0;

                private void createNestedLinearLayoutTree(Context context, LinearLayout parent,
                        int remainingDepth) {
                    if (remainingDepth <= 0) {
                        mColorToggle = (mColorToggle + 1) % 4;
                        parent.setBackgroundColor((mColorToggle < 2) ? Color.RED : Color.BLUE);
                        return;
                    }

                    boolean vertical = remainingDepth % 2 == 0;
                    parent.setOrientation(vertical ? LinearLayout.VERTICAL : LinearLayout.HORIZONTAL);

                    for (int i = 0; i < 2; i++) {
                        LinearLayout child = new LinearLayout(context);
                        // vertical: match parent in x axis, horizontal: y axis.
                        parent.addView(child, new LinearLayout.LayoutParams(
                                (vertical ? ViewGroup.LayoutParams.MATCH_PARENT : 0),
                                (vertical ? 0 : ViewGroup.LayoutParams.MATCH_PARENT),
                                1.0f));

                        createNestedLinearLayoutTree(context, child, remainingDepth - 1);
                    }
                }

                @Override
                public View create(Context context, int depth) {
                    LinearLayout root = new LinearLayout(context);
                    createNestedLinearLayoutTree(context, root, depth - 1);
                    return root;
                }
            },
            new SubTreeFactory("ImageViewList") {
                @Override
                public View create(Context context, int depth) {
                    LinearLayout root = new LinearLayout(context);
                    root.setOrientation(LinearLayout.HORIZONTAL);
                    int childCount = (int) Math.pow(2, depth);
                    for (int i = 0; i < childCount; i++) {
                        ImageView imageView = new ImageView(context);
                        root.addView(imageView, new LinearLayout.LayoutParams(
                                0, ViewGroup.LayoutParams.MATCH_PARENT, 1.0f));
                        imageView.setImageDrawable(new ColorDrawable(Color.RED));
                    }
                    return root;
                }
            },
    };


    @Parameterized.Parameters(name = "Factory:{0},depth:{1}")
    public static Iterable<Object[]> params() {
        List<Object[]> params = new ArrayList<>();
        for (int depth : new int[] { 6 }) {
            for (SubTreeFactory subTreeFactory : sSubTreeFactories) {
                params.add(new Object[]{ subTreeFactory, depth });
            }
        }
        return params;
    }

    private final View mChild;

    public ViewShowHidePerfTest(SubTreeFactory subTreeFactory, int depth) {
        mChild = subTreeFactory.create(getContext(), depth);
    }

    interface TestCallback {
        void run(BenchmarkState state, int width, int height, ViewGroup parent, View child);
    }

    private void testParentWithChild(TestCallback callback) throws Throwable {
        mActivityRule.runOnUiThread(() -> {
            final BenchmarkState state = mPerfStatusReporter.getBenchmarkState();

            FrameLayout parent = new FrameLayout(getContext());
            mActivityRule.getActivity().setContentView(parent);

            final int width = 1000;
            final int height = 1000;
            layout(width, height, parent);

            callback.run(state, width, height, parent, mChild);
        });
    }

    private void updateAndValidateDisplayList(View view) {
        boolean hasDisplayList = view.updateDisplayListIfDirty().isValid();
        assertTrue(hasDisplayList);
    }

    private void layout(int width, int height, View view) {
        view.measure(
                MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY),
                MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY));
        view.layout(0, 0, height, width);
    }

    @Test
    public void testRemove() throws Throwable {
        testParentWithChild((state, width, height, parent, child) -> {
            while (state.keepRunning()) {
                state.pauseTiming();
                updateAndValidateDisplayList(parent); // Note, done to be safe, likely not needed
                parent.addView(child);
                layout(width, height, child);
                updateAndValidateDisplayList(parent);
                state.resumeTiming();

                parent.removeAllViews();
            }
        });
    }

    @Test
    public void testAdd() throws Throwable {
        testParentWithChild((state, width, height, parent, child) -> {
            while (state.keepRunning()) {
                state.pauseTiming();
                layout(width, height, child); // Note, done to be safe, likely not needed
                updateAndValidateDisplayList(parent); // Note, done to be safe, likely not needed
                parent.removeAllViews();
                updateAndValidateDisplayList(parent);
                state.resumeTiming();

                parent.addView(child);
            }
        });
    }

    @Test
    public void testRecordAfterAdd() throws Throwable {
        testParentWithChild((state, width, height, parent, child) -> {
            while (state.keepRunning()) {
                state.pauseTiming();
                parent.removeAllViews();
                updateAndValidateDisplayList(parent); // Note, done to be safe, likely not needed
                parent.addView(child);
                layout(width, height, child);
                state.resumeTiming();

                updateAndValidateDisplayList(parent);
            }
        });
    }

    private void testVisibility(int fromVisibility, int toVisibility) throws Throwable {
        testParentWithChild((state, width, height, parent, child) -> {
            parent.addView(child);

            while (state.keepRunning()) {
                state.pauseTiming();
                layout(width, height, parent);
                updateAndValidateDisplayList(parent);
                child.setVisibility(fromVisibility);
                layout(width, height, parent);
                updateAndValidateDisplayList(parent);
                state.resumeTiming();

                child.setVisibility(toVisibility);
            }
        });
    }

    @Test
    public void testInvisibleToVisible() throws Throwable {
        testVisibility(View.INVISIBLE, View.VISIBLE);
    }

    @Test
    public void testVisibleToInvisible() throws Throwable {
        testVisibility(View.VISIBLE, View.INVISIBLE);
    }
    @Test
    public void testGoneToVisible() throws Throwable {
        testVisibility(View.GONE, View.VISIBLE);
    }

    @Test
    public void testVisibleToGone() throws Throwable {
        testVisibility(View.VISIBLE, View.GONE);
    }
}