1 /* 2 * Copyright (C) 2017 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.development; 18 19 import static com.android.settings.development.HdcpCheckingPreferenceController 20 .HDCP_CHECKING_PROPERTY; 21 22 import static com.google.common.truth.Truth.assertThat; 23 24 import static org.mockito.Mockito.doReturn; 25 import static org.mockito.Mockito.spy; 26 import static org.mockito.Mockito.verify; 27 import static org.mockito.Mockito.when; 28 29 import android.content.Context; 30 import android.os.SystemProperties; 31 32 import androidx.preference.ListPreference; 33 import androidx.preference.PreferenceScreen; 34 35 import com.android.settings.R; 36 37 import org.junit.Before; 38 import org.junit.Test; 39 import org.junit.runner.RunWith; 40 import org.mockito.Mock; 41 import org.mockito.MockitoAnnotations; 42 import org.robolectric.RobolectricTestRunner; 43 import org.robolectric.RuntimeEnvironment; 44 45 @RunWith(RobolectricTestRunner.class) 46 public class HdcpCheckingPreferenceControllerTest { 47 48 private static final String USER_DEBUG = "userdebug"; 49 50 @Mock 51 private ListPreference mPreference; 52 @Mock 53 private PreferenceScreen mScreen; 54 55 private Context mContext; 56 private HdcpCheckingPreferenceController mController; 57 58 /** 59 * Array Values Key 60 * 61 * 0: Never Check 62 * 1: Check for DRM content only 63 * 2: Always Check 64 */ 65 private String[] mValues; 66 private String[] mSummaries; 67 68 @Before setup()69 public void setup() { 70 MockitoAnnotations.initMocks(this); 71 mContext = RuntimeEnvironment.application; 72 mValues = mContext.getResources().getStringArray(R.array.hdcp_checking_values); 73 mSummaries = mContext.getResources().getStringArray(R.array.hdcp_checking_summaries); 74 mController = new HdcpCheckingPreferenceController(mContext); 75 when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference); 76 mController.displayPreference(mScreen); 77 } 78 79 @Test isAvailable_isUserBuildType_shouldReturnFalse()80 public void isAvailable_isUserBuildType_shouldReturnFalse() { 81 mController = spy(mController); 82 doReturn(HdcpCheckingPreferenceController.USER_BUILD_TYPE).when(mController).getBuildType(); 83 84 assertThat(mController.isAvailable()).isFalse(); 85 } 86 87 @Test isAvailable_isUserDebugBuildType_shouldReturnTrue()88 public void isAvailable_isUserDebugBuildType_shouldReturnTrue() { 89 mController = spy(mController); 90 doReturn(USER_DEBUG).when(mController).getBuildType(); 91 92 assertThat(mController.isAvailable()).isTrue(); 93 } 94 95 @Test onPreferenceChange_setNeverCheckHdcp_shouldEnableNeverCheckHdcp()96 public void onPreferenceChange_setNeverCheckHdcp_shouldEnableNeverCheckHdcp() { 97 mController.onPreferenceChange(mPreference, mValues[0]); 98 99 assertThat(SystemProperties.get(HDCP_CHECKING_PROPERTY)).isEqualTo(mValues[0]); 100 } 101 102 @Test onPreferenceChange_setCheckDrm_shouldEnableCheckDrm()103 public void onPreferenceChange_setCheckDrm_shouldEnableCheckDrm() { 104 mController.onPreferenceChange(mPreference, mValues[1]); 105 106 assertThat(SystemProperties.get(HDCP_CHECKING_PROPERTY)).isEqualTo(mValues[1]); 107 } 108 109 @Test updateState_neverCheckHdcp_shouldEnableNeverCheckHdcp()110 public void updateState_neverCheckHdcp_shouldEnableNeverCheckHdcp() { 111 SystemProperties.set(HDCP_CHECKING_PROPERTY, mValues[0]); 112 113 mController.updateState(mPreference); 114 115 verify(mPreference).setValue(mValues[0]); 116 verify(mPreference).setSummary(mSummaries[0]); 117 } 118 119 @Test updateState_checkDrm_shouldEnableCheckDrm()120 public void updateState_checkDrm_shouldEnableCheckDrm() { 121 SystemProperties.set(HDCP_CHECKING_PROPERTY, mValues[1]); 122 123 mController.updateState(mPreference); 124 125 verify(mPreference).setValue(mValues[1]); 126 verify(mPreference).setSummary(mSummaries[1]); 127 } 128 129 @Test updateState_noValueSet_shouldEnableCheckDrmAsDefault()130 public void updateState_noValueSet_shouldEnableCheckDrmAsDefault() { 131 SystemProperties.set(HDCP_CHECKING_PROPERTY, null); 132 133 mController.updateState(mPreference); 134 135 verify(mPreference).setValue(mValues[1]); 136 verify(mPreference).setSummary(mSummaries[1]); 137 } 138 } 139