• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 static com.google.common.truth.Truth.assertThat;
20 import static org.mockito.Mockito.doReturn;
21 import static org.mockito.Mockito.spy;
22 import static org.mockito.Mockito.when;
23 
24 import android.app.Activity;
25 import android.content.ContentResolver;
26 import android.content.Context;
27 import android.provider.Settings.System;
28 import android.support.v14.preference.SwitchPreference;
29 import android.support.v7.preference.PreferenceScreen;
30 
31 import com.android.settings.testutils.SettingsRobolectricTestRunner;
32 
33 import org.junit.Before;
34 import org.junit.Test;
35 import org.junit.runner.RunWith;
36 import org.mockito.Mock;
37 import org.mockito.MockitoAnnotations;
38 import org.robolectric.RuntimeEnvironment;
39 import org.robolectric.annotation.Config;
40 
41 @RunWith(SettingsRobolectricTestRunner.class)
42 public class ScreenLockSoundPreferenceControllerTest {
43 
44     @Mock
45     private PreferenceScreen mScreen;
46     @Mock
47     private Activity mActivity;
48     @Mock
49     private ContentResolver mContentResolver;
50     @Mock
51     private SoundSettings mSetting;
52 
53     private Context mContext;
54     private ScreenLockSoundPreferenceController mController;
55     private SwitchPreference mPreference;
56 
57     @Before
setUp()58     public void setUp() {
59         MockitoAnnotations.initMocks(this);
60         mContext = spy(RuntimeEnvironment.application);
61         when(mSetting.getActivity()).thenReturn(mActivity);
62         when(mActivity.getContentResolver()).thenReturn(mContentResolver);
63         mPreference = new SwitchPreference(RuntimeEnvironment.application);
64         mController = new ScreenLockSoundPreferenceController(mContext, mSetting, null);
65         when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference);
66         doReturn(mScreen).when(mSetting).getPreferenceScreen();
67     }
68 
69     @Test
isAvailable_byDefault_isTrue()70     public void isAvailable_byDefault_isTrue() {
71         assertThat(mController.isAvailable()).isTrue();
72     }
73 
74     @Test
75     @Config(qualifiers = "mcc999")
isAvailable_whenNotVisible_isFalse()76     public void isAvailable_whenNotVisible_isFalse() {
77         assertThat(mController.isAvailable()).isFalse();
78     }
79 
80     @Test
displayPreference_lockScreenSoundEnabled_shouldCheckedPreference()81     public void displayPreference_lockScreenSoundEnabled_shouldCheckedPreference() {
82         System.putInt(mContentResolver, System.LOCKSCREEN_SOUNDS_ENABLED, 1);
83 
84         mController.displayPreference(mScreen);
85 
86         assertThat(mPreference.isChecked()).isTrue();
87     }
88 
89     @Test
displayPreference_lockScreenSoundDisabled_shouldUncheckedPreference()90     public void displayPreference_lockScreenSoundDisabled_shouldUncheckedPreference() {
91         System.putInt(mContentResolver, System.LOCKSCREEN_SOUNDS_ENABLED, 0);
92 
93         mController.displayPreference(mScreen);
94 
95         assertThat(mPreference.isChecked()).isFalse();
96     }
97 
98     @Test
onPreferenceChanged_preferenceChecked_shouldEnabledLockScreenSound()99     public void onPreferenceChanged_preferenceChecked_shouldEnabledLockScreenSound() {
100         mController.displayPreference(mScreen);
101 
102         mPreference.getOnPreferenceChangeListener().onPreferenceChange(mPreference, true);
103 
104         assertThat(System.getInt(mContentResolver, System.LOCKSCREEN_SOUNDS_ENABLED, 1))
105             .isEqualTo(1);
106     }
107 
108     @Test
onPreferenceChanged_preferenceUnchecked_shouldDisabledLockScreenSound()109     public void onPreferenceChanged_preferenceUnchecked_shouldDisabledLockScreenSound() {
110         mController.displayPreference(mScreen);
111 
112         mPreference.getOnPreferenceChangeListener().onPreferenceChange(mPreference, false);
113 
114         assertThat(System.getInt(mContentResolver, System.LOCKSCREEN_SOUNDS_ENABLED, 1))
115             .isEqualTo(0);
116     }
117 }
118