Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit b76f32ef authored by Treehugger Robot's avatar Treehugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Hide search results when Battery info page is disabled." into main

parents f122a93e 43260f6f
Loading
Loading
Loading
Loading
+11 −4
Original line number Diff line number Diff line
@@ -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);
                }
            };
}
+79 −0
Original line number Diff line number Diff line
/*
 * 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();
    }
}