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.Mockito.doReturn;
23 import static org.mockito.Mockito.never;
24 import static org.mockito.Mockito.spy;
25 import static org.mockito.Mockito.verify;
26 
27 import android.content.Context;
28 
29 import androidx.preference.Preference;
30 
31 import com.android.settings.core.BasePreferenceController;
32 
33 import org.junit.Before;
34 import org.junit.Test;
35 import org.junit.runner.RunWith;
36 import org.robolectric.RobolectricTestRunner;
37 import org.robolectric.RuntimeEnvironment;
38 
39 @RunWith(RobolectricTestRunner.class)
40 public class EnterpriseDisclosurePreferenceControllerTest {
41     private static final String TEST_DISCLOSURE = "This is a test disclosure.";
42 
43     private Context mContext;
44     private EnterpriseDisclosurePreferenceController mController;
45     private Preference mPreference;
46 
47     @Before
setUp()48     public void setUp() {
49         mContext = RuntimeEnvironment.application;
50         mController = spy(new EnterpriseDisclosurePreferenceController(mContext, "my_key"));
51         mPreference = spy(new Preference(mContext));
52     }
53 
54     @Test
getAvailabilityStatus_hasDisclosure_shouldBeAvailable()55     public void getAvailabilityStatus_hasDisclosure_shouldBeAvailable() {
56         doReturn(TEST_DISCLOSURE).when(mController).getDisclosure();
57 
58         assertThat(mController.getAvailabilityStatus()).isEqualTo(
59                 BasePreferenceController.AVAILABLE);
60     }
61 
62     @Test
getAvailabilityStatus_noDisclosure_shouldBeDisabled()63     public void getAvailabilityStatus_noDisclosure_shouldBeDisabled() {
64         doReturn(null).when(mController).getDisclosure();
65 
66         assertThat(mController.getAvailabilityStatus()).isEqualTo(
67                 BasePreferenceController.UNSUPPORTED_ON_DEVICE);
68     }
69 
70     @Test
updateState_hasDisclosure_shouldSetTitle()71     public void updateState_hasDisclosure_shouldSetTitle() {
72         doReturn(TEST_DISCLOSURE).when(mController).getDisclosure();
73 
74         mController.updateState(mPreference);
75 
76         assertThat(mPreference.getTitle()).isEqualTo(TEST_DISCLOSURE);
77     }
78 
79     @Test
updateState_noDisclosure_shouldBeInvisible()80     public void updateState_noDisclosure_shouldBeInvisible() {
81         doReturn(null).when(mController).getDisclosure();
82 
83         mController.updateState(mPreference);
84 
85         verify(mPreference, never()).setTitle(any());
86     }
87 }
88