1 /* 2 * Copyright (C) 2025 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 com.android.settings.core.BasePreferenceController.AVAILABLE; 20 import static com.android.settings.core.BasePreferenceController.UNSUPPORTED_ON_DEVICE; 21 22 import static com.google.common.truth.Truth.assertThat; 23 24 import static org.mockito.Mockito.when; 25 26 import android.content.Context; 27 import android.content.res.Resources; 28 29 import androidx.preference.PreferenceScreen; 30 import androidx.test.core.app.ApplicationProvider; 31 32 import com.android.settings.R; 33 import com.android.settingslib.widget.TopIntroPreference; 34 35 import org.junit.Before; 36 import org.junit.Rule; 37 import org.junit.Test; 38 import org.junit.runner.RunWith; 39 import org.mockito.Mock; 40 import org.mockito.Spy; 41 import org.mockito.junit.MockitoJUnit; 42 import org.mockito.junit.MockitoRule; 43 import org.robolectric.RobolectricTestRunner; 44 45 /** 46 * Tests for {@link HearingDeviceIntroPreferenceController}. 47 */ 48 @RunWith(RobolectricTestRunner.class) 49 public class HearingDeviceIntroPreferenceControllerTest { 50 51 @Rule 52 public final MockitoRule mockito = MockitoJUnit.rule(); 53 54 @Spy 55 private final Context mContext = ApplicationProvider.getApplicationContext(); 56 @Spy 57 private final Resources mResources = mContext.getResources(); 58 @Mock 59 private PreferenceScreen mScreen; 60 @Mock 61 private HearingAidHelper mHelper; 62 private HearingDeviceIntroPreferenceController mController; 63 private TopIntroPreference mPreference; 64 65 @Before setUp()66 public void setUp() { 67 mController = new HearingDeviceIntroPreferenceController(mContext, "test_key", mHelper); 68 mPreference = new TopIntroPreference(mContext); 69 mPreference.setKey("test_key"); 70 71 when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference); 72 when(mContext.getResources()).thenReturn(mResources); 73 } 74 75 @Test getAvailabilityStatus_hearingAidSupported_available()76 public void getAvailabilityStatus_hearingAidSupported_available() { 77 when(mHelper.isHearingAidSupported()).thenReturn(true); 78 79 assertThat(mController.getAvailabilityStatus()).isEqualTo(AVAILABLE); 80 } 81 82 @Test getAvailabilityStatus_hearingAidNotSupported_unsupportedOnDevice()83 public void getAvailabilityStatus_hearingAidNotSupported_unsupportedOnDevice() { 84 when(mHelper.isHearingAidSupported()).thenReturn(false); 85 86 assertThat(mController.getAvailabilityStatus()).isEqualTo(UNSUPPORTED_ON_DEVICE); 87 } 88 89 @Test displayPreference_ashaHapSupported_expectedTitle()90 public void displayPreference_ashaHapSupported_expectedTitle() { 91 when(mHelper.isAshaProfileSupported()).thenReturn(true); 92 when(mHelper.isHapClientProfileSupported()).thenReturn(true); 93 94 mController.displayPreference(mScreen); 95 96 assertThat(mPreference.getTitle().toString()).isEqualTo( 97 mContext.getString(R.string.accessibility_hearingaid_intro)); 98 } 99 100 @Test displayPreference_ashaSupported_expectedTitle()101 public void displayPreference_ashaSupported_expectedTitle() { 102 when(mHelper.isAshaProfileSupported()).thenReturn(true); 103 when(mHelper.isHapClientProfileSupported()).thenReturn(false); 104 105 mController.displayPreference(mScreen); 106 107 assertThat(mPreference.getTitle().toString()).isEqualTo( 108 mContext.getString(R.string.accessibility_hearingaid_asha_only_intro)); 109 } 110 111 @Test displayPreference_hapSupported_expectedTitle()112 public void displayPreference_hapSupported_expectedTitle() { 113 when(mHelper.isAshaProfileSupported()).thenReturn(false); 114 when(mHelper.isHapClientProfileSupported()).thenReturn(true); 115 116 mController.displayPreference(mScreen); 117 118 assertThat(mPreference.getTitle().toString()).isEqualTo( 119 mContext.getString(R.string.accessibility_hearingaid_hap_only_intro)); 120 } 121 } 122