1 /* 2 * Copyright (C) 2020 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.spy; 22 import static org.mockito.Mockito.when; 23 24 import android.content.Context; 25 import android.os.SystemProperties; 26 import android.provider.SearchIndexableResource; 27 28 import androidx.test.core.app.ApplicationProvider; 29 30 import com.android.settings.R; 31 import com.android.settings.accessibility.AccessibilityUtil.QuickSettingsTooltipType; 32 33 import org.junit.Before; 34 import org.junit.Test; 35 import org.junit.runner.RunWith; 36 import org.robolectric.RobolectricTestRunner; 37 import org.robolectric.RuntimeEnvironment; 38 import org.robolectric.util.ReflectionHelpers; 39 40 import java.util.List; 41 42 /** Tests for {@link OneHandedSettings}. */ 43 @RunWith(RobolectricTestRunner.class) 44 public class OneHandedSettingsTest { 45 46 private final Context mContext = ApplicationProvider.getApplicationContext(); 47 private OneHandedSettings mSettings; 48 49 @Before setUp()50 public void setUp() { 51 mSettings = spy(new OneHandedSettings()); 52 } 53 54 @Test getTileTooltipContent_returnsExpectedValues()55 public void getTileTooltipContent_returnsExpectedValues() { 56 // Simulate to call getTileTooltipContent after onDetach 57 assertThat(mSettings.getTileTooltipContent(QuickSettingsTooltipType.GUIDE_TO_EDIT)) 58 .isNull(); 59 // Simulate to call getTileTooltipContent after onAttach 60 when(mSettings.getContext()).thenReturn(mContext); 61 assertThat(mSettings.getTileTooltipContent(QuickSettingsTooltipType.GUIDE_TO_EDIT)) 62 .isEqualTo(mContext.getText( 63 R.string.accessibility_one_handed_mode_qs_tooltip_content)); 64 assertThat(mSettings.getTileTooltipContent(QuickSettingsTooltipType.GUIDE_TO_DIRECT_USE)) 65 .isEqualTo(mContext.getText( 66 R.string.accessibility_one_handed_mode_auto_added_qs_tooltip_content)); 67 } 68 69 @Test getLogTag_returnsCorrectTag()70 public void getLogTag_returnsCorrectTag() { 71 assertThat(mSettings.getLogTag()).isEqualTo("OneHandedSettings"); 72 } 73 74 @Test testSearchIndexProvider_shouldIndexResource()75 public void testSearchIndexProvider_shouldIndexResource() { 76 final List<SearchIndexableResource> indexRes = 77 OneHandedSettings.SEARCH_INDEX_DATA_PROVIDER.getXmlResourcesToIndex( 78 RuntimeEnvironment.application, true /* enabled */); 79 80 assertThat(indexRes).isNotNull(); 81 assertThat(indexRes.get(0).xmlResId).isEqualTo(mSettings.getPreferenceScreenResId()); 82 } 83 84 @Test isPageSearchEnabled_setSupportOneHandedModeProperty_shouldReturnTrue()85 public void isPageSearchEnabled_setSupportOneHandedModeProperty_shouldReturnTrue() { 86 SystemProperties.set(OneHandedSettingsUtils.SUPPORT_ONE_HANDED_MODE, "true"); 87 88 final Object obj = ReflectionHelpers.callInstanceMethod( 89 OneHandedSettings.SEARCH_INDEX_DATA_PROVIDER, "isPageSearchEnabled", 90 ReflectionHelpers.ClassParameter.from(Context.class, mContext)); 91 final boolean isEnabled = (Boolean) obj; 92 assertThat(isEnabled).isTrue(); 93 } 94 95 @Test isPageSearchEnabled_unsetSupportOneHandedModeProperty_shouldReturnFalse()96 public void isPageSearchEnabled_unsetSupportOneHandedModeProperty_shouldReturnFalse() { 97 SystemProperties.set(OneHandedSettingsUtils.SUPPORT_ONE_HANDED_MODE, "false"); 98 99 final Object obj = ReflectionHelpers.callInstanceMethod( 100 OneHandedSettings.SEARCH_INDEX_DATA_PROVIDER, "isPageSearchEnabled", 101 ReflectionHelpers.ClassParameter.from(Context.class, mContext)); 102 final boolean isEnabled = (Boolean) obj; 103 assertThat(isEnabled).isFalse(); 104 } 105 } 106