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
17package com.android.settings.security;
18
19import static com.google.common.truth.Truth.assertThat;
20import static org.mockito.Mockito.verify;
21import static org.mockito.Mockito.when;
22
23import android.content.ContentResolver;
24import android.content.Context;
25import android.os.UserHandle;
26import android.provider.Settings;
27import android.support.v7.preference.Preference;
28import android.support.v7.preference.PreferenceScreen;
29
30import com.android.internal.widget.LockPatternUtils;
31import com.android.settings.testutils.FakeFeatureFactory;
32import com.android.settings.testutils.SettingsRobolectricTestRunner;
33
34import org.junit.Before;
35import org.junit.Test;
36import org.junit.runner.RunWith;
37import org.mockito.Mock;
38import org.mockito.MockitoAnnotations;
39import org.robolectric.RuntimeEnvironment;
40import org.robolectric.annotation.Config;
41
42@RunWith(SettingsRobolectricTestRunner.class)
43public class ShowPasswordPreferenceControllerTest {
44
45    @Mock
46    private LockPatternUtils mLockPatternUtils;
47    @Mock
48    private PreferenceScreen mScreen;
49
50    private FakeFeatureFactory mFeatureFactory;
51    private Context mContext;
52    private ShowPasswordPreferenceController mController;
53    private Preference mPreference;
54
55    @Before
56    public void setUp() {
57        MockitoAnnotations.initMocks(this);
58        mContext = RuntimeEnvironment.application;
59        mFeatureFactory = FakeFeatureFactory.setupForTest();
60        when(mFeatureFactory.securityFeatureProvider.getLockPatternUtils(mContext))
61                .thenReturn(mLockPatternUtils);
62        mController = new ShowPasswordPreferenceController(mContext);
63        mPreference = new Preference(mContext);
64        mPreference.setKey(mController.getPreferenceKey());
65        when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference);
66    }
67
68    @Test
69    public void isAvailable_byDefault_isTrue() {
70        assertThat(mController.isAvailable()).isTrue();
71    }
72
73    @Test
74    @Config(qualifiers = "mcc999")
75    public void isAvailable_whenNotVisible_isFalse() {
76        assertThat(mController.isAvailable()).isFalse();
77    }
78
79    @Test
80    public void isChecked_settingIsOff_false() {
81        final ContentResolver contentResolver = mContext.getContentResolver();
82        Settings.System.putInt(contentResolver, Settings.System.TEXT_SHOW_PASSWORD, 0);
83
84        assertThat(mController.isChecked()).isFalse();
85    }
86
87    @Test
88    public void isChecked_settingIsOn_true() {
89        final ContentResolver contentResolver = mContext.getContentResolver();
90        Settings.System.putInt(contentResolver, Settings.System.TEXT_SHOW_PASSWORD, 1);
91        assertThat(mController.isChecked()).isTrue();
92    }
93
94    @Test
95    public void changePref_turnOn_shouldChangeSettingTo1() {
96        mController.onPreferenceChange(mPreference, true);
97
98        assertThat(mController.isChecked()).isTrue();
99        verify(mLockPatternUtils).setVisiblePasswordEnabled(true, UserHandle.myUserId());
100    }
101
102    @Test
103    public void changePref_turnOff_shouldChangeSettingTo0() {
104        mController.onPreferenceChange(mPreference, false);
105
106        assertThat(mController.isChecked()).isFalse();
107        verify(mLockPatternUtils).setVisiblePasswordEnabled(false, UserHandle.myUserId());
108    }
109}
110