1 /* 2 * Copyright (C) 2018 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 org.mockito.ArgumentMatchers.any; 20 import static org.mockito.Mockito.doReturn; 21 import static org.mockito.Mockito.never; 22 import static org.mockito.Mockito.spy; 23 import static org.mockito.Mockito.verify; 24 25 import android.content.Context; 26 import android.content.Intent; 27 import android.net.NetworkTemplate; 28 29 import com.android.settingslib.net.DataUsageController; 30 31 import org.junit.Before; 32 import org.junit.Test; 33 import org.junit.runner.RunWith; 34 import org.mockito.Mock; 35 import org.mockito.MockitoAnnotations; 36 import org.robolectric.RobolectricTestRunner; 37 import org.robolectric.RuntimeEnvironment; 38 39 @RunWith(RobolectricTestRunner.class) 40 public class DataUsagePreferenceTest { 41 42 @Mock 43 private DataUsageController mController; 44 45 private Context mContext; 46 private DataUsagePreference mPreference; 47 48 @Before setUp()49 public void setUp() { 50 MockitoAnnotations.initMocks(this); 51 52 mContext = RuntimeEnvironment.application; 53 mPreference = spy(new DataUsagePreference(mContext, null /* attrs */)); 54 doReturn(mController).when(mPreference).getDataUsageController(); 55 } 56 57 @Test setTemplate_noDataUsage_shouldDisablePreference()58 public void setTemplate_noDataUsage_shouldDisablePreference() { 59 doReturn(0L).when(mController).getHistoricalUsageLevel(any(NetworkTemplate.class)); 60 61 mPreference.setTemplate( 62 NetworkTemplate.buildTemplateMobileWildcard(), 5 /* subId */, null /* services */); 63 64 verify(mPreference).setEnabled(false); 65 verify(mPreference).setIntent(null); 66 } 67 68 @Test setTemplate_hasDataUsage_shouldNotDisablePreference()69 public void setTemplate_hasDataUsage_shouldNotDisablePreference() { 70 doReturn(200L).when(mController).getHistoricalUsageLevel(any(NetworkTemplate.class)); 71 72 mPreference.setTemplate( 73 NetworkTemplate.buildTemplateMobileWildcard(), 5 /* subId */, null /* services */); 74 75 verify(mPreference, never()).setEnabled(false); 76 verify(mPreference).setIntent(any(Intent.class)); 77 } 78 } 79