1 /* 2 * Copyright (C) 2018 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.widget; 18 19 import static com.android.settings.core.BasePreferenceController.AVAILABLE_UNSEARCHABLE; 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.doReturn; 25 import static org.mockito.Mockito.mock; 26 import static org.mockito.Mockito.verify; 27 import static org.mockito.Mockito.when; 28 29 import androidx.preference.PreferenceScreen; 30 31 import org.junit.Before; 32 import org.junit.Test; 33 import org.junit.runner.RunWith; 34 import org.mockito.Mock; 35 import org.mockito.MockitoAnnotations; 36 import org.robolectric.RobolectricTestRunner; 37 import org.robolectric.RuntimeEnvironment; 38 import org.robolectric.util.ReflectionHelpers; 39 40 @RunWith(RobolectricTestRunner.class) 41 public class VideoPreferenceControllerTest { 42 43 @Mock 44 private VideoPreference mPreference; 45 @Mock 46 private PreferenceScreen mScreen; 47 48 private VideoPreferenceController mController; 49 50 @Before setUp()51 public void setUp() { 52 MockitoAnnotations.initMocks(this); 53 mController = new VideoPreferenceController(RuntimeEnvironment.application, "test_pref"); 54 when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference); 55 } 56 57 @Test getAvailabilityStatus_isAvailableUnsearchable()58 public void getAvailabilityStatus_isAvailableUnsearchable() { 59 final VideoPreference videoPreference = mock(VideoPreference.class); 60 61 // Assign mock object to mVideoPreference in controller 62 ReflectionHelpers.setField(mController, "mVideoPreference", videoPreference); 63 doReturn(true).when(videoPreference).isAnimationAvailable(); 64 65 assertThat(mController.getAvailabilityStatus()).isEqualTo(AVAILABLE_UNSEARCHABLE); 66 } 67 68 @Test getAvailabilityStatus_isUnsupportedOnDevice()69 public void getAvailabilityStatus_isUnsupportedOnDevice() { 70 final VideoPreference videoPreference = mock(VideoPreference.class); 71 72 // Assign mock object to mVideoPreference in controller 73 ReflectionHelpers.setField(mController, "mVideoPreference", videoPreference); 74 doReturn(false).when(videoPreference).isAnimationAvailable(); 75 76 assertThat(mController.getAvailabilityStatus()).isEqualTo(UNSUPPORTED_ON_DEVICE); 77 } 78 79 @Test onStop_shouldCallOnViewInvisibleOnPrefernece()80 public void onStop_shouldCallOnViewInvisibleOnPrefernece() { 81 mController.displayPreference(mScreen); 82 83 mController.onStop(); 84 85 verify(mPreference).onViewInvisible(); 86 } 87 88 @Test onStart_shouldCallOnViewVisibleOnPrefernece()89 public void onStart_shouldCallOnViewVisibleOnPrefernece() { 90 mController.displayPreference(mScreen); 91 92 mController.onStart(); 93 94 verify(mPreference).onViewVisible(); 95 } 96 } 97