AdvancedConnectedDeviceDashboardFragmentTest.java revision 58eb43a2e4fe86665229318b96ab351b229c7000
1/* 2 * Copyright (C) 2017 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License 15 */ 16package com.android.settings.connecteddevice; 17 18import static com.google.common.truth.Truth.assertThat; 19 20import static org.mockito.Mockito.when; 21 22import android.content.Context; 23import android.content.pm.PackageManager; 24import android.provider.SearchIndexableResource; 25 26import com.android.settings.TestConfig; 27import com.android.settings.core.PreferenceControllerMixin; 28import com.android.settings.nfc.NfcPreferenceController; 29import com.android.settings.testutils.FakeFeatureFactory; 30import com.android.settings.testutils.SettingsRobolectricTestRunner; 31import com.android.settings.testutils.XmlTestUtils; 32import com.android.settingslib.drawer.CategoryKey; 33 34import org.junit.Before; 35import org.junit.Test; 36import org.junit.runner.RunWith; 37import org.mockito.Answers; 38import org.mockito.Mock; 39import org.mockito.MockitoAnnotations; 40import org.robolectric.RuntimeEnvironment; 41import org.robolectric.annotation.Config; 42 43import java.util.List; 44 45@RunWith(SettingsRobolectricTestRunner.class) 46@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION) 47public class AdvancedConnectedDeviceDashboardFragmentTest { 48 49 @Mock(answer = Answers.RETURNS_DEEP_STUBS) 50 private Context mContext; 51 52 @Mock 53 private PackageManager mManager; 54 55 private FakeFeatureFactory mFeatureFactory; 56 private AdvancedConnectedDeviceDashboardFragment mFragment; 57 58 @Before 59 public void setUp() { 60 MockitoAnnotations.initMocks(this); 61 mFeatureFactory = FakeFeatureFactory.setupForTest(); 62 63 mFragment = new AdvancedConnectedDeviceDashboardFragment(); 64 when(mContext.getPackageManager()).thenReturn(mManager); 65 } 66 67 @Test 68 public void testCategory_isConnectedDevice() { 69 assertThat(mFragment.getCategoryKey()).isEqualTo(CategoryKey.CATEGORY_DEVICE); 70 } 71 72 @Test 73 public void testSearchIndexProvider_shouldIndexResource() { 74 final List<SearchIndexableResource> indexRes = 75 mFragment.SEARCH_INDEX_DATA_PROVIDER.getXmlResourcesToIndex(mContext, 76 true /* enabled */); 77 78 assertThat(indexRes).isNotNull(); 79 assertThat(indexRes.get(0).xmlResId).isEqualTo(mFragment.getPreferenceScreenResId()); 80 } 81 82 @Test 83 public void testSearchIndexProvider_NoNfc_KeyAdded() { 84 when(mManager.hasSystemFeature(PackageManager.FEATURE_NFC)).thenReturn(false); 85 final List<String> keys = mFragment.SEARCH_INDEX_DATA_PROVIDER.getNonIndexableKeys( 86 mContext); 87 88 assertThat(keys).isNotNull(); 89 assertThat(keys).contains(NfcPreferenceController.KEY_TOGGLE_NFC); 90 assertThat(keys).contains(NfcPreferenceController.KEY_ANDROID_BEAM_SETTINGS); 91 } 92 93 @Test 94 public void testSearchIndexProvider_NFC_KeyNotAdded() { 95 when(mManager.hasSystemFeature(PackageManager.FEATURE_NFC)).thenReturn(true); 96 final List<String> keys = mFragment.SEARCH_INDEX_DATA_PROVIDER.getNonIndexableKeys( 97 mContext); 98 99 assertThat(keys).isNotNull(); 100 assertThat(keys).doesNotContain(NfcPreferenceController.KEY_TOGGLE_NFC); 101 assertThat(keys).doesNotContain(NfcPreferenceController.KEY_ANDROID_BEAM_SETTINGS); 102 } 103 104 @Test 105 public void testGetCategoryKey_returnCategoryDevice() { 106 assertThat(mFragment.getCategoryKey()).isEqualTo(CategoryKey.CATEGORY_DEVICE); 107 } 108 109 @Test 110 public void testNonIndexableKeys_existInXmlLayout() { 111 final Context context = RuntimeEnvironment.application; 112 when(mManager.hasSystemFeature(PackageManager.FEATURE_NFC)).thenReturn(false); 113 final List<String> niks = ConnectedDeviceDashboardFragment.SEARCH_INDEX_DATA_PROVIDER 114 .getNonIndexableKeys(mContext); 115 final int xmlId = (new ConnectedDeviceDashboardFragment()).getPreferenceScreenResId(); 116 117 final List<String> keys = XmlTestUtils.getKeysFromPreferenceXml(context, xmlId); 118 119 assertThat(keys).containsAllIn(niks); 120 } 121} 122