TaskPersisterTest.java revision 36f3f0337de86baf8cf8a5fdd67a95e61dff4bcd
1/*
2 * Copyright (C) 2016 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
17package com.android.server.am;
18
19import android.content.pm.UserInfo;
20import android.os.Environment;
21import android.os.UserHandle;
22import android.os.UserManager;
23import android.test.AndroidTestCase;
24import android.util.Log;
25import android.util.SparseBooleanArray;
26
27import com.android.server.am.TaskPersister;
28
29import java.io.File;
30import java.util.Random;
31
32public class TaskPersisterTest extends AndroidTestCase {
33    private static final String TEST_USER_NAME = "AM-Test-User";
34
35    private TaskPersister mTaskPersister;
36    private int testUserId;
37    private UserManager mUserManager;
38
39    @Override
40    public void setUp() throws Exception {
41        super.setUp();
42        mUserManager = UserManager.get(getContext());
43        mTaskPersister = new TaskPersister(getContext().getFilesDir());
44        testUserId = createUser(TEST_USER_NAME, 0);
45    }
46
47    @Override
48    public void tearDown() throws Exception {
49        super.tearDown();
50        mTaskPersister.unloadUserDataFromMemory(testUserId);
51        removeUser(testUserId);
52    }
53
54    private int getRandomTaskIdForUser(int userId) {
55        int taskId = (int) (Math.random() * UserHandle.PER_USER_RANGE);
56        taskId += UserHandle.PER_USER_RANGE * userId;
57        return taskId;
58    }
59
60    public void testTaskIdsPersistence() {
61        SparseBooleanArray taskIdsOnFile = mTaskPersister.loadPersistedTaskIdsForUser(testUserId);
62        for (int i = 0; i < 100; i++) {
63            taskIdsOnFile.put(getRandomTaskIdForUser(testUserId), true);
64        }
65        mTaskPersister.writePersistedTaskIdsForUser(taskIdsOnFile, testUserId);
66        SparseBooleanArray newTaskIdsOnFile = mTaskPersister
67                .loadPersistedTaskIdsForUser(testUserId);
68        assertTrue("TaskIds written differ from TaskIds read back from file",
69                taskIdsOnFile.equals(newTaskIdsOnFile));
70    }
71
72    private int createUser(String name, int flags) {
73        UserInfo user = mUserManager.createUser(name, flags);
74        if (user == null) {
75            fail("Error while creating the test user: " + TEST_USER_NAME);
76        }
77        return user.id;
78    }
79
80    private void removeUser(int userId) {
81        if (!mUserManager.removeUser(userId)) {
82            fail("Error while removing the test user: " + TEST_USER_NAME);
83        }
84    }
85}