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