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