1 /* 2 * Copyright 2020 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.google.android.iwlan; 18 19 import static com.android.dx.mockito.inline.extended.ExtendedMockito.mockitoSession; 20 21 import static org.mockito.ArgumentMatchers.any; 22 import static org.mockito.ArgumentMatchers.anyInt; 23 import static org.mockito.Mockito.eq; 24 import static org.mockito.Mockito.lenient; 25 import static org.mockito.Mockito.times; 26 import static org.mockito.Mockito.verify; 27 28 import android.content.Context; 29 import android.content.Intent; 30 import android.net.Network; 31 import android.net.wifi.WifiManager; 32 import android.telephony.CarrierConfigManager; 33 import android.telephony.SubscriptionManager; 34 import android.telephony.TelephonyManager; 35 import android.telephony.data.ApnSetting; 36 37 import com.google.android.iwlan.epdg.EpdgSelector; 38 39 import org.junit.After; 40 import org.junit.Before; 41 import org.junit.Test; 42 import org.mockito.Mock; 43 import org.mockito.MockitoAnnotations; 44 import org.mockito.MockitoSession; 45 46 public class IwlanBroadcastReceiverTest { 47 private static final String TAG = "IwlanBroadcastReceiverTest"; 48 private IwlanBroadcastReceiver mBroadcastReceiver; 49 50 private static final String ACTION_CARRIER_SIGNAL_PCO_VALUE = 51 TelephonyManager.ACTION_CARRIER_SIGNAL_PCO_VALUE; 52 private static final String EXTRA_APN_TYPE_INT_KEY = TelephonyManager.EXTRA_APN_TYPE; 53 private static final String EXTRA_PCO_ID_KEY = TelephonyManager.EXTRA_PCO_ID; 54 private static final String EXTRA_PCO_VALUE_KEY = TelephonyManager.EXTRA_PCO_VALUE; 55 56 private static final String TEST_PCO_STRING = "testPcoData"; 57 private final byte[] pcoData = TEST_PCO_STRING.getBytes(); 58 private static final int TEST_SUB_ID = 5; 59 private static final int TEST_SLOT_ID = 6; 60 private static final int TEST_PCO_ID_I_PV_6 = 0xFF01; 61 private static final int TEST_PCO_ID_I_PV_4 = 0xFF02; 62 63 MockitoSession mStaticMockSession; 64 @Mock private Context mMockContext; 65 @Mock private Network mMockNetwork; 66 @Mock private EpdgSelector mMockEpdgSelector; 67 @Mock private IwlanEventListener mMockIwlanEventListener; 68 69 @Before setUp()70 public void setUp() throws Exception { 71 MockitoAnnotations.initMocks(this); 72 73 mStaticMockSession = 74 mockitoSession() 75 .mockStatic(EpdgSelector.class) 76 .mockStatic(IwlanDataService.class) 77 .mockStatic(IwlanHelper.class) 78 .mockStatic(SubscriptionManager.class) 79 .mockStatic(IwlanEventListener.class) 80 .startMocking(); 81 82 lenient().when(SubscriptionManager.getSlotIndex(eq(TEST_SUB_ID))).thenReturn(TEST_SLOT_ID); 83 84 lenient().when(IwlanDataService.getContext()).thenReturn(mMockContext); 85 86 lenient() 87 .when(EpdgSelector.getSelectorInstance(eq(mMockContext), eq(TEST_SLOT_ID))) 88 .thenReturn(mMockEpdgSelector); 89 90 lenient() 91 .when(IwlanEventListener.getInstance(eq(mMockContext), eq(TEST_SLOT_ID))) 92 .thenReturn(mMockIwlanEventListener); 93 94 IwlanCarrierConfig.putTestConfigInt( 95 CarrierConfigManager.Iwlan.KEY_EPDG_PCO_ID_IPV6_INT, TEST_PCO_ID_I_PV_6); 96 IwlanCarrierConfig.putTestConfigInt( 97 CarrierConfigManager.Iwlan.KEY_EPDG_PCO_ID_IPV4_INT, TEST_PCO_ID_I_PV_4); 98 99 // New BroadcastReceiver object 100 mBroadcastReceiver = new IwlanBroadcastReceiver(); 101 } 102 103 @After cleanUp()104 public void cleanUp() throws Exception { 105 mStaticMockSession.finishMocking(); 106 } 107 108 @Test testOnReceiveNoPcoData()109 public void testOnReceiveNoPcoData() throws Exception { 110 onReceiveMethodWithArgs(ApnSetting.TYPE_IMS, TEST_PCO_ID_I_PV_6, null); 111 112 // Verify the called times of setPcoData method 113 verify(mMockEpdgSelector, times(0)).setPcoData(anyInt(), any(byte[].class)); 114 } 115 116 @Test testOnReceiveIPv6Pass()117 public void testOnReceiveIPv6Pass() throws Exception { 118 onReceiveMethodWithArgs(ApnSetting.TYPE_IMS, TEST_PCO_ID_I_PV_6); 119 120 // Verify the called times of setPcoData method 121 verify(mMockEpdgSelector, times(1)).setPcoData(TEST_PCO_ID_I_PV_6, pcoData); 122 } 123 124 @Test testOnReceiveIPv4Pass()125 public void testOnReceiveIPv4Pass() throws Exception { 126 onReceiveMethodWithArgs(ApnSetting.TYPE_IMS, TEST_PCO_ID_I_PV_4); 127 128 // Verify the called times of setPcoData method 129 verify(mMockEpdgSelector, times(1)).setPcoData(TEST_PCO_ID_I_PV_4, pcoData); 130 } 131 132 @Test testOnReceiveIncorrectApnType()133 public void testOnReceiveIncorrectApnType() throws Exception { 134 onReceiveMethodWithArgs(ApnSetting.TYPE_DEFAULT, TEST_PCO_ID_I_PV_6); 135 136 // Verify the called times of setPcoData method 137 verify(mMockEpdgSelector, times(0)).setPcoData(TEST_PCO_ID_I_PV_6, pcoData); 138 } 139 140 @Test testOnReceiveMethodIncorrectPcoId()141 public void testOnReceiveMethodIncorrectPcoId() throws Exception { 142 onReceiveMethodWithArgs(ApnSetting.TYPE_IMS, 0xFF00); 143 144 // Verify the called times of setPcoData method 145 verify(mMockEpdgSelector, times(0)).setPcoData(0xFF00, pcoData); 146 } 147 148 @Test testCarrierConfigChanged()149 public void testCarrierConfigChanged() throws Exception { 150 final Intent intent = new Intent(CarrierConfigManager.ACTION_CARRIER_CONFIG_CHANGED); 151 intent.putExtra(CarrierConfigManager.EXTRA_SLOT_INDEX, TEST_SLOT_ID); 152 153 // Trigger the onReceive 154 mBroadcastReceiver.onReceive(mMockContext, intent); 155 156 verify(mMockIwlanEventListener).onBroadcastReceived(intent); 157 } 158 159 @Test testWifiStateChanged()160 public void testWifiStateChanged() throws Exception { 161 final Intent intent = new Intent(WifiManager.WIFI_STATE_CHANGED_ACTION); 162 intent.putExtra(WifiManager.EXTRA_WIFI_STATE, WifiManager.WIFI_STATE_DISABLED); 163 164 // Trigger broadcast 165 mBroadcastReceiver.onReceive(mMockContext, intent); 166 167 verify(mMockIwlanEventListener).onBroadcastReceived(intent); 168 } onReceiveMethodWithArgs(int apnType, int pcoId)169 private void onReceiveMethodWithArgs(int apnType, int pcoId) { 170 // Create intent object 171 final Intent mIntent = new Intent(ACTION_CARRIER_SIGNAL_PCO_VALUE); 172 mIntent.putExtra(SubscriptionManager.EXTRA_SUBSCRIPTION_INDEX, TEST_SUB_ID); 173 mIntent.putExtra(EXTRA_APN_TYPE_INT_KEY, apnType); 174 mIntent.putExtra(EXTRA_PCO_ID_KEY, pcoId); 175 mIntent.putExtra(EXTRA_PCO_VALUE_KEY, pcoData); 176 177 // Trigger onReceive method 178 mBroadcastReceiver.onReceive(mMockContext, mIntent); 179 } 180 onReceiveMethodWithArgs(int apnType, int pcoId, byte[] pcoData)181 private void onReceiveMethodWithArgs(int apnType, int pcoId, byte[] pcoData) { 182 // Create intent object 183 final Intent mIntent = new Intent(ACTION_CARRIER_SIGNAL_PCO_VALUE); 184 mIntent.putExtra(SubscriptionManager.EXTRA_SUBSCRIPTION_INDEX, TEST_SUB_ID); 185 mIntent.putExtra(EXTRA_APN_TYPE_INT_KEY, apnType); 186 mIntent.putExtra(EXTRA_PCO_ID_KEY, pcoId); 187 mIntent.putExtra(EXTRA_PCO_VALUE_KEY, pcoData); 188 189 // Trigger onReceive method 190 mBroadcastReceiver.onReceive(mMockContext, mIntent); 191 } 192 } 193