DrawableLayoutDirectionHelperTest.java revision 38a3832bf55aeb5dc24904d4d1ddf391dffa7d7b
1/*
2 * Copyright (C) 2015 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
17package com.android.setupwizardlib.test;
18
19import android.content.Context;
20import android.content.res.Configuration;
21import android.graphics.Color;
22import android.graphics.Rect;
23import android.graphics.drawable.ColorDrawable;
24import android.graphics.drawable.Drawable;
25import android.graphics.drawable.InsetDrawable;
26import android.os.Build;
27import android.test.AndroidTestCase;
28import android.test.suitebuilder.annotation.SmallTest;
29import android.view.View;
30
31import com.android.setupwizardlib.util.DrawableLayoutDirectionHelper;
32
33import java.util.Locale;
34
35public class DrawableLayoutDirectionHelperTest extends AndroidTestCase {
36
37    @SmallTest
38    public void testCreateRelativeInsetDrawableLtr() {
39        final Drawable drawable = new ColorDrawable(Color.RED);
40        final InsetDrawable insetDrawable =
41                DrawableLayoutDirectionHelper.createRelativeInsetDrawable(drawable,
42                        1 /* start */, 2 /* top */, 3 /* end */, 4 /* bottom */,
43                        View.LAYOUT_DIRECTION_LTR);
44        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
45            assertSame("Drawable from getDrawable() should be same as passed in", drawable,
46                    insetDrawable.getDrawable());
47        }
48        Rect outRect = new Rect();
49        insetDrawable.getPadding(outRect);
50        assertEquals("InsetDrawable padding should be same as inset", new Rect(1, 2, 3, 4),
51                outRect);
52    }
53
54    @SmallTest
55    public void testCreateRelativeInsetDrawableRtl() {
56        final Drawable drawable = new ColorDrawable(Color.RED);
57        final InsetDrawable insetDrawable =
58                DrawableLayoutDirectionHelper.createRelativeInsetDrawable(drawable,
59                        1 /* start */, 2 /* top */, 3 /* end */, 4 /* bottom */,
60                        View.LAYOUT_DIRECTION_RTL);
61        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
62            assertSame("Drawable from getDrawable() should be same as passed in", drawable,
63                    insetDrawable.getDrawable());
64        }
65        Rect outRect = new Rect();
66        insetDrawable.getPadding(outRect);
67        assertEquals("InsetDrawable padding should be same as inset", new Rect(3, 2, 1, 4),
68                outRect);
69    }
70
71    @SmallTest
72    public void testCreateRelativeInsetDrawableViewRtl() {
73        final Drawable drawable = new ColorDrawable(Color.RED);
74        final View view = new ForceRtlView(getContext());
75        final InsetDrawable insetDrawable =
76                DrawableLayoutDirectionHelper.createRelativeInsetDrawable(drawable,
77                        1 /* start */, 2 /* top */, 3 /* end */, 4 /* bottom */, view);
78        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
79            assertSame("Drawable from getDrawable() should be same as passed in", drawable,
80                    insetDrawable.getDrawable());
81        }
82        Rect outRect = new Rect();
83        insetDrawable.getPadding(outRect);
84        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
85            assertEquals("InsetDrawable padding should be same as inset", new Rect(3, 2, 1, 4),
86                    outRect);
87        } else {
88            assertEquals("InsetDrawable padding should be same as inset", new Rect(1, 2, 3, 4),
89                    outRect);
90        }
91    }
92
93    @SmallTest
94    public void testCreateRelativeInsetDrawableContextRtl() {
95        final Drawable drawable = new ColorDrawable(Color.RED);
96        Context context = getContext();
97        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
98            final Configuration config = new Configuration();
99            config.setLayoutDirection(new Locale("fa", "IR"));
100            context = getContext().createConfigurationContext(config);
101        }
102        final InsetDrawable insetDrawable =
103                DrawableLayoutDirectionHelper.createRelativeInsetDrawable(drawable,
104                        1 /* start */, 2 /* top */, 3 /* end */, 4 /* bottom */, context);
105        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
106            assertSame("Drawable from getDrawable() should be same as passed in", drawable,
107                    insetDrawable.getDrawable());
108        }
109        Rect outRect = new Rect();
110        insetDrawable.getPadding(outRect);
111        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
112            assertEquals("InsetDrawable padding should be same as inset", new Rect(3, 2, 1, 4),
113                    outRect);
114        } else {
115            assertEquals("InsetDrawable padding should be same as inset", new Rect(1, 2, 3, 4),
116                    outRect);
117        }
118    }
119
120    private static class ForceRtlView extends View {
121
122        public ForceRtlView(Context context) {
123            super(context);
124        }
125
126        @Override
127        public int getLayoutDirection() {
128            return View.LAYOUT_DIRECTION_RTL;
129        }
130    }
131}
132