1 /*
2  * Copyright (C) 2021 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.scrim;
18 
19 import static junit.framework.Assert.assertEquals;
20 
21 import android.graphics.Color;
22 import android.graphics.Rect;
23 import android.graphics.drawable.ColorDrawable;
24 import android.graphics.drawable.Drawable;
25 import android.testing.AndroidTestingRunner;
26 import android.testing.TestableLooper;
27 import android.testing.TestableLooper.RunWithLooper;
28 import android.testing.ViewUtils;
29 import android.view.View;
30 
31 import androidx.test.filters.SmallTest;
32 
33 import com.android.internal.colorextraction.ColorExtractor;
34 import com.android.systemui.statusbar.policy.ConfigurationController;
35 import com.android.systemui.utils.leaks.LeakCheckedTest;
36 
37 import org.junit.Before;
38 import org.junit.Test;
39 import org.junit.runner.RunWith;
40 
41 @RunWith(AndroidTestingRunner.class)
42 @SmallTest
43 public class ScrimViewTest extends LeakCheckedTest {
44 
45     ScrimView mView;
46 
47     @Before
setUp()48     public void setUp() {
49         injectLeakCheckedDependency(ConfigurationController.class);
50         mView = new ScrimView(getContext());
51         mView.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
52         mView.layout(0, 0, 1920, 1080);
53     }
54 
55     @Test
56     @RunWithLooper
testAttachDetach()57     public void testAttachDetach() {
58         ViewUtils.attachView(mView);
59         TestableLooper.get(this).processAllMessages();
60         ViewUtils.detachView(mView);
61         TestableLooper.get(this).processAllMessages();
62     }
63 
64     @Test
testSetDrawable_UpdateDrawable()65     public void testSetDrawable_UpdateDrawable() {
66         Drawable drawable = new ColorDrawable(Color.GREEN);
67         mView.setDrawable(drawable);
68         assertEquals(drawable, mView.getDrawable());
69     }
70 
71     @Test
testCreation_initialColor()72     public void testCreation_initialColor() {
73         ScrimDrawable drawable = (ScrimDrawable) mView.getDrawable();
74         ColorExtractor.GradientColors colors = mView.getColors();
75         assertEquals("Main color should be set upon creation",
76                 drawable.getMainColor(), colors.getMainColor());
77     }
78 
79     @Test
testSetViewAlpha_propagatesToDrawable()80     public void testSetViewAlpha_propagatesToDrawable() {
81         final float alpha = 0.5f;
82         mView.setViewAlpha(alpha);
83         assertEquals("View alpha did not propagate to drawable", alpha, mView.getViewAlpha());
84     }
85 
86     @Test
setTint_set()87     public void setTint_set() {
88         int tint = Color.BLUE;
89         mView.setTint(tint);
90         assertEquals(mView.getTint(), tint);
91     }
92 
93     @Test
setDrawableBounds_propagatesToDrawable()94     public void setDrawableBounds_propagatesToDrawable() {
95         ColorDrawable drawable = new ColorDrawable();
96         Rect expectedBounds = new Rect(100, 100, 100, 100);
97         mView.setDrawable(drawable);
98         mView.setDrawableBounds(100, 100, 100, 100);
99 
100         assertEquals(expectedBounds, drawable.getBounds());
101         // set bounds that are different from expected drawable bounds
102         mView.onLayout(true, 200, 200, 200, 200);
103         assertEquals(expectedBounds, drawable.getBounds());
104     }
105 }
106