blob: bb6b691226d679d7a816d5131955177ca8dc3494 [file] [log] [blame]
Seigo Nonakaf307adc2018-10-19 19:10:02 -07001/*
2 * Copyright (C) 2018 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 */
16package android.text;
17
18import android.graphics.RecordingCanvas;
19import android.graphics.RenderNode;
20import android.perftests.utils.BenchmarkState;
21import android.perftests.utils.PerfStatusReporter;
KOUSHIK PANUGANTI412796e2018-12-17 14:16:52 -080022
23import androidx.test.filters.LargeTest;
24import androidx.test.runner.AndroidJUnit4;
Seigo Nonakaf307adc2018-10-19 19:10:02 -070025
26import org.junit.Before;
27import org.junit.Rule;
28import org.junit.Test;
29import org.junit.runner.RunWith;
30
31import java.util.Random;
32
33@LargeTest
34@RunWith(AndroidJUnit4.class)
35public class CanvasDrawTextTest {
36 private static final int WORD_LENGTH = 9; // Random word has 9 characters.
37
38 private static final TextPaint PAINT = new TextPaint();
39
40 @Rule
41 public PerfStatusReporter mPerfStatusReporter = new PerfStatusReporter();
42
43 private TextPerfUtils mTextUtil = new TextPerfUtils();
44
45 @Before
46 public void setUp() {
47 mTextUtil.resetRandom(0 /* seed */);
48 }
49
50 @Test
51 public void drawText_LongText_SmallWindow() {
52 final BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
53 final String text = mTextUtil.nextRandomParagraph(
54 WORD_LENGTH, 4 * 1024 * 1024 /* 4mb text */).toString();
55 final RenderNode node = RenderNode.create("benchmark", null);
56 final RenderNode child = RenderNode.create("child", null);
57 child.setLeftTopRightBottom(50, 50, 100, 100);
58
59 RecordingCanvas canvas = node.start(100, 100);
60 node.end(canvas);
61 canvas = child.start(50, 50);
62 child.end(canvas);
63
64 final Random r = new Random(0);
65
66 while (state.keepRunning()) {
67 int start = r.nextInt(text.length() - 100);
68 canvas.drawText(text, start, start + 100, 0, 0, PAINT);
69 }
70 }
71}