| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.view; |
| 18 |
|
| 19 |
import static org.junit.Assert.assertTrue; |
| 20 |
|
| 21 |
import android.content.Context; |
| 22 |
import android.graphics.Color; |
| 23 |
import android.graphics.drawable.ColorDrawable; |
| 24 |
import android.perftests.utils.PerfTestActivity; |
| 25 |
import android.view.View.MeasureSpec; |
| 26 |
import android.widget.FrameLayout; |
| 27 |
import android.widget.ImageView; |
| 28 |
import android.widget.LinearLayout; |
| 29 |
|
| 30 |
import androidx.benchmark.BenchmarkState; |
| 31 |
import androidx.benchmark.junit4.BenchmarkRule; |
| 32 |
import androidx.test.InstrumentationRegistry; |
| 33 |
import androidx.test.filters.LargeTest; |
| 34 |
import androidx.test.rule.ActivityTestRule; |
| 35 |
|
| 36 |
import org.junit.Rule; |
| 37 |
import org.junit.Test; |
| 38 |
import org.junit.runner.RunWith; |
| 39 |
import org.junit.runners.Parameterized; |
| 40 |
|
| 41 |
import java.util.ArrayList; |
| 42 |
import java.util.List; |
| 43 |
|
| 44 |
@RunWith(Parameterized.class) |
| 45 |
@LargeTest |
| 46 |
public class ViewShowHidePerfTest { |
| 47 |
|
| 48 |
@Rule |
| 49 |
public ActivityTestRule<PerfTestActivity> mActivityRule = |
| 50 |
new ActivityTestRule<>(PerfTestActivity.class); |
| 51 |
|
| 52 |
@Rule |
| 53 |
public final BenchmarkRule mBenchmarkRule = new BenchmarkRule(); |
| 54 |
|
| 55 |
public Context getContext() { |
| 56 |
return InstrumentationRegistry.getInstrumentation().getTargetContext(); |
| 57 |
} |
| 58 |
|
| 59 |
static abstract class SubTreeFactory { |
| 60 |
String mName; |
| 61 |
SubTreeFactory(String name) { mName = name; } |
| 62 |
|
| 63 |
abstract View create(Context context, int depth); |
| 64 |
|
| 65 |
@Override |
| 66 |
public String toString() { |
| 67 |
return mName; |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
private static SubTreeFactory[] sSubTreeFactories = new SubTreeFactory[] { |
| 72 |
new SubTreeFactory("NestedLinearLayoutTree") { |
| 73 |
private int mColorToggle = 0; |
| 74 |
|
| 75 |
private void createNestedLinearLayoutTree(Context context, LinearLayout parent, |
| 76 |
int remainingDepth) { |
| 77 |
if (remainingDepth <= 0) { |
| 78 |
mColorToggle = (mColorToggle + 1) % 4; |
| 79 |
parent.setBackgroundColor((mColorToggle < 2) ? Color.RED : Color.BLUE); |
| 80 |
return; |
| 81 |
} |
| 82 |
|
| 83 |
boolean vertical = remainingDepth % 2 == 0; |
| 84 |
parent.setOrientation(vertical ? LinearLayout.VERTICAL : LinearLayout.HORIZONTAL); |
| 85 |
|
| 86 |
for (int i = 0; i < 2; i++) { |
| 87 |
LinearLayout child = new LinearLayout(context); |
| 88 |
|
| 89 |
parent.addView(child, new LinearLayout.LayoutParams( |
| 90 |
(vertical ? ViewGroup.LayoutParams.MATCH_PARENT : 0), |
| 91 |
(vertical ? 0 : ViewGroup.LayoutParams.MATCH_PARENT), |
| 92 |
1.0f)); |
| 93 |
|
| 94 |
createNestedLinearLayoutTree(context, child, remainingDepth - 1); |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
@Override |
| 99 |
public View create(Context context, int depth) { |
| 100 |
LinearLayout root = new LinearLayout(context); |
| 101 |
createNestedLinearLayoutTree(context, root, depth - 1); |
| 102 |
return root; |
| 103 |
} |
| 104 |
}, |
| 105 |
new SubTreeFactory("ImageViewList") { |
| 106 |
@Override |
| 107 |
public View create(Context context, int depth) { |
| 108 |
LinearLayout root = new LinearLayout(context); |
| 109 |
root.setOrientation(LinearLayout.HORIZONTAL); |
| 110 |
int childCount = (int) Math.pow(2, depth); |
| 111 |
for (int i = 0; i < childCount; i++) { |
| 112 |
ImageView imageView = new ImageView(context); |
| 113 |
root.addView(imageView, new LinearLayout.LayoutParams( |
| 114 |
0, ViewGroup.LayoutParams.MATCH_PARENT, 1.0f)); |
| 115 |
imageView.setImageDrawable(new ColorDrawable(Color.RED)); |
| 116 |
} |
| 117 |
return root; |
| 118 |
} |
| 119 |
}, |
| 120 |
}; |
| 121 |
|
| 122 |
|
| 123 |
@Parameterized.Parameters(name = "Factory:{0},depth:{1}") |
| 124 |
public static Iterable<Object[]> params() { |
| 125 |
List<Object[]> params = new ArrayList<>(); |
| 126 |
for (int depth : new int[] { 6 }) { |
| 127 |
for (SubTreeFactory subTreeFactory : sSubTreeFactories) { |
| 128 |
params.add(new Object[]{ subTreeFactory, depth }); |
| 129 |
} |
| 130 |
} |
| 131 |
return params; |
| 132 |
} |
| 133 |
|
| 134 |
private final View mChild; |
| 135 |
|
| 136 |
public ViewShowHidePerfTest(SubTreeFactory subTreeFactory, int depth) { |
| 137 |
mChild = subTreeFactory.create(getContext(), depth); |
| 138 |
} |
| 139 |
|
| 140 |
interface TestCallback { |
| 141 |
void run(BenchmarkState state, int width, int height, ViewGroup parent, View child); |
| 142 |
} |
| 143 |
|
| 144 |
private void testParentWithChild(TestCallback callback) throws Throwable { |
| 145 |
mActivityRule.runOnUiThread(() -> { |
| 146 |
final BenchmarkState state = mBenchmarkRule.getState(); |
| 147 |
|
| 148 |
FrameLayout parent = new FrameLayout(getContext()); |
| 149 |
mActivityRule.getActivity().setContentView(parent); |
| 150 |
|
| 151 |
final int width = 1000; |
| 152 |
final int height = 1000; |
| 153 |
layout(width, height, parent); |
| 154 |
|
| 155 |
callback.run(state, width, height, parent, mChild); |
| 156 |
}); |
| 157 |
} |
| 158 |
|
| 159 |
private void updateAndValidateDisplayList(View view) { |
| 160 |
boolean hasDisplayList = view.updateDisplayListIfDirty().hasDisplayList(); |
| 161 |
assertTrue(hasDisplayList); |
| 162 |
} |
| 163 |
|
| 164 |
private void layout(int width, int height, View view) { |
| 165 |
view.measure( |
| 166 |
MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY), |
| 167 |
MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY)); |
| 168 |
view.layout(0, 0, height, width); |
| 169 |
} |
| 170 |
|
| 171 |
@Test |
| 172 |
public void testRemove() throws Throwable { |
| 173 |
testParentWithChild((state, width, height, parent, child) -> { |
| 174 |
while (state.keepRunning()) { |
| 175 |
state.pauseTiming(); |
| 176 |
updateAndValidateDisplayList(parent); |
| 177 |
parent.addView(child); |
| 178 |
layout(width, height, child); |
| 179 |
updateAndValidateDisplayList(parent); |
| 180 |
state.resumeTiming(); |
| 181 |
|
| 182 |
parent.removeAllViews(); |
| 183 |
} |
| 184 |
}); |
| 185 |
} |
| 186 |
|
| 187 |
@Test |
| 188 |
public void testAdd() throws Throwable { |
| 189 |
testParentWithChild((state, width, height, parent, child) -> { |
| 190 |
while (state.keepRunning()) { |
| 191 |
state.pauseTiming(); |
| 192 |
layout(width, height, child); |
| 193 |
updateAndValidateDisplayList(parent); |
| 194 |
parent.removeAllViews(); |
| 195 |
updateAndValidateDisplayList(parent); |
| 196 |
state.resumeTiming(); |
| 197 |
|
| 198 |
parent.addView(child); |
| 199 |
} |
| 200 |
}); |
| 201 |
} |
| 202 |
|
| 203 |
@Test |
| 204 |
public void testRecordAfterAdd() throws Throwable { |
| 205 |
testParentWithChild((state, width, height, parent, child) -> { |
| 206 |
while (state.keepRunning()) { |
| 207 |
state.pauseTiming(); |
| 208 |
parent.removeAllViews(); |
| 209 |
updateAndValidateDisplayList(parent); |
| 210 |
parent.addView(child); |
| 211 |
layout(width, height, child); |
| 212 |
state.resumeTiming(); |
| 213 |
|
| 214 |
updateAndValidateDisplayList(parent); |
| 215 |
} |
| 216 |
}); |
| 217 |
} |
| 218 |
|
| 219 |
private void testVisibility(int fromVisibility, int toVisibility) throws Throwable { |
| 220 |
testParentWithChild((state, width, height, parent, child) -> { |
| 221 |
parent.addView(child); |
| 222 |
|
| 223 |
while (state.keepRunning()) { |
| 224 |
state.pauseTiming(); |
| 225 |
layout(width, height, parent); |
| 226 |
updateAndValidateDisplayList(parent); |
| 227 |
child.setVisibility(fromVisibility); |
| 228 |
layout(width, height, parent); |
| 229 |
updateAndValidateDisplayList(parent); |
| 230 |
state.resumeTiming(); |
| 231 |
|
| 232 |
child.setVisibility(toVisibility); |
| 233 |
} |
| 234 |
}); |
| 235 |
} |
| 236 |
|
| 237 |
@Test |
| 238 |
public void testInvisibleToVisible() throws Throwable { |
| 239 |
testVisibility(View.INVISIBLE, View.VISIBLE); |
| 240 |
} |
| 241 |
|
| 242 |
@Test |
| 243 |
public void testVisibleToInvisible() throws Throwable { |
| 244 |
testVisibility(View.VISIBLE, View.INVISIBLE); |
| 245 |
} |
| 246 |
@Test |
| 247 |
public void testGoneToVisible() throws Throwable { |
| 248 |
testVisibility(View.GONE, View.VISIBLE); |
| 249 |
} |
| 250 |
|
| 251 |
@Test |
| 252 |
public void testVisibleToGone() throws Throwable { |
| 253 |
testVisibility(View.VISIBLE, View.GONE); |
| 254 |
} |
| 255 |
} |