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