• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2016 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 package com.android.settings.datausage;
17 
18 import static android.net.NetworkPolicy.CYCLE_NONE;
19 import static junit.framework.Assert.assertFalse;
20 import static junit.framework.Assert.assertTrue;
21 import static org.mockito.Matchers.anyInt;
22 import static org.mockito.Matchers.anyLong;
23 import static org.mockito.Matchers.anyObject;
24 import static org.mockito.Mockito.doReturn;
25 import static org.mockito.Mockito.never;
26 import static org.mockito.Mockito.spy;
27 import static org.mockito.Mockito.verify;
28 import static org.mockito.Mockito.when;
29 
30 import android.content.Context;
31 import android.content.DialogInterface;
32 import android.content.SharedPreferences;
33 import android.os.Bundle;
34 import android.support.v7.preference.Preference;
35 import android.support.v7.preference.PreferenceManager;
36 import android.support.v14.preference.SwitchPreference;
37 import android.util.FeatureFlagUtils;
38 
39 import com.android.settings.core.FeatureFlags;
40 import com.android.settings.testutils.SettingsRobolectricTestRunner;
41 import com.android.settingslib.NetworkPolicyEditor;
42 
43 import org.junit.Before;
44 import org.junit.Test;
45 import org.junit.runner.RunWith;
46 import org.mockito.Mock;
47 import org.mockito.MockitoAnnotations;
48 import org.robolectric.RuntimeEnvironment;
49 
50 @RunWith(SettingsRobolectricTestRunner.class)
51 public class BillingCycleSettingsTest {
52 
53     private static final int LIMIT_BYTES = 123;
54 
55     @Mock
56     BillingCycleSettings mMockBillingCycleSettings;
57     BillingCycleSettings.ConfirmLimitFragment mConfirmLimitFragment;
58     @Mock
59     PreferenceManager mMockPreferenceManager;
60     @Mock
61     private NetworkPolicyEditor mNetworkPolicyEditor;
62 
63     private Context mContext;
64     @Mock
65     private Preference mBillingCycle;
66     @Mock
67     private Preference mDataWarning;
68     @Mock
69     private Preference mDataLimit;
70     @Mock
71     private SwitchPreference mEnableDataWarning;
72     @Mock
73     private SwitchPreference mEnableDataLimit;
74 
75     SharedPreferences mSharedPreferences;
76 
77     @Before
setUp()78     public void setUp() {
79         MockitoAnnotations.initMocks(this);
80         mContext = spy(RuntimeEnvironment.application);
81         mConfirmLimitFragment = new BillingCycleSettings.ConfirmLimitFragment();
82         mConfirmLimitFragment.setTargetFragment(mMockBillingCycleSettings, 0);
83         mSharedPreferences = RuntimeEnvironment.application.getSharedPreferences(
84                 "testSharedPreferences", Context.MODE_PRIVATE);
85         when(mMockBillingCycleSettings.getPreferenceManager()).thenReturn(mMockPreferenceManager);
86         when(mMockPreferenceManager.getSharedPreferences()).thenReturn(mSharedPreferences);
87         final Bundle args = new Bundle();
88         args.putLong(BillingCycleSettings.ConfirmLimitFragment.EXTRA_LIMIT_BYTES, LIMIT_BYTES);
89         mConfirmLimitFragment.setArguments(args);
90         mSharedPreferences.edit().putBoolean(
91                 BillingCycleSettings.KEY_SET_DATA_LIMIT, false).apply();
92     }
93 
94     @Test
testDataUsageLimit_shouldNotBeSetOnCancel()95     public void testDataUsageLimit_shouldNotBeSetOnCancel() {
96         mConfirmLimitFragment.onClick(null, DialogInterface.BUTTON_NEGATIVE);
97 
98         assertFalse(mSharedPreferences.getBoolean(BillingCycleSettings.KEY_SET_DATA_LIMIT, true));
99         verify(mMockBillingCycleSettings, never()).setPolicyLimitBytes(anyLong());
100     }
101 
102     @Test
testDataUsageLimit_shouldBeSetOnConfirmation()103     public void testDataUsageLimit_shouldBeSetOnConfirmation() {
104         mConfirmLimitFragment.onClick(null, DialogInterface.BUTTON_POSITIVE);
105 
106         assertTrue(mSharedPreferences.getBoolean(BillingCycleSettings.KEY_SET_DATA_LIMIT, false));
107         verify(mMockBillingCycleSettings).setPolicyLimitBytes(LIMIT_BYTES);
108     }
109 
110     @Test
testDataUsageSummary_shouldBeNullWithV2()111     public void testDataUsageSummary_shouldBeNullWithV2() {
112         final BillingCycleSettings billingCycleSettings = spy(new BillingCycleSettings());
113         when(billingCycleSettings.getContext()).thenReturn(mContext);
114         billingCycleSettings.setUpForTest(mNetworkPolicyEditor, mBillingCycle,
115                 mDataLimit, mDataWarning, mEnableDataLimit, mEnableDataWarning);
116 
117         FeatureFlagUtils.setEnabled(mContext, FeatureFlags.DATA_USAGE_SETTINGS_V2, true);
118 
119         doReturn("some-string").when(billingCycleSettings).getString(anyInt(), anyInt());
120         when(mNetworkPolicyEditor.getPolicyCycleDay(anyObject())).thenReturn(CYCLE_NONE + 1);
121         when(mNetworkPolicyEditor.getPolicyLimitBytes(anyObject())).thenReturn(2000L);
122         when(mNetworkPolicyEditor.getPolicyWarningBytes(anyObject())).thenReturn(1000L);
123 
124         billingCycleSettings.updatePrefs();
125 
126         verify(mBillingCycle).setSummary(null);
127     }
128 }