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 */
16
17package com.android.settings.backup;
18
19import static com.google.common.truth.Truth.assertThat;
20import static org.mockito.Mockito.spy;
21import static org.mockito.Mockito.verify;
22import static org.mockito.Mockito.when;
23
24import android.app.backup.BackupManager;
25import android.content.Context;
26import android.os.UserManager;
27import android.support.v7.preference.Preference;
28
29import com.android.settings.R;
30import com.android.settings.testutils.SettingsRobolectricTestRunner;
31
32import org.junit.Before;
33import org.junit.Test;
34import org.junit.runner.RunWith;
35import org.mockito.Mock;
36import org.mockito.MockitoAnnotations;
37import org.robolectric.RuntimeEnvironment;
38import org.robolectric.annotation.Config;
39import org.robolectric.annotation.Implementation;
40import org.robolectric.annotation.Implements;
41
42@RunWith(SettingsRobolectricTestRunner.class)
43@Config(shadows = BackupSettingsActivityPreferenceControllerTest.ShadowBackupManager.class)
44public class BackupSettingsActivityPreferenceControllerTest {
45
46    private static final String KEY_BACKUP_SETTINGS = "backup_settings";
47
48    private Context mContext;
49    @Mock
50    private UserManager mUserManager;
51
52    @Mock
53    private Preference mBackupPreference;
54
55    private BackupSettingsActivityPreferenceController mController;
56
57    private static boolean mBackupEnabled;
58
59    @Before
60    public void setUp() {
61        MockitoAnnotations.initMocks(this);
62        mContext = spy(RuntimeEnvironment.application.getApplicationContext());
63        when(mContext.getSystemService(Context.USER_SERVICE)).thenReturn(mUserManager);
64
65        mController = new BackupSettingsActivityPreferenceController(mContext);
66    }
67
68    @Test
69    public void updateState_backupOn() {
70        mBackupEnabled = true;
71
72        mController.updateState(mBackupPreference);
73        String summaryString = mContext.getString(R.string.accessibility_feature_state_on);
74        verify(mBackupPreference).setSummary(summaryString);
75    }
76
77    @Test
78    public void updateState_backupOff() {
79        mBackupEnabled = false;
80
81        mController.updateState(mBackupPreference);
82        String summaryString = mContext.getString(R.string.accessibility_feature_state_off);
83        verify(mBackupPreference).setSummary(summaryString);
84    }
85
86    @Test
87    public void isAvailable_systemUser() {
88        when(mUserManager.isAdminUser()).thenReturn(true);
89
90        assertThat(mController.isAvailable()).isTrue();
91    }
92
93    @Test
94    public void isAvailable_nonSystemUser() {
95        when(mUserManager.isAdminUser()).thenReturn(false);
96
97        assertThat(mController.isAvailable()).isFalse();
98    }
99
100    @Test
101    public void getPreferenceKey() {
102        assertThat(mController.getPreferenceKey()).isEqualTo(KEY_BACKUP_SETTINGS);
103    }
104
105    @Implements(BackupManager.class)
106    public static class ShadowBackupManager {
107
108        @Implementation
109        public boolean isBackupEnabled() {
110            return mBackupEnabled;
111        }
112    }
113}
114