1 /* 2 * Copyright (C) 2006 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 17 package android.widget; 18 19 import android.content.Context; 20 import android.graphics.Bitmap; 21 import android.graphics.Canvas; 22 import android.graphics.Paint; 23 import android.test.AndroidTestCase; 24 import android.test.suitebuilder.annotation.MediumTest; 25 import android.test.suitebuilder.annotation.SmallTest; 26 import android.text.SpannedString; 27 import android.view.View; 28 import android.view.ViewGroup; 29 import android.widget.TextView; 30 31 public class TextViewPerformanceTest extends AndroidTestCase { 32 33 private String mString = "The quick brown fox"; 34 private Canvas mCanvas; 35 private PerformanceTextView mTextView; 36 private Paint mPaint; 37 private PerformanceLabelView mLabelView; 38 39 @Override 40 protected void setUp() throws Exception { 41 super.setUp(); 42 43 Bitmap mBitmap = Bitmap.createBitmap(320, 240, Bitmap.Config.RGB_565); 44 mCanvas = new Canvas(mBitmap); 45 46 ViewGroup.LayoutParams p = new ViewGroup.LayoutParams(320, 240); 47 48 mLabelView = new PerformanceLabelView(mContext); 49 mLabelView.setText(mString); 50 mLabelView.measure(View.MeasureSpec.AT_MOST | 320, View.MeasureSpec.AT_MOST | 240); 51 mLabelView.mySetFrame(320, 240); 52 mLabelView.setLayoutParams(p); 53 mLabelView.myDraw(mCanvas); 54 55 mPaint = new Paint(); 56 mCanvas.save(); 57 mTextView = new PerformanceTextView(mContext); 58 mTextView.setLayoutParams(p); 59 mTextView.setText(mString); 60 mTextView.mySetFrame(320, 240); 61 mTextView.measure(View.MeasureSpec.AT_MOST | 320, View.MeasureSpec.AT_MOST | 240); 62 } 63 64 @MediumTest 65 public void testDrawTextViewLine() throws Exception { 66 mTextView.myDraw(mCanvas); 67 mTextView.myDraw(mCanvas); 68 mTextView.myDraw(mCanvas); 69 mTextView.myDraw(mCanvas); 70 mTextView.myDraw(mCanvas); 71 mTextView.myDraw(mCanvas); 72 mTextView.myDraw(mCanvas); 73 mTextView.myDraw(mCanvas); 74 mTextView.myDraw(mCanvas); 75 mTextView.myDraw(mCanvas); 76 } 77 78 @SmallTest 79 public void testSpan() throws Exception { 80 CharSequence charSeq = new SpannedString(mString); 81 mTextView.setText(charSeq); 82 83 mTextView.myDraw(mCanvas); 84 mTextView.myDraw(mCanvas); 85 mTextView.myDraw(mCanvas); 86 mTextView.myDraw(mCanvas); 87 mTextView.myDraw(mCanvas); 88 mTextView.myDraw(mCanvas); 89 mTextView.myDraw(mCanvas); 90 mTextView.myDraw(mCanvas); 91 mTextView.myDraw(mCanvas); 92 mTextView.myDraw(mCanvas); 93 } 94 95 @SmallTest 96 public void testCanvasDrawText() throws Exception { 97 mCanvas.drawText(mString, 30, 30, mPaint); 98 } 99 100 @SmallTest 101 public void testLabelViewDraw() throws Exception { 102 mLabelView.myDraw(mCanvas); 103 } 104 105 private class PerformanceTextView extends TextView { 106 public PerformanceTextView(Context context) { 107 super(context); 108 } 109 110 final void myDraw(Canvas c) { 111 super.onDraw(c); 112 } 113 114 final void mySetFrame(int w, int h) { 115 super.setFrame(0, 0, w, h); 116 } 117 } 118 119 private class PerformanceLabelView extends LabelView { 120 public PerformanceLabelView(Context context) { 121 super(context); 122 } 123 124 final void myDraw(Canvas c) { 125 super.onDraw(c); 126 } 127 128 final void mySetFrame(int w, int h) { 129 super.setFrame(0, 0, w, h); 130 } 131 } 132 } 133