Merge "Add Auto sync account data to account dashboard."

This commit is contained in:
Doris Ling
2016-11-01 20:09:50 +00:00
committed by Android (Google) Code Review
10 changed files with 528 additions and 98 deletions

View File

@@ -7939,4 +7939,11 @@
<!-- Configure section header [CHAR LIMIT=30] -->
<string name="configure_section_header">Configure</string>
<!-- Switch label to enable auto sync account [CHAR LIMIT=30] -->
<string name="auto_sync_account_title">Auto sync account data</string>
<!-- Switch label to enable auto sync personal account [CHAR LIMIT=30] -->
<string name="auto_sync_personal_account_title">Auto sync personal account data</string>
<!-- Switch label to enable auto sync work account [CHAR LIMIT=30] -->
<string name="auto_sync_work_account_title">Auto sync work account data</string>
</resources>

View File

@@ -32,12 +32,24 @@
android:key="account_configuration_header"
android:title="@string/configure_section_header">
<com.android.settingslib.RestrictedSwitchPreference
android:key="add_users_when_locked"
android:title="@string/user_add_on_lockscreen_menu"
android:summary="@string/user_add_on_lockscreen_menu_summary"
settings:useAdditionalSummary="true" />
</PreferenceCategory>
<SwitchPreference
android:key="auto_sync_account_data"
android:title="@string/auto_sync_account_title" />
<SwitchPreference
android:key="auto_sync_work_account_data"
android:title="@string/account_settings_menu_auto_sync_work" />
<SwitchPreference
android:key="auto_sync_personal_account_data"
android:title="@string/account_settings_menu_auto_sync_personal" />
<com.android.settingslib.RestrictedSwitchPreference
android:key="add_users_when_locked"
android:title="@string/user_add_on_lockscreen_menu"
android:summary="@string/user_add_on_lockscreen_menu_summary"
settings:useAdditionalSummary="true" />
</PreferenceScreen>

View File

