Home | History | Annotate | Download | only in cts
      1 /*
      2  * Copyright (C) 2009 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 android.content.res.cts;
     18 
     19 import java.util.Locale;
     20 
     21 import junit.framework.Assert;
     22 import android.content.res.Configuration;
     23 import android.content.res.Resources;
     24 import android.test.AndroidTestCase;
     25 import android.test.suitebuilder.annotation.SmallTest;
     26 import android.util.Log;
     27 
     28 import android.content.cts.R;
     29 
     30 public class PluralResourcesTest extends AndroidTestCase {
     31     public static boolean DEBUG = false;
     32     private static final String TAG = "PluralResourcesTest";
     33 
     34     private Resources mResources;
     35 
     36     @Override
     37     protected void setUp() throws Exception {
     38         super.setUp();
     39         mResources = mContext.getResources();
     40     }
     41 
     42     private Resources resourcesForLanguage(final String lang) {
     43         final Configuration config = new Configuration();
     44         config.updateFrom(mResources.getConfiguration());
     45         config.setLocale(new Locale(lang));
     46         return new Resources(mResources.getAssets(), mResources.getDisplayMetrics(), config);
     47     }
     48 
     49     @SmallTest
     50     public void testPlurals() {
     51         CharSequence cs;
     52         final Resources res = resourcesForLanguage("en");
     53 
     54         cs = res.getQuantityText(R.plurals.plurals_test, 0);
     55         if (DEBUG) {
     56             Log.d(TAG, "english 0 cs=" + cs);
     57         }
     58         Assert.assertEquals(cs.toString(), "Some dogs");
     59 
     60         cs = res.getQuantityText(R.plurals.plurals_test, 1);
     61         if (DEBUG) {
     62             Log.d(TAG, "english 1 cs=" + cs);
     63         }
     64         Assert.assertEquals(cs.toString(), "A dog");
     65 
     66         cs = res.getQuantityText(R.plurals.plurals_test, 2);
     67         Assert.assertEquals(cs.toString(), "Some dogs");
     68 
     69         cs = res.getQuantityText(R.plurals.plurals_test, 5);
     70         Assert.assertEquals(cs.toString(), "Some dogs");
     71 
     72         cs = res.getQuantityText(R.plurals.plurals_test, 500);
     73         Assert.assertEquals(cs.toString(), "Some dogs");
     74     }
     75 
     76     @SmallTest
     77     public void testCzech() {
     78         CharSequence cs;
     79         final Resources res = resourcesForLanguage("cs");
     80 
     81         cs = res.getQuantityText(R.plurals.plurals_test, 0);
     82         if (DEBUG) {
     83             Log.d(TAG, "czech 0 cs=" + cs);
     84         }
     85         Assert.assertEquals(cs.toString(), "Some Czech dogs");
     86 
     87         cs = res.getQuantityText(R.plurals.plurals_test, 1);
     88         if (DEBUG) {
     89             Log.d(TAG, "czech 1 cs=" + cs);
     90         }
     91         Assert.assertEquals(cs.toString(), "A Czech dog");
     92 
     93         cs = res.getQuantityText(R.plurals.plurals_test, 2);
     94         if (DEBUG) {
     95             Log.d(TAG, "czech 2 cs=" + cs);
     96         }
     97         Assert.assertEquals(cs.toString(), "Few Czech dogs");
     98 
     99         cs = res.getQuantityText(R.plurals.plurals_test, 5);
    100         Assert.assertEquals(cs.toString(), "Some Czech dogs");
    101 
    102         cs = res.getQuantityText(R.plurals.plurals_test, 500);
    103         Assert.assertEquals(cs.toString(), "Some Czech dogs");
    104     }
    105 }
    106