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.statusbar.phone;
18 
19 import static org.mockito.Mockito.doReturn;
20 import static org.mockito.Mockito.mock;
21 import static org.mockito.Mockito.never;
22 import static org.mockito.Mockito.verify;
23 
24 import android.hardware.display.ColorDisplayManager;
25 import android.hardware.display.NightDisplayListener;
26 import android.os.Handler;
27 import android.testing.AndroidTestingRunner;
28 import android.testing.TestableLooper;
29 import android.testing.TestableLooper.RunWithLooper;
30 
31 import androidx.test.filters.SmallTest;
32 
33 import com.android.systemui.SysuiTestCase;
34 import com.android.systemui.qs.AutoAddTracker;
35 import com.android.systemui.qs.QSTileHost;
36 import com.android.systemui.statusbar.policy.CastController;
37 import com.android.systemui.statusbar.policy.CastController.CastDevice;
38 import com.android.systemui.statusbar.policy.DataSaverController;
39 import com.android.systemui.statusbar.policy.HotspotController;
40 
41 import org.junit.Before;
42 import org.junit.Test;
43 import org.junit.runner.RunWith;
44 import org.mockito.Mock;
45 import org.mockito.MockitoAnnotations;
46 
47 import java.util.Collections;
48 import java.util.List;
49 
50 @RunWith(AndroidTestingRunner.class)
51 @RunWithLooper
52 @SmallTest
53 public class AutoTileManagerTest extends SysuiTestCase {
54 
55     @Mock private QSTileHost mQsTileHost;
56     @Mock private AutoAddTracker mAutoAddTracker;
57     @Mock private CastController mCastController;
58 
59     private AutoTileManager mAutoTileManager;
60 
61     @Before
setUp()62     public void setUp() throws Exception {
63         MockitoAnnotations.initMocks(this);
64         mAutoTileManager = new AutoTileManager(mContext, mAutoAddTracker, mQsTileHost,
65                 Handler.createAsync(TestableLooper.get(this).getLooper()),
66                 mock(HotspotController.class),
67                 mock(DataSaverController.class),
68                 mock(ManagedProfileController.class),
69                 mock(NightDisplayListener.class),
70                 mCastController);
71     }
72 
73     @Test
nightTileAdded_whenActivated()74     public void nightTileAdded_whenActivated() {
75         if (!ColorDisplayManager.isNightDisplayAvailable(mContext)) {
76             return;
77         }
78         mAutoTileManager.mNightDisplayCallback.onActivated(true);
79         verify(mQsTileHost).addTile("night");
80     }
81 
82     @Test
nightTileNotAdded_whenDeactivated()83     public void nightTileNotAdded_whenDeactivated() {
84         if (!ColorDisplayManager.isNightDisplayAvailable(mContext)) {
85             return;
86         }
87         mAutoTileManager.mNightDisplayCallback.onActivated(false);
88         verify(mQsTileHost, never()).addTile("night");
89     }
90 
91     @Test
nightTileAdded_whenNightModeTwilight()92     public void nightTileAdded_whenNightModeTwilight() {
93         if (!ColorDisplayManager.isNightDisplayAvailable(mContext)) {
94             return;
95         }
96         mAutoTileManager.mNightDisplayCallback.onAutoModeChanged(
97                 ColorDisplayManager.AUTO_MODE_TWILIGHT);
98         verify(mQsTileHost).addTile("night");
99     }
100 
101     @Test
nightTileAdded_whenNightModeCustom()102     public void nightTileAdded_whenNightModeCustom() {
103         if (!ColorDisplayManager.isNightDisplayAvailable(mContext)) {
104             return;
105         }
106         mAutoTileManager.mNightDisplayCallback.onAutoModeChanged(
107                 ColorDisplayManager.AUTO_MODE_CUSTOM_TIME);
108         verify(mQsTileHost).addTile("night");
109     }
110 
111     @Test
nightTileNotAdded_whenNightModeDisabled()112     public void nightTileNotAdded_whenNightModeDisabled() {
113         if (!ColorDisplayManager.isNightDisplayAvailable(mContext)) {
114             return;
115         }
116         mAutoTileManager.mNightDisplayCallback.onAutoModeChanged(
117                 ColorDisplayManager.AUTO_MODE_DISABLED);
118         verify(mQsTileHost, never()).addTile("night");
119     }
120 
buildFakeCastDevice(boolean isCasting)121     private static List<CastDevice> buildFakeCastDevice(boolean isCasting) {
122         CastDevice cd = new CastDevice();
123         cd.state = isCasting ? CastDevice.STATE_CONNECTED : CastDevice.STATE_DISCONNECTED;
124         return Collections.singletonList(cd);
125     }
126 
127     @Test
castTileAdded_whenDeviceIsCasting()128     public void castTileAdded_whenDeviceIsCasting() {
129         doReturn(buildFakeCastDevice(true)).when(mCastController).getCastDevices();
130         mAutoTileManager.mCastCallback.onCastDevicesChanged();
131         verify(mQsTileHost).addTile("cast");
132     }
133 
134     @Test
castTileNotAdded_whenDeviceIsNotCasting()135     public void castTileNotAdded_whenDeviceIsNotCasting() {
136         doReturn(buildFakeCastDevice(false)).when(mCastController).getCastDevices();
137         mAutoTileManager.mCastCallback.onCastDevicesChanged();
138         verify(mQsTileHost, never()).addTile("cast");
139     }
140 }
141