Add searchable index for Bluetooth and Nfc only if the feature exists

Test: make RunSettingsRoboTests
Bug: 35657961
Change-Id: Ie2f4722b2b1599981c933f87f95016909ba92022
Signed-off-by: Ruchi Kandoi <kandoiruchi@google.com>
This commit is contained in:
Ruchi Kandoi
2017-04-20 17:17:50 -07:00
parent 008a9adef9
commit 7ca6c2410c
4 changed files with 74 additions and 7 deletions

View File

@@ -36,7 +36,7 @@ public class BluetoothMasterSwitchPreferenceController extends PreferenceControl
implements OnSummaryChangeListener,
LifecycleObserver, OnResume, OnPause, OnStart, OnStop {
private static final String KEY_TOGGLE_BLUETOOTH = "toggle_bluetooth";
public static final String KEY_TOGGLE_BLUETOOTH = "toggle_bluetooth";
private LocalBluetoothManager mBluetoothManager;
private MasterSwitchPreference mBtPreference;

View File

@@ -16,6 +16,7 @@
package com.android.settings.connecteddevice;
import android.content.Context;
import android.content.pm.PackageManager;
import android.provider.SearchIndexableResource;
import com.android.internal.logging.nano.MetricsProto;
@@ -85,5 +86,20 @@ public class ConnectedDeviceDashboardFragment extends DashboardFragment {
sir.xmlResId = R.xml.connected_devices;
return Arrays.asList(sir);
}
@Override
public List<String> getNonIndexableKeys(Context context) {
PackageManager pm = context.getPackageManager();
final List<String> keys = new ArrayList<String>();
if (!pm.hasSystemFeature(PackageManager.FEATURE_NFC)) {
keys.add(NfcPreferenceController.KEY_TOGGLE_NFC);
keys.add(NfcPreferenceController.KEY_ANDROID_BEAM_SETTINGS);
}
if (!pm.hasSystemFeature(PackageManager.FEATURE_BLUETOOTH)) {
keys.add(BluetoothMasterSwitchPreferenceController.KEY_TOGGLE_BLUETOOTH);
}
return keys;
}
};
}

View File

@@ -39,8 +39,8 @@ import java.util.List;
public class NfcPreferenceController extends PreferenceController
implements LifecycleObserver, OnResume, OnPause {
private static final String KEY_TOGGLE_NFC = "toggle_nfc";
private static final String KEY_ANDROID_BEAM_SETTINGS = "android_beam_settings";
public static final String KEY_TOGGLE_NFC = "toggle_nfc";
public static final String KEY_ANDROID_BEAM_SETTINGS = "android_beam_settings";
private NfcEnabler mNfcEnabler;
private NfcAdapter mNfcAdapter;

View File

@@ -15,8 +15,12 @@
*/
package com.android.settings.connecteddevice;
import android.content.Context;
import android.content.pm.PackageManager;
import android.provider.SearchIndexableResource;
import com.android.settings.bluetooth.BluetoothMasterSwitchPreferenceController;
import com.android.settings.nfc.NfcPreferenceController;
import com.android.settings.SettingsRobolectricTestRunner;
import com.android.settings.TestConfig;
import com.android.settingslib.drawer.CategoryKey;
@@ -24,22 +28,33 @@ import com.android.settingslib.drawer.CategoryKey;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.annotation.Config;
import org.robolectric.shadows.ShadowApplication;
import java.util.List;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
@RunWith(SettingsRobolectricTestRunner.class)
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
public class ConnectedDeviceDashboardFragmentTest {
@Mock
Context mContext;
@Mock
private PackageManager mManager;
private ConnectedDeviceDashboardFragment mFragment;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mFragment = new ConnectedDeviceDashboardFragment();
when(mContext.getPackageManager()).thenReturn(mManager);
}
@Test
@@ -50,11 +65,47 @@ public class ConnectedDeviceDashboardFragmentTest {
@Test
public void testSearchIndexProvider_shouldIndexResource() {
final List<SearchIndexableResource> indexRes =
ConnectedDeviceDashboardFragment.SEARCH_INDEX_DATA_PROVIDER.getXmlResourcesToIndex(
ShadowApplication.getInstance().getApplicationContext(),
true /* enabled */);
mFragment.SEARCH_INDEX_DATA_PROVIDER.getXmlResourcesToIndex(mContext, true /* enabled */);
assertThat(indexRes).isNotNull();
assertThat(indexRes.get(0).xmlResId).isEqualTo(mFragment.getPreferenceScreenResId());
}
@Test
public void testSearchIndexProvider_NoNfc_KeyAdded() {
when(mManager.hasSystemFeature(PackageManager.FEATURE_NFC)).thenReturn(false);
final List<String> keys = mFragment.SEARCH_INDEX_DATA_PROVIDER.getNonIndexableKeys(mContext);
assertThat(keys).isNotNull();
assertThat(keys).contains(NfcPreferenceController.KEY_TOGGLE_NFC);
assertThat(keys).contains(NfcPreferenceController.KEY_ANDROID_BEAM_SETTINGS);
}
@Test
public void testSearchIndexProvider_NFC_KeyNotAdded() {
when(mManager.hasSystemFeature(PackageManager.FEATURE_NFC)).thenReturn(true);
final List<String> keys = mFragment.SEARCH_INDEX_DATA_PROVIDER.getNonIndexableKeys(mContext);
assertThat(keys).isNotNull();
assertThat(keys).doesNotContain(NfcPreferenceController.KEY_TOGGLE_NFC);
assertThat(keys).doesNotContain(NfcPreferenceController.KEY_ANDROID_BEAM_SETTINGS);
}
@Test
public void testSearchIndexProvider_NoBluetooth_KeyAdded() {
when(mManager.hasSystemFeature(PackageManager.FEATURE_BLUETOOTH)).thenReturn(false);
final List<String> keys = mFragment.SEARCH_INDEX_DATA_PROVIDER.getNonIndexableKeys(mContext);
assertThat(keys).isNotNull();
assertThat(keys).contains(BluetoothMasterSwitchPreferenceController.KEY_TOGGLE_BLUETOOTH);
}
@Test
public void testSearchIndexProvider_Bluetooth_KeyNotAdded() {
when(mManager.hasSystemFeature(PackageManager.FEATURE_BLUETOOTH)).thenReturn(true);
final List<String> keys = mFragment.SEARCH_INDEX_DATA_PROVIDER.getNonIndexableKeys(mContext);
assertThat(keys).isNotNull();
assertThat(keys).doesNotContain(BluetoothMasterSwitchPreferenceController.KEY_TOGGLE_BLUETOOTH);
}
}