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