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.notification; 18 19 import android.content.Context; 20 import android.provider.Settings; 21 import android.support.v7.preference.Preference; 22 import android.support.v7.preference.PreferenceScreen; 23 import android.support.v7.preference.TwoStatePreference; 24 25 import com.android.settings.TestConfig; 26 27 import org.junit.Before; 28 import org.junit.Test; 29 import org.junit.runner.RunWith; 30 import org.mockito.Answers; 31 import org.mockito.Mock; 32 import org.mockito.MockitoAnnotations; 33 import org.robolectric.RobolectricTestRunner; 34 import org.robolectric.annotation.Config; 35 import org.robolectric.shadows.ShadowApplication; 36 37 import static android.provider.Settings.Secure.NOTIFICATION_BADGING; 38 import static org.mockito.Mockito.any; 39 import static org.mockito.Mockito.mock; 40 import static org.mockito.Mockito.never; 41 import static org.mockito.Mockito.verify; 42 import static org.mockito.Mockito.when; 43 44 @RunWith(RobolectricTestRunner.class) 45 @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION) 46 public class BadgingNotificationPreferenceControllerTest { 47 48 @Mock(answer = Answers.RETURNS_DEEP_STUBS) 49 private Context mContext; 50 @Mock(answer = Answers.RETURNS_DEEP_STUBS) 51 private PreferenceScreen mScreen; 52 53 private BadgingNotificationPreferenceController mController; 54 55 @Before 56 public void setUp() { 57 MockitoAnnotations.initMocks(this); 58 mController = new BadgingNotificationPreferenceController(mContext); 59 } 60 61 @Test 62 public void display_configIsTrue_shouldDisplay() { 63 when(mContext.getResources(). 64 getBoolean(com.android.internal.R.bool.config_notificationBadging)) 65 .thenReturn(true); 66 mController.displayPreference(mScreen); 67 68 verify(mScreen, never()).removePreference(any(Preference.class)); 69 } 70 71 @Test 72 public void display_configIsFalse_shouldNotDisplay() { 73 when(mContext.getResources(). 74 getBoolean(com.android.internal.R.bool.config_notificationBadging)) 75 .thenReturn(false); 76 final Preference preference = mock(Preference.class); 77 when(mScreen.getPreferenceCount()).thenReturn(1); 78 when(mScreen.getPreference(0)).thenReturn(preference); 79 when(preference.getKey()).thenReturn(mController.getPreferenceKey()); 80 81 mController.displayPreference(mScreen); 82 83 verify(mScreen).removePreference(any(Preference.class)); 84 } 85 86 @Test 87 public void updateState_preferenceSetCheckedWhenSettingIsOn() { 88 final TwoStatePreference preference = mock(TwoStatePreference.class); 89 final Context context = ShadowApplication.getInstance().getApplicationContext(); 90 Settings.Secure.putInt(context.getContentResolver(), NOTIFICATION_BADGING, 1); 91 92 mController = new BadgingNotificationPreferenceController(context); 93 mController.updateState(preference); 94 95 verify(preference).setChecked(true); 96 } 97 98 @Test 99 public void updateState_preferenceSetUncheckedWhenSettingIsOff() { 100 final TwoStatePreference preference = mock(TwoStatePreference.class); 101 final Context context = ShadowApplication.getInstance().getApplicationContext(); 102 Settings.Secure.putInt(context.getContentResolver(), NOTIFICATION_BADGING, 0); 103 104 mController = new BadgingNotificationPreferenceController(context); 105 mController.updateState(preference); 106 107 verify(preference).setChecked(false); 108 } 109 } 110