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 com.android.settings.SettingsRobolectricTestRunner; 20 import com.android.settings.TestConfig; 21 22 import org.junit.Before; 23 import org.junit.Test; 24 import org.junit.runner.RunWith; 25 import org.mockito.Mock; 26 import org.mockito.MockitoAnnotations; 27 import org.robolectric.RuntimeEnvironment; 28 import org.robolectric.annotation.Config; 29 import org.robolectric.annotation.Implementation; 30 import org.robolectric.annotation.Implements; 31 32 import static com.google.common.truth.Truth.assertThat; 33 34 import static org.mockito.Mockito.spy; 35 import static org.mockito.Mockito.verify; 36 import static org.mockito.Mockito.when; 37 38 import android.content.Context; 39 import android.content.Intent; 40 import android.support.v7.preference.Preference; 41 import android.support.v7.preference.PreferenceScreen; 42 43 @RunWith(SettingsRobolectricTestRunner.class) 44 @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION, 45 shadows = {BackupSettingsPreferenceControllerTest.ShadowBackupSettingsHelper.class}) 46 public class BackupSettingsPreferenceControllerTest { 47 private static final String BACKUP_SETTINGS = "backup_settings"; 48 private static final String MANUFACTURER_SETTINGS = "manufacturer_backup"; 49 50 private Context mContext; 51 52 @Mock 53 private BackupSettingsHelper mBackupHelper; 54 @Mock 55 private PreferenceScreen mScreen; 56 @Mock 57 private Preference mBackupPreference; 58 @Mock 59 private Preference mManufacturerPreference; 60 61 @Mock 62 private static Intent mBackupIntent; 63 64 private static String mBackupLabel = "Test Backup Label"; 65 private static String mBackupSummary = "Test Backup Summary"; 66 private static String mManufacturerLabel = "Test Manufacturer Label"; 67 68 @Mock 69 private static Intent mManufacturerIntent; 70 71 private BackupSettingsPreferenceController mController; 72 73 @Before setUp()74 public void setUp() throws Exception { 75 MockitoAnnotations.initMocks(this); 76 mContext = spy(RuntimeEnvironment.application.getApplicationContext()); 77 mController = new BackupSettingsPreferenceController(mContext); 78 } 79 80 @Test testDisplayPreference()81 public void testDisplayPreference() { 82 when(mScreen.findPreference(BACKUP_SETTINGS)).thenReturn(mBackupPreference); 83 when(mScreen.findPreference(MANUFACTURER_SETTINGS)).thenReturn(mManufacturerPreference); 84 85 mController.displayPreference(mScreen); 86 87 verify(mBackupPreference).setIntent(mBackupIntent); 88 verify(mBackupPreference).setTitle(mBackupLabel); 89 verify(mBackupPreference).setSummary(mBackupSummary); 90 verify(mManufacturerPreference).setIntent(mManufacturerIntent); 91 verify(mManufacturerPreference).setTitle(mManufacturerLabel); 92 } 93 94 @Test testIsAvailable_shouldAlwaysReturnTrue()95 public void testIsAvailable_shouldAlwaysReturnTrue() { 96 assertThat(mController.isAvailable()).isTrue(); 97 } 98 99 @Test getPreferenceKey_shouldReturnNull()100 public void getPreferenceKey_shouldReturnNull() { 101 assertThat(mController.getPreferenceKey()).isNull(); 102 } 103 104 @Implements(BackupSettingsHelper.class) 105 public static class ShadowBackupSettingsHelper { 106 107 @Implementation getIntentForBackupSettings()108 public Intent getIntentForBackupSettings() { 109 return mBackupIntent; 110 } 111 112 @Implementation getLabelForBackupSettings()113 public String getLabelForBackupSettings() { 114 return mBackupLabel; 115 } 116 117 @Implementation getSummaryForBackupSettings()118 public String getSummaryForBackupSettings() { 119 return mBackupSummary; 120 } 121 122 @Implementation getIntentProvidedByManufacturer()123 public Intent getIntentProvidedByManufacturer() { 124 return mManufacturerIntent; 125 } 126 127 @Implementation getLabelProvidedByManufacturer()128 public String getLabelProvidedByManufacturer() { 129 return mManufacturerLabel; 130 } 131 } 132 } 133