1/* 2 * Copyright (C) 2017 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 */ 16package com.android.settings; 17 18import static com.google.common.truth.Truth.assertThat; 19import static org.mockito.Matchers.any; 20import static org.mockito.Mockito.never; 21import static org.mockito.Mockito.verify; 22 23import android.content.Context; 24import android.content.Intent; 25import android.net.wifi.WifiManager; 26 27import com.android.settings.testutils.SettingsRobolectricTestRunner; 28 29import java.util.ArrayList; 30import org.junit.Before; 31import org.junit.Test; 32import org.junit.runner.RunWith; 33import org.mockito.Mock; 34import org.mockito.MockitoAnnotations; 35import org.robolectric.annotation.Config; 36import org.robolectric.shadows.ShadowApplication; 37import org.robolectric.util.ReflectionHelpers; 38 39@RunWith(SettingsRobolectricTestRunner.class) 40@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION) 41public class TetherServiceTest { 42 43 @Mock 44 private Context mContext; 45 46 private ShadowApplication mShadowApplication; 47 private Context mAppContext; 48 private TetherService mService; 49 50 @Before 51 public void setUp() { 52 MockitoAnnotations.initMocks(this); 53 mShadowApplication = ShadowApplication.getInstance(); 54 mAppContext = mShadowApplication.getApplicationContext(); 55 mService = new TetherService(); 56 ReflectionHelpers.setField(mService, "mBase", mAppContext); 57 mService.setHotspotOffReceiver(new HotspotOffReceiver(mContext)); 58 } 59 60 @Test 61 public void scheduleAlarm_shouldRegisterReceiver() { 62 mService.setHotspotOffReceiver(new HotspotOffReceiver(mAppContext)); 63 64 mService.scheduleAlarm(); 65 66 assertThat(mShadowApplication.hasReceiverForIntent( 67 new Intent(WifiManager.WIFI_AP_STATE_CHANGED_ACTION))).isTrue(); 68 } 69 70 @Test 71 public void cancelAlarmIfNecessary_hasActiveTethers_shouldNotUnregisterReceiver() { 72 mService.scheduleAlarm(); 73 final ArrayList<Integer> tethers = new ArrayList<>(); 74 tethers.add(1); 75 ReflectionHelpers.setField(mService, "mCurrentTethers", tethers); 76 77 mService.cancelAlarmIfNecessary(); 78 verify(mContext, never()).unregisterReceiver(any(HotspotOffReceiver.class)); 79 } 80 81 @Test 82 public void cancelAlarmIfNecessary_noActiveTethers_shouldUnregisterReceiver() { 83 final ArrayList<Integer> tethers = new ArrayList<>(); 84 ReflectionHelpers.setField(mService, "mCurrentTethers", tethers); 85 mService.scheduleAlarm(); 86 87 mService.cancelAlarmIfNecessary(); 88 verify(mContext).unregisterReceiver(any(HotspotOffReceiver.class)); 89 } 90} 91