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 */ 17 18 package com.android.settings.search; 19 20 import static com.google.common.truth.Truth.assertThat; 21 22 import android.content.Context; 23 import android.util.ArrayMap; 24 25 import com.android.internal.hardware.AmbientDisplayConfiguration; 26 import com.android.settings.testutils.SettingsRobolectricTestRunner; 27 import com.android.settings.TestConfig; 28 import com.android.settings.core.PreferenceControllerMixin; 29 import com.android.settings.deviceinfo.SystemUpdatePreferenceController; 30 31 import org.junit.Before; 32 import org.junit.Test; 33 import org.junit.runner.RunWith; 34 import org.mockito.Mock; 35 import org.mockito.MockitoAnnotations; 36 import org.robolectric.RuntimeEnvironment; 37 import org.robolectric.annotation.Config; 38 39 import java.util.Map; 40 41 @RunWith(SettingsRobolectricTestRunner.class) 42 @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION) 43 public class DatabaseIndexingUtilsTest { 44 45 private Context mContext; 46 @Mock 47 private AmbientDisplayConfiguration mAmbientDisplayConfiguration; 48 49 @Before 50 public void setUp() { 51 MockitoAnnotations.initMocks(this); 52 mContext = RuntimeEnvironment.application; 53 } 54 55 @Test 56 public void testGetPreferenceControllerUriMap_BadClassName_ReturnsNull() { 57 Map map = DatabaseIndexingUtils.getPreferenceControllerUriMap("dummy", mContext); 58 assertThat(map).isNull(); 59 } 60 61 @Test 62 public void testGetPreferenceControllerUriMap_NullContext_ReturnsNull() { 63 Map map = DatabaseIndexingUtils.getPreferenceControllerUriMap("dummy", null); 64 assertThat(map).isNull(); 65 } 66 67 @Test 68 public void testGetPreferenceControllerUriMap_CompatibleClass_ReturnsValidMap() { 69 final String className = "com.android.settings.system.SystemDashboardFragment"; 70 final Map<String, PreferenceControllerMixin> map = 71 DatabaseIndexingUtils.getPreferenceControllerUriMap(className, mContext); 72 assertThat(map.get("system_update_settings")) 73 .isInstanceOf(SystemUpdatePreferenceController.class); 74 } 75 76 @Test 77 public void testGetPayloadFromMap_NullMap_ReturnsNull() { 78 ResultPayload payload = DatabaseIndexingUtils.getPayloadFromUriMap(null, ""); 79 assertThat(payload).isNull(); 80 } 81 82 @Test 83 public void testGetPayloadFromMap_MatchingKey_ReturnsPayload() { 84 final String key = "key"; 85 PreferenceControllerMixin prefController = new PreferenceControllerMixin() { 86 @Override 87 public ResultPayload getResultPayload() { 88 return new ResultPayload(null); 89 } 90 }; 91 ArrayMap<String, PreferenceControllerMixin> map = new ArrayMap<>(); 92 map.put(key, prefController); 93 94 ResultPayload payload = DatabaseIndexingUtils.getPayloadFromUriMap(map, key); 95 assertThat(payload).isInstanceOf(ResultPayload.class); 96 } 97 }