Fix CommunalPreferenceControllerTest
Migrate test to robolectric, as this test doesn't require a device to run. This also speeds up the test, and allows for use of roboelectric shadows. Fixes: 337417918 Test: atest CommunalPreferenceControllerTest Flag: EXEMPT fix broken test Change-Id: I32b2760c98bf527b33a8ccbe46314395a03da1e0
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (C) 2023 The Android Open Source Project
|
* Copyright (C) 2024 The Android Open Source Project
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@@ -18,75 +18,63 @@ package com.android.settings.communal;
|
|||||||
|
|
||||||
import static org.junit.Assert.assertFalse;
|
import static org.junit.Assert.assertFalse;
|
||||||
import static org.junit.Assert.assertTrue;
|
import static org.junit.Assert.assertTrue;
|
||||||
|
import static org.mockito.ArgumentMatchers.any;
|
||||||
|
import static org.mockito.ArgumentMatchers.anyInt;
|
||||||
|
import static org.mockito.Mockito.doReturn;
|
||||||
import static org.mockito.Mockito.spy;
|
import static org.mockito.Mockito.spy;
|
||||||
import static org.mockito.Mockito.when;
|
|
||||||
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.res.Resources;
|
import android.os.UserHandle;
|
||||||
import android.os.UserManager;
|
|
||||||
|
|
||||||
import androidx.test.core.app.ApplicationProvider;
|
import com.android.settings.R;
|
||||||
import androidx.test.ext.junit.runners.AndroidJUnit4;
|
import com.android.settings.testutils.shadow.SettingsShadowResources;
|
||||||
|
import com.android.settings.testutils.shadow.ShadowUserManager;
|
||||||
import com.android.settings.Utils;
|
|
||||||
import com.android.settings.testutils.ResourcesUtils;
|
|
||||||
|
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Ignore;
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
import org.mockito.Mock;
|
import org.robolectric.RobolectricTestRunner;
|
||||||
import org.mockito.MockitoAnnotations;
|
import org.robolectric.RuntimeEnvironment;
|
||||||
|
import org.robolectric.annotation.Config;
|
||||||
|
|
||||||
@RunWith(AndroidJUnit4.class)
|
@RunWith(RobolectricTestRunner.class)
|
||||||
@Ignore("b/337417918")
|
@Config(shadows = {SettingsShadowResources.class, ShadowUserManager.class})
|
||||||
public class CommunalPreferenceControllerTest {
|
public class CommunalPreferenceControllerTest {
|
||||||
@Mock
|
private ShadowUserManager mShadowUserManager;
|
||||||
private UserManager mUserManager;
|
|
||||||
|
|
||||||
private Context mContext;
|
|
||||||
private Resources mResources;
|
|
||||||
private CommunalPreferenceController mController;
|
private CommunalPreferenceController mController;
|
||||||
|
|
||||||
private static final String PREF_KEY = "test_key";
|
private static final String PREF_KEY = "test_key";
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void setup() {
|
public void setup() {
|
||||||
MockitoAnnotations.initMocks(this);
|
final Context context = spy(RuntimeEnvironment.application);
|
||||||
mContext = spy(ApplicationProvider.getApplicationContext());
|
mShadowUserManager = ShadowUserManager.getShadow();
|
||||||
mResources = spy(mContext.getResources());
|
doReturn(context).when(context).createContextAsUser(any(UserHandle.class), anyInt());
|
||||||
|
mController = new CommunalPreferenceController(context, PREF_KEY);
|
||||||
mController = new CommunalPreferenceController(mContext, PREF_KEY);
|
|
||||||
|
|
||||||
when(mContext.getResources()).thenReturn(mResources);
|
|
||||||
when(mContext.getSystemService(UserManager.class)).thenReturn(mUserManager);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void isAvailable_communalEnabled_shouldBeTrueForDockUser() {
|
public void isAvailable_communalEnabled_shouldBeTrueForDockUser() {
|
||||||
setCommunalEnabled(true);
|
setCommunalEnabled(true);
|
||||||
when(Utils.canCurrentUserDream(mContext)).thenReturn(true);
|
mShadowUserManager.setUserForeground(true);
|
||||||
assertTrue(mController.isAvailable());
|
assertTrue(mController.isAvailable());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void isAvailable_communalEnabled_shouldBeFalseForNonDockUser() {
|
public void isAvailable_communalEnabled_shouldBeFalseForNonDockUser() {
|
||||||
setCommunalEnabled(true);
|
setCommunalEnabled(true);
|
||||||
when(Utils.canCurrentUserDream(mContext)).thenReturn(false);
|
mShadowUserManager.setUserForeground(false);
|
||||||
assertFalse(mController.isAvailable());
|
assertFalse(mController.isAvailable());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void isAvailable_communalDisabled_shouldBeFalseForDockUser() {
|
public void isAvailable_communalDisabled_shouldBeFalseForDockUser() {
|
||||||
setCommunalEnabled(false);
|
setCommunalEnabled(false);
|
||||||
when(Utils.canCurrentUserDream(mContext)).thenReturn(true);
|
mShadowUserManager.setUserForeground(true);
|
||||||
assertFalse(mController.isAvailable());
|
assertFalse(mController.isAvailable());
|
||||||
}
|
}
|
||||||
|
|
||||||
private void setCommunalEnabled(boolean enabled) {
|
private void setCommunalEnabled(boolean enabled) {
|
||||||
final int boolId = ResourcesUtils.getResourcesId(
|
SettingsShadowResources.overrideResource(R.bool.config_show_communal_settings, enabled);
|
||||||
ApplicationProvider.getApplicationContext(), "bool",
|
|
||||||
"config_show_communal_settings");
|
|
||||||
when(mResources.getBoolean(boolId)).thenReturn(enabled);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -309,6 +309,11 @@ public class ShadowUserManager extends org.robolectric.shadows.ShadowUserManager
|
|||||||
UserInfo.FLAG_INITIALIZED | UserInfo.FLAG_ADMIN | UserInfo.FLAG_PRIMARY);
|
UserInfo.FLAG_INITIALIZED | UserInfo.FLAG_ADMIN | UserInfo.FLAG_PRIMARY);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Implementation
|
||||||
|
protected UserHandle getMainUser() {
|
||||||
|
return UserHandle.of(PRIMARY_USER_ID);
|
||||||
|
}
|
||||||
|
|
||||||
protected boolean setUserEphemeral(@UserIdInt int userId, boolean enableEphemeral) {
|
protected boolean setUserEphemeral(@UserIdInt int userId, boolean enableEphemeral) {
|
||||||
UserInfo userInfo = mUserProfileInfos.stream()
|
UserInfo userInfo = mUserProfileInfos.stream()
|
||||||
.filter(user -> user.id == userId)
|
.filter(user -> user.id == userId)
|
||||||
|
|||||||
Reference in New Issue
Block a user