1 /* 2 * Copyright (C) 2016 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.datausage; 18 19 import static com.google.common.truth.Truth.assertThat; 20 import static org.mockito.Matchers.anyInt; 21 import static org.mockito.Mockito.when; 22 23 import android.content.Context; 24 import android.net.ConnectivityManager; 25 import android.text.format.Formatter; 26 27 import com.android.settings.R; 28 import com.android.settings.testutils.SettingsRobolectricTestRunner; 29 import com.android.settings.testutils.shadow.SettingsShadowResources; 30 31 import org.junit.Before; 32 import org.junit.Test; 33 import org.junit.runner.RunWith; 34 import org.mockito.Mock; 35 import org.mockito.MockitoAnnotations; 36 import org.robolectric.shadows.ShadowApplication; 37 38 @RunWith(SettingsRobolectricTestRunner.class) 39 public class DataUsageSummaryLegacyTest { 40 41 @Mock 42 private ConnectivityManager mManager; 43 private Context mContext; 44 45 /** 46 * This set up is contrived to get a passing test so that the build doesn't block without tests. 47 * These tests should be updated as code gets refactored to improve testability. 48 */ 49 50 @Before setUp()51 public void setUp() { 52 MockitoAnnotations.initMocks(this); 53 ShadowApplication shadowContext = ShadowApplication.getInstance(); 54 shadowContext.setSystemService(Context.CONNECTIVITY_SERVICE, mManager); 55 mContext = shadowContext.getApplicationContext(); 56 when(mManager.isNetworkSupported(anyInt())).thenReturn(true); 57 } 58 59 @Test formatUsage_shouldLookLikeFormatFileSize()60 public void formatUsage_shouldLookLikeFormatFileSize() { 61 SettingsShadowResources 62 .overrideResource(com.android.internal.R.string.fileSizeSuffix, "%1$s %2$s"); 63 final long usage = 2147483648L; // 2GB 64 final String formattedUsage = 65 DataUsageSummaryLegacy.formatUsage(mContext, "^1", usage).toString(); 66 final String formattedAsFileSize = Formatter.formatFileSize(mContext, usage); 67 assertThat(formattedUsage).isEqualTo(formattedAsFileSize); 68 } 69 } 70