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