1 /* 2 * Copyright (C) 2013 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 package com.android.inputmethod.latin.utils; 18 19 import static org.junit.Assert.assertEquals; 20 import static org.junit.Assert.assertNull; 21 22 import androidx.test.filters.SmallTest; 23 import androidx.test.runner.AndroidJUnit4; 24 25 import org.junit.Test; 26 import org.junit.runner.RunWith; 27 28 import java.util.HashMap; 29 30 @SmallTest 31 @RunWith(AndroidJUnit4.class) 32 public class ResourceUtilsTests { 33 @Test testFindConstantForKeyValuePairsSimple()34 public void testFindConstantForKeyValuePairsSimple() { 35 final HashMap<String,String> anyKeyValue = new HashMap<>(); 36 anyKeyValue.put("anyKey", "anyValue"); 37 final HashMap<String,String> nullKeyValue = null; 38 final HashMap<String,String> emptyKeyValue = new HashMap<>(); 39 40 final String[] nullArray = null; 41 assertNull(ResourceUtils.findConstantForKeyValuePairs(anyKeyValue, nullArray)); 42 assertNull(ResourceUtils.findConstantForKeyValuePairs(emptyKeyValue, nullArray)); 43 assertNull(ResourceUtils.findConstantForKeyValuePairs(nullKeyValue, nullArray)); 44 45 final String[] emptyArray = {}; 46 assertNull(ResourceUtils.findConstantForKeyValuePairs(anyKeyValue, emptyArray)); 47 assertNull(ResourceUtils.findConstantForKeyValuePairs(emptyKeyValue, emptyArray)); 48 assertNull(ResourceUtils.findConstantForKeyValuePairs(nullKeyValue, emptyArray)); 49 50 final String HARDWARE_KEY = "HARDWARE"; 51 final String[] array = { 52 ",defaultValue", 53 "HARDWARE=grouper,0.3", 54 "HARDWARE=mako,0.4", 55 "HARDWARE=manta,0.2", 56 "HARDWARE=mako,0.5", 57 }; 58 59 final HashMap<String,String> keyValues = new HashMap<>(); 60 keyValues.put(HARDWARE_KEY, "grouper"); 61 assertEquals("0.3", ResourceUtils.findConstantForKeyValuePairs(keyValues, array)); 62 keyValues.put(HARDWARE_KEY, "mako"); 63 assertEquals("0.4", ResourceUtils.findConstantForKeyValuePairs(keyValues, array)); 64 keyValues.put(HARDWARE_KEY, "manta"); 65 assertEquals("0.2", ResourceUtils.findConstantForKeyValuePairs(keyValues, array)); 66 67 keyValues.clear(); 68 keyValues.put("hardware", "grouper"); 69 assertNull(ResourceUtils.findConstantForKeyValuePairs(keyValues, array)); 70 71 keyValues.clear(); 72 keyValues.put(HARDWARE_KEY, "MAKO"); 73 assertNull(ResourceUtils.findConstantForKeyValuePairs(keyValues, array)); 74 keyValues.put(HARDWARE_KEY, "mantaray"); 75 assertNull(ResourceUtils.findConstantForKeyValuePairs(keyValues, array)); 76 77 assertNull(ResourceUtils.findConstantForKeyValuePairs(emptyKeyValue, array)); 78 } 79 80 @Test testFindConstantForKeyValuePairsCombined()81 public void testFindConstantForKeyValuePairsCombined() { 82 final String HARDWARE_KEY = "HARDWARE"; 83 final String MODEL_KEY = "MODEL"; 84 final String MANUFACTURER_KEY = "MANUFACTURER"; 85 final String[] array = { 86 ",defaultValue", 87 "no_comma", 88 "error_pattern,0.1", 89 "HARDWARE=grouper:MANUFACTURER=asus,0.3", 90 "HARDWARE=mako:MODEL=Nexus 4,0.4", 91 "HARDWARE=manta:MODEL=Nexus 10:MANUFACTURER=samsung,0.2" 92 }; 93 final String[] failArray = { 94 ",defaultValue", 95 "HARDWARE=grouper:MANUFACTURER=ASUS,0.3", 96 "HARDWARE=mako:MODEL=Nexus_4,0.4", 97 "HARDWARE=mantaray:MODEL=Nexus 10:MANUFACTURER=samsung,0.2" 98 }; 99 100 final HashMap<String,String> keyValues = new HashMap<>(); 101 keyValues.put(HARDWARE_KEY, "grouper"); 102 keyValues.put(MODEL_KEY, "Nexus 7"); 103 keyValues.put(MANUFACTURER_KEY, "asus"); 104 assertEquals("0.3", ResourceUtils.findConstantForKeyValuePairs(keyValues, array)); 105 assertNull(ResourceUtils.findConstantForKeyValuePairs(keyValues, failArray)); 106 107 keyValues.clear(); 108 keyValues.put(HARDWARE_KEY, "mako"); 109 keyValues.put(MODEL_KEY, "Nexus 4"); 110 keyValues.put(MANUFACTURER_KEY, "LGE"); 111 assertEquals("0.4", ResourceUtils.findConstantForKeyValuePairs(keyValues, array)); 112 assertNull(ResourceUtils.findConstantForKeyValuePairs(keyValues, failArray)); 113 114 keyValues.clear(); 115 keyValues.put(HARDWARE_KEY, "manta"); 116 keyValues.put(MODEL_KEY, "Nexus 10"); 117 keyValues.put(MANUFACTURER_KEY, "samsung"); 118 assertEquals("0.2", ResourceUtils.findConstantForKeyValuePairs(keyValues, array)); 119 assertNull(ResourceUtils.findConstantForKeyValuePairs(keyValues, failArray)); 120 keyValues.put(HARDWARE_KEY, "mantaray"); 121 assertNull(ResourceUtils.findConstantForKeyValuePairs(keyValues, array)); 122 assertEquals("0.2", ResourceUtils.findConstantForKeyValuePairs(keyValues, failArray)); 123 } 124 125 @Test testFindConstantForKeyValuePairsRegexp()126 public void testFindConstantForKeyValuePairsRegexp() { 127 final String HARDWARE_KEY = "HARDWARE"; 128 final String MODEL_KEY = "MODEL"; 129 final String MANUFACTURER_KEY = "MANUFACTURER"; 130 final String[] array = { 131 ",defaultValue", 132 "no_comma", 133 "HARDWARE=error_regexp:MANUFACTURER=error[regexp,0.1", 134 "HARDWARE=grouper|tilapia:MANUFACTURER=asus,0.3", 135 "HARDWARE=[mM][aA][kK][oO]:MODEL=Nexus 4,0.4", 136 "HARDWARE=manta.*:MODEL=Nexus 10:MANUFACTURER=samsung,0.2" 137 }; 138 139 final HashMap<String,String> keyValues = new HashMap<>(); 140 keyValues.put(HARDWARE_KEY, "grouper"); 141 keyValues.put(MODEL_KEY, "Nexus 7"); 142 keyValues.put(MANUFACTURER_KEY, "asus"); 143 assertEquals("0.3", ResourceUtils.findConstantForKeyValuePairs(keyValues, array)); 144 keyValues.put(HARDWARE_KEY, "tilapia"); 145 assertEquals("0.3", ResourceUtils.findConstantForKeyValuePairs(keyValues, array)); 146 147 keyValues.clear(); 148 keyValues.put(HARDWARE_KEY, "mako"); 149 keyValues.put(MODEL_KEY, "Nexus 4"); 150 keyValues.put(MANUFACTURER_KEY, "LGE"); 151 assertEquals("0.4", ResourceUtils.findConstantForKeyValuePairs(keyValues, array)); 152 keyValues.put(HARDWARE_KEY, "MAKO"); 153 assertEquals("0.4", ResourceUtils.findConstantForKeyValuePairs(keyValues, array)); 154 155 keyValues.clear(); 156 keyValues.put(HARDWARE_KEY, "manta"); 157 keyValues.put(MODEL_KEY, "Nexus 10"); 158 keyValues.put(MANUFACTURER_KEY, "samsung"); 159 assertEquals("0.2", ResourceUtils.findConstantForKeyValuePairs(keyValues, array)); 160 keyValues.put(HARDWARE_KEY, "mantaray"); 161 assertEquals("0.2", ResourceUtils.findConstantForKeyValuePairs(keyValues, array)); 162 } 163 } 164