1 /* 2 * Copyright 2016, 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.managedprovisioning.task.wifi; 18 19 import static org.mockito.Matchers.eq; 20 import static org.mockito.Mockito.verify; 21 import static org.mockito.Mockito.verifyZeroInteractions; 22 import static org.mockito.Mockito.when; 23 24 import android.content.BroadcastReceiver; 25 import android.content.Context; 26 import android.content.Intent; 27 import android.net.ConnectivityManager; 28 import android.support.test.filters.SmallTest; 29 30 import com.android.managedprovisioning.common.Utils; 31 32 import org.junit.Before; 33 import org.junit.Test; 34 import org.mockito.ArgumentCaptor; 35 import org.mockito.Mock; 36 import org.mockito.MockitoAnnotations; 37 38 /** 39 * Unit tests for {@link NetworkMonitor}. 40 */ 41 @SmallTest 42 public class NetworkMonitorTest { 43 44 @Mock private Context mContext; 45 @Mock private Utils mUtils; 46 @Mock private NetworkMonitor.NetworkConnectedCallback mCallback; 47 private NetworkMonitor mNetworkMonitor; 48 49 @Before 50 public void setUp() { 51 MockitoAnnotations.initMocks(this); 52 53 mNetworkMonitor = new NetworkMonitor(mContext, mUtils); 54 } 55 56 @Test 57 public void testStartListening() { 58 // WHEN starting to listen for connectivity changes 59 mNetworkMonitor.startListening(mCallback); 60 61 // THEN a broadcast receiver should be registered 62 ArgumentCaptor<BroadcastReceiver> receiverCaptor = 63 ArgumentCaptor.forClass(BroadcastReceiver.class); 64 verify(mContext).registerReceiver(receiverCaptor.capture(), eq(NetworkMonitor.FILTER)); 65 66 // WHEN connectivity is not obtained and a broadcast is received 67 when(mUtils.isConnectedToWifi(mContext)).thenReturn(false); 68 receiverCaptor.getValue().onReceive(mContext, 69 new Intent(ConnectivityManager.CONNECTIVITY_ACTION)); 70 71 // THEN no callback should be given 72 verifyZeroInteractions(mCallback); 73 74 // WHEN connectivity is obtained and a broadcast is received 75 when(mUtils.isConnectedToNetwork(mContext)).thenReturn(true); 76 receiverCaptor.getValue().onReceive(mContext, 77 new Intent(ConnectivityManager.CONNECTIVITY_ACTION)); 78 79 // THEN a callback should be given 80 verify(mCallback).onNetworkConnected(); 81 } 82 83 @Test 84 public void testStopListening() { 85 // WHEN starting and stopping to listen for connectivity changes 86 mNetworkMonitor.startListening(mCallback); 87 mNetworkMonitor.stopListening(); 88 89 // THEN a broadcast receiver should be registered and later unregistered 90 ArgumentCaptor<BroadcastReceiver> receiverCaptor = 91 ArgumentCaptor.forClass(BroadcastReceiver.class); 92 verify(mContext).registerReceiver(receiverCaptor.capture(), eq(NetworkMonitor.FILTER)); 93 verify(mContext).unregisterReceiver(receiverCaptor.getValue()); 94 95 // WHEN connectivity is not obtained and a broadcast is received 96 when(mUtils.isConnectedToNetwork(mContext)).thenReturn(false); 97 receiverCaptor.getValue().onReceive(mContext, 98 new Intent(ConnectivityManager.CONNECTIVITY_ACTION)); 99 100 // THEN no callback should be given 101 verifyZeroInteractions(mCallback); 102 103 // WHEN connectivity is obtained and a broadcast is received 104 when(mUtils.isConnectedToWifi(mContext)).thenReturn(true); 105 receiverCaptor.getValue().onReceive(mContext, 106 new Intent(ConnectivityManager.CONNECTIVITY_ACTION)); 107 108 // THEN no callback should be given 109 verifyZeroInteractions(mCallback); 110 } 111 } 112