Home | History | Annotate | Download | only in widget
      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 
     17 package android.widget;
     18 
     19 import java.util.Arrays;
     20 import java.util.Collection;
     21 import java.util.Locale;
     22 import java.util.Random;
     23 
     24 import org.junit.Rule;
     25 import org.junit.Test;
     26 import org.junit.runner.RunWith;
     27 import org.junit.runners.Parameterized;
     28 import org.junit.runners.Parameterized.Parameters;
     29 
     30 import android.app.Activity;
     31 import android.graphics.Bitmap;
     32 import android.graphics.Canvas;
     33 import android.util.Log;
     34 import android.view.KeyEvent;
     35 import android.view.RenderNodeAnimator;
     36 import android.view.ViewGroup;
     37 import android.view.View.MeasureSpec;
     38 
     39 import android.perftests.utils.BenchmarkState;
     40 import android.perftests.utils.PerfStatusReporter;
     41 import android.perftests.utils.StubActivity;
     42 import android.support.test.filters.LargeTest;
     43 import android.support.test.runner.AndroidJUnit4;
     44 import android.support.test.rule.ActivityTestRule;
     45 import android.support.test.InstrumentationRegistry;
     46 
     47 @LargeTest
     48 @RunWith(Parameterized.class)
     49 public class EditTextLongTextPerfTest {
     50     @Parameters(name = "{0}")
     51     public static Collection cases() {
     52         return Arrays.asList(new Object[][] {
     53             { "10x30K", 10, 30000 },
     54             { "300x1K", 300, 1000 },
     55         });
     56     }
     57 
     58     private final String mMetricKey;
     59     private final int mChars;
     60     private final int mLines;
     61 
     62     public EditTextLongTextPerfTest(String metricKey, int chars, int lines) {
     63         mMetricKey = metricKey;
     64         mChars = chars;
     65         mLines = lines;
     66     }
     67 
     68     @Rule
     69     public ActivityTestRule<StubActivity> mActivityRule = new ActivityTestRule(StubActivity.class);
     70 
     71     @Rule
     72     public PerfStatusReporter mPerfStatusReporter = new PerfStatusReporter();
     73 
     74     private EditText setupEditText() {
     75         final EditText editText = new EditText(mActivityRule.getActivity());
     76 
     77         String alphabet = "abcdefghijklmnopqrstuvwxyz";
     78         final long seed = 1234567890;
     79         Random r = new Random(seed);
     80         StringBuilder sb = new StringBuilder();
     81         for (int i = 0; i < mLines; i++) {
     82             for (int j = 0; j < mChars; j++) {
     83                 char c = alphabet.charAt(r.nextInt(alphabet.length()));
     84                 sb.append(c);
     85             }
     86             sb.append('\n');
     87         }
     88 
     89         final int height = 1000;
     90         final int width = 1000;
     91         editText.setHeight(height);
     92         editText.setWidth(width);
     93         editText.setLayoutParams(new ViewGroup.LayoutParams(width, height));
     94 
     95         Activity activity = mActivityRule.getActivity();
     96         activity.setContentView(editText);
     97 
     98         editText.setText(sb.toString(), TextView.BufferType.EDITABLE);
     99         editText.invalidate();
    100         editText.measure(MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY),
    101                          MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY));
    102         editText.layout(0, 0, height, width);
    103 
    104         return editText;
    105     }
    106 
    107     @Test
    108     public void testEditText() throws Throwable {
    109         mActivityRule.runOnUiThread(() -> {
    110             BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
    111             final EditText editText = setupEditText();
    112             final KeyEvent keyEvent = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_ENTER);
    113             final int steps = 100;
    114             while (state.keepRunning()) {
    115                 for (int i = 0; i < steps; i++) {
    116                     int offset = (editText.getText().length() * i) / steps;
    117                     editText.setSelection(offset);
    118                     editText.bringPointIntoView(offset);
    119                     editText.onKeyDown(keyEvent.getKeyCode(), keyEvent);
    120                     editText.updateDisplayListIfDirty();
    121                 }
    122             }
    123         });
    124     }
    125 }
    126