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.accounts;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 
21 import static org.mockito.ArgumentMatchers.any;
22 import static org.mockito.ArgumentMatchers.anyString;
23 import static org.mockito.Mockito.doReturn;
24 import static org.mockito.Mockito.never;
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 
31 import androidx.preference.PreferenceScreen;
32 
33 import com.android.settings.core.BasePreferenceController;
34 import com.android.settingslib.widget.FooterPreference;
35 
36 import org.junit.Before;
37 import org.junit.Test;
38 import org.junit.runner.RunWith;
39 import org.mockito.Mock;
40 import org.mockito.MockitoAnnotations;
41 import org.robolectric.RobolectricTestRunner;
42 import org.robolectric.RuntimeEnvironment;
43 
44 @RunWith(RobolectricTestRunner.class)
45 public class EnterpriseDisclosurePreferenceControllerTest {
46     private static final String TEST_DISCLOSURE = "This is a test disclosure.";
47 
48     private Context mContext;
49     private EnterpriseDisclosurePreferenceController mController;
50     private FooterPreference mPreference;
51 
52     @Mock
53     private PreferenceScreen mPreferenceScreen;
54 
55     @Before
setUp()56     public void setUp() {
57         MockitoAnnotations.initMocks(this);
58         mContext = RuntimeEnvironment.application;
59         mController = spy(new EnterpriseDisclosurePreferenceController(mContext, "my_key"));
60         mPreference = spy(new FooterPreference(mContext));
61         when(mPreferenceScreen.findPreference(anyString())).thenReturn(mPreference);
62     }
63 
64     @Test
getAvailabilityStatus_hasDisclosure_shouldBeAvailable()65     public void getAvailabilityStatus_hasDisclosure_shouldBeAvailable() {
66         doReturn(TEST_DISCLOSURE).when(mController).getDisclosure();
67 
68         assertThat(mController.getAvailabilityStatus()).isEqualTo(
69                 BasePreferenceController.AVAILABLE);
70     }
71 
72     @Test
getAvailabilityStatus_noDisclosure_shouldBeDisabled()73     public void getAvailabilityStatus_noDisclosure_shouldBeDisabled() {
74         doReturn(null).when(mController).getDisclosure();
75 
76         assertThat(mController.getAvailabilityStatus()).isEqualTo(
77                 BasePreferenceController.UNSUPPORTED_ON_DEVICE);
78     }
79 
80     @Test
displayPreference_hasDisclosure_shouldSetTitle()81     public void displayPreference_hasDisclosure_shouldSetTitle() {
82         doReturn(TEST_DISCLOSURE).when(mController).getDisclosure();
83 
84         mController.displayPreference(mPreferenceScreen);
85 
86         assertThat(mPreference.getTitle()).isEqualTo(TEST_DISCLOSURE);
87     }
88 
89     @Test
displayPreference_noDisclosure_shouldBeInvisible()90     public void displayPreference_noDisclosure_shouldBeInvisible() {
91         doReturn(null).when(mController).getDisclosure();
92 
93         mController.displayPreference(mPreferenceScreen);
94 
95         verify(mPreference, never()).setTitle(any());
96     }
97 }
98