Fabian Kozynski | a48d2d0 | 2019-02-27 11:36:02 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 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.mockito.ArgumentMatchers.anyInt; |
| 20 | import static org.mockito.Mockito.mock; |
| 21 | import static org.mockito.Mockito.when; |
| 22 | |
Fabian Kozynski | beeec06 | 2019-09-24 10:34:00 -0400 | [diff] [blame^] | 23 | import android.os.Handler; |
Fabian Kozynski | a48d2d0 | 2019-02-27 11:36:02 -0500 | [diff] [blame] | 24 | import android.telephony.SubscriptionManager; |
| 25 | import android.testing.AndroidTestingRunner; |
| 26 | import android.testing.TestableLooper; |
| 27 | import android.view.LayoutInflater; |
| 28 | |
| 29 | import androidx.test.filters.SmallTest; |
| 30 | |
| 31 | import com.android.keyguard.CarrierTextController; |
Fabian Kozynski | beeec06 | 2019-09-24 10:34:00 -0400 | [diff] [blame^] | 32 | import com.android.systemui.Dependency; |
Fabian Kozynski | a48d2d0 | 2019-02-27 11:36:02 -0500 | [diff] [blame] | 33 | import com.android.systemui.R; |
| 34 | import com.android.systemui.statusbar.policy.NetworkController; |
| 35 | import com.android.systemui.utils.leaks.LeakCheckedTest; |
| 36 | |
| 37 | import org.junit.Before; |
| 38 | import org.junit.Test; |
| 39 | import org.junit.runner.RunWith; |
| 40 | import org.mockito.Mockito; |
| 41 | import org.mockito.invocation.InvocationOnMock; |
| 42 | import org.mockito.stubbing.Answer; |
| 43 | |
| 44 | @RunWith(AndroidTestingRunner.class) |
| 45 | @TestableLooper.RunWithLooper |
| 46 | @SmallTest |
| 47 | public class QSCarrierGroupTest extends LeakCheckedTest { |
| 48 | |
| 49 | private QSCarrierGroup mCarrierGroup; |
Fabian Kozynski | beeec06 | 2019-09-24 10:34:00 -0400 | [diff] [blame^] | 50 | private CarrierTextController.CarrierTextCallback mCallback; |
| 51 | private TestableLooper mTestableLooper; |
Fabian Kozynski | a48d2d0 | 2019-02-27 11:36:02 -0500 | [diff] [blame] | 52 | |
| 53 | @Before |
| 54 | public void setup() throws Exception { |
| 55 | injectLeakCheckedDependencies(ALL_SUPPORTED_CLASSES); |
Fabian Kozynski | beeec06 | 2019-09-24 10:34:00 -0400 | [diff] [blame^] | 56 | mTestableLooper = TestableLooper.get(this); |
| 57 | mDependency.injectTestDependency( |
| 58 | Dependency.BG_HANDLER, new Handler(mTestableLooper.getLooper())); |
| 59 | mDependency.injectTestDependency(Dependency.MAIN_LOOPER, mTestableLooper.getLooper()); |
| 60 | mTestableLooper.runWithLooper( |
Fabian Kozynski | a48d2d0 | 2019-02-27 11:36:02 -0500 | [diff] [blame] | 61 | () -> mCarrierGroup = (QSCarrierGroup) LayoutInflater.from(mContext).inflate( |
| 62 | R.layout.qs_carrier_group, null)); |
Fabian Kozynski | beeec06 | 2019-09-24 10:34:00 -0400 | [diff] [blame^] | 63 | mCallback = mCarrierGroup.getCallback(); |
Fabian Kozynski | a48d2d0 | 2019-02-27 11:36:02 -0500 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | @Test // throws no Exception |
| 67 | public void testUpdateCarrierText_sameLengths() { |
| 68 | QSCarrierGroup spiedCarrierGroup = Mockito.spy(mCarrierGroup); |
| 69 | when(spiedCarrierGroup.getSlotIndex(anyInt())).thenAnswer( |
| 70 | new Answer<Integer>() { |
| 71 | @Override |
| 72 | public Integer answer(InvocationOnMock invocationOnMock) throws Throwable { |
| 73 | return invocationOnMock.getArgument(0); |
| 74 | } |
| 75 | }); |
| 76 | |
| 77 | // listOfCarriers length 1, subscriptionIds length 1, anySims false |
| 78 | CarrierTextController.CarrierTextCallbackInfo |
| 79 | c1 = new CarrierTextController.CarrierTextCallbackInfo( |
| 80 | "", |
| 81 | new CharSequence[]{""}, |
| 82 | false, |
| 83 | new int[]{0}); |
Fabian Kozynski | beeec06 | 2019-09-24 10:34:00 -0400 | [diff] [blame^] | 84 | mCallback.updateCarrierInfo(c1); |
Fabian Kozynski | a48d2d0 | 2019-02-27 11:36:02 -0500 | [diff] [blame] | 85 | |
| 86 | // listOfCarriers length 1, subscriptionIds length 1, anySims true |
| 87 | CarrierTextController.CarrierTextCallbackInfo |
| 88 | c2 = new CarrierTextController.CarrierTextCallbackInfo( |
| 89 | "", |
| 90 | new CharSequence[]{""}, |
| 91 | true, |
| 92 | new int[]{0}); |
Fabian Kozynski | beeec06 | 2019-09-24 10:34:00 -0400 | [diff] [blame^] | 93 | mCallback.updateCarrierInfo(c2); |
Fabian Kozynski | a48d2d0 | 2019-02-27 11:36:02 -0500 | [diff] [blame] | 94 | |
| 95 | // listOfCarriers length 2, subscriptionIds length 2, anySims false |
| 96 | CarrierTextController.CarrierTextCallbackInfo |
| 97 | c3 = new CarrierTextController.CarrierTextCallbackInfo( |
| 98 | "", |
| 99 | new CharSequence[]{"", ""}, |
| 100 | false, |
| 101 | new int[]{0, 1}); |
Fabian Kozynski | beeec06 | 2019-09-24 10:34:00 -0400 | [diff] [blame^] | 102 | mCallback.updateCarrierInfo(c3); |
Fabian Kozynski | a48d2d0 | 2019-02-27 11:36:02 -0500 | [diff] [blame] | 103 | |
| 104 | // listOfCarriers length 2, subscriptionIds length 2, anySims true |
| 105 | CarrierTextController.CarrierTextCallbackInfo |
| 106 | c4 = new CarrierTextController.CarrierTextCallbackInfo( |
| 107 | "", |
| 108 | new CharSequence[]{"", ""}, |
| 109 | true, |
| 110 | new int[]{0, 1}); |
Fabian Kozynski | beeec06 | 2019-09-24 10:34:00 -0400 | [diff] [blame^] | 111 | mCallback.updateCarrierInfo(c4); |
| 112 | |
| 113 | mTestableLooper.processAllMessages(); |
Fabian Kozynski | a48d2d0 | 2019-02-27 11:36:02 -0500 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | @Test // throws no Exception |
| 117 | public void testUpdateCarrierText_differentLength() { |
| 118 | QSCarrierGroup spiedCarrierGroup = Mockito.spy(mCarrierGroup); |
| 119 | when(spiedCarrierGroup.getSlotIndex(anyInt())).thenAnswer( |
| 120 | new Answer<Integer>() { |
| 121 | @Override |
| 122 | public Integer answer(InvocationOnMock invocationOnMock) throws Throwable { |
| 123 | return invocationOnMock.getArgument(0); |
| 124 | } |
| 125 | }); |
| 126 | |
| 127 | // listOfCarriers length 2, subscriptionIds length 1, anySims false |
| 128 | CarrierTextController.CarrierTextCallbackInfo |
| 129 | c1 = new CarrierTextController.CarrierTextCallbackInfo( |
| 130 | "", |
| 131 | new CharSequence[]{"", ""}, |
| 132 | false, |
| 133 | new int[]{0}); |
Fabian Kozynski | beeec06 | 2019-09-24 10:34:00 -0400 | [diff] [blame^] | 134 | mCallback.updateCarrierInfo(c1); |
Fabian Kozynski | a48d2d0 | 2019-02-27 11:36:02 -0500 | [diff] [blame] | 135 | |
| 136 | // listOfCarriers length 2, subscriptionIds length 1, anySims true |
| 137 | CarrierTextController.CarrierTextCallbackInfo |
| 138 | c2 = new CarrierTextController.CarrierTextCallbackInfo( |
| 139 | "", |
| 140 | new CharSequence[]{"", ""}, |
| 141 | true, |
| 142 | new int[]{0}); |
Fabian Kozynski | beeec06 | 2019-09-24 10:34:00 -0400 | [diff] [blame^] | 143 | mCallback.updateCarrierInfo(c2); |
Fabian Kozynski | a48d2d0 | 2019-02-27 11:36:02 -0500 | [diff] [blame] | 144 | |
| 145 | // listOfCarriers length 1, subscriptionIds length 2, anySims false |
| 146 | CarrierTextController.CarrierTextCallbackInfo |
| 147 | c3 = new CarrierTextController.CarrierTextCallbackInfo( |
| 148 | "", |
| 149 | new CharSequence[]{""}, |
| 150 | false, |
| 151 | new int[]{0, 1}); |
Fabian Kozynski | beeec06 | 2019-09-24 10:34:00 -0400 | [diff] [blame^] | 152 | mCallback.updateCarrierInfo(c3); |
Fabian Kozynski | a48d2d0 | 2019-02-27 11:36:02 -0500 | [diff] [blame] | 153 | |
| 154 | // listOfCarriers length 1, subscriptionIds length 2, anySims true |
| 155 | CarrierTextController.CarrierTextCallbackInfo |
| 156 | c4 = new CarrierTextController.CarrierTextCallbackInfo( |
| 157 | "", |
| 158 | new CharSequence[]{""}, |
| 159 | true, |
| 160 | new int[]{0, 1}); |
Fabian Kozynski | beeec06 | 2019-09-24 10:34:00 -0400 | [diff] [blame^] | 161 | mCallback.updateCarrierInfo(c4); |
| 162 | mTestableLooper.processAllMessages(); |
Fabian Kozynski | a48d2d0 | 2019-02-27 11:36:02 -0500 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | @Test // throws no Exception |
| 166 | public void testUpdateCarrierText_invalidSim() { |
| 167 | QSCarrierGroup spiedCarrierGroup = Mockito.spy(mCarrierGroup); |
| 168 | when(spiedCarrierGroup.getSlotIndex(anyInt())).thenReturn( |
| 169 | SubscriptionManager.INVALID_SIM_SLOT_INDEX); |
| 170 | CarrierTextController.CarrierTextCallbackInfo |
| 171 | c4 = new CarrierTextController.CarrierTextCallbackInfo( |
| 172 | "", |
| 173 | new CharSequence[]{"", ""}, |
| 174 | true, |
| 175 | new int[]{0, 1}); |
Fabian Kozynski | beeec06 | 2019-09-24 10:34:00 -0400 | [diff] [blame^] | 176 | mCallback.updateCarrierInfo(c4); |
| 177 | mTestableLooper.processAllMessages(); |
Fabian Kozynski | a48d2d0 | 2019-02-27 11:36:02 -0500 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | @Test // throws no Exception |
| 181 | public void testSetMobileDataIndicators_invalidSim() { |
| 182 | QSCarrierGroup spiedCarrierGroup = Mockito.spy(mCarrierGroup); |
| 183 | when(spiedCarrierGroup.getSlotIndex(anyInt())).thenReturn( |
| 184 | SubscriptionManager.INVALID_SIM_SLOT_INDEX); |
| 185 | spiedCarrierGroup.setMobileDataIndicators( |
| 186 | mock(NetworkController.IconState.class), |
| 187 | mock(NetworkController.IconState.class), |
| 188 | 0, 0, true, true, "", "", true, 0, true); |
| 189 | } |
| 190 | } |