Merge "Added a unit test for TaskPersister" into nyc-dev
This commit is contained in:
committed by
Android (Google) Code Review
commit
07317df6ab
@@ -37,10 +37,7 @@ import android.content.pm.PackageManager;
|
|||||||
import android.graphics.Bitmap;
|
import android.graphics.Bitmap;
|
||||||
import android.os.Environment;
|
import android.os.Environment;
|
||||||
import android.os.RemoteException;
|
import android.os.RemoteException;
|
||||||
import android.os.SystemClock;
|
|
||||||
import android.os.UserHandle;
|
import android.os.UserHandle;
|
||||||
import android.provider.Settings.System;
|
|
||||||
import android.util.ArraySet;
|
|
||||||
import android.util.Slog;
|
import android.util.Slog;
|
||||||
import android.util.SparseArray;
|
import android.util.SparseArray;
|
||||||
import android.util.SparseBooleanArray;
|
import android.util.SparseBooleanArray;
|
||||||
|
|||||||
@@ -88,6 +88,7 @@ public class TaskPersister {
|
|||||||
private final ActivityStackSupervisor mStackSupervisor;
|
private final ActivityStackSupervisor mStackSupervisor;
|
||||||
private final RecentTasks mRecentTasks;
|
private final RecentTasks mRecentTasks;
|
||||||
private final SparseArray<SparseBooleanArray> mTaskIdsInFile = new SparseArray<>();
|
private final SparseArray<SparseBooleanArray> mTaskIdsInFile = new SparseArray<>();
|
||||||
|
private final File mTaskIdsDir;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Value determines write delay mode as follows: < 0 We are Flushing. No delays between writes
|
* Value determines write delay mode as follows: < 0 We are Flushing. No delays between writes
|
||||||
@@ -139,12 +140,22 @@ public class TaskPersister {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
mTaskIdsDir = new File(Environment.getDataDirectory(), "system_de");
|
||||||
mStackSupervisor = stackSupervisor;
|
mStackSupervisor = stackSupervisor;
|
||||||
mService = service;
|
mService = service;
|
||||||
mRecentTasks = recentTasks;
|
mRecentTasks = recentTasks;
|
||||||
mLazyTaskWriterThread = new LazyTaskWriterThread("LazyTaskWriterThread");
|
mLazyTaskWriterThread = new LazyTaskWriterThread("LazyTaskWriterThread");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@VisibleForTesting
|
||||||
|
TaskPersister(File workingDir) {
|
||||||
|
mTaskIdsDir = workingDir;
|
||||||
|
mStackSupervisor = null;
|
||||||
|
mService = null;
|
||||||
|
mRecentTasks = null;
|
||||||
|
mLazyTaskWriterThread = new LazyTaskWriterThread("LazyTaskWriterThreadTest");
|
||||||
|
}
|
||||||
|
|
||||||
void startPersisting() {
|
void startPersisting() {
|
||||||
if (!mLazyTaskWriterThread.isAlive()) {
|
if (!mLazyTaskWriterThread.isAlive()) {
|
||||||
mLazyTaskWriterThread.start();
|
mLazyTaskWriterThread.start();
|
||||||
@@ -207,8 +218,8 @@ public class TaskPersister {
|
|||||||
return persistedTaskIds.clone();
|
return persistedTaskIds.clone();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void maybeWritePersistedTaskIdsForUser(@NonNull SparseBooleanArray taskIds,
|
@VisibleForTesting
|
||||||
int userId) {
|
void maybeWritePersistedTaskIdsForUser(@NonNull SparseBooleanArray taskIds, int userId) {
|
||||||
if (userId < 0) {
|
if (userId < 0) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -565,8 +576,12 @@ public class TaskPersister {
|
|||||||
return BitmapFactory.decodeFile(filename);
|
return BitmapFactory.decodeFile(filename);
|
||||||
}
|
}
|
||||||
|
|
||||||
static File getUserPersistedTaskIdsFile(int userId) {
|
private File getUserPersistedTaskIdsFile(int userId) {
|
||||||
return new File(Environment.getDataSystemDeDirectory(userId), PERSISTED_TASK_IDS_FILENAME);
|
File userTaskIdsDir = new File(mTaskIdsDir, String.valueOf(userId));
|
||||||
|
if (!userTaskIdsDir.exists() && !userTaskIdsDir.mkdirs()) {
|
||||||
|
Slog.e(TAG, "Error while creating user directory: " + userTaskIdsDir);
|
||||||
|
}
|
||||||
|
return new File(userTaskIdsDir, PERSISTED_TASK_IDS_FILENAME);
|
||||||
}
|
}
|
||||||
|
|
||||||
static File getUserTasksDir(int userId) {
|
static File getUserTasksDir(int userId) {
|
||||||
|
|||||||
@@ -0,0 +1,85 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2016 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.server.am;
|
||||||
|
|
||||||
|
import android.content.pm.UserInfo;
|
||||||
|
import android.os.Environment;
|
||||||
|
import android.os.UserHandle;
|
||||||
|
import android.os.UserManager;
|
||||||
|
import android.test.AndroidTestCase;
|
||||||
|
import android.util.Log;
|
||||||
|
import android.util.SparseBooleanArray;
|
||||||
|
|
||||||
|
import com.android.server.am.TaskPersister;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.util.Random;
|
||||||
|
|
||||||
|
public class TaskPersisterTest extends AndroidTestCase {
|
||||||
|
private static final String TEST_USER_NAME = "AM-Test-User";
|
||||||
|
|
||||||
|
private TaskPersister mTaskPersister;
|
||||||
|
private int testUserId;
|
||||||
|
private UserManager mUserManager;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setUp() throws Exception {
|
||||||
|
super.setUp();
|
||||||
|
mUserManager = UserManager.get(getContext());
|
||||||
|
mTaskPersister = new TaskPersister(getContext().getFilesDir());
|
||||||
|
testUserId = createUser(TEST_USER_NAME, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void tearDown() throws Exception {
|
||||||
|
super.tearDown();
|
||||||
|
mTaskPersister.unloadUserDataFromMemory(testUserId);
|
||||||
|
removeUser(testUserId);
|
||||||
|
}
|
||||||
|
|
||||||
|
private int getRandomTaskIdForUser(int userId) {
|
||||||
|
int taskId = (int) (Math.random() * UserHandle.PER_USER_RANGE);
|
||||||
|
taskId += UserHandle.PER_USER_RANGE * userId;
|
||||||
|
return taskId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testTaskIdsPersistence() {
|
||||||
|
SparseBooleanArray taskIdsOnFile = mTaskPersister.loadPersistedTaskIdsForUser(testUserId);
|
||||||
|
for (int i = 0; i < 100; i++) {
|
||||||
|
taskIdsOnFile.put(getRandomTaskIdForUser(testUserId), true);
|
||||||
|
}
|
||||||
|
mTaskPersister.maybeWritePersistedTaskIdsForUser(taskIdsOnFile, testUserId);
|
||||||
|
SparseBooleanArray newTaskIdsOnFile = mTaskPersister
|
||||||
|
.loadPersistedTaskIdsForUser(testUserId);
|
||||||
|
assertTrue("TaskIds written differ from TaskIds read back from file",
|
||||||
|
taskIdsOnFile.equals(newTaskIdsOnFile));
|
||||||
|
}
|
||||||
|
|
||||||
|
private int createUser(String name, int flags) {
|
||||||
|
UserInfo user = mUserManager.createUser(name, flags);
|
||||||
|
if (user == null) {
|
||||||
|
fail("Error while creating the test user: " + TEST_USER_NAME);
|
||||||
|
}
|
||||||
|
return user.id;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void removeUser(int userId) {
|
||||||
|
if (!mUserManager.removeUser(userId)) {
|
||||||
|
fail("Error while removing the test user: " + TEST_USER_NAME);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user