Home | History | Annotate | Download | only in format
      1 /*
      2  * Copyright (C) 2015 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.text.format;
     18 
     19 import android.content.res.Configuration;
     20 import android.content.res.Resources;
     21 import android.test.AndroidTestCase;
     22 import android.test.suitebuilder.annotation.SmallTest;
     23 import android.text.format.Formatter.BytesResult;
     24 
     25 import java.util.Locale;
     26 
     27 public class FormatterTest extends AndroidTestCase {
     28 
     29     private Locale mOriginalLocale;
     30 
     31     @Override
     32     protected void setUp() throws Exception {
     33         super.setUp();
     34 
     35         mOriginalLocale = mContext.getResources().getConfiguration().locale;
     36     }
     37 
     38     @Override
     39     protected void tearDown() throws Exception {
     40         if (mOriginalLocale != null) {
     41             setLocale(mOriginalLocale);
     42         }
     43         super.tearDown();
     44     }
     45 
     46     private void setLocale(Locale locale) {
     47         Resources res = getContext().getResources();
     48         Configuration config = res.getConfiguration();
     49         config.locale = locale;
     50         res.updateConfiguration(config, res.getDisplayMetrics());
     51 
     52         Locale.setDefault(locale);
     53     }
     54 
     55     @SmallTest
     56     public void testFormatBytes() {
     57         setLocale(Locale.ENGLISH);
     58 
     59         checkFormatBytes(0, true, "0", 0);
     60         checkFormatBytes(0, false, "0", 0);
     61 
     62         checkFormatBytes(1, true, "1", 1);
     63         checkFormatBytes(1, false, "1", 1);
     64 
     65         checkFormatBytes(12, true, "12", 12);
     66         checkFormatBytes(12, false, "12", 12);
     67 
     68         checkFormatBytes(123, true, "123", 123);
     69         checkFormatBytes(123, false, "123", 123);
     70 
     71         checkFormatBytes(812, true, "812", 812);
     72         checkFormatBytes(812, false, "812", 812);
     73 
     74         checkFormatBytes(912, true, "0.89", 911);
     75         checkFormatBytes(912, false, "0.89", 911);
     76 
     77         checkFormatBytes(9123, true, "8.9", 9113);
     78         checkFormatBytes(9123, false, "8.91", 9123);
     79 
     80         checkFormatBytes(9123000, true, "8.7", 9122611);
     81         checkFormatBytes(9123000, false, "8.70", 9122611);
     82 
     83         checkFormatBytes(-1, true, "-1", -1);
     84         checkFormatBytes(-1, false, "-1", -1);
     85 
     86         checkFormatBytes(-912, true, "-0.89", -911);
     87         checkFormatBytes(-912, false, "-0.89", -911);
     88 
     89         // Missing FLAG_CALCULATE_ROUNDED case.
     90         BytesResult r = Formatter.formatBytes(getContext().getResources(), 1, 0);
     91         assertEquals("1", r.value);
     92         assertEquals(0, r.roundedBytes); // Didn't pass FLAG_CALCULATE_ROUNDED
     93 
     94         // Make sure it works on different locales.
     95         setLocale(new Locale("es", "ES"));
     96         checkFormatBytes(9123000, false, "8,70", 9122611);
     97     }
     98 
     99     private void checkFormatBytes(long bytes, boolean useShort,
    100             String expectedString, long expectedRounded) {
    101         BytesResult r = Formatter.formatBytes(getContext().getResources(), bytes,
    102                 Formatter.FLAG_CALCULATE_ROUNDED | (useShort ? Formatter.FLAG_SHORTER : 0));
    103         assertEquals(expectedString, r.value);
    104         assertEquals(expectedRounded, r.roundedBytes);
    105     }
    106 }
    107