Home | History | Annotate | Download | only in accounts
      1 /*
      2  * Copyright (C) 2016 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 package com.android.settings.accounts;
     17 
     18 import android.app.Fragment;
     19 import android.content.Context;
     20 import android.content.DialogInterface;
     21 import android.content.pm.UserInfo;
     22 import android.os.UserManager;
     23 import android.support.v7.preference.Preference;
     24 import android.support.v7.preference.PreferenceScreen;
     25 import android.support.v14.preference.SwitchPreference;
     26 import com.android.settings.testutils.SettingsRobolectricTestRunner;
     27 import com.android.settings.TestConfig;
     28 
     29 import java.util.ArrayList;
     30 import java.util.List;
     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.annotation.Config;
     37 import org.robolectric.shadows.ShadowApplication;
     38 
     39 import static com.google.common.truth.Truth.assertThat;
     40 import static org.mockito.Answers.RETURNS_DEEP_STUBS;
     41 import static org.mockito.Matchers.any;
     42 import static org.mockito.Matchers.anyInt;
     43 import static org.mockito.Mockito.mock;
     44 import static org.mockito.Mockito.never;
     45 import static org.mockito.Mockito.verify;
     46 import static org.mockito.Mockito.when;
     47 
     48 @RunWith(SettingsRobolectricTestRunner.class)
     49 @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
     50 public class AutoSyncDataPreferenceControllerTest {
     51 
     52     @Mock(answer = RETURNS_DEEP_STUBS)
     53     private PreferenceScreen mScreen;
     54     @Mock(answer = RETURNS_DEEP_STUBS)
     55     private UserManager mUserManager;
     56     @Mock(answer = RETURNS_DEEP_STUBS)
     57     private Fragment mFragment;
     58     @Mock
     59     private Preference mPreference;
     60 
     61     private Context mContext;
     62     private AutoSyncDataPreferenceController mController;
     63     private AutoSyncDataPreferenceController.ConfirmAutoSyncChangeFragment mConfirmSyncFragment;
     64 
     65     @Before
     66     public void setUp() {
     67         MockitoAnnotations.initMocks(this);
     68         ShadowApplication shadowContext = ShadowApplication.getInstance();
     69         shadowContext.setSystemService(Context.USER_SERVICE, mUserManager);
     70         mContext = shadowContext.getApplicationContext();
     71         mController = new AutoSyncDataPreferenceController(mContext, mFragment);
     72         mConfirmSyncFragment = new AutoSyncDataPreferenceController.ConfirmAutoSyncChangeFragment();
     73         mConfirmSyncFragment.setTargetFragment(mFragment, 0);
     74         when(mScreen.getPreferenceCount()).thenReturn(1);
     75         when(mScreen.getPreference(0)).thenReturn(mPreference);
     76         when(mPreference.getKey()).thenReturn(mController.getPreferenceKey());
     77     }
     78 
     79     @Test
     80     public void displayPref_managedProfile_shouldNotDisplay() {
     81         when(mUserManager.isManagedProfile()).thenReturn(true);
     82 
     83         mController.displayPreference(mScreen);
     84 
     85         verify(mScreen).removePreference(any(Preference.class));
     86     }
     87 
     88     @Test
     89     public void displayPref_linkedUser_shouldDisplay() {
     90         when(mUserManager.isManagedProfile()).thenReturn(false);
     91         when(mUserManager.isLinkedUser()).thenReturn(true);
     92 
     93         mController.displayPreference(mScreen);
     94 
     95         verify(mScreen, never()).removePreference(any(Preference.class));
     96     }
     97 
     98     @Test
     99     public void displayPref_oneProfile_shouldDisplay() {
    100         List<UserInfo> infos = new ArrayList<>();
    101         infos.add(new UserInfo(1, "user 1", 0));
    102         when(mUserManager.isManagedProfile()).thenReturn(false);
    103         when(mUserManager.isLinkedUser()).thenReturn(false);
    104         when(mUserManager.getProfiles(anyInt())).thenReturn(infos);
    105 
    106         mController.displayPreference(mScreen);
    107 
    108         verify(mScreen, never()).removePreference(any(Preference.class));
    109     }
    110 
    111     @Test
    112     public void displayPref_moreThanOneProfile_shouldNotDisplay() {
    113         List<UserInfo> infos = new ArrayList<>();
    114         infos.add(new UserInfo(1, "user 1", 0));
    115         infos.add(new UserInfo(2, "user 2", 0));
    116         when(mUserManager.isManagedProfile()).thenReturn(false);
    117         when(mUserManager.isLinkedUser()).thenReturn(false);
    118         when(mUserManager.getProfiles(anyInt())).thenReturn(infos);
    119 
    120         mController.displayPreference(mScreen);
    121 
    122         verify(mScreen).removePreference(any(Preference.class));
    123     }
    124 
    125     @Test
    126     public void autoSyncData_shouldNotBeSetOnCancel() {
    127         final ShadowApplication application = ShadowApplication.getInstance();
    128         final Context context = application.getApplicationContext();
    129         final SwitchPreference preference = new SwitchPreference(context);
    130         preference.setChecked(false);
    131         mController = new AutoSyncDataPreferenceController(context, mFragment);
    132         mConfirmSyncFragment.mPreference = preference;
    133         mConfirmSyncFragment.mEnabling = true;
    134 
    135         mConfirmSyncFragment.onClick(null, DialogInterface.BUTTON_NEGATIVE);
    136         assertThat(preference.isChecked()).isFalse();
    137     }
    138 
    139 }
    140