• 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 
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.System.NOTIFICATION_LIGHT_PULSE;
38 import static org.mockito.Matchers.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 PulseNotificationPreferenceControllerTest {
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 PulseNotificationPreferenceController mController;
54 
55     @Before
setUp()56     public void setUp() {
57         MockitoAnnotations.initMocks(this);
58         mController = new PulseNotificationPreferenceController(mContext);
59     }
60 
61     @Test
display_configIsTrue_shouldDisplay()62     public void display_configIsTrue_shouldDisplay() {
63         when(mContext.getResources().
64                 getBoolean(com.android.internal.R.bool.config_intrusiveNotificationLed))
65                 .thenReturn(true);
66         mController.displayPreference(mScreen);
67 
68         verify(mScreen, never()).removePreference(any(Preference.class));
69     }
70 
71     @Test
display_configIsFalse_shouldNotDisplay()72     public void display_configIsFalse_shouldNotDisplay() {
73         when(mContext.getResources().
74                 getBoolean(com.android.internal.R.bool.config_intrusiveNotificationLed))
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
updateState_preferenceSetCheckedWhenSettingIsOn()87     public void updateState_preferenceSetCheckedWhenSettingIsOn() {
88         final TwoStatePreference preference = mock(TwoStatePreference.class);
89         final Context context = ShadowApplication.getInstance().getApplicationContext();
90         Settings.System.putInt(context.getContentResolver(), NOTIFICATION_LIGHT_PULSE, 1);
91 
92         mController = new PulseNotificationPreferenceController(context);
93         mController.updateState(preference);
94 
95         verify(preference).setChecked(true);
96     }
97 
98     @Test
updateState_preferenceSetUncheckedWhenSettingIsOff()99     public void updateState_preferenceSetUncheckedWhenSettingIsOff() {
100         final TwoStatePreference preference = mock(TwoStatePreference.class);
101         final Context context = ShadowApplication.getInstance().getApplicationContext();
102         Settings.System.putInt(context.getContentResolver(), NOTIFICATION_LIGHT_PULSE, 0);
103 
104         mController = new PulseNotificationPreferenceController(context);
105         mController.updateState(preference);
106 
107         verify(preference).setChecked(false);
108     }
109 }
110