Merge "Make SmartBatterySettings unsearchable when it's not available." into main
diff --git a/src/com/android/settings/fuelgauge/SmartBatterySettings.java b/src/com/android/settings/fuelgauge/SmartBatterySettings.java
index 44dba9a..1733481 100644
--- a/src/com/android/settings/fuelgauge/SmartBatterySettings.java
+++ b/src/com/android/settings/fuelgauge/SmartBatterySettings.java
@@ -24,6 +24,7 @@
import com.android.settings.SettingsActivity;
import com.android.settings.core.InstrumentedPreferenceFragment;
import com.android.settings.dashboard.DashboardFragment;
+import com.android.settings.overlay.FeatureFactory;
import com.android.settings.search.BaseSearchIndexProvider;
import com.android.settingslib.core.AbstractPreferenceController;
import com.android.settingslib.search.SearchIndexable;
@@ -91,5 +92,12 @@
Context context) {
return buildPreferenceControllers(context, null, null);
}
+
+ @Override
+ protected boolean isPageSearchEnabled(Context context) {
+ return FeatureFactory.getFeatureFactory()
+ .getPowerUsageFeatureProvider()
+ .isSmartBatterySupported();
+ }
};
}
diff --git a/tests/robotests/src/com/android/settings/fuelgauge/SmartBatterySettingsTest.java b/tests/robotests/src/com/android/settings/fuelgauge/SmartBatterySettingsTest.java
new file mode 100644
index 0000000..d841ab6
--- /dev/null
+++ b/tests/robotests/src/com/android/settings/fuelgauge/SmartBatterySettingsTest.java
@@ -0,0 +1,75 @@
+/*
+ * Copyright (C) 2024 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.settings.fuelgauge;
+
+import static com.google.common.truth.Truth.assertThat;
+
+import static org.mockito.Mockito.when;
+
+import android.content.Context;
+
+import com.android.settings.search.BaseSearchIndexProvider;
+import com.android.settings.testutils.FakeFeatureFactory;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.robolectric.RobolectricTestRunner;
+import org.robolectric.RuntimeEnvironment;
+import org.robolectric.util.ReflectionHelpers;
+
+@RunWith(RobolectricTestRunner.class)
+public class SmartBatterySettingsTest {
+ private Context mContext;
+ private PowerUsageFeatureProvider mPowerUsageFeatureProvider;
+ private SmartBatterySettings mFragment;
+
+ @Before
+ public void setUp() {
+ mContext = RuntimeEnvironment.application;
+ mPowerUsageFeatureProvider = FakeFeatureFactory.setupForTest().powerUsageFeatureProvider;
+ mFragment = new SmartBatterySettings();
+ }
+
+ @Test
+ public void isPageSearchEnabled_smartBatterySupported_returnTrue() {
+ when(mPowerUsageFeatureProvider.isSmartBatterySupported()).thenReturn(true);
+ final BaseSearchIndexProvider provider =
+ (BaseSearchIndexProvider) mFragment.SEARCH_INDEX_DATA_PROVIDER;
+
+ assertIsPageSearchEnabled(true);
+ }
+
+ @Test
+ public void isPageSearchEnabled_smartBatteryUnsupported_returnFalse() {
+ when(mPowerUsageFeatureProvider.isSmartBatterySupported()).thenReturn(false);
+
+ assertIsPageSearchEnabled(false);
+ }
+
+ private void assertIsPageSearchEnabled(boolean expectedResult) {
+ final BaseSearchIndexProvider provider =
+ (BaseSearchIndexProvider) mFragment.SEARCH_INDEX_DATA_PROVIDER;
+
+ final Object obj =
+ org.robolectric.util.ReflectionHelpers.callInstanceMethod(
+ provider, /*methodName=*/ "isPageSearchEnabled",
+ ReflectionHelpers.ClassParameter.from(Context.class, mContext));
+ final boolean isEnabled = (Boolean) obj;
+ assertThat(isEnabled).isEqualTo(expectedResult);
+ }
+}