1 /* 2 * Copyright (C) 2018 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 * use this file except in compliance with the License. You may obtain a copy of 6 * 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, WITHOUT 12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 * License for the specific language governing permissions and limitations under 14 * the License. 15 */ 16 17 package android.text; 18 19 import static android.text.TextDirectionHeuristics.LTR; 20 21 import android.perftests.utils.BenchmarkState; 22 import android.perftests.utils.PerfStatusReporter; 23 24 import android.support.test.filters.LargeTest; 25 import android.support.test.runner.AndroidJUnit4; 26 27 import android.content.res.ColorStateList; 28 import android.graphics.Canvas; 29 import android.graphics.Typeface; 30 import android.text.Layout; 31 import android.text.style.TextAppearanceSpan; 32 import android.view.DisplayListCanvas; 33 import android.view.RenderNode; 34 35 import org.junit.Before; 36 import org.junit.Rule; 37 import org.junit.Test; 38 import org.junit.runner.RunWith; 39 40 import java.nio.CharBuffer; 41 import java.util.Random; 42 43 public class TextPerfUtils { 44 45 private static final int PARA_LENGTH = 500; // Number of characters in a paragraph. 46 47 private Random mRandom = new Random(0); 48 49 private static final String ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; 50 private static final int ALPHABET_LENGTH = ALPHABET.length(); 51 52 private static final ColorStateList TEXT_COLOR = ColorStateList.valueOf(0x00000000); 53 private static final String[] FAMILIES = { "sans-serif", "serif", "monospace" }; 54 private static final int[] STYLES = { 55 Typeface.NORMAL, Typeface.BOLD, Typeface.ITALIC, Typeface.BOLD_ITALIC 56 }; 57 58 private final char[] mBuffer = new char[PARA_LENGTH]; 59 60 public void resetRandom(long seed) { 61 mRandom = new Random(seed); 62 } 63 64 public CharSequence nextRandomParagraph(int wordLen, boolean applyRandomStyle) { 65 for (int i = 0; i < PARA_LENGTH; i++) { 66 if (i % (wordLen + 1) == wordLen) { 67 mBuffer[i] = ' '; 68 } else { 69 mBuffer[i] = ALPHABET.charAt(mRandom.nextInt(ALPHABET_LENGTH)); 70 } 71 } 72 73 CharSequence cs = CharBuffer.wrap(mBuffer); 74 if (!applyRandomStyle) { 75 return cs; 76 } 77 78 SpannableStringBuilder ssb = new SpannableStringBuilder(cs); 79 for (int i = 0; i < ssb.length(); i += wordLen + 1) { 80 final int spanStart = i; 81 final int spanEnd = (i + wordLen) > ssb.length() ? ssb.length() : i + wordLen; 82 83 final TextAppearanceSpan span = new TextAppearanceSpan( 84 FAMILIES[mRandom.nextInt(FAMILIES.length)], 85 STYLES[mRandom.nextInt(STYLES.length)], 86 24 + mRandom.nextInt(32), // text size. min 24 max 56 87 TEXT_COLOR, TEXT_COLOR); 88 89 ssb.setSpan(span, spanStart, spanEnd, Spanned.SPAN_INCLUSIVE_EXCLUSIVE); 90 } 91 return ssb; 92 } 93 } 94