1/*
2 * Copyright (C) 2017 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
17package com.android.systemui.statusbar;
18
19import static junit.framework.Assert.assertEquals;
20
21import static org.mockito.ArgumentMatchers.anyInt;
22import static org.mockito.Mockito.mock;
23import static org.mockito.Mockito.times;
24import static org.mockito.Mockito.verify;
25
26import android.graphics.Canvas;
27import android.graphics.Color;
28import android.graphics.Rect;
29import android.graphics.drawable.ColorDrawable;
30import android.graphics.drawable.Drawable;
31import android.support.test.filters.SmallTest;
32import android.testing.AndroidTestingRunner;
33import android.testing.TestableLooper;
34import android.testing.TestableLooper.RunWithLooper;
35import android.testing.ViewUtils;
36import android.view.View;
37
38import com.android.internal.colorextraction.ColorExtractor;
39import com.android.internal.colorextraction.drawable.GradientDrawable;
40import com.android.systemui.statusbar.policy.ConfigurationController;
41import com.android.systemui.utils.leaks.LeakCheckedTest;
42
43import org.junit.Before;
44import org.junit.Test;
45import org.junit.runner.RunWith;
46
47@RunWith(AndroidTestingRunner.class)
48@SmallTest
49public class ScrimViewTest extends LeakCheckedTest {
50
51    ScrimView mView;
52
53    @Before
54    public void setUp() {
55        injectLeakCheckedDependency(ConfigurationController.class);
56        mView = new ScrimView(getContext());
57        mView.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
58        mView.layout(0, 0, 1920, 1080);
59    }
60
61    @Test
62    @RunWithLooper
63    public void testAttachDetach() {
64        ViewUtils.attachView(mView);
65        TestableLooper.get(this).processAllMessages();
66        ViewUtils.detachView(mView);
67        TestableLooper.get(this).processAllMessages();
68    }
69
70    @Test
71    public void testSetDrawable_UpdateDrawable() {
72        Drawable drawable = new ColorDrawable(Color.GREEN);
73        mView.setDrawable(drawable);
74        assertEquals(drawable, mView.getDrawable());
75    }
76
77    @Test
78    public void testCreation_initialColor() {
79        GradientDrawable drawable = (GradientDrawable) mView.getDrawable();
80        ColorExtractor.GradientColors colors = mView.getColors();
81        assertEquals("Main color should be set upon creation",
82                drawable.getMainColor(), colors.getMainColor());
83        assertEquals("Secondary color should be set upon creation",
84                drawable.getSecondaryColor(), colors.getSecondaryColor());
85    }
86
87    @Test
88    public void testSetViewAlpha_propagatesToDrawable() {
89        float alpha = 0.5f;
90        mView.setViewAlpha(alpha);
91        assertEquals(mView.getViewAlpha(), alpha);
92    }
93
94    @Test
95    public void testOnDraw_ExcludeRectDrawable() {
96        mView.setExcludedArea(new Rect(10, 10, 20, 20));
97        Canvas canvas = mock(Canvas.class);
98        mView.onDraw(canvas);
99        // One time for each rect side
100        verify(canvas, times(4)).clipRect(anyInt(), anyInt(), anyInt(), anyInt());
101    }
102
103    @Test
104    public void setTint_set() {
105        int tint = Color.BLUE;
106        mView.setTint(tint);
107        assertEquals(mView.getTint(), tint);
108    }
109}
110