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