• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.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 EnterprisePrivacyPreferenceControllerTest {
38 
39     private static final String KEY_ENTERPRISE_PRIVACY = "enterprise_privacy";
40 
41     @Mock(answer = Answers.RETURNS_DEEP_STUBS)
42     private Context mContext;
43     @Mock
44     private PrivacyPreferenceControllerHelper mPrivacyPreferenceControllerHelper;
45     private EnterprisePrivacyPreferenceController mController;
46 
47     @Before
setUp()48     public void setUp() {
49         MockitoAnnotations.initMocks(this);
50         mController = new EnterprisePrivacyPreferenceController(
51                 mContext, mPrivacyPreferenceControllerHelper, KEY_ENTERPRISE_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_noDeviceOwner_returnsFalse()64     public void testIsAvailable_noDeviceOwner_returnsFalse() {
65         when(mPrivacyPreferenceControllerHelper.hasDeviceOwner()).thenReturn(false);
66 
67         assertThat(mController.isAvailable()).isFalse();
68     }
69 
70     @Test
testIsAvailable_deviceOwner_financedDevice_returnsFalse()71     public void testIsAvailable_deviceOwner_financedDevice_returnsFalse() {
72         when(mPrivacyPreferenceControllerHelper.hasDeviceOwner()).thenReturn(true);
73         when(mPrivacyPreferenceControllerHelper.isFinancedDevice()).thenReturn(true);
74 
75         assertThat(mController.isAvailable()).isFalse();
76     }
77 
78     @Test
testIsAvailable_deviceOwner_enterpriseDevice_returnsTrue()79     public void testIsAvailable_deviceOwner_enterpriseDevice_returnsTrue() {
80         when(mPrivacyPreferenceControllerHelper.hasDeviceOwner()).thenReturn(true);
81         when(mPrivacyPreferenceControllerHelper.isFinancedDevice()).thenReturn(false);
82 
83         assertThat(mController.isAvailable()).isTrue();
84     }
85 
86     @Test
testHandlePreferenceTreeClick()87     public void testHandlePreferenceTreeClick() {
88         assertThat(mController.handlePreferenceTreeClick(new Preference(mContext, null, 0, 0)))
89                 .isFalse();
90     }
91 
92     @Test
getPreferenceKey_byDefault_returnsDefaultValue()93     public void getPreferenceKey_byDefault_returnsDefaultValue() {
94         assertThat(mController.getPreferenceKey()).isEqualTo(KEY_ENTERPRISE_PRIVACY);
95     }
96 
97     @Test
getPreferenceKey_whenGivenValue_returnsGivenValue()98     public void getPreferenceKey_whenGivenValue_returnsGivenValue() {
99         mController = new EnterprisePrivacyPreferenceController(
100                 mContext, mPrivacyPreferenceControllerHelper, "key");
101 
102         assertThat(mController.getPreferenceKey()).isEqualTo("key");
103     }
104 }
105