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