1 /* 2 * Copyright (C) 2022 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.google.common.truth.Truth.assertThat; 20 21 import static org.mockito.Mockito.verify; 22 import static org.mockito.Mockito.when; 23 24 import android.content.ContentResolver; 25 import android.content.Context; 26 import android.provider.Settings; 27 import android.view.accessibility.CaptioningManager.CaptionStyle; 28 29 import androidx.preference.Preference; 30 import androidx.preference.PreferenceScreen; 31 import androidx.test.core.app.ApplicationProvider; 32 33 import org.junit.Before; 34 import org.junit.Rule; 35 import org.junit.Test; 36 import org.junit.runner.RunWith; 37 import org.mockito.Mock; 38 import org.mockito.junit.MockitoJUnit; 39 import org.mockito.junit.MockitoRule; 40 import org.robolectric.RobolectricTestRunner; 41 42 /** Tests for {@link CaptioningCustomController}. */ 43 @RunWith(RobolectricTestRunner.class) 44 public class CaptioningCustomControllerTest { 45 46 private static final String PREF_KEY = "custom"; 47 48 @Rule 49 public final MockitoRule mMockitoRule = MockitoJUnit.rule(); 50 @Mock 51 private PreferenceScreen mScreen; 52 @Mock 53 private AccessibilitySettingsContentObserver mAccessibilitySettingsContentObserver; 54 private final Context mContext = ApplicationProvider.getApplicationContext(); 55 private ContentResolver mContentResolver; 56 private CaptioningCustomController mController; 57 private Preference mPreference; 58 59 @Before setUp()60 public void setUp() { 61 mContentResolver = mContext.getContentResolver(); 62 mController = new CaptioningCustomController(mContext, PREF_KEY, 63 mAccessibilitySettingsContentObserver); 64 mPreference = new Preference(mContext); 65 when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference); 66 } 67 68 @Test displayPreference_byDefault_shouldIsInvisible()69 public void displayPreference_byDefault_shouldIsInvisible() { 70 mController.displayPreference(mScreen); 71 72 assertThat(mPreference.isVisible()).isFalse(); 73 } 74 75 @Test displayPreference_customValue_shouldIsVisible()76 public void displayPreference_customValue_shouldIsVisible() { 77 Settings.Secure.putInt(mContext.getContentResolver(), 78 Settings.Secure.ACCESSIBILITY_CAPTIONING_PRESET, CaptionStyle.PRESET_CUSTOM); 79 80 mController.displayPreference(mScreen); 81 82 assertThat(mPreference.isVisible()).isTrue(); 83 } 84 85 @Test onStart_registerSpecificContentObserverForSpecificKeys()86 public void onStart_registerSpecificContentObserverForSpecificKeys() { 87 mController.onStart(); 88 89 verify(mAccessibilitySettingsContentObserver).register(mContentResolver); 90 } 91 92 @Test onStop_unregisterContentObserver()93 public void onStop_unregisterContentObserver() { 94 mController.onStop(); 95 96 verify(mAccessibilitySettingsContentObserver).unregister(mContentResolver); 97 } 98 } 99