1 /* 2 * Copyright (C) 2015 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 package com.android.test.uibench; 17 18 import java.util.Random; 19 20 public class TextUtils { 21 private static final int STRING_COUNT = 200; 22 private static final int SIMPLE_STRING_LENGTH = 10; 23 24 /** 25 * Create word of random assortment of lower/upper case letters 26 */ 27 private static String randomWord(Random random, int length) { 28 String result = ""; 29 for (int j = 0; j < length; j++) { 30 // add random letter 31 int base = random.nextInt(2) == 0 ? 'A' : 'a'; 32 result += (char)(random.nextInt(26) + base); 33 } 34 return result; 35 } 36 37 public static String[] buildSimpleStringList() { 38 return buildSimpleStringList(SIMPLE_STRING_LENGTH); 39 } 40 41 public static String[] buildSimpleStringList(int stringLength) { 42 String[] strings = new String[STRING_COUNT]; 43 Random random = new Random(0); 44 for (int i = 0; i < strings.length; i++) { 45 strings[i] = randomWord(random, stringLength); 46 } 47 return strings; 48 } 49 50 // a small number of strings reused frequently, expected to hit 51 // in the word-granularity text layout cache 52 static final String[] CACHE_HIT_STRINGS = new String[] { 53 "a", 54 "small", 55 "number", 56 "of", 57 "strings", 58 "reused", 59 "frequently" 60 }; 61 62 private static final int WORDS_IN_PARAGRAPH = 150; 63 64 // misses are fairly long 'words' to ensure they miss 65 private static final int PARAGRAPH_MISS_MIN_LENGTH = 4; 66 private static final int PARAGRAPH_MISS_MAX_LENGTH = 9; 67 68 static String[] buildParagraphListWithHitPercentage(int hitPercentage) { 69 if (hitPercentage < 0 || hitPercentage > 100) throw new IllegalArgumentException(); 70 71 String[] strings = new String[STRING_COUNT]; 72 Random random = new Random(0); 73 for (int i = 0; i < strings.length; i++) { 74 String result = ""; 75 for (int word = 0; word < WORDS_IN_PARAGRAPH; word++) { 76 if (word != 0) { 77 result += " "; 78 } 79 if (random.nextInt(100) < hitPercentage) { 80 // add a common word, which is very likely to hit in the cache 81 result += CACHE_HIT_STRINGS[random.nextInt(CACHE_HIT_STRINGS.length)]; 82 } else { 83 // construct a random word, which will *most likely* miss 84 int length = PARAGRAPH_MISS_MIN_LENGTH; 85 length += random.nextInt(PARAGRAPH_MISS_MAX_LENGTH - PARAGRAPH_MISS_MIN_LENGTH); 86 87 result += randomWord(random, length); 88 } 89 } 90 strings[i] = result; 91 } 92 93 return strings; 94 } 95 } 96