192ea6b273d637460310f0f1b79d8a7b658789f8cKirill Grouchnikov/*
292ea6b273d637460310f0f1b79d8a7b658789f8cKirill Grouchnikov * Copyright (C) 2015 The Android Open Source Project
392ea6b273d637460310f0f1b79d8a7b658789f8cKirill Grouchnikov *
492ea6b273d637460310f0f1b79d8a7b658789f8cKirill Grouchnikov * Licensed under the Apache License, Version 2.0 (the "License");
592ea6b273d637460310f0f1b79d8a7b658789f8cKirill Grouchnikov * you may not use this file except in compliance with the License.
692ea6b273d637460310f0f1b79d8a7b658789f8cKirill Grouchnikov * You may obtain a copy of the License at
792ea6b273d637460310f0f1b79d8a7b658789f8cKirill Grouchnikov *
892ea6b273d637460310f0f1b79d8a7b658789f8cKirill Grouchnikov *      http://www.apache.org/licenses/LICENSE-2.0
992ea6b273d637460310f0f1b79d8a7b658789f8cKirill Grouchnikov *
1092ea6b273d637460310f0f1b79d8a7b658789f8cKirill Grouchnikov * Unless required by applicable law or agreed to in writing, software
1192ea6b273d637460310f0f1b79d8a7b658789f8cKirill Grouchnikov * distributed under the License is distributed on an "AS IS" BASIS,
1292ea6b273d637460310f0f1b79d8a7b658789f8cKirill Grouchnikov * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1392ea6b273d637460310f0f1b79d8a7b658789f8cKirill Grouchnikov * See the License for the specific language governing permissions and
1492ea6b273d637460310f0f1b79d8a7b658789f8cKirill Grouchnikov * limitations under the License.
1592ea6b273d637460310f0f1b79d8a7b658789f8cKirill Grouchnikov */
1692ea6b273d637460310f0f1b79d8a7b658789f8cKirill Grouchnikovpackage android.support.v4.content;
1792ea6b273d637460310f0f1b79d8a7b658789f8cKirill Grouchnikov
1839666da345de0a19b86b547126b6932cf3aeedabAlan Viveretteimport org.junit.Before;
1939666da345de0a19b86b547126b6932cf3aeedabAlan Viveretteimport org.junit.Test;
2039666da345de0a19b86b547126b6932cf3aeedabAlan Viverette
2192ea6b273d637460310f0f1b79d8a7b658789f8cKirill Grouchnikovimport android.content.Context;
225612cde662642ab64dadb220d2833f29f5103798Kirill Grouchnikovimport android.content.pm.PackageManager;
23881c193f64d35ea2a106a2f76c30bd711a971bb1Kirill Grouchnikovimport android.content.res.ColorStateList;
24bc927aa4ec8007a8faaa5672f4b3c7ef59c7e36fKirill Grouchnikovimport android.graphics.drawable.Drawable;
2592ea6b273d637460310f0f1b79d8a7b658789f8cKirill Grouchnikovimport android.os.Build;
26f5ec4487510a0e7226636704860a884f4c1d0ab6Kirill Grouchnikovimport android.support.compat.test.R;
27881c193f64d35ea2a106a2f76c30bd711a971bb1Kirill Grouchnikovimport android.support.v4.BaseInstrumentationTestCase;
2892ea6b273d637460310f0f1b79d8a7b658789f8cKirill Grouchnikovimport android.support.v4.ThemedYellowActivity;
29bc927aa4ec8007a8faaa5672f4b3c7ef59c7e36fKirill Grouchnikovimport android.support.v4.testutils.TestUtils;
3092ea6b273d637460310f0f1b79d8a7b658789f8cKirill Grouchnikovimport android.test.suitebuilder.annotation.SmallTest;
3139666da345de0a19b86b547126b6932cf3aeedabAlan Viveretteimport android.util.DisplayMetrics;
32881c193f64d35ea2a106a2f76c30bd711a971bb1Kirill Grouchnikov
33881c193f64d35ea2a106a2f76c30bd711a971bb1Kirill Grouchnikovimport static org.junit.Assert.assertEquals;
3492ea6b273d637460310f0f1b79d8a7b658789f8cKirill Grouchnikov
35881c193f64d35ea2a106a2f76c30bd711a971bb1Kirill Grouchnikov@SmallTest
36881c193f64d35ea2a106a2f76c30bd711a971bb1Kirill Grouchnikovpublic class ContextCompatTest extends BaseInstrumentationTestCase<ThemedYellowActivity> {
37881c193f64d35ea2a106a2f76c30bd711a971bb1Kirill Grouchnikov    private Context mContext;
3892ea6b273d637460310f0f1b79d8a7b658789f8cKirill Grouchnikov
3992ea6b273d637460310f0f1b79d8a7b658789f8cKirill Grouchnikov    public ContextCompatTest() {
40881c193f64d35ea2a106a2f76c30bd711a971bb1Kirill Grouchnikov        super(ThemedYellowActivity.class);
4192ea6b273d637460310f0f1b79d8a7b658789f8cKirill Grouchnikov    }
4292ea6b273d637460310f0f1b79d8a7b658789f8cKirill Grouchnikov
43881c193f64d35ea2a106a2f76c30bd711a971bb1Kirill Grouchnikov    @Before
44881c193f64d35ea2a106a2f76c30bd711a971bb1Kirill Grouchnikov    public void setup() {
45881c193f64d35ea2a106a2f76c30bd711a971bb1Kirill Grouchnikov        mContext = mActivityTestRule.getActivity();
46881c193f64d35ea2a106a2f76c30bd711a971bb1Kirill Grouchnikov    }
4792ea6b273d637460310f0f1b79d8a7b658789f8cKirill Grouchnikov
48881c193f64d35ea2a106a2f76c30bd711a971bb1Kirill Grouchnikov    @Test
49881c193f64d35ea2a106a2f76c30bd711a971bb1Kirill Grouchnikov    public void testGetColor() throws Throwable {
505612cde662642ab64dadb220d2833f29f5103798Kirill Grouchnikov        assertEquals("Unthemed color load", 0xFFFF8090,
51881c193f64d35ea2a106a2f76c30bd711a971bb1Kirill Grouchnikov                ContextCompat.getColor(mContext, R.color.text_color));
5292ea6b273d637460310f0f1b79d8a7b658789f8cKirill Grouchnikov
5392ea6b273d637460310f0f1b79d8a7b658789f8cKirill Grouchnikov        if (Build.VERSION.SDK_INT >= 23) {
5492ea6b273d637460310f0f1b79d8a7b658789f8cKirill Grouchnikov            // The following test is only expected to pass on v23+ devices. The result of
5592ea6b273d637460310f0f1b79d8a7b658789f8cKirill Grouchnikov            // calling theme-aware getColor() in pre-v23 is undefined.
5692ea6b273d637460310f0f1b79d8a7b658789f8cKirill Grouchnikov            assertEquals("Themed yellow color load",
57881c193f64d35ea2a106a2f76c30bd711a971bb1Kirill Grouchnikov                    ContextCompat.getColor(mContext, R.color.simple_themed_selector),
5892ea6b273d637460310f0f1b79d8a7b658789f8cKirill Grouchnikov                    0xFFF0B000);
5992ea6b273d637460310f0f1b79d8a7b658789f8cKirill Grouchnikov        }
6092ea6b273d637460310f0f1b79d8a7b658789f8cKirill Grouchnikov    }
6192ea6b273d637460310f0f1b79d8a7b658789f8cKirill Grouchnikov
62881c193f64d35ea2a106a2f76c30bd711a971bb1Kirill Grouchnikov    @Test
6392ea6b273d637460310f0f1b79d8a7b658789f8cKirill Grouchnikov    public void testGetColorStateList() throws Throwable {
6492ea6b273d637460310f0f1b79d8a7b658789f8cKirill Grouchnikov        ColorStateList unthemedColorStateList =
65881c193f64d35ea2a106a2f76c30bd711a971bb1Kirill Grouchnikov                ContextCompat.getColorStateList(mContext, R.color.complex_unthemed_selector);
665612cde662642ab64dadb220d2833f29f5103798Kirill Grouchnikov        assertEquals("Unthemed color state list load: default", 0xFF70A0C0,
675612cde662642ab64dadb220d2833f29f5103798Kirill Grouchnikov                unthemedColorStateList.getDefaultColor());
685612cde662642ab64dadb220d2833f29f5103798Kirill Grouchnikov        assertEquals("Unthemed color state list load: focused", 0xFF70B0F0,
6992ea6b273d637460310f0f1b79d8a7b658789f8cKirill Grouchnikov                unthemedColorStateList.getColorForState(
705612cde662642ab64dadb220d2833f29f5103798Kirill Grouchnikov                        new int[]{android.R.attr.state_focused}, 0));
715612cde662642ab64dadb220d2833f29f5103798Kirill Grouchnikov        assertEquals("Unthemed color state list load: pressed", 0xFF6080B0,
7292ea6b273d637460310f0f1b79d8a7b658789f8cKirill Grouchnikov                unthemedColorStateList.getColorForState(
735612cde662642ab64dadb220d2833f29f5103798Kirill Grouchnikov                        new int[]{android.R.attr.state_pressed}, 0));
7492ea6b273d637460310f0f1b79d8a7b658789f8cKirill Grouchnikov
7592ea6b273d637460310f0f1b79d8a7b658789f8cKirill Grouchnikov        if (Build.VERSION.SDK_INT >= 23) {
7692ea6b273d637460310f0f1b79d8a7b658789f8cKirill Grouchnikov            // The following tests are only expected to pass on v23+ devices. The result of
7792ea6b273d637460310f0f1b79d8a7b658789f8cKirill Grouchnikov            // calling theme-aware getColorStateList() in pre-v23 is undefined.
7892ea6b273d637460310f0f1b79d8a7b658789f8cKirill Grouchnikov            ColorStateList themedYellowColorStateList =
79881c193f64d35ea2a106a2f76c30bd711a971bb1Kirill Grouchnikov                    ContextCompat.getColorStateList(mContext, R.color.complex_themed_selector);
805612cde662642ab64dadb220d2833f29f5103798Kirill Grouchnikov            assertEquals("Themed yellow color state list load: default", 0xFFF0B000,
815612cde662642ab64dadb220d2833f29f5103798Kirill Grouchnikov                    themedYellowColorStateList.getDefaultColor());
825612cde662642ab64dadb220d2833f29f5103798Kirill Grouchnikov            assertEquals("Themed yellow color state list load: focused", 0xFFF0A020,
8392ea6b273d637460310f0f1b79d8a7b658789f8cKirill Grouchnikov                    themedYellowColorStateList.getColorForState(
845612cde662642ab64dadb220d2833f29f5103798Kirill Grouchnikov                            new int[]{android.R.attr.state_focused}, 0));
855612cde662642ab64dadb220d2833f29f5103798Kirill Grouchnikov            assertEquals("Themed yellow color state list load: pressed", 0xFFE0A040,
8692ea6b273d637460310f0f1b79d8a7b658789f8cKirill Grouchnikov                    themedYellowColorStateList.getColorForState(
875612cde662642ab64dadb220d2833f29f5103798Kirill Grouchnikov                            new int[]{android.R.attr.state_pressed}, 0));
8892ea6b273d637460310f0f1b79d8a7b658789f8cKirill Grouchnikov        }
8992ea6b273d637460310f0f1b79d8a7b658789f8cKirill Grouchnikov    }
90bc927aa4ec8007a8faaa5672f4b3c7ef59c7e36fKirill Grouchnikov
91881c193f64d35ea2a106a2f76c30bd711a971bb1Kirill Grouchnikov    @Test
92bc927aa4ec8007a8faaa5672f4b3c7ef59c7e36fKirill Grouchnikov    public void testGetDrawable() throws Throwable {
93bc927aa4ec8007a8faaa5672f4b3c7ef59c7e36fKirill Grouchnikov        Drawable unthemedDrawable =
94881c193f64d35ea2a106a2f76c30bd711a971bb1Kirill Grouchnikov                ContextCompat.getDrawable(mContext, R.drawable.test_drawable_red);
95bc927aa4ec8007a8faaa5672f4b3c7ef59c7e36fKirill Grouchnikov        TestUtils.assertAllPixelsOfColor("Unthemed drawable load",
96881c193f64d35ea2a106a2f76c30bd711a971bb1Kirill Grouchnikov                unthemedDrawable, mContext.getResources().getColor(R.color.test_red));
97bc927aa4ec8007a8faaa5672f4b3c7ef59c7e36fKirill Grouchnikov
98bc927aa4ec8007a8faaa5672f4b3c7ef59c7e36fKirill Grouchnikov        if (Build.VERSION.SDK_INT >= 23) {
99bc927aa4ec8007a8faaa5672f4b3c7ef59c7e36fKirill Grouchnikov            // The following test is only expected to pass on v23+ devices. The result of
100bc927aa4ec8007a8faaa5672f4b3c7ef59c7e36fKirill Grouchnikov            // calling theme-aware getDrawable() in pre-v23 is undefined.
101bc927aa4ec8007a8faaa5672f4b3c7ef59c7e36fKirill Grouchnikov            Drawable themedYellowDrawable =
102881c193f64d35ea2a106a2f76c30bd711a971bb1Kirill Grouchnikov                    ContextCompat.getDrawable(mContext, R.drawable.themed_drawable);
103bc927aa4ec8007a8faaa5672f4b3c7ef59c7e36fKirill Grouchnikov            TestUtils.assertAllPixelsOfColor("Themed yellow drawable load",
104bc927aa4ec8007a8faaa5672f4b3c7ef59c7e36fKirill Grouchnikov                    themedYellowDrawable, 0xFFF0B000);
105bc927aa4ec8007a8faaa5672f4b3c7ef59c7e36fKirill Grouchnikov        }
106bc927aa4ec8007a8faaa5672f4b3c7ef59c7e36fKirill Grouchnikov    }
1075612cde662642ab64dadb220d2833f29f5103798Kirill Grouchnikov
10839666da345de0a19b86b547126b6932cf3aeedabAlan Viverette    @Test
10939666da345de0a19b86b547126b6932cf3aeedabAlan Viverette    public void testDrawableConfigurationWorkaround() throws Throwable {
11039666da345de0a19b86b547126b6932cf3aeedabAlan Viverette        final int expectedWidth = scaleFromDensity(7, DisplayMetrics.DENSITY_LOW,
11139666da345de0a19b86b547126b6932cf3aeedabAlan Viverette                mContext.getResources().getDisplayMetrics().densityDpi);
11239666da345de0a19b86b547126b6932cf3aeedabAlan Viverette
11339666da345de0a19b86b547126b6932cf3aeedabAlan Viverette        // Ensure we retrieve the correct drawable configuration. Specifically,
11439666da345de0a19b86b547126b6932cf3aeedabAlan Viverette        // this tests a workaround for a bug in drawable configuration that
11539666da345de0a19b86b547126b6932cf3aeedabAlan Viverette        // exists on API < 16 for references to drawables.
11639666da345de0a19b86b547126b6932cf3aeedabAlan Viverette        Drawable referencedDrawable = ContextCompat.getDrawable(mContext,
11739666da345de0a19b86b547126b6932cf3aeedabAlan Viverette                R.drawable.aliased_drawable);
11839666da345de0a19b86b547126b6932cf3aeedabAlan Viverette        assertEquals("Drawable configuration does not match DisplayMetrics",
11939666da345de0a19b86b547126b6932cf3aeedabAlan Viverette                expectedWidth, referencedDrawable.getIntrinsicWidth());
12039666da345de0a19b86b547126b6932cf3aeedabAlan Viverette    }
12139666da345de0a19b86b547126b6932cf3aeedabAlan Viverette
12239666da345de0a19b86b547126b6932cf3aeedabAlan Viverette    private static int scaleFromDensity(int size, int sdensity, int tdensity) {
12339666da345de0a19b86b547126b6932cf3aeedabAlan Viverette        if (sdensity == tdensity) {
12439666da345de0a19b86b547126b6932cf3aeedabAlan Viverette            return size;
12539666da345de0a19b86b547126b6932cf3aeedabAlan Viverette        }
12639666da345de0a19b86b547126b6932cf3aeedabAlan Viverette
12739666da345de0a19b86b547126b6932cf3aeedabAlan Viverette        // Scale by tdensity / sdensity, rounding up.
12839666da345de0a19b86b547126b6932cf3aeedabAlan Viverette        return ((size * tdensity) + (sdensity >> 1)) / sdensity;
12939666da345de0a19b86b547126b6932cf3aeedabAlan Viverette    }
13039666da345de0a19b86b547126b6932cf3aeedabAlan Viverette
131881c193f64d35ea2a106a2f76c30bd711a971bb1Kirill Grouchnikov    @Test(expected = IllegalArgumentException.class)
132881c193f64d35ea2a106a2f76c30bd711a971bb1Kirill Grouchnikov    public void testCheckSelfPermissionNull() {
133881c193f64d35ea2a106a2f76c30bd711a971bb1Kirill Grouchnikov        ContextCompat.checkSelfPermission(mContext, null);
134881c193f64d35ea2a106a2f76c30bd711a971bb1Kirill Grouchnikov    }
1355612cde662642ab64dadb220d2833f29f5103798Kirill Grouchnikov
136881c193f64d35ea2a106a2f76c30bd711a971bb1Kirill Grouchnikov    @Test
137881c193f64d35ea2a106a2f76c30bd711a971bb1Kirill Grouchnikov    public void testCheckSelfPermission() {
1385612cde662642ab64dadb220d2833f29f5103798Kirill Grouchnikov        assertEquals("Vibrate permission granted", PackageManager.PERMISSION_GRANTED,
139881c193f64d35ea2a106a2f76c30bd711a971bb1Kirill Grouchnikov                ContextCompat.checkSelfPermission(mContext,
1405612cde662642ab64dadb220d2833f29f5103798Kirill Grouchnikov                        android.Manifest.permission.VIBRATE));
1415612cde662642ab64dadb220d2833f29f5103798Kirill Grouchnikov        assertEquals("Wake lock permission granted", PackageManager.PERMISSION_GRANTED,
142881c193f64d35ea2a106a2f76c30bd711a971bb1Kirill Grouchnikov                ContextCompat.checkSelfPermission(mContext,
1435612cde662642ab64dadb220d2833f29f5103798Kirill Grouchnikov                        android.Manifest.permission.WAKE_LOCK));
1445612cde662642ab64dadb220d2833f29f5103798Kirill Grouchnikov
1455612cde662642ab64dadb220d2833f29f5103798Kirill Grouchnikov        // The following permissions (normal and dangerous) are expected to be denied as they are
1465612cde662642ab64dadb220d2833f29f5103798Kirill Grouchnikov        // not declared in our manifest.
1475612cde662642ab64dadb220d2833f29f5103798Kirill Grouchnikov        assertEquals("Access network state permission denied", PackageManager.PERMISSION_DENIED,
148881c193f64d35ea2a106a2f76c30bd711a971bb1Kirill Grouchnikov                ContextCompat.checkSelfPermission(mContext,
1495612cde662642ab64dadb220d2833f29f5103798Kirill Grouchnikov                        android.Manifest.permission.ACCESS_NETWORK_STATE));
1505612cde662642ab64dadb220d2833f29f5103798Kirill Grouchnikov        assertEquals("Bluetooth permission denied", PackageManager.PERMISSION_DENIED,
151881c193f64d35ea2a106a2f76c30bd711a971bb1Kirill Grouchnikov                ContextCompat.checkSelfPermission(mContext,
1525612cde662642ab64dadb220d2833f29f5103798Kirill Grouchnikov                        android.Manifest.permission.BLUETOOTH));
1535612cde662642ab64dadb220d2833f29f5103798Kirill Grouchnikov        assertEquals("Call phone permission denied", PackageManager.PERMISSION_DENIED,
154881c193f64d35ea2a106a2f76c30bd711a971bb1Kirill Grouchnikov                ContextCompat.checkSelfPermission(mContext,
1555612cde662642ab64dadb220d2833f29f5103798Kirill Grouchnikov                        android.Manifest.permission.CALL_PHONE));
1565612cde662642ab64dadb220d2833f29f5103798Kirill Grouchnikov        assertEquals("Delete packages permission denied", PackageManager.PERMISSION_DENIED,
157881c193f64d35ea2a106a2f76c30bd711a971bb1Kirill Grouchnikov                ContextCompat.checkSelfPermission(mContext,
1585612cde662642ab64dadb220d2833f29f5103798Kirill Grouchnikov                        android.Manifest.permission.DELETE_PACKAGES));
1595612cde662642ab64dadb220d2833f29f5103798Kirill Grouchnikov    }
1605612cde662642ab64dadb220d2833f29f5103798Kirill Grouchnikov}