1 /* 2 * Copyright (C) 2021 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.Mockito.mock; 22 import static org.mockito.Mockito.spy; 23 import static org.mockito.Mockito.when; 24 25 import android.content.Context; 26 import android.content.res.Resources; 27 import android.provider.Settings; 28 29 import androidx.test.core.app.ApplicationProvider; 30 31 import org.junit.Before; 32 import org.junit.Test; 33 import org.junit.runner.RunWith; 34 import org.robolectric.RobolectricTestRunner; 35 36 @RunWith(RobolectricTestRunner.class) 37 public class ButtonNavigationSettingsAssistControllerTest { 38 39 private static final String KEY_LONG_PRESS_HOME_FOR_ASSIST = 40 "assistant_long_press_home_gesture"; 41 42 private Context mContext; 43 private Resources mResources; 44 private ButtonNavigationSettingsAssistController mController; 45 46 @Before setUp()47 public void setUp() { 48 mContext = spy(ApplicationProvider.getApplicationContext()); 49 mResources = mock(Resources.class); 50 when(mContext.getResources()).thenReturn(mResources); 51 52 mController = new ButtonNavigationSettingsAssistController( 53 mContext, KEY_LONG_PRESS_HOME_FOR_ASSIST); 54 } 55 56 @Test isChecked_valueUnknownDefaultTrue_shouldReturnTrue()57 public void isChecked_valueUnknownDefaultTrue_shouldReturnTrue() { 58 when(mResources.getBoolean( 59 com.android.internal.R.bool.config_assistLongPressHomeEnabledDefault)).thenReturn( 60 true); 61 assertThat(mController.isChecked()).isTrue(); 62 } 63 64 @Test isChecked_valueUnknownDefaultFalse_shouldReturnFalse()65 public void isChecked_valueUnknownDefaultFalse_shouldReturnFalse() { 66 when(mResources.getBoolean( 67 com.android.internal.R.bool.config_assistLongPressHomeEnabledDefault)).thenReturn( 68 true); 69 assertThat(mController.isChecked()).isTrue(); 70 } 71 72 @Test isChecked_valueTrue_shouldReturnTrue()73 public void isChecked_valueTrue_shouldReturnTrue() { 74 Settings.Secure.putInt(mContext.getContentResolver(), 75 Settings.Secure.ASSIST_LONG_PRESS_HOME_ENABLED, 1); 76 assertThat(mController.isChecked()).isTrue(); 77 } 78 79 @Test isChecked_valueFalse_shouldReturnFalse()80 public void isChecked_valueFalse_shouldReturnFalse() { 81 Settings.Secure.putInt(mContext.getContentResolver(), 82 Settings.Secure.ASSIST_LONG_PRESS_HOME_ENABLED, 0); 83 assertThat(mController.isChecked()).isFalse(); 84 } 85 86 @Test setChecked_valueFalse_shouldSetFalse()87 public void setChecked_valueFalse_shouldSetFalse() { 88 mController.setChecked(false); 89 assertThat(Settings.Secure.getInt(mContext.getContentResolver(), 90 Settings.Secure.ASSIST_LONG_PRESS_HOME_ENABLED, -1)).isEqualTo(0); 91 } 92 93 @Test setChecked_valueTrue_shouldSetTrue()94 public void setChecked_valueTrue_shouldSetTrue() { 95 mController.setChecked(true); 96 assertThat(Settings.Secure.getInt(mContext.getContentResolver(), 97 Settings.Secure.ASSIST_LONG_PRESS_HOME_ENABLED, -1)).isEqualTo(1); 98 } 99 100 } 101