Fix ProfileSelectLocationFragmentTest

The test environment is not properly setup

Bug: 313569889

Test: atest
Change-Id: I5424e998cc22bf7bbddff6f69bfc7c4e56ea870c
This commit is contained in:
Fan Wu
2024-01-24 10:46:32 +08:00
parent 505c91cde9
commit 647c381848

View File

@@ -16,27 +16,58 @@
package com.android.settings.dashboard.profileselector; package com.android.settings.dashboard.profileselector;
import static android.os.UserManager.USER_TYPE_FULL_SYSTEM;
import static android.os.UserManager.USER_TYPE_PROFILE_MANAGED;
import static android.os.UserManager.USER_TYPE_PROFILE_PRIVATE;
import static com.android.settings.dashboard.profileselector.ProfileSelectFragment.EXTRA_PROFILE; import static com.android.settings.dashboard.profileselector.ProfileSelectFragment.EXTRA_PROFILE;
import static com.google.common.truth.Truth.assertThat; import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.when;
import android.content.pm.UserInfo;
import androidx.test.core.app.ApplicationProvider;
import com.android.settings.testutils.shadow.ShadowUserManager;
import org.junit.Before; import org.junit.Before;
import org.junit.Ignore; import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.mockito.MockitoAnnotations; import org.mockito.junit.MockitoJUnit;
import org.mockito.junit.MockitoRule;
import org.robolectric.RobolectricTestRunner; import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;
@Ignore("b/313569889") @Config(shadows = {
ShadowUserManager.class,
})
@RunWith(RobolectricTestRunner.class) @RunWith(RobolectricTestRunner.class)
public class ProfileSelectLocationFragmentTest { public class ProfileSelectLocationFragmentTest {
private static final String PERSONAL_PROFILE_NAME = "personal";
private static final String WORK_PROFILE_NAME = "work";
private static final String PRIVATE_PROFILE_NAME = "private";
@Rule
public final MockitoRule rule = MockitoJUnit.rule();
private ShadowUserManager mUserManager;
private ProfileSelectLocationFragment mProfileSelectLocationFragment; private ProfileSelectLocationFragment mProfileSelectLocationFragment;
@Before @Before
public void setUp() { public void setUp() {
MockitoAnnotations.initMocks(this); mUserManager = ShadowUserManager.getShadow();
mProfileSelectLocationFragment = new ProfileSelectLocationFragment(); mUserManager.addProfile(
new UserInfo(0, PERSONAL_PROFILE_NAME, null, 0, USER_TYPE_FULL_SYSTEM));
mUserManager.addProfile(
new UserInfo(1, WORK_PROFILE_NAME, null, 0, USER_TYPE_PROFILE_MANAGED));
mUserManager.addProfile(
new UserInfo(11, PRIVATE_PROFILE_NAME, null, 0, USER_TYPE_PROFILE_PRIVATE));
mProfileSelectLocationFragment = spy(new ProfileSelectLocationFragment());
when(mProfileSelectLocationFragment.getContext()).thenReturn(
ApplicationProvider.getApplicationContext());
} }
@Test @Test
@@ -46,7 +77,7 @@ public class ProfileSelectLocationFragmentTest {
EXTRA_PROFILE, -1)).isEqualTo(ProfileSelectFragment.ProfileType.PERSONAL); EXTRA_PROFILE, -1)).isEqualTo(ProfileSelectFragment.ProfileType.PERSONAL);
assertThat(mProfileSelectLocationFragment.getFragments()[1].getArguments().getInt( assertThat(mProfileSelectLocationFragment.getFragments()[1].getArguments().getInt(
EXTRA_PROFILE, -1)).isEqualTo(ProfileSelectFragment.ProfileType.WORK); EXTRA_PROFILE, -1)).isEqualTo(ProfileSelectFragment.ProfileType.WORK);
assertThat(mProfileSelectLocationFragment.getFragments()[1].getArguments().getInt( assertThat(mProfileSelectLocationFragment.getFragments()[2].getArguments().getInt(
EXTRA_PROFILE, -1)).isEqualTo(ProfileSelectFragment.ProfileType.PRIVATE); EXTRA_PROFILE, -1)).isEqualTo(ProfileSelectFragment.ProfileType.PRIVATE);
} }
} }