Merge "Use AfW user handle when setting auto_sync for work" into oc-dev
This commit is contained in:
committed by
Android (Google) Code Review
commit
4967646661
@@ -28,8 +28,8 @@ 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.nano.MetricsProto;
|
||||
import com.android.settings.R;
|
||||
import com.android.settings.core.PreferenceController;
|
||||
@@ -41,9 +41,10 @@ public class AutoSyncDataPreferenceController extends PreferenceController {
|
||||
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;
|
||||
protected final UserManager mUserManager;
|
||||
private final Fragment mParentFragment;
|
||||
|
||||
protected UserHandle mUserHandle;
|
||||
|
||||
public AutoSyncDataPreferenceController(Context context, Fragment parent) {
|
||||
super(context);
|
||||
|
@@ -18,6 +18,8 @@ package com.android.settings.accounts;
|
||||
import android.app.Fragment;
|
||||
import android.content.Context;
|
||||
|
||||
import com.android.settings.Utils;
|
||||
|
||||
public class AutoSyncWorkDataPreferenceController extends AutoSyncPersonalDataPreferenceController {
|
||||
|
||||
private static final String TAG = "AutoSyncWorkData";
|
||||
@@ -25,11 +27,11 @@ public class AutoSyncWorkDataPreferenceController extends AutoSyncPersonalDataPr
|
||||
|
||||
public AutoSyncWorkDataPreferenceController(Context context, Fragment parent) {
|
||||
super(context, parent);
|
||||
mUserHandle = Utils.getManagedProfile(mUserManager);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getPreferenceKey() {
|
||||
return KEY_AUTO_SYNC_WORK_ACCOUNT;
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -0,0 +1,108 @@
|
||||
/*
|
||||
* Copyright (C) 2017 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 static android.content.pm.UserInfo.FLAG_MANAGED_PROFILE;
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
import static org.mockito.Answers.RETURNS_DEEP_STUBS;
|
||||
import static org.mockito.Matchers.anyInt;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import android.app.Fragment;
|
||||
import android.content.Context;
|
||||
import android.content.pm.UserInfo;
|
||||
import android.os.UserHandle;
|
||||
import android.os.UserManager;
|
||||
|
||||
import com.android.settings.SettingsRobolectricTestRunner;
|
||||
import com.android.settings.TestConfig;
|
||||
|
||||
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 java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@RunWith(SettingsRobolectricTestRunner.class)
|
||||
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
|
||||
public class AutoSyncWorkDataPreferenceControllerTest {
|
||||
|
||||
@Mock(answer = RETURNS_DEEP_STUBS)
|
||||
private UserManager mUserManager;
|
||||
@Mock(answer = RETURNS_DEEP_STUBS)
|
||||
private Fragment mFragment;
|
||||
@Mock
|
||||
private Context mContext;
|
||||
|
||||
private AutoSyncWorkDataPreferenceController mController;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
when(mContext.getSystemService(Context.USER_SERVICE)).thenReturn(mUserManager);
|
||||
|
||||
mController = new AutoSyncWorkDataPreferenceController(mContext, mFragment);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void checkIsAvailable_managedProfile_shouldNotDisplay() {
|
||||
when(mUserManager.isManagedProfile()).thenReturn(true);
|
||||
|
||||
assertThat(mController.isAvailable()).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void checkIsAvailable_linkedUser_shouldNotDisplay() {
|
||||
when(mUserManager.isManagedProfile()).thenReturn(false);
|
||||
when(mUserManager.isLinkedUser()).thenReturn(true);
|
||||
|
||||
assertThat(mController.isAvailable()).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void checkIsAvailable_singleUserProfile_shouldNotDisplay() {
|
||||
final 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);
|
||||
|
||||
assertThat(mController.isAvailable()).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void multipleProfile_shouldInitWithWorkProfileUserHandle() {
|
||||
final int id1 = 1;
|
||||
final int id2 = 2;
|
||||
final UserInfo managedUser = new UserInfo(id2, "user 2", FLAG_MANAGED_PROFILE);
|
||||
final List<UserHandle> infos = new ArrayList<>();
|
||||
infos.add(new UserHandle(id1));
|
||||
infos.add(new UserHandle(id2));
|
||||
when(mUserManager.getUserProfiles()).thenReturn(infos);
|
||||
when(mUserManager.getUserHandle()).thenReturn(id1);
|
||||
when(mUserManager.getUserInfo(id2)).thenReturn(managedUser);
|
||||
|
||||
mController = new AutoSyncWorkDataPreferenceController(mContext, mFragment);
|
||||
|
||||
assertThat(mController.mUserHandle.getIdentifier()).isEqualTo(id2);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user