Home | History | Annotate | Download | only in am
      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 
     17 package com.android.server.am;
     18 
     19 import android.content.pm.UserInfo;
     20 import android.os.Environment;
     21 import android.os.UserHandle;
     22 import android.os.UserManager;
     23 import android.test.AndroidTestCase;
     24 import android.util.Log;
     25 import android.util.SparseBooleanArray;
     26 
     27 import com.android.server.am.TaskPersister;
     28 
     29 import java.io.File;
     30 import java.util.Random;
     31 
     32 public 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 }