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