1/* 2 * Copyright (C) 2017 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file 5 * except in compliance with the License. You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software distributed under the 10 * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 11 * KIND, either express or implied. See the License for the specific language governing 12 * permissions and limitations under the License. 13 */ 14 15package com.android.systemui.statusbar.policy; 16 17import static org.mockito.Mockito.mock; 18import static org.mockito.Mockito.spy; 19import static org.mockito.Mockito.when; 20 21import android.content.Intent; 22import android.location.LocationManager; 23import android.support.test.filters.SmallTest; 24import android.testing.AndroidTestingRunner; 25import android.testing.TestableLooper; 26import android.testing.TestableLooper.RunWithLooper; 27 28import com.android.systemui.SysuiTestCase; 29import com.android.systemui.statusbar.policy.LocationController.LocationChangeCallback; 30 31import org.junit.Before; 32import org.junit.Test; 33import org.junit.runner.RunWith; 34 35@RunWith(AndroidTestingRunner.class) 36@RunWithLooper 37@SmallTest 38public class LocationControllerImplTest extends SysuiTestCase { 39 40 private LocationControllerImpl mLocationController; 41 42 @Before 43 public void setup() { 44 mLocationController = spy(new LocationControllerImpl(mContext, 45 TestableLooper.get(this).getLooper())); 46 } 47 48 @Test 49 public void testRemoveSelfActive_DoesNotCrash() { 50 LocationController.LocationChangeCallback callback = new LocationChangeCallback() { 51 @Override 52 public void onLocationActiveChanged(boolean active) { 53 mLocationController.removeCallback(this); 54 } 55 }; 56 mLocationController.addCallback(callback); 57 mLocationController.addCallback(mock(LocationChangeCallback.class)); 58 59 when(mLocationController.areActiveHighPowerLocationRequests()).thenReturn(false); 60 mLocationController.onReceive(mContext, new Intent( 61 LocationManager.HIGH_POWER_REQUEST_CHANGE_ACTION)); 62 when(mLocationController.areActiveHighPowerLocationRequests()).thenReturn(true); 63 mLocationController.onReceive(mContext, new Intent( 64 LocationManager.HIGH_POWER_REQUEST_CHANGE_ACTION)); 65 66 TestableLooper.get(this).processAllMessages(); 67 } 68 69 @Test 70 public void testRemoveSelfSettings_DoesNotCrash() { 71 LocationController.LocationChangeCallback callback = new LocationChangeCallback() { 72 @Override 73 public void onLocationSettingsChanged(boolean isEnabled) { 74 mLocationController.removeCallback(this); 75 } 76 }; 77 mLocationController.addCallback(callback); 78 mLocationController.addCallback(mock(LocationChangeCallback.class)); 79 80 TestableLooper.get(this).processAllMessages(); 81 } 82}