Home | History | Annotate | Download | only in configuration
      1 /*
      2  * Copyright (C) 2011 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.ide.common.resources.configuration;
     18 
     19 import java.util.ArrayList;
     20 import java.util.List;
     21 
     22 import junit.framework.TestCase;
     23 
     24 public class FolderConfigurationTest extends TestCase {
     25 
     26     /*
     27      * Test createDefault creates all the qualifiers.
     28      */
     29     public void testCreateDefault() {
     30         FolderConfiguration defaultConfig = new FolderConfiguration();
     31         defaultConfig.createDefault();
     32 
     33         // this is always valid and up to date.
     34         final int count = FolderConfiguration.getQualifierCount();
     35 
     36         // make sure all the qualifiers were created.
     37         for (int i = 0 ; i < count ; i++) {
     38             assertNotNull(defaultConfig.getQualifier(i));
     39         }
     40     }
     41 
     42     public void testSimpleResMatch() {
     43         runConfigMatchTest(
     44                 "en-rGB-port-hdpi-notouch-12key",
     45                 3,
     46                 "",
     47                 "en",
     48                 "fr-rCA",
     49                 "en-port",
     50                 "en-notouch-12key",
     51                 "port-ldpi",
     52                 "port-notouch-12key");
     53     }
     54 
     55     public void testVersionResMatch() {
     56         runConfigMatchTest(
     57                 "en-rUS-w600dp-h1024dp-large-port-mdpi-finger-nokeys-v12",
     58                 3,
     59                 "",
     60                 "large",
     61                 "w540dp");
     62     }
     63 
     64 
     65     // --- helper methods
     66 
     67     private final static class MockConfigurable implements Configurable {
     68 
     69         private final FolderConfiguration mConfig;
     70 
     71         MockConfigurable(String config) {
     72             mConfig = FolderConfiguration.getConfig(getFolderSegments(config));
     73         }
     74 
     75         @Override
     76         public FolderConfiguration getConfiguration() {
     77             return mConfig;
     78         }
     79 
     80         @Override
     81         public String toString() {
     82             return mConfig.toString();
     83         }
     84     }
     85 
     86     private void runConfigMatchTest(String refConfig, int resultIndex, String... configs) {
     87         FolderConfiguration reference = FolderConfiguration.getConfig(getFolderSegments(refConfig));
     88         assertNotNull(reference);
     89 
     90         List<? extends Configurable> list = getConfigurable(configs);
     91 
     92         Configurable match = reference.findMatchingConfigurable(list);
     93         System.out.println(match.toString());
     94         assertEquals(resultIndex, list.indexOf(match));
     95     }
     96 
     97     private List<? extends Configurable> getConfigurable(String... configs) {
     98         ArrayList<MockConfigurable> list = new ArrayList<MockConfigurable>();
     99 
    100         for (String config : configs) {
    101             list.add(new MockConfigurable(config));
    102         }
    103 
    104         return list;
    105     }
    106 
    107     private static String[] getFolderSegments(String config) {
    108         return (config.length() > 0 ? "foo-" + config : "foo").split("-");
    109     }
    110 }
    111