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 
17 package com.android.systemui.qs;
18 
19 import static org.junit.Assert.assertTrue;
20 import static org.mockito.ArgumentMatchers.eq;
21 import static org.mockito.Mockito.mock;
22 import static org.mockito.Mockito.verify;
23 import static org.mockito.Mockito.when;
24 
25 import android.content.Context;
26 import android.graphics.drawable.Drawable;
27 import android.testing.AndroidTestingRunner;
28 import android.testing.TestableLooper.RunWithLooper;
29 
30 import androidx.test.filters.SmallTest;
31 
32 import com.android.systemui.SysuiTestCase;
33 import com.android.systemui.plugins.qs.QSTile.SlashState;
34 import com.android.systemui.qs.tileimpl.SlashImageView;
35 
36 import org.junit.Test;
37 import org.junit.runner.RunWith;
38 
39 @SmallTest
40 @RunWith(AndroidTestingRunner.class)
41 @RunWithLooper
42 public class SlashImageViewTest extends SysuiTestCase {
43     private TestableSlashImageView mSlashView;
44 
45     @Test
testSetNonNullSlashStateCreatesSlashDrawable()46     public void testSetNonNullSlashStateCreatesSlashDrawable() {
47         SlashState mockState = mock(SlashState.class);
48         Drawable mockDrawable = mock(Drawable.class);
49         mSlashView = new TestableSlashImageView(mContext);
50         assertTrue(mSlashView.getSlashDrawable() == null);
51 
52         mSlashView.setState(mockState, mockDrawable);
53 
54         assertTrue(mSlashView.getSlashDrawable() != null);
55     }
56 
57     @Test
testSetNullSlashStateRemovesSlashDrawable()58     public void testSetNullSlashStateRemovesSlashDrawable() {
59         SlashState mockState = mock(SlashState.class);
60         Drawable mockDrawable = mock(Drawable.class);
61         mSlashView = new TestableSlashImageView(mContext);
62         mSlashView.setState(mockState, mockDrawable);
63 
64         assertTrue(mSlashView.getSlashDrawable() != null);
65 
66         mSlashView.setState(null, mockDrawable);
67 
68         assertTrue(mSlashView.getSlashDrawable() == null);
69     }
70 
71     @Test
testSetNullDrawableRemovesSlashDrawable()72     public void testSetNullDrawableRemovesSlashDrawable() {
73         SlashState mockState = mock(SlashState.class);
74         Drawable mockDrawable = mock(Drawable.class);
75 
76         mSlashView = new TestableSlashImageView(mContext);
77         mSlashView.setImageDrawable(mockDrawable);
78         mSlashView.setState(mockState, mockDrawable);
79         mSlashView.setImageDrawable(null);
80 
81         assertTrue(mSlashView.getSlashDrawable() == null);
82     }
83 
84     @Test
testSetImageDrawableUsesDrawableLevel()85     public void testSetImageDrawableUsesDrawableLevel() {
86         SlashImageView iv = new SlashImageView(mContext);
87         Drawable mockDrawable = mock(Drawable.class);
88         when(mockDrawable.getLevel()).thenReturn(2);
89 
90         iv.setImageDrawable(mockDrawable);
91 
92         // Make sure setting the drawable didn't reset its level to 0
93         verify(mockDrawable).setLevel(eq(2));
94     }
95 
96     // Expose getSlashDrawable
97     private static class TestableSlashImageView extends SlashImageView {
TestableSlashImageView(Context c)98         TestableSlashImageView(Context c) {
99             super(c);
100         }
101 
getSlashDrawable()102         private SlashDrawable getSlashDrawable() {
103             return mSlash;
104         }
105     }
106 }
107