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.accessibility; 18 19 import static android.provider.Settings.Secure.ACCESSIBILITY_BUTTON_MODE_GESTURE; 20 import static android.provider.Settings.Secure.NAVIGATION_MODE; 21 import static android.view.WindowManagerPolicyConstants.NAV_BAR_MODE_2BUTTON; 22 import static android.view.WindowManagerPolicyConstants.NAV_BAR_MODE_GESTURAL; 23 24 import static com.google.common.truth.Truth.assertThat; 25 26 import static org.mockito.Mockito.when; 27 28 import android.content.Context; 29 import android.content.res.Resources; 30 import android.icu.text.MessageFormat; 31 import android.platform.test.annotations.DisableFlags; 32 import android.platform.test.annotations.EnableFlags; 33 import android.platform.test.flag.junit.SetFlagsRule; 34 import android.provider.Flags; 35 import android.provider.Settings; 36 import android.text.Html; 37 38 import androidx.preference.PreferenceScreen; 39 import androidx.test.core.app.ApplicationProvider; 40 41 import com.android.internal.accessibility.util.ShortcutUtils; 42 import com.android.settings.R; 43 44 import org.junit.Before; 45 import org.junit.Rule; 46 import org.junit.Test; 47 import org.junit.runner.RunWith; 48 import org.mockito.Mock; 49 import org.mockito.Spy; 50 import org.mockito.junit.MockitoJUnit; 51 import org.mockito.junit.MockitoRule; 52 import org.robolectric.RobolectricTestRunner; 53 54 /** Tests for {@link AccessibilityButtonFooterPreferenceController}. */ 55 @RunWith(RobolectricTestRunner.class) 56 public class AccessibilityButtonFooterPreferenceControllerTest { 57 58 @Rule 59 public final SetFlagsRule mCheckFlagsRule = new SetFlagsRule(); 60 @Rule 61 public final MockitoRule mockito = MockitoJUnit.rule(); 62 @Spy 63 private final Context mContext = ApplicationProvider.getApplicationContext(); 64 @Spy 65 private final Resources mResources = mContext.getResources(); 66 @Mock 67 private PreferenceScreen mScreen; 68 private AccessibilityButtonFooterPreferenceController mController; 69 private AccessibilityFooterPreference mPreference; 70 71 @Before setUp()72 public void setUp() { 73 mController = new AccessibilityButtonFooterPreferenceController(mContext, "test_key"); 74 mPreference = new AccessibilityFooterPreference(mContext); 75 mPreference.setKey("test_key"); 76 77 when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference); 78 when(mContext.getResources()).thenReturn(mResources); 79 } 80 81 @Test 82 @DisableFlags(Flags.FLAG_A11Y_STANDALONE_GESTURE_ENABLED) displayPreference_navigationGestureEnabled_setCorrectTitle()83 public void displayPreference_navigationGestureEnabled_setCorrectTitle() { 84 Settings.Secure.putIntForUser(mContext.getContentResolver(), 85 NAVIGATION_MODE, NAV_BAR_MODE_GESTURAL, mContext.getUserId()); 86 ShortcutUtils.setButtonMode( 87 mContext, ACCESSIBILITY_BUTTON_MODE_GESTURE, mContext.getUserId()); 88 89 mController.displayPreference(mScreen); 90 91 assertThat(mPreference.getTitle().toString()).isEqualTo( 92 Html.fromHtml( 93 MessageFormat.format(mContext.getString( 94 R.string.accessibility_button_gesture_description), 1, 2, 3), 95 Html.FROM_HTML_MODE_COMPACT).toString()); 96 } 97 98 @Test 99 @EnableFlags(Flags.FLAG_A11Y_STANDALONE_GESTURE_ENABLED) displayPreference_navigationGestureEnabled_flag_setCorrectTitle()100 public void displayPreference_navigationGestureEnabled_flag_setCorrectTitle() { 101 Settings.Secure.putIntForUser(mContext.getContentResolver(), 102 NAVIGATION_MODE, NAV_BAR_MODE_GESTURAL, mContext.getUserId()); 103 104 assertThat(AccessibilityUtil.isGestureNavigateEnabled(mContext)).isTrue(); 105 mController.displayPreference(mScreen); 106 107 assertThat(mPreference.getTitle().toString()).isEqualTo( 108 Html.fromHtml( 109 MessageFormat.format(mContext.getString( 110 R.string.accessibility_button_description), 1, 2, 3), 111 Html.FROM_HTML_MODE_COMPACT).toString()); 112 } 113 114 @Test displayPreference_navigationGestureDisabled_setCorrectTitle()115 public void displayPreference_navigationGestureDisabled_setCorrectTitle() { 116 Settings.Secure.putIntForUser(mContext.getContentResolver(), 117 NAVIGATION_MODE, NAV_BAR_MODE_2BUTTON, mContext.getUserId()); 118 119 mController.displayPreference(mScreen); 120 121 assertThat(mPreference.getTitle().toString()).isEqualTo( 122 Html.fromHtml( 123 MessageFormat.format( 124 mContext.getString( 125 R.string.accessibility_button_description), 1, 2, 3), 126 Html.FROM_HTML_MODE_COMPACT).toString()); 127 } 128 } 129