1 /* 2 * Copyright (C) 2021 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.settings.display; 18 19 import static com.android.settings.core.BasePreferenceController.AVAILABLE_UNSEARCHABLE; 20 import static com.android.settings.core.BasePreferenceController.UNSUPPORTED_ON_DEVICE; 21 22 import static com.google.common.truth.Truth.assertThat; 23 24 import static org.mockito.ArgumentMatchers.any; 25 import static org.mockito.ArgumentMatchers.anyInt; 26 import static org.mockito.Mockito.doReturn; 27 import static org.mockito.Mockito.when; 28 29 import android.content.ContentResolver; 30 import android.content.Context; 31 import android.content.pm.PackageManager; 32 import android.content.pm.ResolveInfo; 33 import android.content.pm.ServiceInfo; 34 35 import com.android.settings.testutils.ResolveInfoBuilder; 36 37 import org.junit.Before; 38 import org.junit.Test; 39 import org.junit.runner.RunWith; 40 import org.mockito.Mockito; 41 import org.mockito.MockitoAnnotations; 42 import org.robolectric.RobolectricTestRunner; 43 import org.robolectric.RuntimeEnvironment; 44 45 @RunWith(RobolectricTestRunner.class) 46 public class SmartAutoRotateBatterySaverControllerTest { 47 48 private static final String PACKAGE_NAME = "package_name"; 49 50 private SmartAutoRotateBatterySaverController mController; 51 52 @Before setUp()53 public void setUp() { 54 MockitoAnnotations.initMocks(this); 55 final Context context = Mockito.spy(RuntimeEnvironment.application); 56 final ContentResolver contentResolver = RuntimeEnvironment.application.getContentResolver(); 57 when(context.getContentResolver()).thenReturn(contentResolver); 58 final PackageManager packageManager = Mockito.mock(PackageManager.class); 59 when(context.getPackageManager()).thenReturn(packageManager); 60 doReturn(PACKAGE_NAME).when(packageManager).getRotationResolverPackageName(); 61 mController = Mockito.spy( 62 new SmartAutoRotateBatterySaverController(context, "smart_auto_rotate")); 63 when(mController.isPowerSaveMode()).thenReturn(false); 64 65 final ResolveInfo resolveInfo = new ResolveInfoBuilder(PACKAGE_NAME).build(); 66 resolveInfo.serviceInfo = new ServiceInfo(); 67 when(packageManager.resolveService(any(), anyInt())).thenReturn(resolveInfo); 68 } 69 70 @Test getAvailabilityStatus_returnUnsupportedOnDevice()71 public void getAvailabilityStatus_returnUnsupportedOnDevice() { 72 assertThat(mController.getAvailabilityStatus()).isEqualTo(UNSUPPORTED_ON_DEVICE); 73 } 74 75 @Test getAvailabilityStatus_powerSaveModeEnabled_returnAvailableUnSearchAble()76 public void getAvailabilityStatus_powerSaveModeEnabled_returnAvailableUnSearchAble() { 77 when(mController.isPowerSaveMode()).thenReturn(true); 78 assertThat(mController.getAvailabilityStatus()).isEqualTo(AVAILABLE_UNSEARCHABLE); 79 } 80 } 81