From 93c0dbf98db990750b2c877414d6e6accb71bf5a Mon Sep 17 00:00:00 2001 From: Yiling Chuang Date: Thu, 29 Feb 2024 12:00:50 +0000 Subject: [PATCH] Make SmartBatterySettings unsearchable when it's not available. Bug: 323915905 Test: robotest Change-Id: I5adc9d19b482b8e07237f5c7d62d315556095c38 --- .../fuelgauge/SmartBatterySettings.java | 8 ++ .../fuelgauge/SmartBatterySettingsTest.java | 75 +++++++++++++++++++ 2 files changed, 83 insertions(+) create mode 100644 tests/robotests/src/com/android/settings/fuelgauge/SmartBatterySettingsTest.java diff --git a/src/com/android/settings/fuelgauge/SmartBatterySettings.java b/src/com/android/settings/fuelgauge/SmartBatterySettings.java index 44dba9ae3a7..17334813aa7 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.R; 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 @@ public class SmartBatterySettings extends DashboardFragment { 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 00000000000..d841ab6a5f4 --- /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); + } +}