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 android.content.Context; 19 import android.content.DialogInterface; 20 import android.content.SharedPreferences; 21 import android.os.Bundle; 22 import android.support.v7.preference.PreferenceManager; 23 import com.android.settings.SettingsRobolectricTestRunner; 24 import com.android.settings.TestConfig; 25 import org.junit.Before; 26 import org.junit.Test; 27 import org.junit.runner.RunWith; 28 import org.mockito.Mock; 29 import org.mockito.MockitoAnnotations; 30 import org.robolectric.RuntimeEnvironment; 31 import org.robolectric.annotation.Config; 32 33 import static junit.framework.Assert.assertFalse; 34 import static junit.framework.Assert.assertTrue; 35 import static org.mockito.Matchers.anyLong; 36 import static org.mockito.Mockito.never; 37 import static org.mockito.Mockito.verify; 38 import static org.mockito.Mockito.when; 39 40 @RunWith(SettingsRobolectricTestRunner.class) 41 @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION) 42 public class BillingCycleSettingsTest { 43 44 private static final int LIMIT_BYTES = 123; 45 46 @Mock 47 BillingCycleSettings mMockBillingCycleSettings; 48 BillingCycleSettings.ConfirmLimitFragment mConfirmLimitFragment; 49 @Mock 50 PreferenceManager mMockPreferenceManager; 51 SharedPreferences mSharedPreferences; 52 53 @Before 54 public void setUp() { 55 MockitoAnnotations.initMocks(this); 56 mConfirmLimitFragment = new BillingCycleSettings.ConfirmLimitFragment(); 57 mConfirmLimitFragment.setTargetFragment(mMockBillingCycleSettings, 0); 58 mSharedPreferences = RuntimeEnvironment.application.getSharedPreferences( 59 "testSharedPreferences", Context.MODE_PRIVATE); 60 when(mMockBillingCycleSettings.getPreferenceManager()).thenReturn(mMockPreferenceManager); 61 when(mMockPreferenceManager.getSharedPreferences()).thenReturn(mSharedPreferences); 62 final Bundle args = new Bundle(); 63 args.putLong(BillingCycleSettings.ConfirmLimitFragment.EXTRA_LIMIT_BYTES, LIMIT_BYTES); 64 mConfirmLimitFragment.setArguments(args); 65 mSharedPreferences.edit().putBoolean( 66 BillingCycleSettings.KEY_SET_DATA_LIMIT, false).apply(); 67 } 68 69 @Test 70 public void testDataUsageLimit_shouldNotBeSetOnCancel() { 71 mConfirmLimitFragment.onClick(null, DialogInterface.BUTTON_NEGATIVE); 72 73 assertFalse(mSharedPreferences.getBoolean(BillingCycleSettings.KEY_SET_DATA_LIMIT, true)); 74 verify(mMockBillingCycleSettings, never()).setPolicyLimitBytes(anyLong()); 75 } 76 77 @Test 78 public void testDataUsageLimit_shouldBeSetOnConfirmation() { 79 mConfirmLimitFragment.onClick(null, DialogInterface.BUTTON_POSITIVE); 80 81 assertTrue(mSharedPreferences.getBoolean(BillingCycleSettings.KEY_SET_DATA_LIMIT, false)); 82 verify(mMockBillingCycleSettings).setPolicyLimitBytes(LIMIT_BYTES); 83 } 84 }