Merge "Hide search results when Battery info page is disabled." into main
diff --git a/src/com/android/settings/deviceinfo/batteryinfo/BatteryInfoFragment.java b/src/com/android/settings/deviceinfo/batteryinfo/BatteryInfoFragment.java
index 1731212..c017022 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 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 0000000..d0dda84
--- /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();
+    }
+}