• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 static com.google.common.truth.Truth.assertThat;
20 
21 import static org.mockito.ArgumentMatchers.anyString;
22 import static org.mockito.Mockito.mock;
23 import static org.mockito.Mockito.reset;
24 import static org.mockito.Mockito.verify;
25 import static org.mockito.Mockito.when;
26 
27 import android.content.Context;
28 import android.os.Bundle;
29 
30 import androidx.preference.Preference;
31 import androidx.preference.PreferenceScreen;
32 import androidx.preference.TwoStatePreference;
33 
34 import com.android.settings.widget.VideoPreference;
35 
36 import org.junit.Before;
37 import org.junit.Test;
38 import org.junit.runner.RunWith;
39 import org.mockito.Answers;
40 import org.mockito.Mock;
41 import org.mockito.MockitoAnnotations;
42 import org.robolectric.RobolectricTestRunner;
43 import org.robolectric.RuntimeEnvironment;
44 
45 @RunWith(RobolectricTestRunner.class)
46 public class GesturePreferenceControllerTest {
47 
48     @Mock(answer = Answers.RETURNS_DEEP_STUBS)
49     private Context mContext;
50     @Mock(answer = Answers.RETURNS_DEEP_STUBS)
51     private PreferenceScreen mScreen;
52     @Mock(answer = Answers.RETURNS_DEEP_STUBS)
53 
54     private TestPrefController mController;
55     private Preference mPreference;
56 
57     @Before
setUp()58     public void setUp() {
59         MockitoAnnotations.initMocks(this);
60         mController = new TestPrefController(mContext, "testKey");
61         mPreference = new Preference(RuntimeEnvironment.application);
62         mPreference.setKey(mController.getPreferenceKey());
63         when(mScreen.findPreference(mPreference.getKey())).thenReturn(mPreference);
64     }
65 
66     @Test
display_configIsTrue_shouldDisplay()67     public void display_configIsTrue_shouldDisplay() {
68         mController.mIsPrefAvailable = true;
69         when(mScreen.findPreference(anyString())).thenReturn(mock(VideoPreference.class));
70 
71         mController.displayPreference(mScreen);
72 
73         assertThat(mPreference.isVisible()).isTrue();
74     }
75 
76     @Test
display_configIsFalse_shouldNotDisplay()77     public void display_configIsFalse_shouldNotDisplay() {
78         mController.mIsPrefAvailable = false;
79 
80         mController.displayPreference(mScreen);
81 
82         assertThat(mPreference.isVisible()).isFalse();
83     }
84 
85     @Test
onResume_shouldStartVideoPreferenceWithVideoPauseState()86     public void onResume_shouldStartVideoPreferenceWithVideoPauseState() {
87         final VideoPreference videoPreference = mock(VideoPreference.class);
88         when(mScreen.findPreference(mController.getVideoPrefKey())).thenReturn(videoPreference);
89         mController.mIsPrefAvailable = true;
90 
91         mController.displayPreference(mScreen);
92         final Bundle savedState = new Bundle();
93 
94         mController.onCreate(null);
95         mController.onResume();
96         verify(videoPreference).onViewVisible(false);
97 
98         reset(videoPreference);
99         savedState.putBoolean(GesturePreferenceController.KEY_VIDEO_PAUSED, true);
100         mController.onCreate(savedState);
101         mController.onResume();
102         verify(videoPreference).onViewVisible(true);
103 
104         reset(videoPreference);
105         savedState.putBoolean(GesturePreferenceController.KEY_VIDEO_PAUSED, false);
106         mController.onCreate(savedState);
107         mController.onResume();
108         verify(videoPreference).onViewVisible(false);
109     }
110 
111     @Test
onPause_shouldStopVideoPreference()112     public void onPause_shouldStopVideoPreference() {
113         final VideoPreference videoPreference = mock(VideoPreference.class);
114         when(mScreen.findPreference(mController.getVideoPrefKey())).thenReturn(videoPreference);
115         mController.mIsPrefAvailable = true;
116 
117         mController.displayPreference(mScreen);
118         mController.onPause();
119 
120         verify(videoPreference).onViewInvisible();
121     }
122 
123     @Test
onPause_shouldUpdateVideoPauseState()124     public void onPause_shouldUpdateVideoPauseState() {
125         final VideoPreference videoPreference = mock(VideoPreference.class);
126         when(mScreen.findPreference(mController.getVideoPrefKey())).thenReturn(videoPreference);
127         mController.mIsPrefAvailable = true;
128         mController.displayPreference(mScreen);
129 
130         when(videoPreference.isVideoPaused()).thenReturn(true);
131         mController.onPause();
132         assertThat(mController.mVideoPaused).isTrue();
133 
134         when(videoPreference.isVideoPaused()).thenReturn(false);
135         mController.onPause();
136         assertThat(mController.mVideoPaused).isFalse();
137     }
138 
139     @Test
onSaveInstanceState_shouldSaveVideoPauseState()140     public void onSaveInstanceState_shouldSaveVideoPauseState() {
141         final Bundle outState = mock(Bundle.class);
142 
143         mController.mVideoPaused = true;
144         mController.onSaveInstanceState(outState);
145         verify(outState).putBoolean(GesturePreferenceController.KEY_VIDEO_PAUSED, true);
146 
147         mController.mVideoPaused = false;
148         mController.onSaveInstanceState(outState);
149         verify(outState).putBoolean(GesturePreferenceController.KEY_VIDEO_PAUSED, false);
150     }
151 
152     @Test
updateState_preferenceSetCheckedWhenSettingIsOn()153     public void updateState_preferenceSetCheckedWhenSettingIsOn() {
154         // Mock a TwoStatePreference
155         final TwoStatePreference preference = mock(TwoStatePreference.class);
156         // Set the setting to be enabled.
157         mController.mIsPrefEnabled = true;
158         // Run through updateState
159         mController.updateState(preference);
160 
161         // Verify pref is checked (as setting is enabled).
162         verify(preference).setChecked(true);
163     }
164 
165     @Test
updateState_preferenceSetUncheckedWhenSettingIsOff()166     public void updateState_preferenceSetUncheckedWhenSettingIsOff() {
167         // Mock a TwoStatePreference
168         final TwoStatePreference preference = mock(TwoStatePreference.class);
169         // Set the setting to be disabled.
170         mController.mIsPrefEnabled = false;
171 
172         // Run through updateState
173         mController.updateState(preference);
174 
175         // Verify pref is unchecked (as setting is disabled).
176         verify(preference).setChecked(false);
177     }
178 
179     private class TestPrefController extends GesturePreferenceController {
180 
181         boolean mIsPrefAvailable;
182         boolean mIsPrefEnabled;
183 
TestPrefController(Context context, String key)184         private TestPrefController(Context context, String key) {
185             super(context, key);
186         }
187 
188         @Override
getAvailabilityStatus()189         public int getAvailabilityStatus() {
190             return mIsPrefAvailable ? AVAILABLE : UNSUPPORTED_ON_DEVICE;
191         }
192 
193         @Override
getVideoPrefKey()194         protected String getVideoPrefKey() {
195             return "videoKey";
196         }
197 
198         @Override
isChecked()199         public boolean isChecked() {
200             return mIsPrefEnabled;
201         }
202 
203         @Override
setChecked(boolean isChecked)204         public boolean setChecked(boolean isChecked) {
205             return false;
206         }
207     }
208 }
209