• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.Mockito.doReturn;
22 import static org.mockito.Mockito.spy;
23 
24 import android.content.Context;
25 
26 import androidx.preference.PreferenceManager;
27 import androidx.preference.PreferenceScreen;
28 
29 import com.android.settings.core.BasePreferenceController;
30 import com.android.settingslib.widget.FooterPreferenceMixinCompat;
31 
32 import org.junit.Before;
33 import org.junit.Test;
34 import org.junit.runner.RunWith;
35 import org.mockito.MockitoAnnotations;
36 import org.robolectric.RobolectricTestRunner;
37 import org.robolectric.RuntimeEnvironment;
38 
39 @RunWith(RobolectricTestRunner.class)
40 public class EnterpriseDisclosurePreferenceControllerTest {
41 
42     private static final String TEST_DISCLOSURE = "This is a test disclosure.";
43 
44     private ChooseAccountFragment mFragment;
45     private Context mContext;
46     private EnterpriseDisclosurePreferenceController mController;
47     private FooterPreferenceMixinCompat mFooterPreferenceMixin;
48     private PreferenceManager mPreferenceManager;
49     private PreferenceScreen mPreferenceScreen;
50 
51     @Before
setUp()52     public void setUp() {
53         MockitoAnnotations.initMocks(this);
54         mContext = RuntimeEnvironment.application;
55         mController = spy(new EnterpriseDisclosurePreferenceController(mContext));
56         mFragment = spy(new ChooseAccountFragment());
57         mFooterPreferenceMixin = new FooterPreferenceMixinCompat(mFragment,
58                 mFragment.getSettingsLifecycle());
59         mPreferenceManager = new PreferenceManager(mContext);
60         mPreferenceScreen = mPreferenceManager.createPreferenceScreen(mContext);
61     }
62 
63     @Test
getAvailabilityStatus_hasDisclosure_shouldBeAvailable()64     public void getAvailabilityStatus_hasDisclosure_shouldBeAvailable() {
65         doReturn(TEST_DISCLOSURE).when(mController).getDisclosure();
66 
67         assertThat(mController.getAvailabilityStatus()).isEqualTo(
68                 BasePreferenceController.AVAILABLE);
69     }
70 
71     @Test
getAvailabilityStatus_noDisclosure_shouldBeDisabled()72     public void getAvailabilityStatus_noDisclosure_shouldBeDisabled() {
73         doReturn(null).when(mController).getDisclosure();
74 
75         assertThat(mController.getAvailabilityStatus()).isEqualTo(
76                 BasePreferenceController.UNSUPPORTED_ON_DEVICE);
77     }
78 
79     @Test
displayPreference_hasDisclosure_shouldSetTitle()80     public void displayPreference_hasDisclosure_shouldSetTitle() {
81         doReturn(TEST_DISCLOSURE).when(mController).getDisclosure();
82         doReturn(mPreferenceScreen).when(mFragment).getPreferenceScreen();
83         doReturn(mPreferenceManager).when(mFragment).getPreferenceManager();
84 
85         mController.setFooterPreferenceMixin(mFooterPreferenceMixin);
86         mController.displayPreference(mPreferenceScreen);
87 
88         assertThat(mPreferenceScreen.getPreferenceCount()).isEqualTo(1);
89         assertThat(mPreferenceScreen.getPreference(0).getTitle()).isEqualTo(TEST_DISCLOSURE);
90     }
91 
92     @Test
displayPreference_noDisclosure_shouldBeInvisible()93     public void displayPreference_noDisclosure_shouldBeInvisible() {
94         doReturn(null).when(mController).getDisclosure();
95 
96         mController.displayPreference(mPreferenceScreen);
97 
98         assertThat(mPreferenceScreen.getPreferenceCount()).isEqualTo(0);
99     }
100 }
101