Home | History | Annotate | Download | only in widget
      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.support.test.InstrumentationRegistry;
     24 import android.support.test.filters.MediumTest;
     25 import android.support.test.filters.SmallTest;
     26 import android.support.test.runner.AndroidJUnit4;
     27 import android.text.SpannedString;
     28 import android.view.View;
     29 import android.view.ViewGroup;
     30 
     31 import org.junit.Before;
     32 import org.junit.Test;
     33 import org.junit.runner.RunWith;
     34 
     35 @RunWith(AndroidJUnit4.class)
     36 public class TextViewPerformanceTest {
     37 
     38     private String mString = "The quick brown fox";
     39     private Canvas mCanvas;
     40     private PerformanceTextView mTextView;
     41     private Paint mPaint;
     42     private PerformanceLabelView mLabelView;
     43 
     44     @Before
     45     public void setUp() {
     46         Bitmap mBitmap = Bitmap.createBitmap(320, 240, Bitmap.Config.RGB_565);
     47         mCanvas = new Canvas(mBitmap);
     48 
     49         ViewGroup.LayoutParams p = new ViewGroup.LayoutParams(320, 240);
     50 
     51         final Context context = InstrumentationRegistry.getContext();
     52 
     53         mLabelView = new PerformanceLabelView(context);
     54         mLabelView.setText(mString);
     55         mLabelView.measure(View.MeasureSpec.AT_MOST | 320, View.MeasureSpec.AT_MOST | 240);
     56         mLabelView.mySetFrame(320, 240);
     57         mLabelView.setLayoutParams(p);
     58         mLabelView.myDraw(mCanvas);
     59 
     60         mPaint = new Paint();
     61         mCanvas.save();
     62         mTextView = new PerformanceTextView(context);
     63         mTextView.setLayoutParams(p);
     64         mTextView.setText(mString);
     65         mTextView.mySetFrame(320, 240);
     66         mTextView.measure(View.MeasureSpec.AT_MOST | 320, View.MeasureSpec.AT_MOST | 240);
     67     }
     68 
     69     @MediumTest
     70     @Test
     71     public void testDrawTextViewLine() {
     72         mTextView.myDraw(mCanvas);
     73         mTextView.myDraw(mCanvas);
     74         mTextView.myDraw(mCanvas);
     75         mTextView.myDraw(mCanvas);
     76         mTextView.myDraw(mCanvas);
     77         mTextView.myDraw(mCanvas);
     78         mTextView.myDraw(mCanvas);
     79         mTextView.myDraw(mCanvas);
     80         mTextView.myDraw(mCanvas);
     81         mTextView.myDraw(mCanvas);
     82     }
     83 
     84     @SmallTest
     85     @Test
     86     public void testSpan() {
     87         CharSequence charSeq = new SpannedString(mString);
     88         mTextView.setText(charSeq);
     89 
     90         mTextView.myDraw(mCanvas);
     91         mTextView.myDraw(mCanvas);
     92         mTextView.myDraw(mCanvas);
     93         mTextView.myDraw(mCanvas);
     94         mTextView.myDraw(mCanvas);
     95         mTextView.myDraw(mCanvas);
     96         mTextView.myDraw(mCanvas);
     97         mTextView.myDraw(mCanvas);
     98         mTextView.myDraw(mCanvas);
     99         mTextView.myDraw(mCanvas);
    100     }
    101 
    102     @SmallTest
    103     @Test
    104     public void testCanvasDrawText() {
    105         mCanvas.drawText(mString, 30, 30, mPaint);
    106     }
    107 
    108     @SmallTest
    109     @Test
    110     public void testLabelViewDraw() {
    111         mLabelView.myDraw(mCanvas);
    112     }
    113 
    114     private class PerformanceTextView extends TextView {
    115         public PerformanceTextView(Context context) {
    116             super(context);
    117         }
    118 
    119         final void myDraw(Canvas c) {
    120             super.onDraw(c);
    121         }
    122 
    123         final void mySetFrame(int w, int h) {
    124             super.setFrame(0, 0, w, h);
    125         }
    126     }
    127 
    128     private class PerformanceLabelView extends LabelView {
    129         public PerformanceLabelView(Context context) {
    130             super(context);
    131         }
    132 
    133         final void myDraw(Canvas c) {
    134             super.onDraw(c);
    135         }
    136 
    137         final void mySetFrame(int w, int h) {
    138             super.setFrame(0, 0, w, h);
    139         }
    140     }
    141 }
    142