1 /* 2 * Copyright (C) 2024 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 package com.android.launcher3.icons 17 18 import android.graphics.Canvas 19 import android.graphics.Color 20 import android.graphics.Paint 21 import androidx.test.ext.junit.runners.AndroidJUnit4 22 import androidx.test.platform.app.InstrumentationRegistry 23 import com.android.launcher3.icons.UserBadgeDrawable.SHADOW_COLOR 24 import com.google.common.truth.Truth.assertThat 25 import org.junit.Test 26 import org.junit.runner.RunWith 27 import org.mockito.kotlin.any 28 import org.mockito.kotlin.mock 29 import org.mockito.kotlin.whenever 30 31 /** Test for [UserBadgeDrawable] */ 32 @RunWith(AndroidJUnit4::class) 33 class UserBadgeDrawableTest { 34 private val context = InstrumentationRegistry.getInstrumentation().targetContext 35 private val canvas = mock<Canvas>() 36 private val systemUnderTest = 37 UserBadgeDrawable( 38 context, 39 R.drawable.ic_work_app_badge, 40 R.color.badge_tint_work, 41 false /* isThemed */, 42 null, /* shape */ 43 ) 44 45 @Test draw_opaquenull46 fun draw_opaque() { 47 val colorList = mutableListOf<Int>() 48 whenever(canvas.drawCircle(any(), any(), any(), any())).then { 49 colorList.add(it.getArgument<Paint>(3).color) 50 } 51 52 systemUnderTest.alpha = 255 53 systemUnderTest.draw(canvas) 54 55 assertThat(colorList).containsExactly(SHADOW_COLOR, Color.WHITE) 56 } 57 58 @Test draw_transparentnull59 fun draw_transparent() { 60 val colorList = mutableListOf<Int>() 61 whenever(canvas.drawCircle(any(), any(), any(), any())).then { 62 colorList.add(it.getArgument<Paint>(3).color) 63 } 64 65 systemUnderTest.alpha = 0 66 systemUnderTest.draw(canvas) 67 68 assertThat(colorList).hasSize(2) 69 assertThat(Color.valueOf(colorList[0]).alpha()).isEqualTo(0) 70 assertThat(Color.valueOf(colorList[1]).alpha()).isEqualTo(0) 71 } 72 } 73