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 21 import static org.mockito.ArgumentMatchers.any; 22 import static org.mockito.ArgumentMatchers.anyInt; 23 import static org.mockito.ArgumentMatchers.anyString; 24 import static org.mockito.Mockito.doNothing; 25 import static org.mockito.Mockito.doReturn; 26 import static org.mockito.Mockito.never; 27 import static org.mockito.Mockito.spy; 28 import static org.mockito.Mockito.verify; 29 30 import android.content.Context; 31 import android.content.res.Resources; 32 import android.os.UserManager; 33 import android.telephony.SubscriptionManager; 34 35 import androidx.test.core.app.ApplicationProvider; 36 37 import com.android.settings.R; 38 import com.android.settings.testutils.shadow.ShadowDashboardFragment; 39 import com.android.settings.testutils.shadow.ShadowDataUsageUtils; 40 import com.android.settings.testutils.shadow.ShadowUserManager; 41 import com.android.settings.testutils.shadow.ShadowUtils; 42 43 import org.junit.Before; 44 import org.junit.Rule; 45 import org.junit.Test; 46 import org.junit.runner.RunWith; 47 import org.mockito.Mock; 48 import org.mockito.Spy; 49 import org.mockito.junit.MockitoJUnit; 50 import org.mockito.junit.MockitoRule; 51 import org.robolectric.RobolectricTestRunner; 52 import org.robolectric.annotation.Config; 53 import org.robolectric.shadows.ShadowSubscriptionManager; 54 55 @Config(shadows = { 56 ShadowUtils.class, 57 ShadowDataUsageUtils.class, 58 ShadowDashboardFragment.class, 59 ShadowUserManager.class, 60 }) 61 @RunWith(RobolectricTestRunner.class) 62 public class DataUsageSummaryTest { 63 @Rule 64 public final MockitoRule mMockitoRule = MockitoJUnit.rule(); 65 @Spy 66 Context mContext = ApplicationProvider.getApplicationContext(); 67 @Mock 68 private UserManager mUserManager; 69 70 private DataUsageSummary mDataUsageSummary; 71 72 /** 73 * This set up is contrived to get a passing test so that the build doesn't block without tests. 74 * These tests should be updated as code gets refactored to improve testability. 75 */ 76 77 @Before setUp()78 public void setUp() { 79 doReturn(mUserManager).when(mContext).getSystemService(UserManager.class); 80 doReturn(false).when(mUserManager).isGuestUser(); 81 82 ShadowUserManager.getShadow().setIsAdminUser(true); 83 84 Resources mResources = spy(mContext.getResources()); 85 doReturn(mResources).when(mContext).getResources(); 86 doReturn(true).when(mResources).getBoolean(R.bool.config_show_sim_info); 87 88 mDataUsageSummary = spy(new DataUsageSummary()); 89 doReturn(mContext).when(mDataUsageSummary).getContext(); 90 doNothing().when(mDataUsageSummary).enableProxySubscriptionManager(any()); 91 doReturn(true).when(mDataUsageSummary).removePreference(anyString()); 92 doNothing().when(mDataUsageSummary).addWifiSection(); 93 } 94 95 @Test formatUsage_shouldLookLikeFormatFileSize()96 public void formatUsage_shouldLookLikeFormatFileSize() { 97 final long usage = 2147483648L; // 2GB 98 final String formattedUsage = 99 DataUsageSummary.formatUsage(mContext, "^1", usage).toString(); 100 final CharSequence formattedInIECUnit = DataUsageUtils.formatDataUsage(mContext, usage); 101 assertThat(formattedUsage).isEqualTo(formattedInIECUnit); 102 } 103 104 @Test 105 @Config(shadows = ShadowSubscriptionManager.class) configuration_withSim_shouldShowMobileAndWifi()106 public void configuration_withSim_shouldShowMobileAndWifi() { 107 ShadowDataUsageUtils.IS_MOBILE_DATA_SUPPORTED = true; 108 ShadowDataUsageUtils.IS_WIFI_SUPPORTED = true; 109 ShadowSubscriptionManager.setDefaultDataSubscriptionId(1); 110 111 final DataUsageSummary dataUsageSummary = spy(new DataUsageSummary()); 112 doNothing().when(dataUsageSummary).enableProxySubscriptionManager(any()); 113 doReturn(true).when(dataUsageSummary).hasActiveSubscription(); 114 doReturn(mContext).when(dataUsageSummary).getContext(); 115 116 doReturn(true).when(dataUsageSummary).removePreference(anyString()); 117 doNothing().when(dataUsageSummary).addWifiSection(); 118 doNothing().when(dataUsageSummary).addMobileSection(1); 119 120 dataUsageSummary.onCreate(null); 121 122 verify(dataUsageSummary).addWifiSection(); 123 verify(dataUsageSummary).addMobileSection(anyInt()); 124 } 125 126 @Test configuration_withoutSim_shouldShowWifiSectionOnly()127 public void configuration_withoutSim_shouldShowWifiSectionOnly() { 128 ShadowDataUsageUtils.IS_MOBILE_DATA_SUPPORTED = true; 129 ShadowDataUsageUtils.IS_WIFI_SUPPORTED = true; 130 131 final DataUsageSummary dataUsageSummary = spy(new DataUsageSummary()); 132 doNothing().when(dataUsageSummary).enableProxySubscriptionManager(any()); 133 doReturn(false).when(dataUsageSummary).hasActiveSubscription(); 134 doReturn(mContext).when(dataUsageSummary).getContext(); 135 136 doReturn(true).when(dataUsageSummary).removePreference(anyString()); 137 doNothing().when(dataUsageSummary).addWifiSection(); 138 doNothing().when(dataUsageSummary).addMobileSection(1); 139 140 dataUsageSummary.onCreate(null); 141 142 verify(dataUsageSummary).addWifiSection(); 143 verify(dataUsageSummary, never()).addMobileSection(anyInt()); 144 } 145 146 @Test configuration_withoutMobile_shouldShowWifiSectionOnly()147 public void configuration_withoutMobile_shouldShowWifiSectionOnly() { 148 ShadowDataUsageUtils.IS_MOBILE_DATA_SUPPORTED = false; 149 ShadowDataUsageUtils.IS_WIFI_SUPPORTED = true; 150 151 final DataUsageSummary dataUsageSummary = spy(new DataUsageSummary()); 152 doNothing().when(dataUsageSummary).enableProxySubscriptionManager(any()); 153 doReturn(false).when(dataUsageSummary).hasActiveSubscription(); 154 doReturn(mContext).when(dataUsageSummary).getContext(); 155 156 doReturn(true).when(dataUsageSummary).removePreference(anyString()); 157 doNothing().when(dataUsageSummary).addWifiSection(); 158 doNothing().when(dataUsageSummary).addMobileSection(1); 159 160 dataUsageSummary.onCreate(null); 161 162 verify(dataUsageSummary).addWifiSection(); 163 verify(dataUsageSummary, never()).addMobileSection(anyInt()); 164 } 165 166 @Test 167 @Config(shadows = ShadowSubscriptionManager.class) configuration_invalidDataSusbscription_shouldShowWifiSectionOnly()168 public void configuration_invalidDataSusbscription_shouldShowWifiSectionOnly() { 169 ShadowDataUsageUtils.IS_MOBILE_DATA_SUPPORTED = true; 170 ShadowDataUsageUtils.IS_WIFI_SUPPORTED = true; 171 ShadowSubscriptionManager.setDefaultDataSubscriptionId( 172 SubscriptionManager.INVALID_SUBSCRIPTION_ID); 173 174 final DataUsageSummary dataUsageSummary = spy(new DataUsageSummary()); 175 doNothing().when(dataUsageSummary).enableProxySubscriptionManager(any()); 176 doReturn(false).when(dataUsageSummary).hasActiveSubscription(); 177 doReturn(mContext).when(dataUsageSummary).getContext(); 178 179 doReturn(true).when(dataUsageSummary).removePreference(anyString()); 180 doNothing().when(dataUsageSummary).addWifiSection(); 181 doNothing().when(dataUsageSummary).addMobileSection(1); 182 183 dataUsageSummary.onCreate(null); 184 185 verify(dataUsageSummary).addWifiSection(); 186 verify(dataUsageSummary, never()).addMobileSection(anyInt()); 187 } 188 189 @Test onCreate_isNotGuestUser_shouldNotFinish()190 public void onCreate_isNotGuestUser_shouldNotFinish() { 191 doReturn(false).when(mUserManager).isGuestUser(); 192 193 mDataUsageSummary.onCreate(null); 194 195 verify(mDataUsageSummary, never()).finish(); 196 } 197 198 @Test onCreate_isGuestUser_shouldFinish()199 public void onCreate_isGuestUser_shouldFinish() { 200 doReturn(true).when(mUserManager).isGuestUser(); 201 202 mDataUsageSummary.onCreate(null); 203 204 verify(mDataUsageSummary).finish(); 205 } 206 } 207