1 /* 2 * Copyright (C) 2019 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.car.settings.datausage; 18 19 import static android.net.TrafficStats.UID_TETHERING; 20 21 import static com.google.common.truth.Truth.assertThat; 22 23 import static org.mockito.Mockito.when; 24 25 import android.car.userlib.CarUserManagerHelper; 26 import android.content.Context; 27 import android.net.NetworkStats; 28 29 import androidx.lifecycle.Lifecycle; 30 31 import com.android.car.settings.CarSettingsRobolectricTestRunner; 32 import com.android.car.settings.common.LogicalPreferenceGroup; 33 import com.android.car.settings.common.PreferenceControllerTestHelper; 34 import com.android.car.settings.common.ProgressBarPreference; 35 import com.android.car.settings.testutils.ShadowCarUserManagerHelper; 36 import com.android.car.settings.testutils.ShadowUidDetailProvider; 37 import com.android.settingslib.net.UidDetail; 38 39 import org.junit.After; 40 import org.junit.Before; 41 import org.junit.Test; 42 import org.junit.runner.RunWith; 43 import org.mockito.Mock; 44 import org.mockito.MockitoAnnotations; 45 import org.robolectric.RuntimeEnvironment; 46 import org.robolectric.annotation.Config; 47 48 /** Unit test for {@link AppDataUsagePreferenceController}. */ 49 @RunWith(CarSettingsRobolectricTestRunner.class) 50 @Config(shadows = {ShadowCarUserManagerHelper.class, ShadowUidDetailProvider.class}) 51 public class AppDataUsagePreferenceControllerTest { 52 53 private static final int USER_ID = 10; 54 55 private Context mContext; 56 private LogicalPreferenceGroup mLogicalPreferenceGroup; 57 private AppDataUsagePreferenceController mController; 58 private PreferenceControllerTestHelper<AppDataUsagePreferenceController> 59 mPreferenceControllerHelper; 60 61 @Mock 62 private CarUserManagerHelper mCarUserManagerHelper; 63 64 @Mock 65 private UidDetail mUidDetail; 66 67 @Before setUp()68 public void setUp() { 69 MockitoAnnotations.initMocks(this); 70 71 mContext = RuntimeEnvironment.application; 72 mLogicalPreferenceGroup = new LogicalPreferenceGroup(mContext); 73 mPreferenceControllerHelper = new PreferenceControllerTestHelper<>(mContext, 74 AppDataUsagePreferenceController.class, mLogicalPreferenceGroup); 75 mController = mPreferenceControllerHelper.getController(); 76 when(mCarUserManagerHelper.getCurrentProcessUserId()).thenReturn(USER_ID); 77 78 ShadowCarUserManagerHelper.setMockInstance(mCarUserManagerHelper); 79 mPreferenceControllerHelper.markState(Lifecycle.State.CREATED); 80 } 81 82 @After tearDown()83 public void tearDown() { 84 ShadowUidDetailProvider.reset(); 85 ShadowCarUserManagerHelper.reset(); 86 } 87 88 @Test defaultInitialize_hasNoPreference()89 public void defaultInitialize_hasNoPreference() { 90 assertThat(mLogicalPreferenceGroup.getPreferenceCount()).isEqualTo(0); 91 } 92 93 @Test onDataLoaded_dataNotLoaded_hasNoPreference()94 public void onDataLoaded_dataNotLoaded_hasNoPreference() { 95 mController.onDataLoaded(null, new int[0]); 96 97 assertThat(mLogicalPreferenceGroup.getPreferenceCount()).isEqualTo(0); 98 } 99 100 @Test onDataLoaded_statsSizeZero_hasNoPreference()101 public void onDataLoaded_statsSizeZero_hasNoPreference() { 102 NetworkStats networkStats = new NetworkStats(0, 0); 103 104 mController.onDataLoaded(networkStats, new int[0]); 105 106 assertThat(mLogicalPreferenceGroup.getPreferenceCount()).isEqualTo(0); 107 } 108 109 @Test onDataLoaded_statsLoaded_hasTwoPreference()110 public void onDataLoaded_statsLoaded_hasTwoPreference() { 111 NetworkStats networkStats = new NetworkStats(0, 0); 112 NetworkStats.Entry entry1 = new NetworkStats.Entry(); 113 entry1.rxBytes = 100; 114 networkStats.addValues(entry1); 115 116 NetworkStats.Entry entry2 = new NetworkStats.Entry(); 117 entry2.uid = UID_TETHERING; 118 entry2.rxBytes = 200; 119 networkStats.addValues(entry2); 120 121 mController.onDataLoaded(networkStats, new int[0]); 122 123 assertThat(mLogicalPreferenceGroup.getPreferenceCount()).isEqualTo(2); 124 } 125 126 @Test onDataLoaded_statsLoaded_hasOnePreference()127 public void onDataLoaded_statsLoaded_hasOnePreference() { 128 ShadowUidDetailProvider.setUidDetail(mUidDetail); 129 NetworkStats networkStats = new NetworkStats(0, 0); 130 NetworkStats.Entry entry1 = new NetworkStats.Entry(); 131 entry1.rxBytes = 100; 132 networkStats.addValues(entry1); 133 134 NetworkStats.Entry entry2 = new NetworkStats.Entry(); 135 entry2.uid = UID_TETHERING; 136 entry2.rxBytes = 200; 137 networkStats.addValues(entry2); 138 139 mController.onDataLoaded(networkStats, new int[0]); 140 141 ProgressBarPreference preference1 = 142 (ProgressBarPreference) mLogicalPreferenceGroup.getPreference(0); 143 ProgressBarPreference preference2 = 144 (ProgressBarPreference) mLogicalPreferenceGroup.getPreference(1); 145 146 assertThat(mLogicalPreferenceGroup.getPreferenceCount()).isEqualTo(2); 147 assertThat(preference1.getProgress()).isEqualTo(100); 148 assertThat(preference2.getProgress()).isEqualTo(50); 149 } 150 } 151