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.Mockito.when; 22 23 import android.graphics.Color; 24 import android.graphics.drawable.ColorDrawable; 25 import android.view.LayoutInflater; 26 import android.view.View; 27 import android.widget.ProgressBar; 28 29 import androidx.preference.PreferenceViewHolder; 30 31 import com.android.settingslib.AppItem; 32 import com.android.settingslib.net.UidDetail; 33 import com.android.settingslib.net.UidDetailProvider; 34 35 import org.junit.Before; 36 import org.junit.Test; 37 import org.junit.runner.RunWith; 38 import org.mockito.Mock; 39 import org.mockito.MockitoAnnotations; 40 import org.robolectric.RobolectricTestRunner; 41 import org.robolectric.RuntimeEnvironment; 42 43 @RunWith(RobolectricTestRunner.class) 44 public class AppDataUsagePreferenceTest { 45 private static final String FIFTY_PERCENT = "50%"; 46 47 @Mock 48 private UidDetailProvider mUidDetailProvider; 49 private AppItem mAppItem; 50 private UidDetail mUidDetail; 51 52 private AppDataUsagePreference mPreference; 53 54 @Before setUp()55 public void setUp() { 56 MockitoAnnotations.initMocks(this); 57 mAppItem = new AppItem(123); 58 mUidDetail = new UidDetail(); 59 mUidDetail.icon = new ColorDrawable(Color.BLUE); 60 mUidDetail.label = "title"; 61 } 62 63 @Test createPref_hasCachedUidDetail_shouldSetAppInfo()64 public void createPref_hasCachedUidDetail_shouldSetAppInfo() { 65 when(mUidDetailProvider.getUidDetail(mAppItem.key, false /* blocking */)) 66 .thenReturn(mUidDetail); 67 68 mPreference = new AppDataUsagePreference(RuntimeEnvironment.application, mAppItem, 69 50 /* percent */, mUidDetailProvider); 70 71 assertThat(mPreference.getTitle()).isEqualTo(mUidDetail.label); 72 assertThat(mPreference.getIcon()).isEqualTo(mUidDetail.icon); 73 } 74 75 @Test createPref_noCachedUidDetail_shouldSetAppInfo()76 public void createPref_noCachedUidDetail_shouldSetAppInfo() { 77 when(mUidDetailProvider.getUidDetail(mAppItem.key, true /* blocking */)) 78 .thenReturn(mUidDetail); 79 80 mPreference = new AppDataUsagePreference(RuntimeEnvironment.application, mAppItem, 81 50 /* percent */, mUidDetailProvider); 82 83 assertThat(mPreference.getTitle()).isEqualTo(mUidDetail.label); 84 assertThat(mPreference.getIcon()).isEqualTo(mUidDetail.icon); 85 } 86 87 @Test createPref_progressBarShouldSetPercentageContentDescription()88 public void createPref_progressBarShouldSetPercentageContentDescription() { 89 when(mUidDetailProvider.getUidDetail(mAppItem.key, true /* blocking */)) 90 .thenReturn(mUidDetail); 91 mPreference = new AppDataUsagePreference(RuntimeEnvironment.application, mAppItem, 92 50 /* percent */, mUidDetailProvider); 93 final View view = LayoutInflater.from(RuntimeEnvironment.application).inflate( 94 com.android.settingslib.R.layout.preference_app, null); 95 final PreferenceViewHolder preferenceViewHolder = 96 PreferenceViewHolder.createInstanceForTests(view); 97 final ProgressBar progressBar = (ProgressBar) preferenceViewHolder.findViewById( 98 android.R.id.progress); 99 100 mPreference.onBindViewHolder(preferenceViewHolder); 101 102 assertThat(progressBar.getContentDescription()).isEqualTo(FIFTY_PERCENT); 103 } 104 } 105