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 */
16package com.android.settings.security;
17
18import android.app.FragmentManager;
19import android.app.FragmentTransaction;
20import android.content.Context;
21import android.support.v7.preference.PreferenceScreen;
22import android.support.v14.preference.PreferenceFragment;
23
24import com.android.internal.widget.LockPatternUtils;
25import com.android.settings.OwnerInfoSettings;
26import com.android.settings.testutils.SettingsRobolectricTestRunner;
27import com.android.settings.TestConfig;
28import com.android.settingslib.RestrictedLockUtils.EnforcedAdmin;
29import com.android.settingslib.RestrictedPreference;
30
31import org.junit.Before;
32import org.junit.Test;
33import org.junit.runner.RunWith;
34import org.mockito.Mock;
35import org.mockito.MockitoAnnotations;
36import org.robolectric.annotation.Config;
37import org.robolectric.shadows.ShadowApplication;
38import org.robolectric.util.ReflectionHelpers;
39
40import static com.google.common.truth.Truth.assertThat;
41import static org.mockito.Answers.RETURNS_DEEP_STUBS;
42import static org.mockito.Matchers.any;
43import static org.mockito.Matchers.anyInt;
44import static org.mockito.Matchers.anyString;
45import static org.mockito.Mockito.doReturn;
46import static org.mockito.Matchers.eq;
47import static org.mockito.Mockito.mock;
48import static org.mockito.Mockito.spy;
49import static org.mockito.Mockito.verify;
50import static org.mockito.Mockito.when;
51
52@RunWith(SettingsRobolectricTestRunner.class)
53@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
54public class OwnerInfoPreferenceControllerTest {
55
56    @Mock(answer = RETURNS_DEEP_STUBS)
57    private PreferenceFragment mFragment;
58    @Mock
59    private PreferenceScreen mScreen;
60    @Mock
61    private FragmentManager mFragmentManager;
62    @Mock
63    private FragmentTransaction mFragmentTransaction;
64    @Mock
65    private RestrictedPreference mPreference;
66    @Mock
67    private LockPatternUtils mLockPatternUtils;
68
69    private Context mContext;
70    private OwnerInfoPreferenceController mController;
71
72    @Before
73    public void setUp() {
74        MockitoAnnotations.initMocks(this);
75        ShadowApplication shadowContext = ShadowApplication.getInstance();
76        mContext = spy(shadowContext.getApplicationContext());
77
78        when(mFragment.isAdded()).thenReturn(true);
79        when(mFragment.getPreferenceScreen()).thenReturn(mScreen);
80        when(mFragment.getPreferenceManager().getContext()).thenReturn(mContext);
81        when(mFragment.getFragmentManager()).thenReturn(mFragmentManager);
82        when(mFragmentManager.beginTransaction()).thenReturn(mFragmentTransaction);
83
84        mController = spy(new OwnerInfoPreferenceController(mContext, mFragment, null));
85        when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference);
86        ReflectionHelpers.setField(mController, "mLockPatternUtils", mLockPatternUtils);
87    }
88
89    @Test
90    public void isAvailable_shouldReturnTrue() {
91        assertThat(mController.isAvailable()).isTrue();
92    }
93
94    @Test
95    public void onResume_shouldUpdateEnableState() {
96        mController.onResume();
97
98        verify(mController).updateEnableState();
99    }
100
101    @Test
102    public void onResume_shouldUpdateSummary() {
103        mController.onResume();
104
105        verify(mController).updateSummary();
106    }
107
108    @Test
109    public void updateSummary_deviceOwnerInfoEnabled_shouldSetDeviceOwnerInfoSummary() {
110        final String deviceOwnerInfo = "Test Device Owner Info";
111        doReturn(true).when(mController).isDeviceOwnerInfoEnabled();
112        doReturn(deviceOwnerInfo).when(mController).getDeviceOwnerInfo();
113        mController.displayPreference(mScreen);
114
115        mController.updateSummary();
116
117        verify(mPreference).setSummary(deviceOwnerInfo);
118    }
119
120    @Test
121    public void updateSummary_ownerInfoEnabled_shouldSetOwnerInfoSummary() {
122        final String ownerInfo = "Test Owner Info";
123        doReturn(false).when(mController).isDeviceOwnerInfoEnabled();
124        doReturn(true).when(mController).isOwnerInfoEnabled();
125        doReturn(ownerInfo).when(mController).getOwnerInfo();
126        mController.displayPreference(mScreen);
127
128        mController.updateSummary();
129
130        verify(mPreference).setSummary(ownerInfo);
131    }
132
133    @Test
134    public void updateSummary_ownerInfoDisabled_shouldSetDefaultSummary() {
135        doReturn(false).when(mController).isDeviceOwnerInfoEnabled();
136        doReturn(false).when(mController).isOwnerInfoEnabled();
137        mController.displayPreference(mScreen);
138
139        mController.updateSummary();
140
141        verify(mPreference).setSummary(mContext.getString(
142            com.android.settings.R.string.owner_info_settings_summary));
143    }
144
145    @Test
146    public void updateEnableState_deviceOwnerInfoEnabled_shouldSetDisabledByAdmin() {
147        doReturn(true).when(mController).isDeviceOwnerInfoEnabled();
148        doReturn(mock(EnforcedAdmin.class)).when(mController).getDeviceOwner();
149        mController.displayPreference(mScreen);
150
151        mController.updateEnableState();
152
153        verify(mPreference).setDisabledByAdmin(any(EnforcedAdmin.class));
154    }
155
156    @Test
157    public void updateEnableState_lockScreenDisabled_shouldDisablePreference() {
158        doReturn(false).when(mController).isDeviceOwnerInfoEnabled();
159        doReturn(true).when(mLockPatternUtils).isLockScreenDisabled(anyInt());
160        mController.displayPreference(mScreen);
161
162        mController.updateEnableState();
163
164        verify(mPreference).setEnabled(false);
165    }
166
167    @Test
168    public void updateEnableState_lockScreenEnabled_shouldEnablePreference() {
169        doReturn(false).when(mController).isDeviceOwnerInfoEnabled();
170        doReturn(false).when(mLockPatternUtils).isLockScreenDisabled(anyInt());
171        mController.displayPreference(mScreen);
172
173        mController.updateEnableState();
174
175        verify(mPreference).setEnabled(true);
176    }
177
178    @Test
179    public void performClick_shouldLaunchOwnerInfoSettings() {
180        final ShadowApplication application = ShadowApplication.getInstance();
181        final RestrictedPreference preference =
182            new RestrictedPreference(application.getApplicationContext());
183        when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(preference);
184        doReturn(false).when(mController).isDeviceOwnerInfoEnabled();
185        doReturn(false).when(mLockPatternUtils).isLockScreenDisabled(anyInt());
186        mController.displayPreference(mScreen);
187        mController.updateEnableState();
188
189        preference.performClick();
190
191        verify(mFragment).getFragmentManager();
192        verify(mFragment.getFragmentManager().beginTransaction())
193            .add(any(OwnerInfoSettings.class), anyString());
194    }
195
196}