1 /* 2 * Copyright (C) 2023 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.ArgumentMatchers.eq; 22 import static org.mockito.Mockito.mock; 23 import static org.mockito.Mockito.verify; 24 25 import android.app.settings.SettingsEnums; 26 import android.content.Context; 27 28 import androidx.test.core.app.ApplicationProvider; 29 30 import com.android.settings.R; 31 import com.android.settingslib.core.AbstractPreferenceController; 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.MockitoAnnotations; 38 import org.mockito.Spy; 39 import org.mockito.junit.MockitoJUnit; 40 import org.mockito.junit.MockitoRule; 41 import org.robolectric.RobolectricTestRunner; 42 43 @RunWith(RobolectricTestRunner.class) 44 public class FlashNotificationsPreferenceFragmentTest { 45 46 @Rule 47 public MockitoRule mMockitoRule = MockitoJUnit.rule(); 48 @Spy 49 private final Context mContext = ApplicationProvider.getApplicationContext(); 50 private FlashNotificationsPreferenceFragment mFragment; 51 52 @Before setUp()53 public void setUp() { 54 MockitoAnnotations.initMocks(this); 55 mFragment = new FlashNotificationsPreferenceFragment(); 56 } 57 58 @Test getPreferenceScreenResId_isFlashNotificationsSettings()59 public void getPreferenceScreenResId_isFlashNotificationsSettings() { 60 assertThat(mFragment.getPreferenceScreenResId()) 61 .isEqualTo(R.xml.flash_notifications_settings); 62 } 63 64 @Test getLogTag_isSpecifyTag()65 public void getLogTag_isSpecifyTag() { 66 assertThat(mFragment.getLogTag()).isEqualTo("FlashNotificationsPreferenceFragment"); 67 } 68 69 @Test getMetricsCategory_returnsCorrectCategory()70 public void getMetricsCategory_returnsCorrectCategory() { 71 assertThat(mFragment.getMetricsCategory()).isEqualTo( 72 SettingsEnums.FLASH_NOTIFICATION_SETTINGS); 73 } 74 75 @Test onAttach_attachedTheTestFragment()76 public void onAttach_attachedTheTestFragment() { 77 ScreenFlashNotificationPreferenceController controller = mock( 78 ScreenFlashNotificationPreferenceController.class); 79 TestFlashNotificationsPreferenceFragment testFragment = 80 new TestFlashNotificationsPreferenceFragment(controller); 81 testFragment.onAttach(mContext); 82 verify(controller).setParentFragment(eq(testFragment)); 83 } 84 85 @Test getHelpResource_isExpectedResource()86 public void getHelpResource_isExpectedResource() { 87 assertThat(mFragment.getHelpResource()) 88 .isEqualTo(R.string.help_url_flash_notifications); 89 } 90 91 static class TestFlashNotificationsPreferenceFragment extends 92 FlashNotificationsPreferenceFragment { 93 final AbstractPreferenceController mController; 94 TestFlashNotificationsPreferenceFragment(AbstractPreferenceController controller)95 TestFlashNotificationsPreferenceFragment(AbstractPreferenceController controller) { 96 mController = controller; 97 } 98 99 @Override use(Class<T> clazz)100 protected <T extends AbstractPreferenceController> T use(Class<T> clazz) { 101 return (T) mController; 102 } 103 } 104 } 105