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.doReturn; 23 import static org.mockito.Mockito.mock; 24 import static org.mockito.Mockito.verify; 25 import static org.mockito.Mockito.when; 26 27 import android.app.admin.DevicePolicyManager; 28 import android.content.Context; 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 doReturn(mock(DevicePolicyManager.class)).when(mContext) 61 .getSystemService(Context.DEVICE_POLICY_SERVICE); 62 mController = new TestPrefController(mContext, "testKey"); 63 mPreference = new Preference(RuntimeEnvironment.application); 64 mPreference.setKey(mController.getPreferenceKey()); 65 when(mScreen.findPreference(mPreference.getKey())).thenReturn(mPreference); 66 } 67 68 @Test display_configIsTrue_shouldDisplay()69 public void display_configIsTrue_shouldDisplay() { 70 mController.mIsPrefAvailable = true; 71 when(mScreen.findPreference(anyString())).thenReturn(mock(VideoPreference.class)); 72 73 mController.displayPreference(mScreen); 74 75 assertThat(mPreference.isVisible()).isTrue(); 76 } 77 78 @Test display_configIsFalse_shouldNotDisplay()79 public void display_configIsFalse_shouldNotDisplay() { 80 mController.mIsPrefAvailable = false; 81 82 mController.displayPreference(mScreen); 83 84 assertThat(mPreference.isVisible()).isFalse(); 85 } 86 87 @Test onStart_shouldStartVideoPreference()88 public void onStart_shouldStartVideoPreference() { 89 final VideoPreference videoPreference = mock(VideoPreference.class); 90 when(mScreen.findPreference(mController.getVideoPrefKey())).thenReturn(videoPreference); 91 mController.mIsPrefAvailable = true; 92 mController.displayPreference(mScreen); 93 94 mController.onStart(); 95 96 verify(videoPreference).onViewVisible(); 97 } 98 99 @Test onStop_shouldStopVideoPreference()100 public void onStop_shouldStopVideoPreference() { 101 final VideoPreference videoPreference = mock(VideoPreference.class); 102 when(mScreen.findPreference(mController.getVideoPrefKey())).thenReturn(videoPreference); 103 mController.mIsPrefAvailable = true; 104 105 mController.displayPreference(mScreen); 106 mController.onStop(); 107 108 verify(videoPreference).onViewInvisible(); 109 } 110 111 @Test updateState_preferenceSetCheckedWhenSettingIsOn()112 public void updateState_preferenceSetCheckedWhenSettingIsOn() { 113 // Mock a TwoStatePreference 114 final TwoStatePreference preference = mock(TwoStatePreference.class); 115 // Set the setting to be enabled. 116 mController.mIsPrefEnabled = true; 117 // Run through updateState 118 mController.updateState(preference); 119 120 // Verify pref is checked (as setting is enabled). 121 verify(preference).setChecked(true); 122 } 123 124 @Test updateState_preferenceSetUncheckedWhenSettingIsOff()125 public void updateState_preferenceSetUncheckedWhenSettingIsOff() { 126 // Mock a TwoStatePreference 127 final TwoStatePreference preference = mock(TwoStatePreference.class); 128 // Set the setting to be disabled. 129 mController.mIsPrefEnabled = false; 130 131 // Run through updateState 132 mController.updateState(preference); 133 134 // Verify pref is unchecked (as setting is disabled). 135 verify(preference).setChecked(false); 136 } 137 138 private class TestPrefController extends GesturePreferenceController { 139 140 boolean mIsPrefAvailable; 141 boolean mIsPrefEnabled; 142 TestPrefController(Context context, String key)143 private TestPrefController(Context context, String key) { 144 super(context, key); 145 } 146 147 @Override getAvailabilityStatus()148 public int getAvailabilityStatus() { 149 return mIsPrefAvailable ? AVAILABLE : UNSUPPORTED_ON_DEVICE; 150 } 151 152 @Override getVideoPrefKey()153 protected String getVideoPrefKey() { 154 return "videoKey"; 155 } 156 157 @Override isChecked()158 public boolean isChecked() { 159 return mIsPrefEnabled; 160 } 161 162 @Override setChecked(boolean isChecked)163 public boolean setChecked(boolean isChecked) { 164 return false; 165 } 166 } 167 } 168