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.Mockito.eq; 22 import static org.mockito.Mockito.lenient; 23 import static org.mockito.Mockito.verify; 24 25 import android.content.Context; 26 import android.content.Intent; 27 import android.net.wifi.WifiManager; 28 import android.telephony.CarrierConfigManager; 29 import android.telephony.SubscriptionManager; 30 31 import com.google.android.iwlan.epdg.EpdgSelector; 32 33 import org.junit.After; 34 import org.junit.Before; 35 import org.junit.Test; 36 import org.mockito.Mock; 37 import org.mockito.MockitoAnnotations; 38 import org.mockito.MockitoSession; 39 40 public class IwlanBroadcastReceiverTest { 41 private static final String TAG = "IwlanBroadcastReceiverTest"; 42 private IwlanBroadcastReceiver mBroadcastReceiver; 43 44 private static final int TEST_SUB_ID = 5; 45 private static final int TEST_SLOT_ID = 6; 46 47 MockitoSession mStaticMockSession; 48 @Mock private Context mMockContext; 49 @Mock private IwlanEventListener mMockIwlanEventListener; 50 51 @Before setUp()52 public void setUp() throws Exception { 53 MockitoAnnotations.initMocks(this); 54 55 mStaticMockSession = 56 mockitoSession() 57 .mockStatic(EpdgSelector.class) 58 .mockStatic(IwlanDataService.class) 59 .mockStatic(IwlanHelper.class) 60 .mockStatic(SubscriptionManager.class) 61 .mockStatic(IwlanEventListener.class) 62 .startMocking(); 63 64 lenient().when(SubscriptionManager.getSlotIndex(eq(TEST_SUB_ID))).thenReturn(TEST_SLOT_ID); 65 66 lenient().when(IwlanDataService.getContext()).thenReturn(mMockContext); 67 68 lenient() 69 .when(IwlanEventListener.getInstance(eq(mMockContext), eq(TEST_SLOT_ID))) 70 .thenReturn(mMockIwlanEventListener); 71 72 // New BroadcastReceiver object 73 mBroadcastReceiver = new IwlanBroadcastReceiver(); 74 } 75 76 @After cleanUp()77 public void cleanUp() throws Exception { 78 mStaticMockSession.finishMocking(); 79 } 80 81 @Test testCarrierConfigChanged()82 public void testCarrierConfigChanged() throws Exception { 83 final Intent intent = new Intent(CarrierConfigManager.ACTION_CARRIER_CONFIG_CHANGED); 84 intent.putExtra(CarrierConfigManager.EXTRA_SLOT_INDEX, TEST_SLOT_ID); 85 86 // Trigger the onReceive 87 mBroadcastReceiver.onReceive(mMockContext, intent); 88 89 verify(mMockIwlanEventListener).onBroadcastReceived(intent); 90 } 91 92 @Test testWifiStateChanged()93 public void testWifiStateChanged() throws Exception { 94 final Intent intent = new Intent(WifiManager.WIFI_STATE_CHANGED_ACTION); 95 intent.putExtra(WifiManager.EXTRA_WIFI_STATE, WifiManager.WIFI_STATE_DISABLED); 96 97 // Trigger broadcast 98 mBroadcastReceiver.onReceive(mMockContext, intent); 99 100 verify(mMockIwlanEventListener).onBroadcastReceived(intent); 101 } 102 103 @Test testScreenOn_shouldSendToListener()104 public void testScreenOn_shouldSendToListener() throws Exception { 105 final Intent intent = new Intent(Intent.ACTION_SCREEN_ON); 106 107 // Trigger broadcast 108 mBroadcastReceiver.onReceive(mMockContext, intent); 109 110 verify(mMockIwlanEventListener).onBroadcastReceived(intent); 111 } 112 } 113