1 /* 2 * Copyright (C) 2016 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 */ 16 package com.android.settings.connecteddevice; 17 18 import android.content.Context; 19 import android.content.pm.PackageManager; 20 import android.nfc.NfcAdapter; 21 import android.nfc.NfcManager; 22 import android.provider.SearchIndexableResource; 23 24 import com.android.settings.R; 25 import com.android.settings.core.PreferenceControllerMixin; 26 import com.android.settings.nfc.NfcPreferenceController; 27 import com.android.settings.testutils.SettingsRobolectricTestRunner; 28 import com.android.settings.TestConfig; 29 import com.android.settings.dashboard.SummaryLoader; 30 import com.android.settings.testutils.FakeFeatureFactory; 31 import com.android.settings.testutils.XmlTestUtils; 32 import com.android.settingslib.drawer.CategoryKey; 33 34 import org.junit.Before; 35 import org.junit.Test; 36 import org.junit.runner.RunWith; 37 import org.mockito.Answers; 38 import org.mockito.Mock; 39 import org.mockito.MockitoAnnotations; 40 import org.robolectric.RuntimeEnvironment; 41 import org.robolectric.annotation.Config; 42 43 import java.util.List; 44 45 import static android.content.Context.NFC_SERVICE; 46 import static com.google.common.truth.Truth.assertThat; 47 import static org.mockito.Mockito.mock; 48 import static org.mockito.Mockito.verify; 49 import static org.mockito.Mockito.verifyZeroInteractions; 50 import static org.mockito.Mockito.when; 51 52 @RunWith(SettingsRobolectricTestRunner.class) 53 @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION) 54 public class ConnectedDeviceDashboardFragmentTest { 55 56 @Mock(answer = Answers.RETURNS_DEEP_STUBS) 57 Context mContext; 58 59 @Mock 60 private PackageManager mManager; 61 62 private FakeFeatureFactory mFeatureFactory; 63 private SmsMirroringFeatureProvider mFeatureProvider; 64 private ConnectedDeviceDashboardFragment mFragment; 65 private TestSmsMirroringPreferenceController mSmsMirroringPreferenceController; 66 67 private static final class TestSmsMirroringPreferenceController 68 extends SmsMirroringPreferenceController implements PreferenceControllerMixin { 69 70 private boolean mIsAvailable; 71 72 public TestSmsMirroringPreferenceController(Context context) { 73 super(context); 74 } 75 76 @Override 77 public boolean isAvailable() { 78 return mIsAvailable; 79 } 80 } 81 82 @Before 83 public void setUp() { 84 MockitoAnnotations.initMocks(this); 85 FakeFeatureFactory.setupForTest(mContext); 86 mFeatureFactory = (FakeFeatureFactory) FakeFeatureFactory.getFactory(mContext); 87 mFeatureProvider = mFeatureFactory.smsMirroringFeatureProvider; 88 89 mFragment = new ConnectedDeviceDashboardFragment(); 90 when(mContext.getPackageManager()).thenReturn(mManager); 91 92 mSmsMirroringPreferenceController = new TestSmsMirroringPreferenceController(mContext); 93 when(mFeatureProvider.getController(mContext)).thenReturn( 94 mSmsMirroringPreferenceController); 95 } 96 97 @Test 98 public void testCategory_isConnectedDevice() { 99 assertThat(mFragment.getCategoryKey()).isEqualTo(CategoryKey.CATEGORY_DEVICE); 100 } 101 102 @Test 103 public void testSearchIndexProvider_shouldIndexResource() { 104 final List<SearchIndexableResource> indexRes = 105 mFragment.SEARCH_INDEX_DATA_PROVIDER.getXmlResourcesToIndex(mContext, 106 true /* enabled */); 107 108 assertThat(indexRes).isNotNull(); 109 assertThat(indexRes.get(0).xmlResId).isEqualTo(mFragment.getPreferenceScreenResId()); 110 } 111 112 @Test 113 public void testSearchIndexProvider_NoNfc_KeyAdded() { 114 when(mManager.hasSystemFeature(PackageManager.FEATURE_NFC)).thenReturn(false); 115 final List<String> keys = mFragment.SEARCH_INDEX_DATA_PROVIDER.getNonIndexableKeys( 116 mContext); 117 118 assertThat(keys).isNotNull(); 119 assertThat(keys).contains(NfcPreferenceController.KEY_TOGGLE_NFC); 120 assertThat(keys).contains(NfcPreferenceController.KEY_ANDROID_BEAM_SETTINGS); 121 } 122 123 @Test 124 public void testSearchIndexProvider_NFC_KeyNotAdded() { 125 when(mManager.hasSystemFeature(PackageManager.FEATURE_NFC)).thenReturn(true); 126 final List<String> keys = mFragment.SEARCH_INDEX_DATA_PROVIDER.getNonIndexableKeys( 127 mContext); 128 129 assertThat(keys).isNotNull(); 130 assertThat(keys).doesNotContain(NfcPreferenceController.KEY_TOGGLE_NFC); 131 assertThat(keys).doesNotContain(NfcPreferenceController.KEY_ANDROID_BEAM_SETTINGS); 132 } 133 134 @Test 135 public void testSearchIndexProvider_NoSmsMirroring_KeyAdded() { 136 when(mFeatureProvider.shouldShowSmsMirroring(mContext)).thenReturn(false); 137 mSmsMirroringPreferenceController.mIsAvailable = false; 138 139 final List<String> keys = mFragment.SEARCH_INDEX_DATA_PROVIDER.getNonIndexableKeys( 140 mContext); 141 142 assertThat(keys).isNotNull(); 143 assertThat(keys).contains(mSmsMirroringPreferenceController.getPreferenceKey()); 144 } 145 146 @Test 147 public void testSearchIndexProvider_SmsMirroring_KeyNotAdded() { 148 when(mFeatureProvider.shouldShowSmsMirroring(mContext)).thenReturn(true); 149 mSmsMirroringPreferenceController.mIsAvailable = true; 150 151 final List<String> keys = mFragment.SEARCH_INDEX_DATA_PROVIDER.getNonIndexableKeys( 152 mContext); 153 154 assertThat(keys).isNotNull(); 155 assertThat(keys).doesNotContain(mSmsMirroringPreferenceController.getPreferenceKey()); 156 } 157 158 @Test 159 public void testNonIndexableKeys_existInXmlLayout() { 160 final Context context = RuntimeEnvironment.application; 161 when(mManager.hasSystemFeature(PackageManager.FEATURE_NFC)).thenReturn(false); 162 final List<String> niks = ConnectedDeviceDashboardFragment.SEARCH_INDEX_DATA_PROVIDER 163 .getNonIndexableKeys(mContext); 164 final int xmlId = (new ConnectedDeviceDashboardFragment()).getPreferenceScreenResId(); 165 166 final List<String> keys = XmlTestUtils.getKeysFromPreferenceXml(context, xmlId); 167 168 assertThat(keys).containsAllIn(niks); 169 } 170 171 @Test 172 public void testSummaryProvider_hasNfc_shouldReturnNfcSummary() { 173 final NfcManager nfcManager = mock(NfcManager.class); 174 final SummaryLoader summaryLoader = mock(SummaryLoader.class); 175 176 when(mContext.getApplicationContext()).thenReturn(mContext); 177 when(mContext.getSystemService(NFC_SERVICE)).thenReturn(nfcManager); 178 when(nfcManager.getDefaultAdapter()).thenReturn(mock(NfcAdapter.class)); 179 180 SummaryLoader.SummaryProvider provider = 181 new ConnectedDeviceDashboardFragment.SummaryProvider(mContext, summaryLoader); 182 183 provider.setListening(false); 184 185 verifyZeroInteractions(summaryLoader); 186 187 provider.setListening(true); 188 189 verify(mContext).getString(R.string.connected_devices_dashboard_summary); 190 } 191 192 @Test 193 public void testSummaryProvider_noNfc_shouldReturnNoNfcSummary() { 194 final SummaryLoader summaryLoader = mock(SummaryLoader.class); 195 196 when(mContext.getApplicationContext()).thenReturn(mContext); 197 when(mContext.getSystemService(NFC_SERVICE)).thenReturn(null); 198 199 SummaryLoader.SummaryProvider provider = 200 new ConnectedDeviceDashboardFragment.SummaryProvider(mContext, summaryLoader); 201 202 provider.setListening(false); 203 204 verifyZeroInteractions(summaryLoader); 205 206 provider.setListening(true); 207 208 verify(mContext).getString(R.string.connected_devices_dashboard_no_nfc_summary); 209 } 210 } 211