Seigo Nonaka | f307adc | 2018-10-19 19:10:02 -0700 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package android.text; |
| 17 | |
| 18 | import android.graphics.RecordingCanvas; |
| 19 | import android.graphics.RenderNode; |
| 20 | import android.perftests.utils.BenchmarkState; |
| 21 | import android.perftests.utils.PerfStatusReporter; |
KOUSHIK PANUGANTI | 412796e | 2018-12-17 14:16:52 -0800 | [diff] [blame] | 22 | |
| 23 | import androidx.test.filters.LargeTest; |
| 24 | import androidx.test.runner.AndroidJUnit4; |
Seigo Nonaka | f307adc | 2018-10-19 19:10:02 -0700 | [diff] [blame] | 25 | |
| 26 | import org.junit.Before; |
| 27 | import org.junit.Rule; |
| 28 | import org.junit.Test; |
| 29 | import org.junit.runner.RunWith; |
| 30 | |
| 31 | import java.util.Random; |
| 32 | |
| 33 | @LargeTest |
| 34 | @RunWith(AndroidJUnit4.class) |
| 35 | public 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 | } |