• 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.android.settings.notification.SettingPref.TYPE_GLOBAL;
20 
21 import static com.google.common.truth.Truth.assertThat;
22 
23 import static org.mockito.Mockito.doReturn;
24 import static org.mockito.Mockito.spy;
25 import static org.mockito.Mockito.verify;
26 import static org.mockito.Mockito.when;
27 
28 import android.content.ContentResolver;
29 import android.content.Context;
30 import android.provider.Settings.Global;
31 
32 import androidx.fragment.app.FragmentActivity;
33 import androidx.preference.PreferenceScreen;
34 
35 import com.android.settings.SettingsPreferenceFragment;
36 import com.android.settingslib.core.lifecycle.Lifecycle;
37 
38 import org.junit.Before;
39 import org.junit.Test;
40 import org.junit.runner.RunWith;
41 import org.mockito.Mock;
42 import org.mockito.MockitoAnnotations;
43 import org.robolectric.RobolectricTestRunner;
44 import org.robolectric.RuntimeEnvironment;
45 import org.robolectric.annotation.Config;
46 
47 import java.util.ArrayList;
48 import java.util.List;
49 
50 @RunWith(RobolectricTestRunner.class)
51 @Config(shadows = {
52         com.android.settings.testutils.shadow.ShadowFragment.class,
53 })
54 public class SettingPrefControllerTest {
55 
56     @Mock
57     private PreferenceScreen mScreen;
58     @Mock
59     private SoundSettings mSetting;
60     @Mock
61     private FragmentActivity mActivity;
62     @Mock
63     private ContentResolver mContentResolver;
64 
65     private Context mContext;
66     private PreferenceControllerTestable mController;
67     private SettingPref mPreference;
68 
69     @Before
setUp()70     public void setUp() {
71         MockitoAnnotations.initMocks(this);
72         mContext = spy(RuntimeEnvironment.application);
73         when(mContext.getContentResolver()).thenReturn(mContentResolver);
74         when(mSetting.getActivity()).thenReturn(mActivity);
75         doReturn(mScreen).when(mSetting).getPreferenceScreen();
76         mController = new PreferenceControllerTestable(mContext, mSetting, null);
77         mPreference = mController.getPref();
78     }
79 
80     @Test
displayPreference_shouldInitPreference()81     public void displayPreference_shouldInitPreference() {
82         mController.displayPreference(mScreen);
83 
84         verify(mPreference).init(mSetting);
85     }
86 
87     @Test
isAvailable_shouldCallisApplicable()88     public void isAvailable_shouldCallisApplicable() {
89         mController.isAvailable();
90 
91         verify(mPreference).isApplicable(mContext);
92     }
93 
94     @Test
getPreferenceKey_shouldReturnPrefKey()95     public void getPreferenceKey_shouldReturnPrefKey() {
96         assertThat(mController.getPreferenceKey()).isEqualTo(PreferenceControllerTestable.KEY_TEST);
97     }
98 
99     @Test
updateState_shouldUpdatePreference()100     public void updateState_shouldUpdatePreference() {
101         mController.updateState(null);
102 
103         verify(mPreference).update(mContext);
104     }
105 
106     @Test
onResume_shouldRegisterContentObserver()107     public void onResume_shouldRegisterContentObserver() {
108         mController.displayPreference(mScreen);
109         mController.onResume();
110 
111         verify(mContentResolver).registerContentObserver(
112             Global.getUriFor("Setting1"), false, mController.getObserver());
113     }
114 
115     @Test
onPause_shouldUnregisterContentObserver()116     public void onPause_shouldUnregisterContentObserver() {
117         mController.displayPreference(mScreen);
118         mController.onPause();
119 
120         verify(mContentResolver).unregisterContentObserver(mController.getObserver());
121     }
122 
123     @Test
onContentChange_shouldUpdatePreference()124     public void onContentChange_shouldUpdatePreference() {
125         mController.displayPreference(mScreen);
126         mController.onResume();
127         mController.getObserver().onChange(false, Global.getUriFor("Setting1"));
128 
129         verify(mPreference).update(mContext);
130     }
131 
132     @Test
updateNonIndexableKeys_applicable_shouldNotUpdate()133     public void updateNonIndexableKeys_applicable_shouldNotUpdate() {
134         final List<String> keys = new ArrayList<>();
135 
136         mController.updateNonIndexableKeys(keys);
137 
138         assertThat(keys).isEmpty();
139     }
140 
141     @Test
updateNonIndexableKeys_notApplicable_shouldUpdate()142     public void updateNonIndexableKeys_notApplicable_shouldUpdate() {
143         mController.setApplicable(false);
144         final List<String> keys = new ArrayList<>();
145 
146         mController.updateNonIndexableKeys(keys);
147 
148         assertThat(keys).isNotEmpty();
149     }
150 
151     private class PreferenceControllerTestable extends SettingPrefController {
152 
153         private static final String KEY_TEST = "key1";
154         private boolean mApplicable = true;
155 
PreferenceControllerTestable(Context context, SettingsPreferenceFragment parent, Lifecycle lifecycle)156         private PreferenceControllerTestable(Context context, SettingsPreferenceFragment parent,
157             Lifecycle lifecycle) {
158             super(context, parent, lifecycle);
159             mPreference = spy(new SettingPref(
160                 TYPE_GLOBAL, KEY_TEST, "Setting1", 1) {
161                 @Override
162                 public boolean isApplicable(Context context) {
163                     return mApplicable;
164                 }
165             });
166         }
167 
getPref()168         SettingPref getPref() {
169             return mPreference;
170         }
171 
getObserver()172         PreferenceControllerTestable.SettingsObserver getObserver() {
173             return mSettingsObserver;
174         }
175 
setApplicable(boolean applicable)176         void setApplicable(boolean applicable) {
177             mApplicable = applicable;
178         }
179     }
180 }
181