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 17 package com.android.settings.fuelgauge; 18 19 import static com.google.common.truth.Truth.assertThat; 20 21 import static org.mockito.ArgumentMatchers.any; 22 import static org.mockito.ArgumentMatchers.eq; 23 import static org.mockito.Mockito.doReturn; 24 import static org.mockito.Mockito.spy; 25 import static org.mockito.Mockito.verify; 26 import static org.mockito.Mockito.when; 27 28 import android.app.AppOpsManager; 29 import android.content.Context; 30 import android.content.DialogInterface; 31 32 import com.android.internal.logging.nano.MetricsProto; 33 import com.android.settings.R; 34 import com.android.settings.testutils.FakeFeatureFactory; 35 import com.android.settingslib.fuelgauge.PowerWhitelistBackend; 36 37 import org.junit.Before; 38 import org.junit.Test; 39 import org.junit.runner.RunWith; 40 import org.mockito.Mock; 41 import org.mockito.MockitoAnnotations; 42 import org.robolectric.RobolectricTestRunner; 43 import org.robolectric.RuntimeEnvironment; 44 45 @RunWith(RobolectricTestRunner.class) 46 public class HighPowerDetailTest { 47 private static final int TEST_UID = 12000; 48 private static final String TEST_PACKAGE = "com.test.package"; 49 50 private FakeFeatureFactory mFeatureFactory; 51 private HighPowerDetail mFragment; 52 53 private Context mContext; 54 @Mock 55 private PowerWhitelistBackend mPowerWhitelistBackend; 56 @Mock 57 private BatteryUtils mBatteryUtils; 58 59 @Before setUp()60 public void setUp() { 61 mFeatureFactory = FakeFeatureFactory.setupForTest(); 62 63 MockitoAnnotations.initMocks(this); 64 mContext = RuntimeEnvironment.application; 65 mFragment = spy(new HighPowerDetail()); 66 mFragment.mBackend = mPowerWhitelistBackend; 67 mFragment.mBatteryUtils = mBatteryUtils; 68 mFragment.mPackageUid = TEST_UID; 69 mFragment.mPackageName = TEST_PACKAGE; 70 } 71 72 @Test logSpecialPermissionChange()73 public void logSpecialPermissionChange() { 74 // Deny means app is whitelisted to opt out of power save restrictions 75 HighPowerDetail.logSpecialPermissionChange(true, "app", RuntimeEnvironment.application); 76 verify(mFeatureFactory.metricsFeatureProvider).action(any(Context.class), 77 eq(MetricsProto.MetricsEvent.APP_SPECIAL_PERMISSION_BATTERY_DENY), eq("app")); 78 79 // Allow means app is NOT whitelisted to opt out of power save restrictions 80 HighPowerDetail.logSpecialPermissionChange(false, "app", RuntimeEnvironment.application); 81 verify(mFeatureFactory.metricsFeatureProvider).action(any(Context.class), 82 eq(MetricsProto.MetricsEvent.APP_SPECIAL_PERMISSION_BATTERY_ALLOW), eq("app")); 83 } 84 85 @Test onClick_appAddedToDozeWhitelist_getsUnrestricted()86 public void onClick_appAddedToDozeWhitelist_getsUnrestricted() { 87 mFragment.mIsEnabled = true; 88 when(mPowerWhitelistBackend.isWhitelisted(TEST_PACKAGE)).thenReturn(false); 89 mFragment.onClick(null, DialogInterface.BUTTON_POSITIVE); 90 verify(mBatteryUtils).setForceAppStandby(TEST_UID, TEST_PACKAGE, 91 AppOpsManager.MODE_ALLOWED); 92 } 93 94 @Test getSummary_defaultActivePackage_returnUnavailable()95 public void getSummary_defaultActivePackage_returnUnavailable() { 96 doReturn(true).when(mPowerWhitelistBackend).isDefaultActiveApp(TEST_PACKAGE); 97 98 assertThat(HighPowerDetail.getSummary(mContext, mPowerWhitelistBackend, TEST_PACKAGE)) 99 .isEqualTo(mContext.getString(R.string.high_power_system)); 100 } 101 } 102