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.anyInt;
22 import static org.mockito.ArgumentMatchers.anyLong;
23 import static org.mockito.ArgumentMatchers.eq;
24 import static org.mockito.Mockito.verify;
25 import static org.mockito.Mockito.when;
26 
27 import android.app.usage.NetworkStatsManager;
28 import android.content.Context;
29 import android.net.ConnectivityManager;
30 import android.telephony.TelephonyManager;
31 import android.util.DataUnit;
32 
33 import org.junit.Before;
34 import org.junit.Test;
35 import org.junit.runner.RunWith;
36 import org.mockito.Mock;
37 import org.mockito.MockitoAnnotations;
38 import org.robolectric.RobolectricTestRunner;
39 import org.robolectric.RuntimeEnvironment;
40 import org.robolectric.shadows.ShadowApplication;
41 
42 @RunWith(RobolectricTestRunner.class)
43 public final class DataUsageUtilsTest {
44 
45     @Mock
46     private ConnectivityManager mManager;
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.CONNECTIVITY_SERVICE, mManager);
60         shadowContext.setSystemService(Context.TELEPHONY_SERVICE, mTelephonyManager);
61         shadowContext.setSystemService(Context.NETWORK_STATS_SERVICE, mNetworkStatsManager);
62     }
63 
64     @Test
mobileDataStatus_whenNetworkIsSupported()65     public void mobileDataStatus_whenNetworkIsSupported() {
66         when(mManager.isNetworkSupported(anyInt())).thenReturn(true);
67         final boolean hasMobileData = DataUsageUtils.hasMobileData(mContext);
68         assertThat(hasMobileData).isTrue();
69     }
70 
71     @Test
mobileDataStatus_whenNetworkIsNotSupported()72     public void mobileDataStatus_whenNetworkIsNotSupported() {
73         when(mManager.isNetworkSupported(anyInt())).thenReturn(false);
74         final boolean hasMobileData = DataUsageUtils.hasMobileData(mContext);
75         assertThat(hasMobileData).isFalse();
76     }
77 
78     @Test
formatDataUsage_useIECUnit()79     public void formatDataUsage_useIECUnit() {
80         final CharSequence formattedDataUsage = DataUsageUtils.formatDataUsage(
81                 mContext, DataUnit.GIBIBYTES.toBytes(1));
82 
83         assertThat(formattedDataUsage).isEqualTo("1.00 GB");
84     }
85 
86     @Test
hasEthernet_shouldQueryEthernetSummaryForUser()87     public void hasEthernet_shouldQueryEthernetSummaryForUser() throws Exception {
88         when(mManager.isNetworkSupported(anyInt())).thenReturn(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