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