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