jovanak | 310c8cb | 2018-07-10 03:11:21 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 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 | |
| 17 | package com.android.car.settings.users; |
| 18 | |
| 19 | import static com.google.common.truth.Truth.assertThat; |
| 20 | |
| 21 | import static org.mockito.ArgumentMatchers.any; |
jovanak | 310c8cb | 2018-07-10 03:11:21 -0700 | [diff] [blame] | 22 | import static org.mockito.Mockito.doReturn; |
| 23 | import static org.mockito.Mockito.verify; |
jovanak | 310c8cb | 2018-07-10 03:11:21 -0700 | [diff] [blame] | 24 | |
Ying Zheng | 9fafdac | 2018-09-14 10:22:41 -0700 | [diff] [blame] | 25 | import android.car.userlib.CarUserManagerHelper; |
jovanak | 310c8cb | 2018-07-10 03:11:21 -0700 | [diff] [blame] | 26 | import android.content.Context; |
| 27 | import android.content.pm.UserInfo; |
davidln | 5a2ca27 | 2019-07-11 15:51:15 -0700 | [diff] [blame^] | 28 | import android.os.UserHandle; |
jovanak | 310c8cb | 2018-07-10 03:11:21 -0700 | [diff] [blame] | 29 | import android.os.UserManager; |
| 30 | import android.widget.Button; |
| 31 | |
jovanak | 310c8cb | 2018-07-10 03:11:21 -0700 | [diff] [blame] | 32 | import com.android.car.settings.R; |
kwaky | fc2851b | 2019-03-01 14:56:56 -0800 | [diff] [blame] | 33 | import com.android.car.settings.common.ConfirmationDialogFragment; |
jovanak | 310c8cb | 2018-07-10 03:11:21 -0700 | [diff] [blame] | 34 | import com.android.car.settings.testutils.BaseTestActivity; |
| 35 | import com.android.car.settings.testutils.ShadowCarUserManagerHelper; |
jovanak | 310c8cb | 2018-07-10 03:11:21 -0700 | [diff] [blame] | 36 | import com.android.car.settings.testutils.ShadowUserIconProvider; |
| 37 | |
davidln | 69968b4 | 2018-10-26 11:21:26 -0700 | [diff] [blame] | 38 | import org.junit.After; |
jovanak | 310c8cb | 2018-07-10 03:11:21 -0700 | [diff] [blame] | 39 | import org.junit.Before; |
| 40 | import org.junit.Test; |
| 41 | import org.junit.runner.RunWith; |
| 42 | import org.mockito.Mock; |
| 43 | import org.mockito.MockitoAnnotations; |
| 44 | import org.robolectric.Robolectric; |
davidln | 15c9351 | 2019-06-07 16:58:00 -0700 | [diff] [blame] | 45 | import org.robolectric.RobolectricTestRunner; |
Heemin Seog | 4da1679 | 2018-11-09 13:36:08 -0800 | [diff] [blame] | 46 | import org.robolectric.RuntimeEnvironment; |
davidln | 5a2ca27 | 2019-07-11 15:51:15 -0700 | [diff] [blame^] | 47 | import org.robolectric.Shadows; |
jovanak | 310c8cb | 2018-07-10 03:11:21 -0700 | [diff] [blame] | 48 | import org.robolectric.annotation.Config; |
jovanak | 310c8cb | 2018-07-10 03:11:21 -0700 | [diff] [blame] | 49 | |
| 50 | import java.util.ArrayList; |
| 51 | |
| 52 | /** |
| 53 | * Tests for UserDetailsFragment. |
| 54 | */ |
davidln | 15c9351 | 2019-06-07 16:58:00 -0700 | [diff] [blame] | 55 | @RunWith(RobolectricTestRunner.class) |
davidln | 5a2ca27 | 2019-07-11 15:51:15 -0700 | [diff] [blame^] | 56 | @Config(shadows = {ShadowCarUserManagerHelper.class, ShadowUserIconProvider.class}) |
jovanak | 310c8cb | 2018-07-10 03:11:21 -0700 | [diff] [blame] | 57 | public class UsersListFragmentTest { |
Heemin Seog | 4da1679 | 2018-11-09 13:36:08 -0800 | [diff] [blame] | 58 | |
| 59 | private Context mContext; |
jovanak | 310c8cb | 2018-07-10 03:11:21 -0700 | [diff] [blame] | 60 | private BaseTestActivity mTestActivity; |
| 61 | private UsersListFragment mFragment; |
Heemin Seog | 4da1679 | 2018-11-09 13:36:08 -0800 | [diff] [blame] | 62 | private Button mActionButton; |
jovanak | 310c8cb | 2018-07-10 03:11:21 -0700 | [diff] [blame] | 63 | |
| 64 | @Mock |
| 65 | private CarUserManagerHelper mCarUserManagerHelper; |
Heemin Seog | 4da1679 | 2018-11-09 13:36:08 -0800 | [diff] [blame] | 66 | |
jovanak | 310c8cb | 2018-07-10 03:11:21 -0700 | [diff] [blame] | 67 | |
| 68 | @Before |
Heemin Seog | 4da1679 | 2018-11-09 13:36:08 -0800 | [diff] [blame] | 69 | public void setUp() { |
jovanak | 310c8cb | 2018-07-10 03:11:21 -0700 | [diff] [blame] | 70 | MockitoAnnotations.initMocks(this); |
| 71 | ShadowCarUserManagerHelper.setMockInstance(mCarUserManagerHelper); |
Heemin Seog | 4da1679 | 2018-11-09 13:36:08 -0800 | [diff] [blame] | 72 | mContext = RuntimeEnvironment.application; |
davidln | e304de1 | 2018-11-28 14:18:35 -0800 | [diff] [blame] | 73 | mTestActivity = Robolectric.setupActivity(BaseTestActivity.class); |
jovanak | 310c8cb | 2018-07-10 03:11:21 -0700 | [diff] [blame] | 74 | } |
| 75 | |
davidln | 69968b4 | 2018-10-26 11:21:26 -0700 | [diff] [blame] | 76 | @After |
| 77 | public void tearDown() { |
| 78 | ShadowCarUserManagerHelper.reset(); |
| 79 | } |
| 80 | |
jovanak | 310c8cb | 2018-07-10 03:11:21 -0700 | [diff] [blame] | 81 | /* Test that onCreateNewUserConfirmed invokes a creation of a new non-admin. */ |
| 82 | @Test |
| 83 | public void testOnCreateNewUserConfirmedInvokesCreateNewNonAdminUser() { |
| 84 | createUsersListFragment(); |
kwaky | fc2851b | 2019-03-01 14:56:56 -0800 | [diff] [blame] | 85 | mFragment.mConfirmListener.onConfirm(/* arguments= */ null); |
jovanak | 310c8cb | 2018-07-10 03:11:21 -0700 | [diff] [blame] | 86 | Robolectric.flushBackgroundThreadScheduler(); |
| 87 | verify(mCarUserManagerHelper) |
Heemin Seog | 4da1679 | 2018-11-09 13:36:08 -0800 | [diff] [blame] | 88 | .createNewNonAdminUser(mContext.getString(R.string.user_new_user_name)); |
jovanak | 310c8cb | 2018-07-10 03:11:21 -0700 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | /* Test that if we're in demo user, click on the button starts exit out of the retail mode. */ |
| 92 | @Test |
Heemin Seog | 4da1679 | 2018-11-09 13:36:08 -0800 | [diff] [blame] | 93 | public void testCallOnClick_demoUser_exitRetailMode() { |
jovanak | 310c8cb | 2018-07-10 03:11:21 -0700 | [diff] [blame] | 94 | doReturn(true).when(mCarUserManagerHelper).isCurrentProcessDemoUser(); |
| 95 | createUsersListFragment(); |
| 96 | mActionButton.callOnClick(); |
| 97 | assertThat(isDialogShown(ConfirmExitRetailModeDialog.DIALOG_TAG)).isTrue(); |
| 98 | } |
| 99 | |
| 100 | /* Test that if the max num of users is reached, click on the button informs user of that. */ |
| 101 | @Test |
Heemin Seog | 4da1679 | 2018-11-09 13:36:08 -0800 | [diff] [blame] | 102 | public void testCallOnClick_userLimitReached_showErrorDialog() { |
jovanak | 310c8cb | 2018-07-10 03:11:21 -0700 | [diff] [blame] | 103 | doReturn(5).when(mCarUserManagerHelper).getMaxSupportedRealUsers(); |
| 104 | doReturn(true).when(mCarUserManagerHelper).isUserLimitReached(); |
| 105 | createUsersListFragment(); |
| 106 | |
| 107 | mActionButton.callOnClick(); |
| 108 | assertThat(isDialogShown(MaxUsersLimitReachedDialog.DIALOG_TAG)).isTrue(); |
| 109 | } |
| 110 | |
| 111 | /* Test that if user can add other users, click on the button creates a dialog to confirm. */ |
| 112 | @Test |
Heemin Seog | 4da1679 | 2018-11-09 13:36:08 -0800 | [diff] [blame] | 113 | public void testCallOnClick_showAddUserDialog() { |
jovanak | 310c8cb | 2018-07-10 03:11:21 -0700 | [diff] [blame] | 114 | createUsersListFragment(); |
| 115 | |
| 116 | mActionButton.callOnClick(); |
kwaky | fc2851b | 2019-03-01 14:56:56 -0800 | [diff] [blame] | 117 | assertThat(isDialogShown(ConfirmationDialogFragment.TAG)).isTrue(); |
jovanak | 310c8cb | 2018-07-10 03:11:21 -0700 | [diff] [blame] | 118 | } |
| 119 | |
Heemin Seog | 4da1679 | 2018-11-09 13:36:08 -0800 | [diff] [blame] | 120 | private void createUsersListFragment() { |
davidln | 5a2ca27 | 2019-07-11 15:51:15 -0700 | [diff] [blame^] | 121 | Shadows.shadowOf(UserManager.get(mContext)).addUser(UserHandle.myUserId(), |
| 122 | "User Name", /* flags= */ 0); |
| 123 | UserInfo testUser = UserManager.get(mContext).getUserInfo(UserHandle.myUserId()); |
davidln | cb0bfa2 | 2018-08-27 14:13:05 -0700 | [diff] [blame] | 124 | mFragment = new UsersListFragment(); |
jovanak | 310c8cb | 2018-07-10 03:11:21 -0700 | [diff] [blame] | 125 | doReturn(testUser).when(mCarUserManagerHelper).getCurrentProcessUserInfo(); |
jovanak | 310c8cb | 2018-07-10 03:11:21 -0700 | [diff] [blame] | 126 | doReturn(new ArrayList<UserInfo>()).when(mCarUserManagerHelper).getAllSwitchableUsers(); |
| 127 | doReturn(null).when(mCarUserManagerHelper).createNewNonAdminUser(any()); |
| 128 | mTestActivity.launchFragment(mFragment); |
jovanak | 310c8cb | 2018-07-10 03:11:21 -0700 | [diff] [blame] | 129 | refreshButtons(); |
| 130 | } |
| 131 | |
jovanak | 310c8cb | 2018-07-10 03:11:21 -0700 | [diff] [blame] | 132 | private void refreshButtons() { |
davidln | 5a2ca27 | 2019-07-11 15:51:15 -0700 | [diff] [blame^] | 133 | mActionButton = mTestActivity.findViewById(R.id.action_button1); |
jovanak | 310c8cb | 2018-07-10 03:11:21 -0700 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | private boolean isDialogShown(String tag) { |
| 137 | return mTestActivity.getSupportFragmentManager().findFragmentByTag(tag) != null; |
| 138 | } |
jovanak | 310c8cb | 2018-07-10 03:11:21 -0700 | [diff] [blame] | 139 | } |