Home | History | Annotate | Download | only in utils
      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 android.test.AndroidTestCase;
     20 import android.test.suitebuilder.annotation.SmallTest;
     21 
     22 import com.android.inputmethod.latin.utils.ResourceUtils.DeviceOverridePatternSyntaxError;
     23 
     24 import java.util.HashMap;
     25 
     26 @SmallTest
     27 public class ResourceUtilsTests extends AndroidTestCase {
     28     public void testFindDefaultConstant() {
     29         final String[] nullArray = null;
     30         final String[] emptyArray = {};
     31         final String[] array = {
     32                 "HARDWARE=grouper,0.3",
     33                 "HARDWARE=mako,0.4",
     34                 ",defaultValue1",
     35                 "HARDWARE=manta,0.2",
     36                 ",defaultValue2",
     37         };
     38 
     39         try {
     40             assertNull(ResourceUtils.findDefaultConstant(nullArray));
     41             assertNull(ResourceUtils.findDefaultConstant(emptyArray));
     42             assertEquals(ResourceUtils.findDefaultConstant(array), "defaultValue1");
     43         } catch (final DeviceOverridePatternSyntaxError e) {
     44             fail(e.getMessage());
     45         }
     46 
     47         final String[] errorArray = {
     48             "HARDWARE=grouper,0.3",
     49             "no_comma"
     50         };
     51         try {
     52             final String defaultValue = ResourceUtils.findDefaultConstant(errorArray);
     53             fail("exception should be thrown: defaultValue=" + defaultValue);
     54         } catch (final DeviceOverridePatternSyntaxError e) {
     55             assertEquals("Array element has no comma: no_comma", e.getMessage());
     56         }
     57     }
     58 
     59     public void testFindConstantForKeyValuePairsSimple() {
     60         final HashMap<String,String> anyKeyValue = CollectionUtils.newHashMap();
     61         anyKeyValue.put("anyKey", "anyValue");
     62         final HashMap<String,String> nullKeyValue = null;
     63         final HashMap<String,String> emptyKeyValue = CollectionUtils.newHashMap();
     64 
     65         final String[] nullArray = null;
     66         assertNull(ResourceUtils.findConstantForKeyValuePairs(anyKeyValue, nullArray));
     67         assertNull(ResourceUtils.findConstantForKeyValuePairs(emptyKeyValue, nullArray));
     68         assertNull(ResourceUtils.findConstantForKeyValuePairs(nullKeyValue, nullArray));
     69 
     70         final String[] emptyArray = {};
     71         assertNull(ResourceUtils.findConstantForKeyValuePairs(anyKeyValue, emptyArray));
     72         assertNull(ResourceUtils.findConstantForKeyValuePairs(emptyKeyValue, emptyArray));
     73         assertNull(ResourceUtils.findConstantForKeyValuePairs(nullKeyValue, emptyArray));
     74 
     75         final String HARDWARE_KEY = "HARDWARE";
     76         final String[] array = {
     77             ",defaultValue",
     78             "HARDWARE=grouper,0.3",
     79             "HARDWARE=mako,0.4",
     80             "HARDWARE=manta,0.2",
     81             "HARDWARE=mako,0.5",
     82         };
     83 
     84         final HashMap<String,String> keyValues = CollectionUtils.newHashMap();
     85         keyValues.put(HARDWARE_KEY, "grouper");
     86         assertEquals("0.3", ResourceUtils.findConstantForKeyValuePairs(keyValues, array));
     87         keyValues.put(HARDWARE_KEY, "mako");
     88         assertEquals("0.4", ResourceUtils.findConstantForKeyValuePairs(keyValues, array));
     89         keyValues.put(HARDWARE_KEY, "manta");
     90         assertEquals("0.2", ResourceUtils.findConstantForKeyValuePairs(keyValues, array));
     91 
     92         keyValues.clear();
     93         keyValues.put("hardware", "grouper");
     94         assertNull(ResourceUtils.findConstantForKeyValuePairs(keyValues, array));
     95 
     96         keyValues.clear();
     97         keyValues.put(HARDWARE_KEY, "MAKO");
     98         assertNull(ResourceUtils.findConstantForKeyValuePairs(keyValues, array));
     99         keyValues.put(HARDWARE_KEY, "mantaray");
    100         assertNull(ResourceUtils.findConstantForKeyValuePairs(keyValues, array));
    101 
    102         assertNull(ResourceUtils.findConstantForKeyValuePairs(emptyKeyValue, array));
    103     }
    104 
    105     public void testFindConstantForKeyValuePairsCombined() {
    106         final String HARDWARE_KEY = "HARDWARE";
    107         final String MODEL_KEY = "MODEL";
    108         final String MANUFACTURER_KEY = "MANUFACTURER";
    109         final String[] array = {
    110             ",defaultValue",
    111             "no_comma",
    112             "error_pattern,0.1",
    113             "HARDWARE=grouper:MANUFACTURER=asus,0.3",
    114             "HARDWARE=mako:MODEL=Nexus 4,0.4",
    115             "HARDWARE=manta:MODEL=Nexus 10:MANUFACTURER=samsung,0.2"
    116         };
    117         final String[] failArray = {
    118             ",defaultValue",
    119             "HARDWARE=grouper:MANUFACTURER=ASUS,0.3",
    120             "HARDWARE=mako:MODEL=Nexus_4,0.4",
    121             "HARDWARE=mantaray:MODEL=Nexus 10:MANUFACTURER=samsung,0.2"
    122         };
    123 
    124         final HashMap<String,String> keyValues = CollectionUtils.newHashMap();
    125         keyValues.put(HARDWARE_KEY, "grouper");
    126         keyValues.put(MODEL_KEY, "Nexus 7");
    127         keyValues.put(MANUFACTURER_KEY, "asus");
    128         assertEquals("0.3", ResourceUtils.findConstantForKeyValuePairs(keyValues, array));
    129         assertNull(ResourceUtils.findConstantForKeyValuePairs(keyValues, failArray));
    130 
    131         keyValues.clear();
    132         keyValues.put(HARDWARE_KEY, "mako");
    133         keyValues.put(MODEL_KEY, "Nexus 4");
    134         keyValues.put(MANUFACTURER_KEY, "LGE");
    135         assertEquals("0.4", ResourceUtils.findConstantForKeyValuePairs(keyValues, array));
    136         assertNull(ResourceUtils.findConstantForKeyValuePairs(keyValues, failArray));
    137 
    138         keyValues.clear();
    139         keyValues.put(HARDWARE_KEY, "manta");
    140         keyValues.put(MODEL_KEY, "Nexus 10");
    141         keyValues.put(MANUFACTURER_KEY, "samsung");
    142         assertEquals("0.2", ResourceUtils.findConstantForKeyValuePairs(keyValues, array));
    143         assertNull(ResourceUtils.findConstantForKeyValuePairs(keyValues, failArray));
    144         keyValues.put(HARDWARE_KEY, "mantaray");
    145         assertNull(ResourceUtils.findConstantForKeyValuePairs(keyValues, array));
    146         assertEquals("0.2", ResourceUtils.findConstantForKeyValuePairs(keyValues, failArray));
    147     }
    148 
    149     public void testFindConstantForKeyValuePairsRegexp() {
    150         final String HARDWARE_KEY = "HARDWARE";
    151         final String MODEL_KEY = "MODEL";
    152         final String MANUFACTURER_KEY = "MANUFACTURER";
    153         final String[] array = {
    154             ",defaultValue",
    155             "no_comma",
    156             "HARDWARE=error_regexp:MANUFACTURER=error[regexp,0.1",
    157             "HARDWARE=grouper|tilapia:MANUFACTURER=asus,0.3",
    158             "HARDWARE=[mM][aA][kK][oO]:MODEL=Nexus 4,0.4",
    159             "HARDWARE=manta.*:MODEL=Nexus 10:MANUFACTURER=samsung,0.2"
    160         };
    161 
    162         final HashMap<String,String> keyValues = CollectionUtils.newHashMap();
    163         keyValues.put(HARDWARE_KEY, "grouper");
    164         keyValues.put(MODEL_KEY, "Nexus 7");
    165         keyValues.put(MANUFACTURER_KEY, "asus");
    166         assertEquals("0.3", ResourceUtils.findConstantForKeyValuePairs(keyValues, array));
    167         keyValues.put(HARDWARE_KEY, "tilapia");
    168         assertEquals("0.3", ResourceUtils.findConstantForKeyValuePairs(keyValues, array));
    169 
    170         keyValues.clear();
    171         keyValues.put(HARDWARE_KEY, "mako");
    172         keyValues.put(MODEL_KEY, "Nexus 4");
    173         keyValues.put(MANUFACTURER_KEY, "LGE");
    174         assertEquals("0.4", ResourceUtils.findConstantForKeyValuePairs(keyValues, array));
    175         keyValues.put(HARDWARE_KEY, "MAKO");
    176         assertEquals("0.4", ResourceUtils.findConstantForKeyValuePairs(keyValues, array));
    177 
    178         keyValues.clear();
    179         keyValues.put(HARDWARE_KEY, "manta");
    180         keyValues.put(MODEL_KEY, "Nexus 10");
    181         keyValues.put(MANUFACTURER_KEY, "samsung");
    182         assertEquals("0.2", ResourceUtils.findConstantForKeyValuePairs(keyValues, array));
    183         keyValues.put(HARDWARE_KEY, "mantaray");
    184         assertEquals("0.2", ResourceUtils.findConstantForKeyValuePairs(keyValues, array));
    185     }
    186 }
    187