1 /* 2 * Copyright (C) 2021 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.enterprise; 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.Context; 25 26 import androidx.preference.Preference; 27 28 import org.junit.Before; 29 import org.junit.Test; 30 import org.junit.runner.RunWith; 31 import org.mockito.Answers; 32 import org.mockito.Mock; 33 import org.mockito.MockitoAnnotations; 34 import org.robolectric.RobolectricTestRunner; 35 36 @RunWith(RobolectricTestRunner.class) 37 public class FinancedPrivacyPreferenceControllerTest { 38 39 private static final String PREF_KEY_FINANCED_PRIVACY = "financed_privacy"; 40 41 @Mock(answer = Answers.RETURNS_DEEP_STUBS) 42 private Context mContext; 43 @Mock 44 private PrivacyPreferenceControllerHelper mPrivacyPreferenceControllerHelper; 45 private FinancedPrivacyPreferenceController mController; 46 47 @Before setUp()48 public void setUp() { 49 MockitoAnnotations.initMocks(this); 50 mController = new FinancedPrivacyPreferenceController( 51 mContext, mPrivacyPreferenceControllerHelper, PREF_KEY_FINANCED_PRIVACY); 52 } 53 54 @Test testUpdateState()55 public void testUpdateState() { 56 final Preference preference = new Preference(mContext, null, 0, 0); 57 58 mController.updateState(preference); 59 60 verify(mPrivacyPreferenceControllerHelper).updateState(preference); 61 } 62 63 @Test testIsAvailable_financedDevice_returnsTrue()64 public void testIsAvailable_financedDevice_returnsTrue() { 65 when(mPrivacyPreferenceControllerHelper.isFinancedDevice()).thenReturn(true); 66 67 assertThat(mController.isAvailable()).isTrue(); 68 } 69 70 @Test testIsAvailable_enterpriseDevice_returnsFalse()71 public void testIsAvailable_enterpriseDevice_returnsFalse() { 72 when(mPrivacyPreferenceControllerHelper.isFinancedDevice()).thenReturn(false); 73 74 assertThat(mController.isAvailable()).isFalse(); 75 } 76 77 @Test testHandlePreferenceTreeClick()78 public void testHandlePreferenceTreeClick() { 79 assertThat(mController.handlePreferenceTreeClick(new Preference(mContext, null, 0, 0))) 80 .isFalse(); 81 } 82 83 @Test getPreferenceKey_byDefault_returnsDefaultValue()84 public void getPreferenceKey_byDefault_returnsDefaultValue() { 85 assertThat(mController.getPreferenceKey()).isEqualTo(PREF_KEY_FINANCED_PRIVACY); 86 } 87 88 @Test getPreferenceKey_whenGivenValue_returnsGivenValue()89 public void getPreferenceKey_whenGivenValue_returnsGivenValue() { 90 mController = new FinancedPrivacyPreferenceController( 91 mContext, mPrivacyPreferenceControllerHelper, "key"); 92 93 assertThat(mController.getPreferenceKey()).isEqualTo("key"); 94 } 95 } 96