From 43260f6f283210e7fbc61a65c1e9e7c61ec5b08e Mon Sep 17 00:00:00 2001 From: Yi-Ling Chuang Date: Fri, 24 Nov 2023 11:33:20 +0800 Subject: [PATCH] Hide search results when Battery info page is disabled. Fixes: 312639417 Test: robotest Change-Id: I04279c4068ebb0e06b575ae2df44836d193f9bac --- .../batteryinfo/BatteryInfoFragment.java | 15 +++- .../batteryinfo/BatteryInfoFragmentTest.java | 79 +++++++++++++++++++ 2 files changed, 90 insertions(+), 4 deletions(-) create mode 100644 tests/robotests/src/com/android/settings/deviceinfo/batteryinfo/BatteryInfoFragmentTest.java diff --git a/src/com/android/settings/deviceinfo/batteryinfo/BatteryInfoFragment.java b/src/com/android/settings/deviceinfo/batteryinfo/BatteryInfoFragment.java index 1731212fa07..c0170227bae 100644 --- a/src/com/android/settings/deviceinfo/batteryinfo/BatteryInfoFragment.java +++ b/src/com/android/settings/deviceinfo/batteryinfo/BatteryInfoFragment.java @@ -17,15 +17,15 @@ package com.android.settings.deviceinfo.batteryinfo; import android.app.settings.SettingsEnums; +import android.content.Context; import com.android.settings.R; import com.android.settings.dashboard.DashboardFragment; +import com.android.settings.overlay.FeatureFactory; import com.android.settings.search.BaseSearchIndexProvider; import com.android.settingslib.search.SearchIndexable; -/** - * A fragment that shows battery hardware information. - */ +/** A fragment that shows battery hardware information. */ @SearchIndexable public class BatteryInfoFragment extends DashboardFragment { @@ -47,5 +47,12 @@ public class BatteryInfoFragment extends DashboardFragment { } public static final BaseSearchIndexProvider SEARCH_INDEX_DATA_PROVIDER = - new BaseSearchIndexProvider(R.xml.battery_info); + new BaseSearchIndexProvider(R.xml.battery_info) { + @Override + protected boolean isPageSearchEnabled(Context context) { + return FeatureFactory.getFeatureFactory() + .getBatterySettingsFeatureProvider() + .isBatteryInfoEnabled(context); + } + }; } diff --git a/tests/robotests/src/com/android/settings/deviceinfo/batteryinfo/BatteryInfoFragmentTest.java b/tests/robotests/src/com/android/settings/deviceinfo/batteryinfo/BatteryInfoFragmentTest.java new file mode 100644 index 00000000000..d0dda845a06 --- /dev/null +++ b/tests/robotests/src/com/android/settings/deviceinfo/batteryinfo/BatteryInfoFragmentTest.java @@ -0,0 +1,79 @@ +/* + * Copyright (C) 2023 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.deviceinfo.batteryinfo; + +import static com.google.common.truth.Truth.assertThat; + +import static org.mockito.Mockito.when; + +import android.content.Context; + +import androidx.test.core.app.ApplicationProvider; + +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.util.ReflectionHelpers; + +@RunWith(org.robolectric.RobolectricTestRunner.class) +public class BatteryInfoFragmentTest { + private Context mContext; + private FakeFeatureFactory mFactory; + private BatteryInfoFragment mFragment; + + @Before + public void setUp() { + mContext = ApplicationProvider.getApplicationContext(); + mFactory = FakeFeatureFactory.setupForTest(); + mFragment = new BatteryInfoFragment(); + } + + @Test + public void isPageSearchEnabled_batteryInfoEnabled_returnTrue() { + when(mFactory.batterySettingsFeatureProvider.isBatteryInfoEnabled(mContext)) + .thenReturn(true); + + 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).isTrue(); + } + + @Test + public void isPageSearchEnabled_batteryInfoDisabled_returnFalse() { + when(mFactory.batterySettingsFeatureProvider.isBatteryInfoEnabled(mContext)) + .thenReturn(false); + + 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).isFalse(); + } +}