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