Fabian Kozynski | 4235b2a | 2018-10-04 17:46:59 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file |
| 5 | * except in compliance with the License. You may obtain a copy of the License at |
| 6 | * |
| 7 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | * |
| 9 | * Unless required by applicable law or agreed to in writing, software distributed under the |
| 10 | * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 11 | * KIND, either express or implied. See the License for the specific language governing |
| 12 | * permissions and limitations under the License. |
| 13 | */ |
| 14 | |
| 15 | package com.android.systemui.qs.tiles; |
| 16 | |
| 17 | import static junit.framework.TestCase.assertEquals; |
Fabian Kozynski | 4235b2a | 2018-10-04 17:46:59 -0400 | [diff] [blame] | 18 | |
| 19 | import static org.mockito.Mockito.mock; |
| 20 | import static org.mockito.Mockito.verify; |
| 21 | import static org.mockito.Mockito.when; |
| 22 | |
| 23 | import android.service.quicksettings.Tile; |
| 24 | import android.support.test.filters.SmallTest; |
| 25 | import android.testing.AndroidTestingRunner; |
| 26 | import android.testing.TestableLooper; |
| 27 | |
| 28 | import com.android.systemui.Dependency; |
| 29 | import com.android.systemui.SysuiTestCase; |
| 30 | import com.android.systemui.plugins.ActivityStarter; |
| 31 | import com.android.systemui.qs.QSTileHost; |
| 32 | import com.android.systemui.statusbar.policy.CastController; |
| 33 | import com.android.systemui.statusbar.policy.KeyguardMonitor; |
| 34 | import com.android.systemui.statusbar.policy.NetworkController; |
| 35 | |
| 36 | import org.junit.Before; |
| 37 | import org.junit.Test; |
| 38 | import org.junit.runner.RunWith; |
| 39 | import org.mockito.ArgumentCaptor; |
| 40 | import org.mockito.Mock; |
| 41 | import org.mockito.MockitoAnnotations; |
| 42 | |
| 43 | import java.util.HashSet; |
| 44 | import java.util.Set; |
| 45 | |
| 46 | |
| 47 | @RunWith(AndroidTestingRunner.class) |
| 48 | @TestableLooper.RunWithLooper |
| 49 | @SmallTest |
| 50 | public class CastTileTest extends SysuiTestCase { |
| 51 | |
| 52 | @Mock |
| 53 | private CastController mController; |
| 54 | @Mock |
| 55 | private ActivityStarter mActivityStarter; |
| 56 | @Mock |
| 57 | private KeyguardMonitor mKeyguard; |
| 58 | @Mock |
| 59 | private NetworkController mNetworkController; |
| 60 | @Mock |
| 61 | private QSTileHost mHost; |
| 62 | @Mock |
| 63 | NetworkController.SignalCallback mCallback; |
| 64 | |
| 65 | private TestableLooper mTestableLooper; |
| 66 | private CastTile mCastTile; |
| 67 | |
| 68 | @Before |
| 69 | public void setUp() throws Exception { |
| 70 | MockitoAnnotations.initMocks(this); |
| 71 | mTestableLooper = TestableLooper.get(this); |
| 72 | |
| 73 | mDependency.injectTestDependency(Dependency.BG_LOOPER, mTestableLooper.getLooper()); |
| 74 | mController = mDependency.injectMockDependency(CastController.class); |
| 75 | mActivityStarter = mDependency.injectMockDependency(ActivityStarter.class); |
| 76 | mKeyguard = mDependency.injectMockDependency(KeyguardMonitor.class); |
| 77 | mNetworkController = mDependency.injectMockDependency(NetworkController.class); |
| 78 | |
| 79 | when(mHost.getContext()).thenReturn(mContext); |
| 80 | |
Jason Monk | c0e0e2b | 2018-12-26 16:47:41 -0500 | [diff] [blame^] | 81 | mCastTile = new CastTile(mHost, mController, mKeyguard, mNetworkController, |
| 82 | mActivityStarter); |
Fabian Kozynski | 4235b2a | 2018-10-04 17:46:59 -0400 | [diff] [blame] | 83 | |
| 84 | // We are not setting the mocks to listening, so we trigger a first refresh state to |
| 85 | // set the initial state |
| 86 | mCastTile.refreshState(); |
| 87 | |
| 88 | mCastTile.handleSetListening(true); |
| 89 | ArgumentCaptor<NetworkController.SignalCallback> signalCallbackArgumentCaptor = |
| 90 | ArgumentCaptor.forClass(NetworkController.SignalCallback.class); |
| 91 | verify(mNetworkController).addCallback(signalCallbackArgumentCaptor.capture()); |
| 92 | mCallback = signalCallbackArgumentCaptor.getValue(); |
| 93 | |
| 94 | } |
| 95 | |
| 96 | @Test |
| 97 | public void testStateUnavailable_wifiDisabled() { |
| 98 | NetworkController.IconState qsIcon = |
| 99 | new NetworkController.IconState(false, 0, ""); |
| 100 | mCallback.setWifiIndicators(false, mock(NetworkController.IconState.class), |
| 101 | qsIcon, false,false, "", |
| 102 | false, ""); |
| 103 | mTestableLooper.processAllMessages(); |
| 104 | |
| 105 | assertEquals(Tile.STATE_UNAVAILABLE, mCastTile.getState().state); |
| 106 | } |
| 107 | |
| 108 | @Test |
| 109 | public void testStateUnavailable_wifiNotConnected() { |
| 110 | NetworkController.IconState qsIcon = |
| 111 | new NetworkController.IconState(false, 0, ""); |
| 112 | mCallback.setWifiIndicators(true, mock(NetworkController.IconState.class), |
| 113 | qsIcon, false,false, "", |
| 114 | false, ""); |
| 115 | mTestableLooper.processAllMessages(); |
| 116 | |
| 117 | assertEquals(Tile.STATE_UNAVAILABLE, mCastTile.getState().state); |
| 118 | } |
| 119 | |
| 120 | @Test |
| 121 | public void testStateActive_wifiEnabledAndCasting() { |
| 122 | CastController.CastDevice device = mock(CastController.CastDevice.class); |
| 123 | device.state = CastController.CastDevice.STATE_CONNECTED; |
| 124 | Set<CastController.CastDevice> devices = new HashSet<>(); |
| 125 | devices.add(device); |
| 126 | when(mController.getCastDevices()).thenReturn(devices); |
| 127 | |
| 128 | NetworkController.IconState qsIcon = |
| 129 | new NetworkController.IconState(true, 0, ""); |
| 130 | mCallback.setWifiIndicators(true, mock(NetworkController.IconState.class), |
| 131 | qsIcon, false,false, "", |
| 132 | false, ""); |
| 133 | mTestableLooper.processAllMessages(); |
| 134 | |
| 135 | assertEquals(Tile.STATE_ACTIVE, mCastTile.getState().state); |
| 136 | } |
| 137 | |
| 138 | @Test |
| 139 | public void testStateInactive_wifiEnabledNotCasting() { |
| 140 | NetworkController.IconState qsIcon = |
| 141 | new NetworkController.IconState(true, 0, ""); |
| 142 | mCallback.setWifiIndicators(true, mock(NetworkController.IconState.class), |
| 143 | qsIcon, false,false, "", |
| 144 | false, ""); |
| 145 | mTestableLooper.processAllMessages(); |
| 146 | |
| 147 | assertEquals(Tile.STATE_INACTIVE, mCastTile.getState().state); |
| 148 | } |
| 149 | } |