summaryrefslogtreecommitdiffstats
path: root/tests/UiBench/src/com/android/test/uibench/TextUtils.java
blob: d88ca1e1a8899dd3d459f15cdabfc85ffc227d5c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/*
 * Copyright (C) 2015 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.android.test.uibench;

import java.util.Random;

public class TextUtils {
    private static final int STRING_COUNT = 200;
    private static final int SIMPLE_STRING_LENGTH = 10;

    /**
     * Create word of random assortment of lower/upper case letters
     */
    private static String randomWord(Random random, int length) {
        String result = "";
        for (int j = 0; j < length; j++) {
            // add random letter
            int base = random.nextInt(2) == 0 ? 'A' : 'a';
            result += (char)(random.nextInt(26) + base);
        }
        return result;
    }

    public static String[] buildSimpleStringList() {
        String[] strings = new String[STRING_COUNT];
        Random random = new Random(0);
        for (int i = 0; i < strings.length; i++) {
            strings[i] = randomWord(random, SIMPLE_STRING_LENGTH);
        }
        return strings;
    }

    // a small number of strings reused frequently, expected to hit
    // in the word-granularity text layout cache
    static final String[] CACHE_HIT_STRINGS = new String[] {
            "a",
            "small",
            "number",
            "of",
            "strings",
            "reused",
            "frequently"
    };

    private static final int WORDS_IN_PARAGRAPH = 150;

    // misses are fairly long 'words' to ensure they miss
    private static final int PARAGRAPH_MISS_MIN_LENGTH = 4;
    private static final int PARAGRAPH_MISS_MAX_LENGTH = 9;

    static String[] buildParagraphListWithHitPercentage(int hitPercentage) {
        if (hitPercentage < 0 || hitPercentage > 100) throw new IllegalArgumentException();

        String[] strings = new String[STRING_COUNT];
        Random random = new Random(0);
        for (int i = 0; i < strings.length; i++) {
            String result = "";
            for (int word = 0; word < WORDS_IN_PARAGRAPH; word++) {
                if (word != 0) {
                    result += " ";
                }
                if (random.nextInt(100) < hitPercentage) {
                    // add a common word, which is very likely to hit in the cache
                    result += CACHE_HIT_STRINGS[random.nextInt(CACHE_HIT_STRINGS.length)];
                } else {
                    // construct a random word, which will *most likely* miss
                    int length = PARAGRAPH_MISS_MIN_LENGTH;
                    length += random.nextInt(PARAGRAPH_MISS_MAX_LENGTH - PARAGRAPH_MISS_MIN_LENGTH);

                    result += randomWord(random, length);
                }
            }
            strings[i] = result;
        }

        return strings;
    }
}