| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.widget; |
| 18 |
|
| 19 |
import static android.perftests.utils.LayoutUtils.gatherViewTree; |
| 20 |
import static android.perftests.utils.LayoutUtils.requestLayoutForAllNodes; |
| 21 |
import static android.view.View.MeasureSpec.AT_MOST; |
| 22 |
import static android.view.View.MeasureSpec.EXACTLY; |
| 23 |
import static android.view.View.MeasureSpec.UNSPECIFIED; |
| 24 |
|
| 25 |
import static org.junit.Assert.assertTrue; |
| 26 |
|
| 27 |
import android.app.Activity; |
| 28 |
import android.os.Looper; |
| 29 |
import android.perftests.utils.BenchmarkState; |
| 30 |
import android.perftests.utils.PerfStatusReporter; |
| 31 |
import android.perftests.utils.PerfTestActivity; |
| 32 |
import android.view.View; |
| 33 |
import android.view.ViewGroup; |
| 34 |
|
| 35 |
import androidx.test.annotation.UiThreadTest; |
| 36 |
import androidx.test.filters.LargeTest; |
| 37 |
import androidx.test.rule.ActivityTestRule; |
| 38 |
|
| 39 |
import com.android.perftests.core.R; |
| 40 |
|
| 41 |
import org.junit.Rule; |
| 42 |
import org.junit.Test; |
| 43 |
import org.junit.runner.RunWith; |
| 44 |
import org.junit.runners.Parameterized; |
| 45 |
|
| 46 |
import java.util.Arrays; |
| 47 |
import java.util.Collection; |
| 48 |
import java.util.List; |
| 49 |
|
| 50 |
@LargeTest |
| 51 |
@RunWith(Parameterized.class) |
| 52 |
public class LayoutPerfTest { |
| 53 |
@Parameterized.Parameters(name = "{0}") |
| 54 |
public static Collection measureSpecs() { |
| 55 |
return Arrays.asList(new Object[][] { |
| 56 |
{ "relative", R.layout.test_relative_layout, R.id.relative_layout_root }, |
| 57 |
{ "linear", R.layout.test_linear_layout, R.id.linear_layout_root }, |
| 58 |
{ "linear_weighted", R.layout.test_linear_layout_weighted, |
| 59 |
R.id.linear_layout_weighted_root }, |
| 60 |
}); |
| 61 |
} |
| 62 |
|
| 63 |
private int[] mMeasureSpecs = {EXACTLY, AT_MOST, UNSPECIFIED}; |
| 64 |
|
| 65 |
private int mLayoutId; |
| 66 |
private int mViewId; |
| 67 |
|
| 68 |
public LayoutPerfTest(String key, int layoutId, int viewId) { |
| 69 |
|
| 70 |
mLayoutId = layoutId; |
| 71 |
mViewId = viewId; |
| 72 |
} |
| 73 |
|
| 74 |
@Rule |
| 75 |
public ActivityTestRule<PerfTestActivity> mActivityRule = |
| 76 |
new ActivityTestRule(PerfTestActivity.class); |
| 77 |
|
| 78 |
@Rule |
| 79 |
public PerfStatusReporter mPerfStatusReporter = new PerfStatusReporter(); |
| 80 |
|
| 81 |
@Test |
| 82 |
@UiThreadTest |
| 83 |
public void testLayoutPerf() throws Throwable { |
| 84 |
mActivityRule.runOnUiThread(() -> { |
| 85 |
assertTrue("We should be running on the main thread", |
| 86 |
Looper.getMainLooper().getThread() == Thread.currentThread()); |
| 87 |
assertTrue("We should be running on the main thread", |
| 88 |
Looper.myLooper() == Looper.getMainLooper()); |
| 89 |
|
| 90 |
Activity activity = mActivityRule.getActivity(); |
| 91 |
activity.setContentView(mLayoutId); |
| 92 |
|
| 93 |
ViewGroup viewGroup = (ViewGroup) activity.findViewById(mViewId); |
| 94 |
|
| 95 |
List<View> allNodes = gatherViewTree(viewGroup); |
| 96 |
BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); |
| 97 |
|
| 98 |
int length = mMeasureSpecs.length; |
| 99 |
while (state.keepRunning()) { |
| 100 |
for (int i = 0; i < length; i++) { |
| 101 |
|
| 102 |
requestLayoutForAllNodes(allNodes); |
| 103 |
|
| 104 |
viewGroup.measure(mMeasureSpecs[i % length], mMeasureSpecs[i % length]); |
| 105 |
viewGroup.layout(0, 0, viewGroup.getMeasuredWidth(), viewGroup.getMeasuredHeight()); |
| 106 |
} |
| 107 |
} |
| 108 |
}); |
| 109 |
} |
| 110 |
} |