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.gestures;
18 
19 import android.content.Context;
20 import android.support.v7.preference.Preference;
21 import android.support.v7.preference.PreferenceScreen;
22 import android.support.v7.preference.TwoStatePreference;
23 
24 import com.android.settings.SettingsRobolectricTestRunner;
25 import com.android.settings.TestConfig;
26 import com.android.settings.core.lifecycle.Lifecycle;
27 import com.android.settings.widget.VideoPreference;
28 
29 import org.junit.Before;
30 import org.junit.Test;
31 import org.junit.runner.RunWith;
32 import org.mockito.Answers;
33 import org.mockito.Mock;
34 import org.mockito.MockitoAnnotations;
35 import org.robolectric.annotation.Config;
36 
37 import static org.mockito.Matchers.any;
38 import static org.mockito.Matchers.anyString;
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(SettingsRobolectricTestRunner.class)
45 @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
46 public class GesturePreferenceControllerTest {
47 
48 
49     @Mock(answer = Answers.RETURNS_DEEP_STUBS)
50     private Context mContext;
51     @Mock(answer = Answers.RETURNS_DEEP_STUBS)
52     private PreferenceScreen mScreen;
53     @Mock(answer = Answers.RETURNS_DEEP_STUBS)
54     private Lifecycle mLifecycle;
55     private TestPrefController mController;
56 
57     @Before
setUp()58     public void setUp() {
59         MockitoAnnotations.initMocks(this);
60         mController = new TestPrefController(mContext, mLifecycle);
61     }
62 
63     @Test
display_configIsTrue_shouldDisplay()64     public void display_configIsTrue_shouldDisplay() {
65         mController.mIsPrefAvailable = true;
66         when(mScreen.findPreference(anyString())).thenReturn(mock(VideoPreference.class));
67 
68         mController.displayPreference(mScreen);
69 
70         verify(mScreen, never()).removePreference(any(Preference.class));
71     }
72 
73     @Test
display_configIsFalse_shouldNotDisplay()74     public void display_configIsFalse_shouldNotDisplay() {
75         mController.mIsPrefAvailable = 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
onStart_shouldStartVideoPreference()87     public void onStart_shouldStartVideoPreference() {
88         final VideoPreference videoPreference = mock(VideoPreference.class);
89         when(mScreen.findPreference(mController.getVideoPrefKey())).thenReturn(videoPreference);
90         mController.mIsPrefAvailable = true;
91 
92         mController.displayPreference(mScreen);
93         mController.onStart();
94 
95         verify(videoPreference).onViewVisible();
96     }
97 
98     @Test
onStop_shouldStopVideoPreference()99     public void onStop_shouldStopVideoPreference() {
100         final VideoPreference videoPreference = mock(VideoPreference.class);
101         when(mScreen.findPreference(mController.getVideoPrefKey())).thenReturn(videoPreference);
102         mController.mIsPrefAvailable = true;
103 
104         mController.displayPreference(mScreen);
105         mController.onStop();
106 
107         verify(videoPreference).onViewInvisible();
108     }
109 
110     @Test
updateState_preferenceSetCheckedWhenSettingIsOn()111     public void updateState_preferenceSetCheckedWhenSettingIsOn() {
112         // Mock a TwoStatePreference
113         final TwoStatePreference preference = mock(TwoStatePreference.class);
114         // Set the setting to be enabled.
115         mController.mIsPrefEnabled = true;
116         // Run through updateState
117         mController.updateState(preference);
118 
119         // Verify pref is checked (as setting is enabled).
120         verify(preference).setChecked(true);
121     }
122 
123     @Test
updateState_preferenceSetUncheckedWhenSettingIsOff()124     public void updateState_preferenceSetUncheckedWhenSettingIsOff() {
125         // Mock a TwoStatePreference
126         final TwoStatePreference preference = mock(TwoStatePreference.class);
127         // Set the setting to be disabled.
128         mController.mIsPrefEnabled = false;
129 
130         // Run through updateState
131         mController.updateState(preference);
132 
133         // Verify pref is unchecked (as setting is disabled).
134         verify(preference).setChecked(false);
135     }
136 
137     @Test
updateState_notTwoStatePreference_setSummary()138     public void updateState_notTwoStatePreference_setSummary() {
139         // Mock a regular preference
140         final Preference preference = mock(Preference.class);
141         // Set the setting to be disabled.
142         mController.mIsPrefEnabled = false;
143 
144         // Run through updateState
145         mController.updateState(preference);
146 
147         // Verify summary is set to off (as setting is disabled).
148         verify(preference).setSummary(com.android.settings.R.string.gesture_setting_off);
149     }
150 
151     private class TestPrefController extends GesturePreferenceController {
152 
153         boolean mIsPrefAvailable;
154         boolean mIsPrefEnabled;
155 
TestPrefController(Context context, Lifecycle lifecycle)156         public TestPrefController(Context context,
157                 Lifecycle lifecycle) {
158             super(context, lifecycle);
159         }
160 
161         @Override
isAvailable()162         public boolean isAvailable() {
163             return mIsPrefAvailable;
164         }
165 
166         @Override
getPreferenceKey()167         public String getPreferenceKey() {
168             return "testKey";
169         }
170 
171         @Override
getVideoPrefKey()172         protected String getVideoPrefKey() {
173             return "videoKey";
174         }
175 
176         @Override
isSwitchPrefEnabled()177         protected boolean isSwitchPrefEnabled() {
178             return mIsPrefEnabled;
179         }
180 
181         @Override
onPreferenceChange(Preference preference, Object newValue)182         public boolean onPreferenceChange(Preference preference, Object newValue) {
183             return false;
184         }
185     }
186 }
187