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