1 /*
2  * Copyright (C) 2019 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 android.testing.AndroidTestingRunner
20 import android.view.View
21 import androidx.test.filters.SmallTest
22 import com.android.systemui.SysuiTestCase
23 import com.google.common.truth.Truth.assertThat
24 import org.junit.Assert.assertFalse
25 import org.junit.Assert.assertTrue
26 import org.junit.Before
27 import org.junit.Test
28 import org.junit.runner.RunWith
29 import java.util.function.Consumer
30 
31 @RunWith(AndroidTestingRunner::class)
32 @SmallTest
33 class LightRevealScrimTest : SysuiTestCase() {
34 
35   private lateinit var scrim: LightRevealScrim
36   private var isOpaque = false
37 
38   @Before
39   fun setUp() {
40     scrim = LightRevealScrim(context, null, DEFAULT_WIDTH, DEFAULT_HEIGHT)
41     scrim.isScrimOpaqueChangedListener = Consumer { opaque ->
42       isOpaque = opaque
43     }
44     scrim.revealAmount = 0f
45     assertTrue("Scrim is not opaque in initial setup", scrim.isScrimOpaque)
46   }
47 
48   @Test
49   fun testAlphaSetsOpaque() {
50     scrim.alpha = 0.5f
51     assertFalse("Scrim is opaque even though alpha is set", scrim.isScrimOpaque)
52   }
53 
54   @Test
55   fun testVisibilitySetsOpaque() {
56     scrim.visibility = View.INVISIBLE
57     assertFalse("Scrim is opaque even though it's invisible", scrim.isScrimOpaque)
58     scrim.visibility = View.GONE
59     assertFalse("Scrim is opaque even though it's gone", scrim.isScrimOpaque)
60   }
61 
62   @Test
63   fun testRevealSetsOpaque() {
64     scrim.revealAmount = 0.5f
65     assertFalse("Scrim is opaque even though it's revealed", scrim.isScrimOpaque)
66   }
67 
68   @Test
69   fun testBeforeOnMeasure_defaultDimensions() {
70     assertThat(scrim.viewWidth).isEqualTo(DEFAULT_WIDTH)
71     assertThat(scrim.viewHeight).isEqualTo(DEFAULT_HEIGHT)
72   }
73 
74   @Test
75   fun testAfterOnMeasure_measuredDimensions() {
76     scrim.measure(/* widthMeasureSpec= */ exact(1), /* heightMeasureSpec= */ exact(2))
77 
78     assertThat(scrim.viewWidth).isEqualTo(1)
79     assertThat(scrim.viewHeight).isEqualTo(2)
80   }
81 
82   private fun exact(value: Int) = View.MeasureSpec.makeMeasureSpec(value, View.MeasureSpec.EXACTLY)
83 
84   private companion object {
85     private const val DEFAULT_WIDTH = 42
86     private const val DEFAULT_HEIGHT = 24
87   }
88 }