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