• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.PowerAllowlistBackend;
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 private PowerAllowlistBackend mPowerAllowlistBackend;
55     @Mock private BatteryUtils mBatteryUtils;
56 
57     @Before
setUp()58     public void setUp() {
59         mFeatureFactory = FakeFeatureFactory.setupForTest();
60 
61         MockitoAnnotations.initMocks(this);
62         mContext = RuntimeEnvironment.application;
63         mFragment = spy(new HighPowerDetail());
64         mFragment.mBackend = mPowerAllowlistBackend;
65         mFragment.mBatteryUtils = mBatteryUtils;
66         mFragment.mPackageUid = TEST_UID;
67         mFragment.mPackageName = TEST_PACKAGE;
68     }
69 
70     @Test
logSpecialPermissionChange()71     public void logSpecialPermissionChange() {
72         // Deny means app is allowlisted to opt out of power save restrictions
73         HighPowerDetail.logSpecialPermissionChange(true, "app", RuntimeEnvironment.application);
74         verify(mFeatureFactory.metricsFeatureProvider)
75                 .action(
76                         any(Context.class),
77                         eq(MetricsProto.MetricsEvent.APP_SPECIAL_PERMISSION_BATTERY_DENY),
78                         eq("app"));
79 
80         // Allow means app is NOT allowlisted to opt out of power save restrictions
81         HighPowerDetail.logSpecialPermissionChange(false, "app", RuntimeEnvironment.application);
82         verify(mFeatureFactory.metricsFeatureProvider)
83                 .action(
84                         any(Context.class),
85                         eq(MetricsProto.MetricsEvent.APP_SPECIAL_PERMISSION_BATTERY_ALLOW),
86                         eq("app"));
87     }
88 
89     @Test
onClick_appAddedToDozeAllowlist_getsUnrestricted()90     public void onClick_appAddedToDozeAllowlist_getsUnrestricted() {
91         mFragment.mIsEnabled = true;
92         when(mPowerAllowlistBackend.isAllowlisted(TEST_PACKAGE, TEST_UID)).thenReturn(false);
93         mFragment.onClick(null, DialogInterface.BUTTON_POSITIVE);
94         verify(mBatteryUtils)
95                 .setForceAppStandby(TEST_UID, TEST_PACKAGE, AppOpsManager.MODE_ALLOWED);
96     }
97 
98     @Test
getSummary_defaultActivePackage_returnUnavailable()99     public void getSummary_defaultActivePackage_returnUnavailable() {
100         doReturn(true).when(mPowerAllowlistBackend).isDefaultActiveApp(TEST_PACKAGE, TEST_UID);
101 
102         assertThat(
103                         HighPowerDetail.getSummary(
104                                 mContext, mPowerAllowlistBackend, TEST_PACKAGE, TEST_UID))
105                 .isEqualTo(mContext.getString(R.string.high_power_system));
106     }
107 }
108