Selim Cinek | 2b6eb803 | 2016-12-29 14:22:21 +0100 | [diff] [blame] | 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 | |
| 17 | package com.android.systemui.statusbar; |
| 18 | |
Lucas Dupin | 83519da | 2017-06-21 11:58:31 -0700 | [diff] [blame] | 19 | import static junit.framework.Assert.assertEquals; |
Adrian Roos | fb2d0cc | 2017-01-31 15:10:00 -0800 | [diff] [blame] | 20 | import static junit.framework.Assert.assertFalse; |
| 21 | import static junit.framework.Assert.assertNull; |
Lucas Dupin | 83519da | 2017-06-21 11:58:31 -0700 | [diff] [blame] | 22 | import static junit.framework.Assert.assertTrue; |
Adrian Roos | fb2d0cc | 2017-01-31 15:10:00 -0800 | [diff] [blame] | 23 | |
| 24 | import static org.mockito.ArgumentMatchers.any; |
| 25 | import static org.mockito.ArgumentMatchers.anyInt; |
| 26 | import static org.mockito.ArgumentMatchers.argThat; |
| 27 | import static org.mockito.ArgumentMatchers.eq; |
| 28 | import static org.mockito.Mockito.doReturn; |
| 29 | import static org.mockito.Mockito.mock; |
| 30 | import static org.mockito.Mockito.spy; |
| 31 | import static org.mockito.Mockito.when; |
| 32 | |
| 33 | import android.content.Context; |
| 34 | import android.content.ContextWrapper; |
| 35 | import android.content.pm.ApplicationInfo; |
| 36 | import android.content.pm.PackageManager; |
| 37 | import android.content.res.Resources; |
Lucas Dupin | 83519da | 2017-06-21 11:58:31 -0700 | [diff] [blame] | 38 | import android.graphics.Color; |
Selim Cinek | 2b6eb803 | 2016-12-29 14:22:21 +0100 | [diff] [blame] | 39 | import android.graphics.drawable.Icon; |
Selim Cinek | 2b6eb803 | 2016-12-29 14:22:21 +0100 | [diff] [blame] | 40 | import android.os.UserHandle; |
Adrian Roos | fb2d0cc | 2017-01-31 15:10:00 -0800 | [diff] [blame] | 41 | import android.support.test.filters.SmallTest; |
Selim Cinek | 2b6eb803 | 2016-12-29 14:22:21 +0100 | [diff] [blame] | 42 | import android.support.test.runner.AndroidJUnit4; |
Selim Cinek | 2b6eb803 | 2016-12-29 14:22:21 +0100 | [diff] [blame] | 43 | |
| 44 | import com.android.internal.statusbar.StatusBarIcon; |
Lucas Dupin | a291d19 | 2018-06-07 13:59:42 -0700 | [diff] [blame] | 45 | import com.android.internal.util.ContrastColorUtil; |
Selim Cinek | 2b6eb803 | 2016-12-29 14:22:21 +0100 | [diff] [blame] | 46 | import com.android.systemui.R; |
| 47 | import com.android.systemui.SysuiTestCase; |
| 48 | |
| 49 | import org.junit.Before; |
Adrian Roos | fb2d0cc | 2017-01-31 15:10:00 -0800 | [diff] [blame] | 50 | import org.junit.Rule; |
Selim Cinek | 2b6eb803 | 2016-12-29 14:22:21 +0100 | [diff] [blame] | 51 | import org.junit.Test; |
Adrian Roos | fb2d0cc | 2017-01-31 15:10:00 -0800 | [diff] [blame] | 52 | import org.junit.rules.ExpectedException; |
Selim Cinek | 2b6eb803 | 2016-12-29 14:22:21 +0100 | [diff] [blame] | 53 | import org.junit.runner.RunWith; |
Adrian Roos | fb2d0cc | 2017-01-31 15:10:00 -0800 | [diff] [blame] | 54 | import org.mockito.ArgumentMatcher; |
Selim Cinek | 2b6eb803 | 2016-12-29 14:22:21 +0100 | [diff] [blame] | 55 | |
| 56 | @SmallTest |
| 57 | @RunWith(AndroidJUnit4.class) |
| 58 | public class StatusBarIconViewTest extends SysuiTestCase { |
| 59 | |
Adrian Roos | fb2d0cc | 2017-01-31 15:10:00 -0800 | [diff] [blame] | 60 | @Rule |
| 61 | public ExpectedException mThrown = ExpectedException.none(); |
| 62 | |
Selim Cinek | 2b6eb803 | 2016-12-29 14:22:21 +0100 | [diff] [blame] | 63 | private StatusBarIconView mIconView; |
| 64 | private StatusBarIcon mStatusBarIcon = mock(StatusBarIcon.class); |
| 65 | |
Adrian Roos | fb2d0cc | 2017-01-31 15:10:00 -0800 | [diff] [blame] | 66 | private PackageManager mPackageManagerSpy; |
| 67 | private Context mContext; |
| 68 | private Resources mMockResources; |
| 69 | |
Selim Cinek | 2b6eb803 | 2016-12-29 14:22:21 +0100 | [diff] [blame] | 70 | @Before |
Adrian Roos | fb2d0cc | 2017-01-31 15:10:00 -0800 | [diff] [blame] | 71 | public void setUp() throws Exception { |
| 72 | // Set up context such that asking for "mockPackage" resources returns mMockResources. |
| 73 | mMockResources = mock(Resources.class); |
| 74 | mPackageManagerSpy = spy(getContext().getPackageManager()); |
| 75 | doReturn(mMockResources).when(mPackageManagerSpy) |
| 76 | .getResourcesForApplicationAsUser(eq("mockPackage"), anyInt()); |
| 77 | doReturn(mMockResources).when(mPackageManagerSpy) |
| 78 | .getResourcesForApplication(eq("mockPackage")); |
| 79 | doReturn(mMockResources).when(mPackageManagerSpy).getResourcesForApplication(argThat( |
| 80 | (ArgumentMatcher<ApplicationInfo>) o -> "mockPackage".equals(o.packageName))); |
| 81 | mContext = new ContextWrapper(getContext()) { |
| 82 | @Override |
| 83 | public PackageManager getPackageManager() { |
| 84 | return mPackageManagerSpy; |
| 85 | } |
| 86 | }; |
| 87 | |
| 88 | mIconView = new StatusBarIconView(mContext, "test_slot", null); |
| 89 | mStatusBarIcon = new StatusBarIcon(UserHandle.ALL, "mockPackage", |
| 90 | Icon.createWithResource(mContext, R.drawable.ic_android), 0, 0, ""); |
Selim Cinek | 2b6eb803 | 2016-12-29 14:22:21 +0100 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | @Test |
| 94 | public void testSetClearsGrayscale() { |
| 95 | mIconView.setTag(R.id.icon_is_grayscale, true); |
| 96 | mIconView.set(mStatusBarIcon); |
| 97 | assertNull(mIconView.getTag(R.id.icon_is_grayscale)); |
| 98 | } |
| 99 | |
Adrian Roos | fb2d0cc | 2017-01-31 15:10:00 -0800 | [diff] [blame] | 100 | @Test |
| 101 | public void testSettingOomingIconDoesNotThrowOom() { |
| 102 | when(mMockResources.getDrawable(anyInt(), any())).thenThrow(new OutOfMemoryError("mocked")); |
| 103 | mStatusBarIcon.icon = Icon.createWithResource("mockPackage", R.drawable.ic_android); |
| 104 | |
| 105 | assertFalse(mIconView.set(mStatusBarIcon)); |
| 106 | } |
Lucas Dupin | 83519da | 2017-06-21 11:58:31 -0700 | [diff] [blame] | 107 | |
| 108 | @Test |
| 109 | public void testGetContrastedStaticDrawableColor() { |
| 110 | mIconView.setStaticDrawableColor(Color.DKGRAY); |
| 111 | int color = mIconView.getContrastedStaticDrawableColor(Color.WHITE); |
| 112 | assertEquals("Color should not change when we have enough contrast", |
| 113 | Color.DKGRAY, color); |
| 114 | |
| 115 | mIconView.setStaticDrawableColor(Color.WHITE); |
| 116 | color = mIconView.getContrastedStaticDrawableColor(Color.WHITE); |
| 117 | assertTrue("Similar colors should be shifted to satisfy contrast", |
Lucas Dupin | a291d19 | 2018-06-07 13:59:42 -0700 | [diff] [blame] | 118 | ContrastColorUtil.satisfiesTextContrast(Color.WHITE, color)); |
Lucas Dupin | ddbaf84 | 2017-06-23 09:30:49 -0700 | [diff] [blame] | 119 | |
| 120 | mIconView.setStaticDrawableColor(Color.GREEN); |
| 121 | color = mIconView.getContrastedStaticDrawableColor(0xcc000000); |
| 122 | assertEquals("Transparent backgrounds should fallback to drawable color", |
| 123 | color, mIconView.getStaticDrawableColor()); |
Lucas Dupin | 83519da | 2017-06-21 11:58:31 -0700 | [diff] [blame] | 124 | } |
Selim Cinek | 2b6eb803 | 2016-12-29 14:22:21 +0100 | [diff] [blame] | 125 | } |