Christine Franks | 69c2d1d | 2017-01-23 14:45:29 -0800 | [diff] [blame] | 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 | |
Narayan Kamath | fa0ec0c | 2019-02-22 17:49:38 +0000 | [diff] [blame] | 19 | import static org.mockito.Mockito.doReturn; |
Jason Monk | a26641f | 2018-12-27 14:20:53 -0500 | [diff] [blame] | 20 | import static org.mockito.Mockito.mock; |
Christine Franks | 69c2d1d | 2017-01-23 14:45:29 -0800 | [diff] [blame] | 21 | import static org.mockito.Mockito.never; |
| 22 | import static org.mockito.Mockito.verify; |
| 23 | |
Christine Franks | ea7d71b | 2018-07-03 14:46:07 -0700 | [diff] [blame] | 24 | import android.hardware.display.ColorDisplayManager; |
Christine Franks | 71e003e | 2019-01-24 14:40:20 -0800 | [diff] [blame] | 25 | import android.hardware.display.NightDisplayListener; |
Amin Shaikh | 3585574 | 2018-03-09 10:54:50 -0500 | [diff] [blame] | 26 | import android.os.Handler; |
Jason Monk | c141dfd | 2017-07-06 09:51:47 -0400 | [diff] [blame] | 27 | import android.testing.AndroidTestingRunner; |
Geoffrey Pitsch | 05b81b4 | 2017-10-06 13:47:46 -0400 | [diff] [blame] | 28 | import android.testing.TestableLooper; |
Jason Monk | c141dfd | 2017-07-06 09:51:47 -0400 | [diff] [blame] | 29 | import android.testing.TestableLooper.RunWithLooper; |
Christine Franks | ea7d71b | 2018-07-03 14:46:07 -0700 | [diff] [blame] | 30 | |
Brett Chabot | 84151d9 | 2019-02-27 15:37:59 -0800 | [diff] [blame] | 31 | import androidx.test.filters.SmallTest; |
| 32 | |
Christine Franks | 69c2d1d | 2017-01-23 14:45:29 -0800 | [diff] [blame] | 33 | import com.android.systemui.SysuiTestCase; |
Amin Shaikh | 3585574 | 2018-03-09 10:54:50 -0500 | [diff] [blame] | 34 | import com.android.systemui.qs.AutoAddTracker; |
Jason Monk | e5b770e | 2017-03-03 21:49:29 -0500 | [diff] [blame] | 35 | import com.android.systemui.qs.QSTileHost; |
Narayan Kamath | fa0ec0c | 2019-02-22 17:49:38 +0000 | [diff] [blame] | 36 | import com.android.systemui.statusbar.policy.CastController; |
| 37 | import com.android.systemui.statusbar.policy.CastController.CastDevice; |
Jason Monk | a26641f | 2018-12-27 14:20:53 -0500 | [diff] [blame] | 38 | import com.android.systemui.statusbar.policy.DataSaverController; |
| 39 | import com.android.systemui.statusbar.policy.HotspotController; |
Christine Franks | ea7d71b | 2018-07-03 14:46:07 -0700 | [diff] [blame] | 40 | |
Christine Franks | 69c2d1d | 2017-01-23 14:45:29 -0800 | [diff] [blame] | 41 | import org.junit.Before; |
| 42 | import org.junit.Test; |
| 43 | import org.junit.runner.RunWith; |
Amin Shaikh | 4bd8e05 | 2018-01-29 09:52:15 -0500 | [diff] [blame] | 44 | import org.mockito.Mock; |
Amin Shaikh | 3585574 | 2018-03-09 10:54:50 -0500 | [diff] [blame] | 45 | import org.mockito.MockitoAnnotations; |
Christine Franks | 69c2d1d | 2017-01-23 14:45:29 -0800 | [diff] [blame] | 46 | |
Narayan Kamath | fa0ec0c | 2019-02-22 17:49:38 +0000 | [diff] [blame] | 47 | import java.util.Collections; |
| 48 | import java.util.Set; |
| 49 | |
Jason Monk | 340b0e5 | 2017-03-08 14:57:56 -0500 | [diff] [blame] | 50 | @RunWith(AndroidTestingRunner.class) |
Jason Monk | c141dfd | 2017-07-06 09:51:47 -0400 | [diff] [blame] | 51 | @RunWithLooper |
Jason Monk | fba8faf | 2017-05-23 10:42:59 -0400 | [diff] [blame] | 52 | @SmallTest |
Christine Franks | 69c2d1d | 2017-01-23 14:45:29 -0800 | [diff] [blame] | 53 | public class AutoTileManagerTest extends SysuiTestCase { |
| 54 | |
Amin Shaikh | 4bd8e05 | 2018-01-29 09:52:15 -0500 | [diff] [blame] | 55 | @Mock private QSTileHost mQsTileHost; |
Amin Shaikh | 3585574 | 2018-03-09 10:54:50 -0500 | [diff] [blame] | 56 | @Mock private AutoAddTracker mAutoAddTracker; |
Narayan Kamath | fa0ec0c | 2019-02-22 17:49:38 +0000 | [diff] [blame] | 57 | @Mock private CastController mCastController; |
Amin Shaikh | 4bd8e05 | 2018-01-29 09:52:15 -0500 | [diff] [blame] | 58 | |
Christine Franks | 69c2d1d | 2017-01-23 14:45:29 -0800 | [diff] [blame] | 59 | private AutoTileManager mAutoTileManager; |
| 60 | |
| 61 | @Before |
| 62 | public void setUp() throws Exception { |
Amin Shaikh | 3585574 | 2018-03-09 10:54:50 -0500 | [diff] [blame] | 63 | MockitoAnnotations.initMocks(this); |
| 64 | mAutoTileManager = new AutoTileManager(mContext, mAutoAddTracker, mQsTileHost, |
Jason Monk | a26641f | 2018-12-27 14:20:53 -0500 | [diff] [blame] | 65 | Handler.createAsync(TestableLooper.get(this).getLooper()), |
| 66 | mock(HotspotController.class), |
| 67 | mock(DataSaverController.class), |
| 68 | mock(ManagedProfileController.class), |
Narayan Kamath | fa0ec0c | 2019-02-22 17:49:38 +0000 | [diff] [blame] | 69 | mock(NightDisplayListener.class), |
| 70 | mCastController); |
Christine Franks | 69c2d1d | 2017-01-23 14:45:29 -0800 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | @Test |
| 74 | public void nightTileAdded_whenActivated() { |
Christine Franks | ea7d71b | 2018-07-03 14:46:07 -0700 | [diff] [blame] | 75 | if (!ColorDisplayManager.isNightDisplayAvailable(mContext)) { |
Christine Franks | 69c2d1d | 2017-01-23 14:45:29 -0800 | [diff] [blame] | 76 | return; |
| 77 | } |
Christine Franks | 71e003e | 2019-01-24 14:40:20 -0800 | [diff] [blame] | 78 | mAutoTileManager.mNightDisplayCallback.onActivated(true); |
Christine Franks | 69c2d1d | 2017-01-23 14:45:29 -0800 | [diff] [blame] | 79 | verify(mQsTileHost).addTile("night"); |
| 80 | } |
| 81 | |
| 82 | @Test |
| 83 | public void nightTileNotAdded_whenDeactivated() { |
Christine Franks | ea7d71b | 2018-07-03 14:46:07 -0700 | [diff] [blame] | 84 | if (!ColorDisplayManager.isNightDisplayAvailable(mContext)) { |
Christine Franks | 69c2d1d | 2017-01-23 14:45:29 -0800 | [diff] [blame] | 85 | return; |
| 86 | } |
Christine Franks | 71e003e | 2019-01-24 14:40:20 -0800 | [diff] [blame] | 87 | mAutoTileManager.mNightDisplayCallback.onActivated(false); |
Christine Franks | 69c2d1d | 2017-01-23 14:45:29 -0800 | [diff] [blame] | 88 | verify(mQsTileHost, never()).addTile("night"); |
| 89 | } |
| 90 | |
| 91 | @Test |
| 92 | public void nightTileAdded_whenNightModeTwilight() { |
Christine Franks | ea7d71b | 2018-07-03 14:46:07 -0700 | [diff] [blame] | 93 | if (!ColorDisplayManager.isNightDisplayAvailable(mContext)) { |
Christine Franks | 69c2d1d | 2017-01-23 14:45:29 -0800 | [diff] [blame] | 94 | return; |
| 95 | } |
Christine Franks | 71e003e | 2019-01-24 14:40:20 -0800 | [diff] [blame] | 96 | mAutoTileManager.mNightDisplayCallback.onAutoModeChanged( |
Christine Franks | 83cc541 | 2018-07-03 14:46:07 -0700 | [diff] [blame] | 97 | ColorDisplayManager.AUTO_MODE_TWILIGHT); |
Christine Franks | 69c2d1d | 2017-01-23 14:45:29 -0800 | [diff] [blame] | 98 | verify(mQsTileHost).addTile("night"); |
| 99 | } |
| 100 | |
| 101 | @Test |
| 102 | public void nightTileAdded_whenNightModeCustom() { |
Christine Franks | ea7d71b | 2018-07-03 14:46:07 -0700 | [diff] [blame] | 103 | if (!ColorDisplayManager.isNightDisplayAvailable(mContext)) { |
Christine Franks | 69c2d1d | 2017-01-23 14:45:29 -0800 | [diff] [blame] | 104 | return; |
| 105 | } |
Christine Franks | 71e003e | 2019-01-24 14:40:20 -0800 | [diff] [blame] | 106 | mAutoTileManager.mNightDisplayCallback.onAutoModeChanged( |
Christine Franks | 83cc541 | 2018-07-03 14:46:07 -0700 | [diff] [blame] | 107 | ColorDisplayManager.AUTO_MODE_CUSTOM_TIME); |
Christine Franks | 69c2d1d | 2017-01-23 14:45:29 -0800 | [diff] [blame] | 108 | verify(mQsTileHost).addTile("night"); |
| 109 | } |
| 110 | |
| 111 | @Test |
| 112 | public void nightTileNotAdded_whenNightModeDisabled() { |
Christine Franks | ea7d71b | 2018-07-03 14:46:07 -0700 | [diff] [blame] | 113 | if (!ColorDisplayManager.isNightDisplayAvailable(mContext)) { |
Christine Franks | 69c2d1d | 2017-01-23 14:45:29 -0800 | [diff] [blame] | 114 | return; |
| 115 | } |
Christine Franks | 71e003e | 2019-01-24 14:40:20 -0800 | [diff] [blame] | 116 | mAutoTileManager.mNightDisplayCallback.onAutoModeChanged( |
Christine Franks | 83cc541 | 2018-07-03 14:46:07 -0700 | [diff] [blame] | 117 | ColorDisplayManager.AUTO_MODE_DISABLED); |
Christine Franks | 69c2d1d | 2017-01-23 14:45:29 -0800 | [diff] [blame] | 118 | verify(mQsTileHost, never()).addTile("night"); |
| 119 | } |
Narayan Kamath | fa0ec0c | 2019-02-22 17:49:38 +0000 | [diff] [blame] | 120 | |
| 121 | private static Set<CastDevice> buildFakeCastDevice(boolean isCasting) { |
| 122 | CastDevice cd = new CastDevice(); |
| 123 | cd.state = isCasting ? CastDevice.STATE_CONNECTED : CastDevice.STATE_DISCONNECTED; |
| 124 | return Collections.singleton(cd); |
| 125 | } |
| 126 | |
| 127 | @Test |
| 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 |
| 135 | public void castTileNotAdded_whenDeviceIsNotCasting() { |
| 136 | doReturn(buildFakeCastDevice(false)).when(mCastController).getCastDevices(); |
| 137 | mAutoTileManager.mCastCallback.onCastDevicesChanged(); |
| 138 | verify(mQsTileHost, never()).addTile("cast"); |
| 139 | } |
Christine Franks | 69c2d1d | 2017-01-23 14:45:29 -0800 | [diff] [blame] | 140 | } |