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 19 import static junit.framework.Assert.assertEquals; 20 import static junit.framework.Assert.assertFalse; 21 import static junit.framework.Assert.assertNull; 22 import static junit.framework.Assert.assertTrue; 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; 38 import android.graphics.Color; 39 import android.graphics.drawable.Icon; 40 import android.os.UserHandle; 41 import android.support.test.filters.SmallTest; 42 import android.support.test.runner.AndroidJUnit4; 43 44 import com.android.internal.statusbar.StatusBarIcon; 45 import com.android.internal.util.NotificationColorUtil; 46 import com.android.systemui.R; 47 import com.android.systemui.SysuiTestCase; 48 49 import org.junit.Before; 50 import org.junit.Rule; 51 import org.junit.Test; 52 import org.junit.rules.ExpectedException; 53 import org.junit.runner.RunWith; 54 import org.mockito.ArgumentMatcher; 55 56 @SmallTest 57 @RunWith(AndroidJUnit4.class) 58 public class StatusBarIconViewTest extends SysuiTestCase { 59 60 @Rule 61 public ExpectedException mThrown = ExpectedException.none(); 62 63 private StatusBarIconView mIconView; 64 private StatusBarIcon mStatusBarIcon = mock(StatusBarIcon.class); 65 66 private PackageManager mPackageManagerSpy; 67 private Context mContext; 68 private Resources mMockResources; 69 70 @Before 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, ""); 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 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 } 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", 118 NotificationColorUtil.satisfiesTextContrast(Color.WHITE, color)); 119 120 mIconView.setStaticDrawableColor(Color.GREEN); 121 color = mIconView.getContrastedStaticDrawableColor(0xcc000000); 122 assertEquals("Transparent backgrounds should fallback to drawable color", 123 color, mIconView.getStaticDrawableColor()); 124 } 125 }