Home | History | Annotate | Download | only in util
      1 /*
      2  * Copyright (C) 2017 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 com.android.setupwizardlib.test.util;
     18 
     19 import android.app.Activity;
     20 import android.app.Application;
     21 import android.app.Instrumentation;
     22 import android.content.Context;
     23 import android.content.Intent;
     24 import android.content.pm.ActivityInfo;
     25 import android.graphics.Bitmap;
     26 import android.graphics.Canvas;
     27 import android.support.annotation.StyleRes;
     28 import android.support.test.InstrumentationRegistry;
     29 import android.view.View;
     30 import android.view.View.MeasureSpec;
     31 
     32 public class DrawingTestHelper {
     33 
     34     /**
     35      * Creates an activity of which to inflate views and drawables for drawing tests. This method
     36      * will return an instance of AppCompatActivity which allows testing of drawing behavior
     37      * injected by support libraries (like drawable tinting) as well.
     38      */
     39     public static Activity createCanvasActivity(@StyleRes int theme)
     40             throws IllegalAccessException, InstantiationException {
     41         final Context context = InstrumentationRegistry.getTargetContext();
     42         final Instrumentation instrumentation = InstrumentationRegistry.getInstrumentation();
     43 
     44         final Intent intent = new Intent(context, DrawingTestActivity.class);
     45         final Activity activity = instrumentation.newActivity(
     46                 DrawingTestActivity.class,
     47                 context,
     48                 null, /* token */
     49                 new Application(),
     50                 intent,
     51                 new ActivityInfo(),
     52                 "", /* title */
     53                 null, /* parent */
     54                 null, /* id */
     55                 null /* lastNonConfigurationInstance */);
     56         instrumentation.callActivityOnCreate(activity, null);
     57         activity.setTheme(theme);
     58         return activity;
     59     }
     60 
     61     private final int mWidth;
     62     private final int mHeight;
     63     private final Canvas mCanvas;
     64     private final Bitmap mBitmap;
     65 
     66     public DrawingTestHelper(int width, int height) {
     67         mWidth = width;
     68         mHeight = height;
     69 
     70         mBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
     71         mCanvas = new Canvas(mBitmap);
     72     }
     73 
     74     public void drawView(View view) {
     75         view.measure(
     76                 MeasureSpec.makeMeasureSpec(mWidth, MeasureSpec.EXACTLY),
     77                 MeasureSpec.makeMeasureSpec(mHeight, MeasureSpec.EXACTLY));
     78         view.layout(0, 0, mWidth, mHeight);
     79         view.draw(mCanvas);
     80     }
     81 
     82     public int[] getPixels() {
     83         int[] out = new int[mWidth * mHeight];
     84         mBitmap.getPixels(out, 0, mWidth, 0, 0, mWidth, mHeight);
     85         return out;
     86     }
     87 
     88     public int getPixel(int x, int y) {
     89         return mBitmap.getPixel(x, y);
     90     }
     91 }
     92