diff options
author | Ying Zheng <yizheng@google.com> | 2018-05-11 11:42:48 -0700 |
---|---|---|
committer | Ying Zheng <yizheng@google.com> | 2018-05-18 15:32:53 -0700 |
commit | 5bdb9e5b32f8199dbded0c76ce5302d105be0f29 (patch) | |
tree | b75f3f4443a4ddae9a8f09a794fc378b0a708306 /tests/carservice_unit_test | |
parent | da9284562d80ac7540b7b046e7a6724b240dbc3d (diff) | |
download | Car-5bdb9e5b32f8199dbded0c76ce5302d105be0f29.tar.gz |
Create a car user service that creates a secondary admin on first run.
Test: Unit test
Bug: 79536548
Change-Id: Id38b32c6a9c1dd885b8e04f41f6f7e45a69131ff
Diffstat (limited to 'tests/carservice_unit_test')
-rw-r--r-- | tests/carservice_unit_test/src/com/android/car/CarUserManagerHelperTest.java (renamed from tests/carservice_unit_test/src/com/android/car/CarUserManagerTest.java) | 30 | ||||
-rw-r--r-- | tests/carservice_unit_test/src/com/android/car/user/CarUserServiceTest.java | 121 |
2 files changed, 122 insertions, 29 deletions
diff --git a/tests/carservice_unit_test/src/com/android/car/CarUserManagerTest.java b/tests/carservice_unit_test/src/com/android/car/CarUserManagerHelperTest.java index ff24b8e49f..480b84b970 100644 --- a/tests/carservice_unit_test/src/com/android/car/CarUserManagerTest.java +++ b/tests/carservice_unit_test/src/com/android/car/CarUserManagerHelperTest.java @@ -57,7 +57,7 @@ import java.util.List; * 4. {@link CarUserManagerHelper.OnUsersUpdateListener} registers a listener for user updates. */ @RunWith(AndroidJUnit4.class) -public class CarUserManagerTest { +public class CarUserManagerHelperTest { @Mock private Context mContext; @Mock @@ -142,34 +142,6 @@ public class CarUserManagerTest { .containsExactly(mCurrentProcessUser, otherUser1, otherUser2, otherUser3); } - // Get all users should exclude system user by default. - @Test - public void testGetAllSwitchableUsers() { - UserInfo user1 = createUserInfoForId(10); - UserInfo user2 = createUserInfoForId(11); - UserInfo user3 = createUserInfoForId(12); - - List<UserInfo> testUsers = new ArrayList<>(); - testUsers.add(mSystemUser); - testUsers.add(user1); - testUsers.add(user2); - testUsers.add(user3); - - when(mUserManager.getUsers(true)).thenReturn(new ArrayList<>(testUsers)); - - // Should return all 3 non-system users. - assertThat(mHelper.getAllUsers().size()) - .isEqualTo(3); - - when(mUserManager.getUserInfo(UserHandle.myUserId())).thenReturn(user1); - // Should return user 10, 11 and 12. - assertThat(mHelper.getAllSwitchableUsers().size()) - .isEqualTo(3); - assertThat(mHelper.getAllSwitchableUsers()).contains(user1); - assertThat(mHelper.getAllSwitchableUsers()).contains(user2); - assertThat(mHelper.getAllSwitchableUsers()).contains(user3); - } - @Test public void testUserCanBeRemoved() { UserInfo testInfo = new UserInfo(); diff --git a/tests/carservice_unit_test/src/com/android/car/user/CarUserServiceTest.java b/tests/carservice_unit_test/src/com/android/car/user/CarUserServiceTest.java new file mode 100644 index 0000000000..faa7bd0965 --- /dev/null +++ b/tests/carservice_unit_test/src/com/android/car/user/CarUserServiceTest.java @@ -0,0 +1,121 @@ +/* + * Copyright (C) 2018 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.car.user; + +import static com.google.common.truth.Truth.assertThat; + +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import android.car.user.CarUserManagerHelper; +import android.content.Context; +import android.content.Intent; +import android.content.IntentFilter; +import android.content.pm.UserInfo; +import android.support.test.runner.AndroidJUnit4; + +import java.util.ArrayList; +import java.util.List; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.ArgumentCaptor; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; + +/** + * This class contains unit tests for the {@link CarUserService}. + * + * The following mocks are used: + * <ol> + * <li> {@link Context} provides system services and resources. + * <li> {@link CarUserManagerHelper} provides user info and actions. + * <ol/> + */ +@RunWith(AndroidJUnit4.class) +public class CarUserServiceTest { + private CarUserService mCarUserService; + + @Mock + private Context mMockContext; + + @Mock + private Context mApplicationContext; + + @Mock + private CarUserManagerHelper mCarUserManagerHelper; + + /** + * Initialize all of the objects with the @Mock annotation. + */ + @Before + public void setUp() throws Exception { + MockitoAnnotations.initMocks(this); + when(mMockContext.getApplicationContext()).thenReturn(mApplicationContext); + + mCarUserService = new CarUserService(mMockContext, mCarUserManagerHelper); + } + + /** + * Test that the {@link CarUserService} registers to receive the locked boot completed + * intent. + */ + @Test + public void testRegistersToReceiveEvents() { + ArgumentCaptor<IntentFilter> argument = ArgumentCaptor.forClass(IntentFilter.class); + mCarUserService.init(); + verify(mMockContext).registerReceiver(eq(mCarUserService), argument.capture()); + IntentFilter intentFilter = argument.getValue(); + assertThat(intentFilter.countActions()).isEqualTo(1); + + assertThat(intentFilter.getAction(0)).isEqualTo(Intent.ACTION_LOCKED_BOOT_COMPLETED); + } + + /** + * Test that the {@link CarUserService} unregisters its event receivers. + */ + @Test + public void testUnregistersEventReceivers() { + mCarUserService.release(); + verify(mMockContext).unregisterReceiver(mCarUserService); + } + + /** + * Test that the {@link CarUserService} starts up a secondary admin user upon first run. + */ + @Test + public void testStartsSecondaryAdminUserOnFirstRun() { + List<UserInfo> users = new ArrayList<>(); + + int adminUserId = 10; + UserInfo admin = new UserInfo(adminUserId, CarUserService.OWNER_NAME, UserInfo.FLAG_ADMIN); + + doReturn(users).when(mCarUserManagerHelper).getAllUsers(); + // doReturn(users).when(mCarUserManagerHelper.getAllUsers()); + doReturn(admin).when(mCarUserManagerHelper).createNewAdminUser(CarUserService.OWNER_NAME); + doReturn(true).when(mCarUserManagerHelper).switchToUser(admin); + + mCarUserService.onReceive(mMockContext, + new Intent(Intent.ACTION_LOCKED_BOOT_COMPLETED)); + + verify(mCarUserManagerHelper).createNewAdminUser(CarUserService.OWNER_NAME); + verify(mCarUserManagerHelper).switchToUser(admin); + } +} |