Home | History | Annotate | Download | only in datausage
      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 import static org.mockito.Mockito.when;
     21 
     22 import android.graphics.Color;
     23 import android.graphics.drawable.ColorDrawable;
     24 
     25 import com.android.settings.testutils.SettingsRobolectricTestRunner;
     26 import com.android.settingslib.AppItem;
     27 import com.android.settingslib.net.UidDetail;
     28 import com.android.settingslib.net.UidDetailProvider;
     29 
     30 import org.junit.Before;
     31 import org.junit.Test;
     32 import org.junit.runner.RunWith;
     33 import org.mockito.Mock;
     34 import org.mockito.MockitoAnnotations;
     35 import org.robolectric.RuntimeEnvironment;
     36 
     37 @RunWith(SettingsRobolectricTestRunner.class)
     38 public class AppDataUsagePreferenceTest {
     39 
     40     @Mock
     41     private UidDetailProvider mUidDetailProvider;
     42     private AppItem mAppItem;
     43     private UidDetail mUidDetail;
     44 
     45     private AppDataUsagePreference mPreference;
     46 
     47     @Before
     48     public void setUp() {
     49         MockitoAnnotations.initMocks(this);
     50         mAppItem = new AppItem(123);
     51         mUidDetail = new UidDetail();
     52         mUidDetail.icon = new ColorDrawable(Color.BLUE);
     53         mUidDetail.label = "title";
     54     }
     55 
     56     @Test
     57     public void createPref_hasCachedUidDetail_shouldSetAppInfo() {
     58         when(mUidDetailProvider.getUidDetail(mAppItem.key, false /* blocking */))
     59                 .thenReturn(mUidDetail);
     60 
     61         mPreference = new AppDataUsagePreference(RuntimeEnvironment.application, mAppItem,
     62                 50 /* percent */, mUidDetailProvider);
     63 
     64         assertThat(mPreference.getTitle()).isEqualTo(mUidDetail.label);
     65         assertThat(mPreference.getIcon()).isEqualTo(mUidDetail.icon);
     66     }
     67 
     68     @Test
     69     public void createPref_noCachedUidDetail_shouldSetAppInfo() {
     70         when(mUidDetailProvider.getUidDetail(mAppItem.key, true /* blocking */))
     71                 .thenReturn(mUidDetail);
     72 
     73         mPreference = new AppDataUsagePreference(RuntimeEnvironment.application, mAppItem,
     74                 50 /* percent */, mUidDetailProvider);
     75 
     76         assertThat(mPreference.getTitle()).isEqualTo(mUidDetail.label);
     77         assertThat(mPreference.getIcon()).isEqualTo(mUidDetail.icon);
     78     }
     79 }
     80