2 * Copyright (C) 2016 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.settings.gestures;
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;
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;
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;
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;
44 @RunWith(SettingsRobolectricTestRunner.class)
45 @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
46 public class GesturePreferenceControllerTest {
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;
59 MockitoAnnotations.initMocks(this);
60 mController = new TestPrefController(mContext, mLifecycle);
64 public void display_configIsTrue_shouldDisplay() {
65 mController.mIsPrefAvailable = true;
66 when(mScreen.findPreference(anyString())).thenReturn(mock(VideoPreference.class));
68 mController.displayPreference(mScreen);
70 verify(mScreen, never()).removePreference(any(Preference.class));
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());
81 mController.displayPreference(mScreen);
83 verify(mScreen).removePreference(any(Preference.class));
87 public void onStart_shouldStartVideoPreference() {
88 final VideoPreference videoPreference = mock(VideoPreference.class);
89 when(mScreen.findPreference(mController.getVideoPrefKey())).thenReturn(videoPreference);
90 mController.mIsPrefAvailable = true;
92 mController.displayPreference(mScreen);
93 mController.onStart();
95 verify(videoPreference).onViewVisible();
99 public void onStop_shouldStopVideoPreference() {
100 final VideoPreference videoPreference = mock(VideoPreference.class);
101 when(mScreen.findPreference(mController.getVideoPrefKey())).thenReturn(videoPreference);
102 mController.mIsPrefAvailable = true;
104 mController.displayPreference(mScreen);
105 mController.onStop();
107 verify(videoPreference).onViewInvisible();
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);
119 // Verify pref is checked (as setting is enabled).
120 verify(preference).setChecked(true);
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;
130 // Run through updateState
131 mController.updateState(preference);
133 // Verify pref is unchecked (as setting is disabled).
134 verify(preference).setChecked(false);
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;
144 // Run through updateState
145 mController.updateState(preference);
147 // Verify summary is set to off (as setting is disabled).
148 verify(preference).setSummary(com.android.settings.R.string.gesture_setting_off);
151 private class TestPrefController extends GesturePreferenceController {
153 boolean mIsPrefAvailable;
154 boolean mIsPrefEnabled;
156 public TestPrefController(Context context,
157 Lifecycle lifecycle) {
158 super(context, lifecycle);
162 public boolean isAvailable() {
163 return mIsPrefAvailable;
167 public String getPreferenceKey() {
172 protected String getVideoPrefKey() {
177 protected boolean isSwitchPrefEnabled() {
178 return mIsPrefEnabled;
182 public boolean onPreferenceChange(Preference preference, Object newValue) {