1 /* 2 * Copyright (C) 2017 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.settings.utils; 18 19 import static com.android.settings.utils.FileSizeFormatter.GIGABYTE_IN_BYTES; 20 import static com.android.settings.utils.FileSizeFormatter.MEGABYTE_IN_BYTES; 21 22 import static com.google.common.truth.Truth.assertThat; 23 24 import android.content.Context; 25 import android.support.test.InstrumentationRegistry; 26 import android.support.test.filters.SmallTest; 27 import android.support.test.runner.AndroidJUnit4; 28 29 import org.junit.Before; 30 import org.junit.Test; 31 import org.junit.runner.RunWith; 32 33 @SmallTest 34 @RunWith(AndroidJUnit4.class) 35 public class FileSizeFormatterTest { 36 private Context mContext; 37 38 @Before 39 public void setUp() throws Exception { 40 mContext = InstrumentationRegistry.getTargetContext(); 41 } 42 43 @Test 44 public void formatFileSize_zero() throws Exception { 45 assertThat( 46 FileSizeFormatter.formatFileSize( 47 mContext, 48 0 /* size */, 49 com.android.internal.R.string.gigabyteShort, 50 GIGABYTE_IN_BYTES)) 51 .isEqualTo("0.00 GB"); 52 } 53 54 @Test 55 public void formatFileSize_smallSize() throws Exception { 56 assertThat( 57 FileSizeFormatter.formatFileSize( 58 mContext, 59 MEGABYTE_IN_BYTES * 11 /* size */, 60 com.android.internal.R.string.gigabyteShort, 61 GIGABYTE_IN_BYTES)) 62 .isEqualTo("0.01 GB"); 63 } 64 65 @Test 66 public void formatFileSize_lessThanOneSize() throws Exception { 67 assertThat( 68 FileSizeFormatter.formatFileSize( 69 mContext, 70 MEGABYTE_IN_BYTES * 155 /* size */, 71 com.android.internal.R.string.gigabyteShort, 72 GIGABYTE_IN_BYTES)) 73 .isEqualTo("0.16 GB"); 74 } 75 76 @Test 77 public void formatFileSize_greaterThanOneSize() throws Exception { 78 assertThat( 79 FileSizeFormatter.formatFileSize( 80 mContext, 81 MEGABYTE_IN_BYTES * 1551 /* size */, 82 com.android.internal.R.string.gigabyteShort, 83 GIGABYTE_IN_BYTES)) 84 .isEqualTo("1.6 GB"); 85 } 86 87 @Test 88 public void formatFileSize_greaterThanTen() throws Exception { 89 // Should round down due to truncation 90 assertThat( 91 FileSizeFormatter.formatFileSize( 92 mContext, 93 GIGABYTE_IN_BYTES * 15 + MEGABYTE_IN_BYTES * 50 /* size */, 94 com.android.internal.R.string.gigabyteShort, 95 GIGABYTE_IN_BYTES)) 96 .isEqualTo("15 GB"); 97 } 98 99 @Test 100 public void formatFileSize_handlesNegativeFileSizes() throws Exception { 101 assertThat( 102 FileSizeFormatter.formatFileSize( 103 mContext, 104 MEGABYTE_IN_BYTES * -155 /* size */, 105 com.android.internal.R.string.gigabyteShort, 106 GIGABYTE_IN_BYTES)) 107 .isEqualTo("-0.16 GB"); 108 } 109 } 110