• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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      */
randomWord(Random random, int length)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 
buildSimpleStringList()37     public static String[] buildSimpleStringList() {
38         String[] strings = new String[STRING_COUNT];
39         Random random = new Random(0);
40         for (int i = 0; i < strings.length; i++) {
41             strings[i] = randomWord(random, SIMPLE_STRING_LENGTH);
42         }
43         return strings;
44     }
45 
46     // a small number of strings reused frequently, expected to hit
47     // in the word-granularity text layout cache
48     static final String[] CACHE_HIT_STRINGS = new String[] {
49             "a",
50             "small",
51             "number",
52             "of",
53             "strings",
54             "reused",
55             "frequently"
56     };
57 
58     private static final int WORDS_IN_PARAGRAPH = 150;
59 
60     // misses are fairly long 'words' to ensure they miss
61     private static final int PARAGRAPH_MISS_MIN_LENGTH = 4;
62     private static final int PARAGRAPH_MISS_MAX_LENGTH = 9;
63 
buildParagraphListWithHitPercentage(int hitPercentage)64     static String[] buildParagraphListWithHitPercentage(int hitPercentage) {
65         if (hitPercentage < 0 || hitPercentage > 100) throw new IllegalArgumentException();
66 
67         String[] strings = new String[STRING_COUNT];
68         Random random = new Random(0);
69         for (int i = 0; i < strings.length; i++) {
70             String result = "";
71             for (int word = 0; word < WORDS_IN_PARAGRAPH; word++) {
72                 if (word != 0) {
73                     result += " ";
74                 }
75                 if (random.nextInt(100) < hitPercentage) {
76                     // add a common word, which is very likely to hit in the cache
77                     result += CACHE_HIT_STRINGS[random.nextInt(CACHE_HIT_STRINGS.length)];
78                 } else {
79                     // construct a random word, which will *most likely* miss
80                     int length = PARAGRAPH_MISS_MIN_LENGTH;
81                     length += random.nextInt(PARAGRAPH_MISS_MAX_LENGTH - PARAGRAPH_MISS_MIN_LENGTH);
82 
83                     result += randomWord(random, length);
84                 }
85             }
86             strings[i] = result;
87         }
88 
89         return strings;
90     }
91 }
92