• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.datausage;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 
21 import static org.mockito.ArgumentMatchers.anyLong;
22 import static org.mockito.ArgumentMatchers.eq;
23 import static org.mockito.Mockito.verify;
24 import static org.mockito.Mockito.when;
25 import static org.robolectric.Shadows.shadowOf;
26 
27 import android.app.usage.NetworkStatsManager;
28 import android.content.Context;
29 import android.content.pm.PackageManager;
30 import android.net.ConnectivityManager;
31 import android.telephony.TelephonyManager;
32 import android.util.DataUnit;
33 
34 import org.junit.Before;
35 import org.junit.Test;
36 import org.junit.runner.RunWith;
37 import org.mockito.Mock;
38 import org.mockito.MockitoAnnotations;
39 import org.robolectric.RobolectricTestRunner;
40 import org.robolectric.RuntimeEnvironment;
41 import org.robolectric.shadows.ShadowApplication;
42 import org.robolectric.shadows.ShadowPackageManager;
43 
44 @RunWith(RobolectricTestRunner.class)
45 public final class DataUsageUtilsTest {
46 
47     @Mock
48     private TelephonyManager mTelephonyManager;
49     @Mock
50     private NetworkStatsManager mNetworkStatsManager;
51 
52     private Context mContext;
53 
54     @Before
setUp()55     public void setUp() {
56         MockitoAnnotations.initMocks(this);
57         final ShadowApplication shadowContext = ShadowApplication.getInstance();
58         mContext = RuntimeEnvironment.application;
59         shadowContext.setSystemService(Context.TELEPHONY_SERVICE, mTelephonyManager);
60         shadowContext.setSystemService(Context.NETWORK_STATS_SERVICE, mNetworkStatsManager);
61     }
62 
63     @Test
mobileDataStatus_whenNetworkIsSupported()64     public void mobileDataStatus_whenNetworkIsSupported() {
65         when(mTelephonyManager.isDataCapable()).thenReturn(true);
66         final boolean hasMobileData = DataUsageUtils.hasMobileData(mContext);
67         assertThat(hasMobileData).isTrue();
68     }
69 
70     @Test
mobileDataStatus_whenNetworkIsNotSupported()71     public void mobileDataStatus_whenNetworkIsNotSupported() {
72         when(mTelephonyManager.isDataCapable()).thenReturn(false);
73         final boolean hasMobileData = DataUsageUtils.hasMobileData(mContext);
74         assertThat(hasMobileData).isFalse();
75     }
76 
77     @Test
formatDataUsage_useIECUnit()78     public void formatDataUsage_useIECUnit() {
79         final CharSequence formattedDataUsage = DataUsageUtils.formatDataUsage(
80                 mContext, DataUnit.GIBIBYTES.toBytes(1));
81 
82         assertThat(formattedDataUsage).isEqualTo("1.00 GB");
83     }
84 
85     @Test
hasEthernet_shouldQueryEthernetSummaryForUser()86     public void hasEthernet_shouldQueryEthernetSummaryForUser() throws Exception {
87         ShadowPackageManager pm = shadowOf(RuntimeEnvironment.application.getPackageManager());
88         pm.setSystemFeature(PackageManager.FEATURE_ETHERNET, true);
89         final String subscriber = "TestSub";
90         when(mTelephonyManager.getSubscriberId()).thenReturn(subscriber);
91 
92         DataUsageUtils.hasEthernet(mContext);
93 
94         verify(mNetworkStatsManager).querySummaryForUser(eq(ConnectivityManager.TYPE_ETHERNET),
95                 eq(subscriber), anyLong() /* startTime */, anyLong() /* endTime */);
96     }
97 }
98