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.server.wm; 18 19 import static androidx.test.platform.app.InstrumentationRegistry.getInstrumentation; 20 21 import static org.junit.Assert.assertEquals; 22 import static org.junit.Assert.assertNotNull; 23 import static org.junit.Assert.assertTrue; 24 25 import android.content.Context; 26 import android.content.pm.UserInfo; 27 import android.os.UserHandle; 28 import android.os.UserManager; 29 import android.platform.test.annotations.Presubmit; 30 import android.util.SparseBooleanArray; 31 32 import androidx.test.filters.FlakyTest; 33 34 import org.junit.After; 35 import org.junit.Before; 36 import org.junit.Test; 37 38 /** 39 * Tests for {@link TaskPersister}. 40 * 41 * Build/Install/Run: 42 * atest WmTests:TaskPersisterTest 43 */ 44 @Presubmit 45 public class TaskPersisterTest { 46 private static final String TEST_USER_NAME = "AM-Test-User"; 47 48 private TaskPersister mTaskPersister; 49 private int mTestUserId; 50 private UserManager mUserManager; 51 52 @Before setUp()53 public void setUp() throws Exception { 54 final Context context = getInstrumentation().getTargetContext(); 55 mUserManager = UserManager.get(context); 56 mTaskPersister = new TaskPersister(context.getFilesDir()); 57 // In ARC, the maximum number of supported users is one, which is different from the ones of 58 // most phones (more than 4). This prevents TaskPersisterTest from creating another user for 59 // test. However, since guest users can be added as much as possible, we create guest user 60 // in the test. 61 mTestUserId = createUser(TEST_USER_NAME, UserInfo.FLAG_GUEST); 62 } 63 64 @After tearDown()65 public void tearDown() throws Exception { 66 mTaskPersister.unloadUserDataFromMemory(mTestUserId); 67 removeUser(mTestUserId); 68 } 69 getRandomTaskIdForUser(int userId)70 private int getRandomTaskIdForUser(int userId) { 71 int taskId = (int) (Math.random() * UserHandle.PER_USER_RANGE); 72 taskId += UserHandle.PER_USER_RANGE * userId; 73 return taskId; 74 } 75 76 @Test testTaskIdsPersistence()77 public void testTaskIdsPersistence() { 78 SparseBooleanArray taskIdsOnFile = new SparseBooleanArray(); 79 for (int i = 0; i < 100; i++) { 80 taskIdsOnFile.put(getRandomTaskIdForUser(mTestUserId), true); 81 } 82 mTaskPersister.writePersistedTaskIdsForUser(taskIdsOnFile, mTestUserId); 83 SparseBooleanArray newTaskIdsOnFile = mTaskPersister 84 .loadPersistedTaskIdsForUser(mTestUserId); 85 assertEquals("TaskIds written differ from TaskIds read back from file", 86 taskIdsOnFile, newTaskIdsOnFile); 87 } 88 createUser(String name, int flags)89 private int createUser(String name, int flags) { 90 UserInfo user = mUserManager.createUser(name, flags); 91 assertNotNull("Error while creating the test user: " + TEST_USER_NAME, user); 92 return user.id; 93 } 94 removeUser(int userId)95 private void removeUser(int userId) { 96 boolean userRemoved = mUserManager.removeUser(userId); 97 assertTrue("Error while removing the test user: " + TEST_USER_NAME, userRemoved); 98 } 99 } 100