@@ -20,9 +20,7 @@ package com.android.settings.accounts;
import android.accounts.Account;
import android.accounts.AccountManager;
import android.app.ActivityManager;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.BroadcastReceiver;
import android.content.ContentResolver;
import android.content.Context;
@@ -107,7 +105,7 @@ public class AccountSettings extends SettingsPreferenceFragment
/**
* Holds data related to the accounts belonging to one profile.
*/
private static class ProfileData {
public static class ProfileData {
/**
* The preference that displays the accounts.
*/
@@ -619,8 +617,8 @@ public class AccountSettings extends SettingsPreferenceFragment
if (ActivityManager.isUserAMonkey()) {
Log.d(TAG, "ignoring monkey's attempt to flip sync state");
} else {
ConfirmAutoSyncChangeFragment.show(AccountSettings.this, !item.isChecked(),
mUserHandle);
AutoSyncDataPreferenceController.ConfirmAutoSyncChangeFragment.show(
AccountSettings.this, !item.isChecked(), mUserHandle, /*preference*/null);
}
return true;
}
@@ -657,67 +655,6 @@ public class AccountSettings extends SettingsPreferenceFragment
}
}
/**
* Dialog to inform user about changing auto-sync setting
*/
public static class ConfirmAutoSyncChangeFragment extends InstrumentedDialogFragment {
private static final String SAVE_ENABLING = "enabling";
private static final String SAVE_USER_HANDLE = "userHandle";
private boolean mEnabling;
private UserHandle mUserHandle;
public static void show(AccountSettings parent, boolean enabling, UserHandle userHandle) {
if (!parent.isAdded()) return;
final ConfirmAutoSyncChangeFragment dialog = new ConfirmAutoSyncChangeFragment();
dialog.mEnabling = enabling;
dialog.mUserHandle = userHandle;
dialog.setTargetFragment(parent, 0);
dialog.show(parent.getFragmentManager(), TAG_CONFIRM_AUTO_SYNC_CHANGE);
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final Context context = getActivity();
if (savedInstanceState != null) {
mEnabling = savedInstanceState.getBoolean(SAVE_ENABLING);
mUserHandle = (UserHandle) savedInstanceState.getParcelable(SAVE_USER_HANDLE);
}
final AlertDialog.Builder builder = new AlertDialog.Builder(context);
if (!mEnabling) {
builder.setTitle(R.string.data_usage_auto_sync_off_dialog_title);
builder.setMessage(R.string.data_usage_auto_sync_off_dialog);
} else {
builder.setTitle(R.string.data_usage_auto_sync_on_dialog_title);
builder.setMessage(R.string.data_usage_auto_sync_on_dialog);
}
builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
ContentResolver.setMasterSyncAutomaticallyAsUser(mEnabling,
mUserHandle.getIdentifier());
}
});
builder.setNegativeButton(android.R.string.cancel, null);
return builder.create();
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putBoolean(SAVE_ENABLING, mEnabling);
outState.putParcelable(SAVE_USER_HANDLE, mUserHandle);
}
@Override
public int getMetricsCategory() {
return MetricsEvent.DIALOG_CONFIRM_AUTO_SYNC_CHANGE;
}
}
public static final SearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
new BaseSearchIndexProvider() {
@Override

View File

@@ -0,0 +1,161 @@
/*
* Copyright (C) 2016 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.accounts;
import android.app.ActivityManager;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.Fragment;
import android.content.ContentResolver;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Process;
import android.os.UserHandle;
import android.os.UserManager;
import android.support.v14.preference.SwitchPreference;
import android.support.v7.preference.Preference;
import android.util.Log;
import com.android.internal.logging.MetricsProto;
import com.android.settings.R;
import com.android.settings.core.PreferenceController;
import com.android.settings.core.instrumentation.InstrumentedDialogFragment;
public class AutoSyncDataPreferenceController extends PreferenceController {
private static final String TAG = "AutoSyncDataController";
private static final String TAG_CONFIRM_AUTO_SYNC_CHANGE = "confirmAutoSyncChange";
private static final String KEY_AUTO_SYNC_ACCOUNT = "auto_sync_account_data";
protected UserManager mUserManager;
private UserHandle mUserHandle;
private Fragment mParentFragment;
public AutoSyncDataPreferenceController(Context context, Fragment parent) {
super(context);
mUserManager = (UserManager) context.getSystemService(Context.USER_SERVICE);
mParentFragment = parent;
mUserHandle = Process.myUserHandle();
}
@Override
public void updateState(Preference preference) {
SwitchPreference switchPreference = (SwitchPreference) preference;
switchPreference.setChecked(ContentResolver.getMasterSyncAutomaticallyAsUser(
mUserHandle.getIdentifier()));
}
@Override
public boolean handlePreferenceTreeClick(Preference preference) {
if (getPreferenceKey().equals(preference.getKey())) {
SwitchPreference switchPreference = (SwitchPreference) preference;
boolean checked = switchPreference.isChecked();
switchPreference.setChecked(!checked);
if (ActivityManager.isUserAMonkey()) {
Log.d(TAG, "ignoring monkey's attempt to flip sync state");
} else {
ConfirmAutoSyncChangeFragment.show(mParentFragment, checked, mUserHandle,
switchPreference);
}
return true;
}
return false;
}
@Override
public boolean isAvailable() {
return !mUserManager.isManagedProfile()
&& (mUserManager.isLinkedUser()
|| mUserManager.getProfiles(UserHandle.myUserId()).size() == 1);
}
@Override
public String getPreferenceKey() {
return KEY_AUTO_SYNC_ACCOUNT;
}
/**
* Dialog to inform user about changing auto-sync setting
*/
public static class ConfirmAutoSyncChangeFragment extends InstrumentedDialogFragment implements
DialogInterface.OnClickListener {
private static final String SAVE_ENABLING = "enabling";
private static final String SAVE_USER_HANDLE = "userHandle";
boolean mEnabling;
UserHandle mUserHandle;
SwitchPreference mPreference;
public static void show(Fragment parent, boolean enabling, UserHandle userHandle,
SwitchPreference preference) {
if (!parent.isAdded()) return;
final ConfirmAutoSyncChangeFragment dialog = new ConfirmAutoSyncChangeFragment();
dialog.mEnabling = enabling;
dialog.mUserHandle = userHandle;
dialog.setTargetFragment(parent, 0);
dialog.mPreference = preference;
dialog.show(parent.getFragmentManager(), TAG_CONFIRM_AUTO_SYNC_CHANGE);
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final Context context = getActivity();
if (savedInstanceState != null) {
mEnabling = savedInstanceState.getBoolean(SAVE_ENABLING);
mUserHandle = (UserHandle) savedInstanceState.getParcelable(SAVE_USER_HANDLE);
}
final AlertDialog.Builder builder = new AlertDialog.Builder(context);
if (!mEnabling) {
builder.setTitle(R.string.data_usage_auto_sync_off_dialog_title);
builder.setMessage(R.string.data_usage_auto_sync_off_dialog);
} else {
builder.setTitle(R.string.data_usage_auto_sync_on_dialog_title);
builder.setMessage(R.string.data_usage_auto_sync_on_dialog);
}
builder.setPositiveButton(android.R.string.ok, this);
builder.setNegativeButton(android.R.string.cancel, null);
return builder.create();
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putBoolean(SAVE_ENABLING, mEnabling);
outState.putParcelable(SAVE_USER_HANDLE, mUserHandle);
}
@Override
public int getMetricsCategory() {
return MetricsProto.MetricsEvent.DIALOG_CONFIRM_AUTO_SYNC_CHANGE;
}
@Override
public void onClick(DialogInterface dialog, int which) {
if (which == DialogInterface.BUTTON_POSITIVE) {
ContentResolver.setMasterSyncAutomaticallyAsUser(mEnabling,
mUserHandle.getIdentifier());
if (mPreference != null) {
mPreference.setChecked(mEnabling);
}
}
}
}
}

