1 /*
2  * Copyright (C) 2024 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.Mockito.when;
22 
23 import android.content.Context;
24 
25 import com.android.settings.search.BaseSearchIndexProvider;
26 import com.android.settings.testutils.FakeFeatureFactory;
27 
28 import org.junit.Before;
29 import org.junit.Test;
30 import org.junit.runner.RunWith;
31 import org.robolectric.RobolectricTestRunner;
32 import org.robolectric.RuntimeEnvironment;
33 import org.robolectric.util.ReflectionHelpers;
34 
35 @RunWith(RobolectricTestRunner.class)
36 public class SmartBatterySettingsTest {
37     private Context mContext;
38     private PowerUsageFeatureProvider mPowerUsageFeatureProvider;
39     private SmartBatterySettings mFragment;
40 
41     @Before
setUp()42     public void setUp() {
43         mContext = RuntimeEnvironment.application;
44         mPowerUsageFeatureProvider = FakeFeatureFactory.setupForTest().powerUsageFeatureProvider;
45         mFragment = new SmartBatterySettings();
46     }
47 
48     @Test
isPageSearchEnabled_smartBatterySupported_returnTrue()49     public void isPageSearchEnabled_smartBatterySupported_returnTrue() {
50         when(mPowerUsageFeatureProvider.isSmartBatterySupported()).thenReturn(true);
51         final BaseSearchIndexProvider provider =
52                 (BaseSearchIndexProvider) mFragment.SEARCH_INDEX_DATA_PROVIDER;
53 
54         assertIsPageSearchEnabled(true);
55     }
56 
57     @Test
isPageSearchEnabled_smartBatteryUnsupported_returnFalse()58     public void isPageSearchEnabled_smartBatteryUnsupported_returnFalse() {
59         when(mPowerUsageFeatureProvider.isSmartBatterySupported()).thenReturn(false);
60 
61         assertIsPageSearchEnabled(false);
62     }
63 
assertIsPageSearchEnabled(boolean expectedResult)64     private void assertIsPageSearchEnabled(boolean expectedResult) {
65         final BaseSearchIndexProvider provider =
66                 (BaseSearchIndexProvider) mFragment.SEARCH_INDEX_DATA_PROVIDER;
67 
68         final Object obj =
69                 org.robolectric.util.ReflectionHelpers.callInstanceMethod(
70                         provider, /*methodName=*/ "isPageSearchEnabled",
71                         ReflectionHelpers.ClassParameter.from(Context.class, mContext));
72         final boolean isEnabled = (Boolean) obj;
73         assertThat(isEnabled).isEqualTo(expectedResult);
74     }
75 }
76