FormatterTest.java revision c7a14e442dcab7efd89af1bc671e6869904d19f6
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
17package android.text.format;
18
19import android.content.res.Configuration;
20import android.content.res.Resources;
21import android.test.AndroidTestCase;
22import android.test.suitebuilder.annotation.SmallTest;
23import android.text.format.Formatter.BytesResult;
24
25import java.util.Locale;
26
27public 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.00", 0);
60        checkFormatBytes(0, false, "0.00", 0);
61
62        checkFormatBytes(1, true, "1.0", 1);
63        checkFormatBytes(1, false, "1.00", 1);
64
65        checkFormatBytes(12, true, "12", 12);
66        checkFormatBytes(12, false, "12.00", 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        // The method doesn't really support negative values, but apparently people pass -1...
84        checkFormatBytes(-1, true, "-1.00", -1);
85        checkFormatBytes(-1, false, "-1.00", -1);
86
87        // Missing FLAG_CALCULATE_ROUNDED case.
88        BytesResult r = Formatter.formatBytes(getContext().getResources(), 1, 0);
89        assertEquals("1.00", r.value);
90        assertEquals(0, r.roundedBytes); // Didn't pass FLAG_CALCULATE_ROUNDED
91
92        // Make sure it works on different locales.
93        setLocale(new Locale("es", "ES"));
94        checkFormatBytes(9123000, false, "8,70", 9122611);
95    }
96
97    private void checkFormatBytes(long bytes, boolean useShort,
98            String expectedString, long expectedRounded) {
99        BytesResult r = Formatter.formatBytes(getContext().getResources(), bytes,
100                Formatter.FLAG_CALCULATE_ROUNDED | (useShort ? Formatter.FLAG_SHORTER : 0));
101        assertEquals(expectedString, r.value);
102        assertEquals(expectedRounded, r.roundedBytes);
103    }
104}
105