Home | History | Annotate | Download | only in app
      1 /*
      2  * Copyright (C) 2016 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.support.v7.app;
     17 
     18 import static org.junit.Assert.assertEquals;
     19 
     20 import android.app.Activity;
     21 import android.graphics.Bitmap;
     22 import android.graphics.Canvas;
     23 import android.graphics.Color;
     24 import android.graphics.drawable.Drawable;
     25 import android.os.Build;
     26 import android.support.test.annotation.UiThreadTest;
     27 import android.support.v7.appcompat.test.R;
     28 import android.test.suitebuilder.annotation.SmallTest;
     29 import android.view.View;
     30 import android.widget.ImageView;
     31 
     32 import org.junit.Before;
     33 import org.junit.Test;
     34 
     35 @SmallTest
     36 public class AppCompatVectorDrawableIntegrationTest
     37         extends BaseInstrumentationTestCase<AppCompatVectorDrawableIntegrationActivity> {
     38 
     39     private Bitmap mBitmap;
     40     private Canvas mCanvas;
     41 
     42     private static final int WIDTH = 64;
     43     private static final int HEIGHT = 64;
     44     private static final int LEFT_CENTER_X = WIDTH / 4;
     45     private static final int RIGHT_CENTER_X = WIDTH * 3 / 4;
     46     private static final int CENTER_Y = HEIGHT / 2;
     47 
     48     public AppCompatVectorDrawableIntegrationTest() {
     49         super(AppCompatVectorDrawableIntegrationActivity.class);
     50     }
     51 
     52     @Before
     53     public void setup() {
     54         mBitmap = Bitmap.createBitmap(WIDTH, HEIGHT, Bitmap.Config.ARGB_8888);
     55         mCanvas = new Canvas(mBitmap);
     56     }
     57 
     58     @Test
     59     @UiThreadTest
     60     public void testVectorDrawableAutoMirrored() {
     61         Activity activity = mActivityTestRule.getActivity();
     62         ImageView view1 = (ImageView) activity.findViewById(R.id.view_vector_1);
     63         Drawable vectorDrawable = view1.getDrawable();
     64         // We update the bounds here for writing into the bitmap correctly.
     65         vectorDrawable.setBounds(0, 0, WIDTH, HEIGHT);
     66         vectorDrawable.draw(mCanvas);
     67 
     68         int leftColor = mBitmap.getPixel(LEFT_CENTER_X, CENTER_Y);
     69         int rightColor = mBitmap.getPixel(RIGHT_CENTER_X, CENTER_Y);
     70 
     71         // Gingerbread seems treating the alpha differently, so checking red channel only here.
     72         // It is enough to tell the difference.
     73         assertEquals("Left side should be white", Color.red(leftColor), 255);
     74         assertEquals("Right side should be black", Color.red(rightColor), 0);
     75 
     76         if (Build.VERSION.SDK_INT >= 19) {
     77             // setLayoutDirection is only available after API 17. However, it correctly set its
     78             // drawable's layout direction until API 19.
     79             view1.setLayoutDirection(View.LAYOUT_DIRECTION_RTL);
     80             vectorDrawable.draw(mCanvas);
     81 
     82             leftColor = mBitmap.getPixel(LEFT_CENTER_X, CENTER_Y);
     83             rightColor = mBitmap.getPixel(RIGHT_CENTER_X, CENTER_Y);
     84 
     85             assertEquals("Left side should be black", Color.red(leftColor), 0);
     86             assertEquals("Right side should be white", Color.red(rightColor), 255);
     87         }
     88     }
     89 }
     90