View File

@@ -0,0 +1,42 @@
/*
* Copyright (C) 2016 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.accounts;
import android.app.Fragment;
import android.content.Context;
import android.os.UserHandle;
public class AutoSyncPersonalDataPreferenceController extends AutoSyncDataPreferenceController {
private static final String TAG = "AutoSyncPersonalData";
private static final String KEY_AUTO_SYNC_PERSONAL_ACCOUNT = "auto_sync_personal_account_data";
public AutoSyncPersonalDataPreferenceController(Context context, Fragment parent) {
super(context, parent);
}
@Override
public boolean isAvailable() {
return !mUserManager.isManagedProfile() && !mUserManager.isLinkedUser()
&& mUserManager.getProfiles(UserHandle.myUserId()).size() > 1;
}
@Override
public String getPreferenceKey() {
return KEY_AUTO_SYNC_PERSONAL_ACCOUNT;
}
}

View File

@@ -0,0 +1,35 @@
/*
* Copyright (C) 2016 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.accounts;
import android.app.Fragment;
import android.content.Context;
public class AutoSyncWorkDataPreferenceController extends AutoSyncPersonalDataPreferenceController {
private static final String TAG = "AutoSyncWorkData";
private static final String KEY_AUTO_SYNC_WORK_ACCOUNT = "auto_sync_work_account_data";
public AutoSyncWorkDataPreferenceController(Context context, Fragment parent) {
super(context, parent);
}
@Override
public String getPreferenceKey() {
return KEY_AUTO_SYNC_WORK_ACCOUNT;
}
}

View File

@@ -58,6 +58,9 @@ public class UserAndAccountDashboardFragment extends DashboardFragment {
new AddUserWhenLockedPreferenceController(context);
controllers.add(addUserWhenLockedPrefController);
getLifecycle().addObserver(addUserWhenLockedPrefController);
controllers.add(new AutoSyncDataPreferenceController(context, this));
controllers.add(new AutoSyncPersonalDataPreferenceController(context, this));
controllers.add(new AutoSyncWorkDataPreferenceController(context, this));
return controllers;
}

View File

@@ -63,6 +63,8 @@ import com.android.settings.R;
import com.android.settings.SettingsActivity;
import com.android.settings.SettingsPreferenceFragment;
import com.android.settings.Utils;
import com.android.settings.accounts.AddUserWhenLockedPreferenceController;
import com.android.settings.accounts.EmergencyInfoPreferenceController;
import com.android.settings.dashboard.SummaryLoader;
import com.android.settings.search.BaseSearchIndexProvider;
import com.android.settings.search.Indexable;
@@ -88,7 +90,6 @@ import static com.android.settingslib.RestrictedLockUtils.EnforcedAdmin;
*/
public class UserSettings extends SettingsPreferenceFragment
implements OnPreferenceClickListener, OnClickListener, DialogInterface.OnDismissListener,
Preference.OnPreferenceChangeListener,
EditUserInfoController.OnContentChangedCallback, Indexable {
private static final String TAG = "UserSettings";
@@ -136,7 +137,6 @@ public class UserSettings extends SettingsPreferenceFragment
private UserPreference mMePreference;
private DimmableIconPreference mAddUser;
private PreferenceGroup mLockScreenSettings;
private RestrictedSwitchPreference mAddUserWhenLocked;
private Preference mEmergencyInfoPreference;
private int mRemovingUserId = -1;
private int mAddedUserId = 0;
@@ -150,6 +150,8 @@ public class UserSettings extends SettingsPreferenceFragment
private EditUserInfoController mEditUserInfoController =
new EditUserInfoController();
private EmergencyInfoPreferenceController mEnergencyInfoController;
private AddUserWhenLockedPreferenceController mAddUserWhenLockedController;
// A place to cache the generated default avatar
private Drawable mDefaultIconDrawable;
@@ -234,8 +236,9 @@ public class UserSettings extends SettingsPreferenceFragment
}
}
mLockScreenSettings = (PreferenceGroup) findPreference("lock_screen_settings");
mAddUserWhenLocked = (RestrictedSwitchPreference) findPreference("add_users_when_locked");
mEmergencyInfoPreference = findPreference(KEY_EMERGENCY_INFO);
mEnergencyInfoController = new EmergencyInfoPreferenceController(context);
mAddUserWhenLockedController = new AddUserWhenLockedPreferenceController(context);
setHasOptionsMenu(true);
IntentFilter filter = new IntentFilter(Intent.ACTION_USER_REMOVED);
filter.addAction(Intent.ACTION_USER_INFO_CHANGED);
@@ -887,15 +890,10 @@ public class UserSettings extends SettingsPreferenceFragment
mUserCaps.mDisallowAddUser ? mUserCaps.mEnforcedAdmin : null);
}
}
if (mUserCaps.mIsAdmin &&
(!mUserCaps.mDisallowAddUser || mUserCaps.mDisallowAddUserSetByAdmin)) {
if (mAddUserWhenLockedController.isAvailable()) {
mLockScreenSettings.setOrder(Preference.DEFAULT_ORDER);
preferenceScreen.addPreference(mLockScreenSettings);
mAddUserWhenLocked.setChecked(Settings.Global.getInt(getContentResolver(),
Settings.Global.ADD_USERS_WHEN_LOCKED, 0) == 1);
mAddUserWhenLocked.setOnPreferenceChangeListener(this);
mAddUserWhenLocked.setDisabledByAdmin(
mUserCaps.mDisallowAddUser ? mUserCaps.mEnforcedAdmin : null);
}
if (emergencyInfoActivityPresent(getContext())) {
@@ -983,10 +981,8 @@ public class UserSettings extends SettingsPreferenceFragment
} else {
onAddUserClicked(USER_TYPE_USER);
}
} else if (pref == mEmergencyInfoPreference) {
Intent intent = new Intent(ACTION_EDIT_EMERGENCY_INFO);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
} else {
mEnergencyInfoController.handlePreferenceTreeClick(pref);
}
return false;
}
@@ -1031,18 +1027,6 @@ public class UserSettings extends SettingsPreferenceFragment
}
}
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
if (preference == mAddUserWhenLocked) {
Boolean value = (Boolean) newValue;
Settings.Global.putInt(getContentResolver(), Settings.Global.ADD_USERS_WHEN_LOCKED,
value != null && value ? 1 : 0);
return true;
}
return false;
}
@Override
public int getHelpResource() {
return R.string.help_url_users;

View File

@@ -0,0 +1,135 @@
/*
* Copyright (C) 2016 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.accounts;
import android.app.Fragment;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.UserInfo;
import android.os.UserManager;
import android.support.v7.preference.Preference;
import android.support.v7.preference.PreferenceScreen;
import android.support.v14.preference.SwitchPreference;
import com.android.settings.SettingsRobolectricTestRunner;
import com.android.settings.TestConfig;
import java.util.ArrayList;
import java.util.List;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.annotation.Config;
import org.robolectric.shadows.ShadowApplication;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Answers.RETURNS_DEEP_STUBS;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyInt;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@RunWith(SettingsRobolectricTestRunner.class)
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
public class AutoSyncDataPreferenceControllerTest {
@Mock(answer = RETURNS_DEEP_STUBS)
private PreferenceScreen mScreen;
@Mock(answer = RETURNS_DEEP_STUBS)
private UserManager mUserManager;
@Mock(answer = RETURNS_DEEP_STUBS)
private Fragment mFragment;
private Context mContext;
private AutoSyncDataPreferenceController mController;
private AutoSyncDataPreferenceController.ConfirmAutoSyncChangeFragment mConfirmSyncFragment;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
ShadowApplication shadowContext = ShadowApplication.getInstance();
shadowContext.setSystemService(Context.USER_SERVICE, mUserManager);
mContext = shadowContext.getApplicationContext();
mController = new AutoSyncDataPreferenceController(mContext, mFragment);
mConfirmSyncFragment = new AutoSyncDataPreferenceController.ConfirmAutoSyncChangeFragment();
mConfirmSyncFragment.setTargetFragment(mFragment, 0);
}
@Test
public void displayPref_managedProfile_shouldNotDisplay() {
when(mUserManager.isManagedProfile()).thenReturn(true);
mController.displayPreference(mScreen);
verify(mScreen).removePreference(any(Preference.class));
}
@Test
public void displayPref_linkedUser_shouldDisplay() {
when(mUserManager.isManagedProfile()).thenReturn(false);
when(mUserManager.isLinkedUser()).thenReturn(true);
mController.displayPreference(mScreen);
verify(mScreen, never()).removePreference(any(Preference.class));
}
@Test
public void displayPref_oneProfile_shouldDisplay() {
List<UserInfo> infos = new ArrayList<>();
infos.add(new UserInfo(1, "user 1", 0));
when(mUserManager.isManagedProfile()).thenReturn(false);
when(mUserManager.isLinkedUser()).thenReturn(false);
when(mUserManager.getProfiles(anyInt())).thenReturn(infos);
mController.displayPreference(mScreen);
verify(mScreen, never()).removePreference(any(Preference.class));
}
@Test
public void displayPref_moreThanOneProfile_shouldNotDisplay() {
List<UserInfo> infos = new ArrayList<>();
infos.add(new UserInfo(1, "user 1", 0));
infos.add(new UserInfo(2, "user 2", 0));
when(mUserManager.isManagedProfile()).thenReturn(false);
when(mUserManager.isLinkedUser()).thenReturn(false);
when(mUserManager.getProfiles(anyInt())).thenReturn(infos);
mController.displayPreference(mScreen);
verify(mScreen).removePreference(any(Preference.class));
}
@Test
public void autoSyncData_shouldNotBeSetOnCancel() {
final ShadowApplication application = ShadowApplication.getInstance();
final Context context = application.getApplicationContext();
final SwitchPreference preference = new SwitchPreference(context);
preference.setChecked(false);
mController = new AutoSyncDataPreferenceController(context, mFragment);
mConfirmSyncFragment.mPreference = preference;
mConfirmSyncFragment.mEnabling = true;
mConfirmSyncFragment.onClick(null, DialogInterface.BUTTON_NEGATIVE);
assertThat(preference.isChecked()).isFalse();
}
}

View File

@@ -0,0 +1,114 @@
/*
* Copyright (C) 2016 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.accounts;
import android.app.Fragment;
import android.content.Context;
import android.content.pm.UserInfo;
import android.os.UserManager;
import android.support.v7.preference.Preference;
import android.support.v7.preference.PreferenceScreen;
import com.android.settings.SettingsRobolectricTestRunner;
import com.android.settings.TestConfig;
import com.android.settings.search.SearchIndexableRaw;
import java.util.ArrayList;
import java.util.List;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.annotation.Config;
import org.robolectric.shadows.ShadowApplication;
import static org.mockito.Answers.RETURNS_DEEP_STUBS;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyInt;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@RunWith(SettingsRobolectricTestRunner.class)
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
public class AutoSyncPersonalDataPreferenceControllerTest {
@Mock(answer = RETURNS_DEEP_STUBS)
private PreferenceScreen mScreen;
@Mock(answer = RETURNS_DEEP_STUBS)
private UserManager mUserManager;
@Mock(answer = RETURNS_DEEP_STUBS)
private Fragment mFragment;
private Context mContext;
private AutoSyncPersonalDataPreferenceController mController;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
ShadowApplication shadowContext = ShadowApplication.getInstance();
shadowContext.setSystemService(Context.USER_SERVICE, mUserManager);
mContext = shadowContext.getApplicationContext();
mController = new AutoSyncPersonalDataPreferenceController(mContext, mFragment);
}
@Test
public void displayPref_managedProfile_shouldNotDisplay() {
when(mUserManager.isManagedProfile()).thenReturn(true);
mController.displayPreference(mScreen);
verify(mScreen).removePreference(any(Preference.class));
}
@Test
public void displayPref_linkedUser_shouldNotDisplay() {
when(mUserManager.isManagedProfile()).thenReturn(false);
when(mUserManager.isLinkedUser()).thenReturn(true);
mController.displayPreference(mScreen);
verify(mScreen).removePreference(any(Preference.class));
}
@Test
public void displayPref_oneProfile_shouldNotDisplay() {
List<UserInfo> infos = new ArrayList<>();
infos.add(new UserInfo(1, "user 1", 0));
when(mUserManager.isManagedProfile()).thenReturn(false);
when(mUserManager.isLinkedUser()).thenReturn(false);
when(mUserManager.getProfiles(anyInt())).thenReturn(infos);
mController.displayPreference(mScreen);
verify(mScreen).removePreference(any(Preference.class));
}
@Test
public void displayPref_prefAvaiable_shouldDisplay() {
List<UserInfo> infos = new ArrayList<>();
infos.add(new UserInfo(1, "user 1", 0));
infos.add(new UserInfo(2, "user 2", 0));
when(mUserManager.isManagedProfile()).thenReturn(false);
when(mUserManager.isLinkedUser()).thenReturn(false);
when(mUserManager.getProfiles(anyInt())).thenReturn(infos);
mController.displayPreference(mScreen);
verify(mScreen, never()).removePreference(any(Preference.class));
}
}