Merge "Make AccountManagerServiceTest work again" into nyc-dev
am: 518bae5
* commit '518bae5fa411327b14ca1e1496f6c9836da0eccd':
Make AccountManagerServiceTest work again
Change-Id: I162a0022c6f532d2b14dabc71ed891a3e5132d3e
This commit is contained in:
@@ -85,6 +85,7 @@ import android.util.SparseArray;
|
|||||||
import android.util.SparseBooleanArray;
|
import android.util.SparseBooleanArray;
|
||||||
|
|
||||||
import com.android.internal.R;
|
import com.android.internal.R;
|
||||||
|
import com.android.internal.annotations.VisibleForTesting;
|
||||||
import com.android.internal.util.ArrayUtils;
|
import com.android.internal.util.ArrayUtils;
|
||||||
import com.android.internal.util.IndentingPrintWriter;
|
import com.android.internal.util.IndentingPrintWriter;
|
||||||
import com.android.internal.util.Preconditions;
|
import com.android.internal.util.Preconditions;
|
||||||
@@ -267,10 +268,10 @@ public class AccountManagerService
|
|||||||
private int debugDbInsertionPoint = -1;
|
private int debugDbInsertionPoint = -1;
|
||||||
private SQLiteStatement statementForLogging;
|
private SQLiteStatement statementForLogging;
|
||||||
|
|
||||||
UserAccounts(Context context, int userId) {
|
UserAccounts(Context context, int userId, File preNDbFile, File deDbFile) {
|
||||||
this.userId = userId;
|
this.userId = userId;
|
||||||
synchronized (cacheLock) {
|
synchronized (cacheLock) {
|
||||||
openHelper = DeDatabaseHelper.create(context, userId);
|
openHelper = DeDatabaseHelper.create(context, userId, preNDbFile, deDbFile);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -540,7 +541,9 @@ public class AccountManagerService
|
|||||||
UserAccounts accounts = mUsers.get(userId);
|
UserAccounts accounts = mUsers.get(userId);
|
||||||
boolean validateAccounts = false;
|
boolean validateAccounts = false;
|
||||||
if (accounts == null) {
|
if (accounts == null) {
|
||||||
accounts = new UserAccounts(mContext, userId);
|
File preNDbFile = new File(getPreNDatabaseName(userId));
|
||||||
|
File deDbFile = new File(getDeDatabaseName(userId));
|
||||||
|
accounts = new UserAccounts(mContext, userId, preNDbFile, deDbFile);
|
||||||
initializeDebugDbSizeAndCompileSqlStatementForLogging(
|
initializeDebugDbSizeAndCompileSqlStatementForLogging(
|
||||||
accounts.openHelper.getWritableDatabase(), accounts);
|
accounts.openHelper.getWritableDatabase(), accounts);
|
||||||
mUsers.append(userId, accounts);
|
mUsers.append(userId, accounts);
|
||||||
@@ -551,8 +554,10 @@ public class AccountManagerService
|
|||||||
if (!accounts.openHelper.isCeDatabaseAttached() && mUnlockedUsers.get(userId)) {
|
if (!accounts.openHelper.isCeDatabaseAttached() && mUnlockedUsers.get(userId)) {
|
||||||
Log.i(TAG, "User " + userId + " is unlocked - opening CE database");
|
Log.i(TAG, "User " + userId + " is unlocked - opening CE database");
|
||||||
synchronized (accounts.cacheLock) {
|
synchronized (accounts.cacheLock) {
|
||||||
CeDatabaseHelper.create(mContext, userId);
|
File preNDatabaseFile = new File(getPreNDatabaseName(userId));
|
||||||
accounts.openHelper.attachCeDatabase();
|
File ceDatabaseFile = new File(getCeDatabaseName(userId));
|
||||||
|
CeDatabaseHelper.create(mContext, userId, preNDatabaseFile, ceDatabaseFile);
|
||||||
|
accounts.openHelper.attachCeDatabase(ceDatabaseFile);
|
||||||
}
|
}
|
||||||
syncDeCeAccountsLocked(accounts);
|
syncDeCeAccountsLocked(accounts);
|
||||||
}
|
}
|
||||||
@@ -647,7 +652,8 @@ public class AccountManagerService
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void onUserUnlocked(Intent intent) {
|
@VisibleForTesting
|
||||||
|
void onUserUnlocked(Intent intent) {
|
||||||
int userId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, -1);
|
int userId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, -1);
|
||||||
if (Log.isLoggable(TAG, Log.VERBOSE)) {
|
if (Log.isLoggable(TAG, Log.VERBOSE)) {
|
||||||
Log.v(TAG, "onUserUnlocked " + userId);
|
Log.v(TAG, "onUserUnlocked " + userId);
|
||||||
@@ -1553,7 +1559,7 @@ public class AccountManagerService
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* For testing */
|
@VisibleForTesting
|
||||||
protected void removeAccountInternal(Account account) {
|
protected void removeAccountInternal(Account account) {
|
||||||
removeAccountInternal(getUserAccountsForCaller(), account, getCallingUid());
|
removeAccountInternal(getUserAccountsForCaller(), account, getCallingUid());
|
||||||
}
|
}
|
||||||
@@ -4044,7 +4050,8 @@ public class AccountManagerService
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static String getPreNDatabaseName(int userId) {
|
@VisibleForTesting
|
||||||
|
String getPreNDatabaseName(int userId) {
|
||||||
File systemDir = Environment.getDataSystemDirectory();
|
File systemDir = Environment.getDataSystemDirectory();
|
||||||
File databaseFile = new File(Environment.getUserSystemDirectory(userId),
|
File databaseFile = new File(Environment.getUserSystemDirectory(userId),
|
||||||
PRE_N_DATABASE_NAME);
|
PRE_N_DATABASE_NAME);
|
||||||
@@ -4070,13 +4077,15 @@ public class AccountManagerService
|
|||||||
return databaseFile.getPath();
|
return databaseFile.getPath();
|
||||||
}
|
}
|
||||||
|
|
||||||
static String getDeDatabaseName(int userId) {
|
@VisibleForTesting
|
||||||
|
String getDeDatabaseName(int userId) {
|
||||||
File databaseFile = new File(Environment.getDataSystemDeDirectory(userId),
|
File databaseFile = new File(Environment.getDataSystemDeDirectory(userId),
|
||||||
DE_DATABASE_NAME);
|
DE_DATABASE_NAME);
|
||||||
return databaseFile.getPath();
|
return databaseFile.getPath();
|
||||||
}
|
}
|
||||||
|
|
||||||
static String getCeDatabaseName(int userId) {
|
@VisibleForTesting
|
||||||
|
String getCeDatabaseName(int userId) {
|
||||||
File databaseFile = new File(Environment.getDataSystemCeDirectory(userId),
|
File databaseFile = new File(Environment.getDataSystemCeDirectory(userId),
|
||||||
CE_DATABASE_NAME);
|
CE_DATABASE_NAME);
|
||||||
return databaseFile.getPath();
|
return databaseFile.getPath();
|
||||||
@@ -4217,13 +4226,11 @@ public class AccountManagerService
|
|||||||
}
|
}
|
||||||
|
|
||||||
static class PreNDatabaseHelper extends SQLiteOpenHelper {
|
static class PreNDatabaseHelper extends SQLiteOpenHelper {
|
||||||
|
|
||||||
private final Context mContext;
|
private final Context mContext;
|
||||||
private final int mUserId;
|
private final int mUserId;
|
||||||
|
|
||||||
public PreNDatabaseHelper(Context context, int userId) {
|
public PreNDatabaseHelper(Context context, int userId, String preNDatabaseName) {
|
||||||
super(context, AccountManagerService.getPreNDatabaseName(userId), null,
|
super(context, preNDatabaseName, null, PRE_N_DATABASE_VERSION);
|
||||||
PRE_N_DATABASE_VERSION);
|
|
||||||
mContext = context;
|
mContext = context;
|
||||||
mUserId = userId;
|
mUserId = userId;
|
||||||
}
|
}
|
||||||
@@ -4360,8 +4367,8 @@ public class AccountManagerService
|
|||||||
private final int mUserId;
|
private final int mUserId;
|
||||||
private volatile boolean mCeAttached;
|
private volatile boolean mCeAttached;
|
||||||
|
|
||||||
private DeDatabaseHelper(Context context, int userId) {
|
private DeDatabaseHelper(Context context, int userId, String deDatabaseName) {
|
||||||
super(context, getDeDatabaseName(userId), null, DE_DATABASE_VERSION);
|
super(context, deDatabaseName, null, DE_DATABASE_VERSION);
|
||||||
mUserId = userId;
|
mUserId = userId;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -4426,8 +4433,7 @@ public class AccountManagerService
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void attachCeDatabase() {
|
public void attachCeDatabase(File ceDbFile) {
|
||||||
File ceDbFile = new File(getCeDatabaseName(mUserId));
|
|
||||||
SQLiteDatabase db = getWritableDatabase();
|
SQLiteDatabase db = getWritableDatabase();
|
||||||
db.execSQL("ATTACH DATABASE '" + ceDbFile.getPath()+ "' AS ceDb");
|
db.execSQL("ATTACH DATABASE '" + ceDbFile.getPath()+ "' AS ceDb");
|
||||||
mCeAttached = true;
|
mCeAttached = true;
|
||||||
@@ -4440,8 +4446,8 @@ public class AccountManagerService
|
|||||||
|
|
||||||
public SQLiteDatabase getReadableDatabaseUserIsUnlocked() {
|
public SQLiteDatabase getReadableDatabaseUserIsUnlocked() {
|
||||||
if(!mCeAttached) {
|
if(!mCeAttached) {
|
||||||
Log.wtf(TAG, "getReadableDatabaseUserIsUnlocked called while user "
|
Log.wtf(TAG, "getReadableDatabaseUserIsUnlocked called while user " + mUserId
|
||||||
+ mUserId + " is still locked ", new Throwable());
|
+ " is still locked. CE database is not yet available.", new Throwable());
|
||||||
}
|
}
|
||||||
return super.getReadableDatabase();
|
return super.getReadableDatabase();
|
||||||
}
|
}
|
||||||
@@ -4449,7 +4455,7 @@ public class AccountManagerService
|
|||||||
public SQLiteDatabase getWritableDatabaseUserIsUnlocked() {
|
public SQLiteDatabase getWritableDatabaseUserIsUnlocked() {
|
||||||
if(!mCeAttached) {
|
if(!mCeAttached) {
|
||||||
Log.wtf(TAG, "getWritableDatabaseUserIsUnlocked called while user " + mUserId
|
Log.wtf(TAG, "getWritableDatabaseUserIsUnlocked called while user " + mUserId
|
||||||
+ " is still locked ", new Throwable());
|
+ " is still locked. CE database is not yet available.", new Throwable());
|
||||||
}
|
}
|
||||||
return super.getWritableDatabase();
|
return super.getWritableDatabase();
|
||||||
}
|
}
|
||||||
@@ -4502,20 +4508,24 @@ public class AccountManagerService
|
|||||||
db.execSQL("DETACH DATABASE preNDb");
|
db.execSQL("DETACH DATABASE preNDb");
|
||||||
}
|
}
|
||||||
|
|
||||||
static DeDatabaseHelper create(Context context, int userId) {
|
static DeDatabaseHelper create(
|
||||||
File oldDb = new File(getPreNDatabaseName(userId));
|
Context context,
|
||||||
File newDb = new File(getDeDatabaseName(userId));
|
int userId,
|
||||||
boolean newDbExists = newDb.exists();
|
File preNDatabaseFile,
|
||||||
DeDatabaseHelper deDatabaseHelper = new DeDatabaseHelper(context, userId);
|
File deDatabaseFile) {
|
||||||
|
boolean newDbExists = deDatabaseFile.exists();
|
||||||
|
DeDatabaseHelper deDatabaseHelper = new DeDatabaseHelper(context, userId,
|
||||||
|
deDatabaseFile.getPath());
|
||||||
// If the db just created, and there is a legacy db, migrate it
|
// If the db just created, and there is a legacy db, migrate it
|
||||||
if (!newDbExists && oldDb.exists()) {
|
if (!newDbExists && preNDatabaseFile.exists()) {
|
||||||
// Migrate legacy db to the latest version - PRE_N_DATABASE_VERSION
|
// Migrate legacy db to the latest version - PRE_N_DATABASE_VERSION
|
||||||
PreNDatabaseHelper preNDatabaseHelper = new PreNDatabaseHelper(context, userId);
|
PreNDatabaseHelper preNDatabaseHelper = new PreNDatabaseHelper(context, userId,
|
||||||
|
preNDatabaseFile.getPath());
|
||||||
// Open the database to force upgrade if required
|
// Open the database to force upgrade if required
|
||||||
preNDatabaseHelper.getWritableDatabase();
|
preNDatabaseHelper.getWritableDatabase();
|
||||||
preNDatabaseHelper.close();
|
preNDatabaseHelper.close();
|
||||||
// Move data without SPII to DE
|
// Move data without SPII to DE
|
||||||
deDatabaseHelper.migratePreNDbToDe(oldDb);
|
deDatabaseHelper.migratePreNDbToDe(preNDatabaseFile);
|
||||||
}
|
}
|
||||||
return deDatabaseHelper;
|
return deDatabaseHelper;
|
||||||
}
|
}
|
||||||
@@ -4523,8 +4533,8 @@ public class AccountManagerService
|
|||||||
|
|
||||||
static class CeDatabaseHelper extends SQLiteOpenHelper {
|
static class CeDatabaseHelper extends SQLiteOpenHelper {
|
||||||
|
|
||||||
public CeDatabaseHelper(Context context, int userId) {
|
public CeDatabaseHelper(Context context, String ceDatabaseName) {
|
||||||
super(context, getCeDatabaseName(userId), null, CE_DATABASE_VERSION);
|
super(context, ceDatabaseName, null, CE_DATABASE_VERSION);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -4640,27 +4650,28 @@ public class AccountManagerService
|
|||||||
* @param context
|
* @param context
|
||||||
* @param userId id of the user where the database is located
|
* @param userId id of the user where the database is located
|
||||||
*/
|
*/
|
||||||
static CeDatabaseHelper create(Context context, int userId) {
|
static CeDatabaseHelper create(
|
||||||
|
Context context,
|
||||||
File oldDatabaseFile = new File(getPreNDatabaseName(userId));
|
int userId,
|
||||||
File ceDatabaseFile = new File(getCeDatabaseName(userId));
|
File preNDatabaseFile,
|
||||||
|
File ceDatabaseFile) {
|
||||||
boolean newDbExists = ceDatabaseFile.exists();
|
boolean newDbExists = ceDatabaseFile.exists();
|
||||||
if (Log.isLoggable(TAG, Log.VERBOSE)) {
|
if (Log.isLoggable(TAG, Log.VERBOSE)) {
|
||||||
Log.v(TAG, "CeDatabaseHelper.create userId=" + userId + " oldDbExists="
|
Log.v(TAG, "CeDatabaseHelper.create userId=" + userId + " oldDbExists="
|
||||||
+ oldDatabaseFile.exists() + " newDbExists=" + newDbExists);
|
+ preNDatabaseFile.exists() + " newDbExists=" + newDbExists);
|
||||||
}
|
}
|
||||||
boolean removeOldDb = false;
|
boolean removeOldDb = false;
|
||||||
if (!newDbExists && oldDatabaseFile.exists()) {
|
if (!newDbExists && preNDatabaseFile.exists()) {
|
||||||
removeOldDb = migratePreNDbToCe(oldDatabaseFile, ceDatabaseFile);
|
removeOldDb = migratePreNDbToCe(preNDatabaseFile, ceDatabaseFile);
|
||||||
}
|
}
|
||||||
// Try to open and upgrade if necessary
|
// Try to open and upgrade if necessary
|
||||||
CeDatabaseHelper ceHelper = new CeDatabaseHelper(context, userId);
|
CeDatabaseHelper ceHelper = new CeDatabaseHelper(context, ceDatabaseFile.getPath());
|
||||||
ceHelper.getWritableDatabase();
|
ceHelper.getWritableDatabase();
|
||||||
ceHelper.close();
|
ceHelper.close();
|
||||||
if (removeOldDb) {
|
if (removeOldDb) {
|
||||||
// TODO STOPSHIP - backup file during testing. Remove file before the release
|
// TODO STOPSHIP - backup file during testing. Remove file before the release
|
||||||
Log.i(TAG, "Migration complete - creating backup of old db " + oldDatabaseFile);
|
Log.i(TAG, "Migration complete - creating backup of old db " + preNDatabaseFile);
|
||||||
renameToBakFile(oldDatabaseFile);
|
renameToBakFile(preNDatabaseFile);
|
||||||
}
|
}
|
||||||
return ceHelper;
|
return ceHelper;
|
||||||
}
|
}
|
||||||
@@ -4825,12 +4836,14 @@ public class AccountManagerService
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@VisibleForTesting
|
||||||
protected void installNotification(final int notificationId, final Notification n,
|
protected void installNotification(final int notificationId, final Notification n,
|
||||||
UserHandle user) {
|
UserHandle user) {
|
||||||
((NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE))
|
((NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE))
|
||||||
.notifyAsUser(null, notificationId, n, user);
|
.notifyAsUser(null, notificationId, n, user);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@VisibleForTesting
|
||||||
protected void cancelNotification(int id, UserHandle user) {
|
protected void cancelNotification(int id, UserHandle user) {
|
||||||
long identityToken = clearCallingIdentity();
|
long identityToken = clearCallingIdentity();
|
||||||
try {
|
try {
|
||||||
@@ -5368,7 +5381,7 @@ public class AccountManagerService
|
|||||||
HashMap<String, String> userDataForAccount = accounts.userDataCache.get(account);
|
HashMap<String, String> userDataForAccount = accounts.userDataCache.get(account);
|
||||||
if (userDataForAccount == null) {
|
if (userDataForAccount == null) {
|
||||||
// need to populate the cache for this account
|
// need to populate the cache for this account
|
||||||
final SQLiteDatabase db = accounts.openHelper.getReadableDatabase();
|
final SQLiteDatabase db = accounts.openHelper.getReadableDatabaseUserIsUnlocked();
|
||||||
userDataForAccount = readUserDataForAccountFromDatabaseLocked(db, account);
|
userDataForAccount = readUserDataForAccountFromDatabaseLocked(db, account);
|
||||||
accounts.userDataCache.put(account, userDataForAccount);
|
accounts.userDataCache.put(account, userDataForAccount);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,23 +16,34 @@
|
|||||||
|
|
||||||
package com.android.server.accounts;
|
package com.android.server.accounts;
|
||||||
|
|
||||||
|
import static org.mockito.Matchers.eq;
|
||||||
|
import static org.mockito.Mockito.mock;
|
||||||
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
import android.accounts.Account;
|
import android.accounts.Account;
|
||||||
import android.accounts.AuthenticatorDescription;
|
import android.accounts.AuthenticatorDescription;
|
||||||
|
import android.app.AppOpsManager;
|
||||||
import android.app.Notification;
|
import android.app.Notification;
|
||||||
|
import android.content.BroadcastReceiver;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
|
import android.content.Intent;
|
||||||
|
import android.content.IntentFilter;
|
||||||
import android.content.pm.PackageManager;
|
import android.content.pm.PackageManager;
|
||||||
import android.content.pm.RegisteredServicesCache.ServiceInfo;
|
import android.content.pm.RegisteredServicesCache.ServiceInfo;
|
||||||
import android.content.pm.RegisteredServicesCacheListener;
|
import android.content.pm.RegisteredServicesCacheListener;
|
||||||
|
import android.content.pm.UserInfo;
|
||||||
|
import android.database.DatabaseErrorHandler;
|
||||||
|
import android.database.sqlite.SQLiteDatabase;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.os.Handler;
|
import android.os.Handler;
|
||||||
import android.os.UserHandle;
|
import android.os.UserHandle;
|
||||||
|
import android.os.UserManager;
|
||||||
import android.test.AndroidTestCase;
|
import android.test.AndroidTestCase;
|
||||||
import android.test.IsolatedContext;
|
|
||||||
import android.test.RenamingDelegatingContext;
|
|
||||||
import android.test.mock.MockContentResolver;
|
|
||||||
import android.test.mock.MockContext;
|
import android.test.mock.MockContext;
|
||||||
import android.test.mock.MockPackageManager;
|
import android.test.mock.MockPackageManager;
|
||||||
|
import android.util.Log;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
import java.io.FileDescriptor;
|
import java.io.FileDescriptor;
|
||||||
import java.io.PrintWriter;
|
import java.io.PrintWriter;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
@@ -41,20 +52,28 @@ import java.util.Collection;
|
|||||||
import java.util.Comparator;
|
import java.util.Comparator;
|
||||||
|
|
||||||
public class AccountManagerServiceTest extends AndroidTestCase {
|
public class AccountManagerServiceTest extends AndroidTestCase {
|
||||||
|
private static final String TAG = AccountManagerServiceTest.class.getSimpleName();
|
||||||
|
|
||||||
|
static final String PREN_DB = "pren.db";
|
||||||
|
static final String DE_DB = "de.db";
|
||||||
|
static final String CE_DB = "ce.db";
|
||||||
private AccountManagerService mAms;
|
private AccountManagerService mAms;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void setUp() throws Exception {
|
protected void setUp() throws Exception {
|
||||||
final String filenamePrefix = "test.";
|
Context realTestContext = getContext();
|
||||||
MockContentResolver resolver = new MockContentResolver();
|
Context mockContext = new MyMockContext(realTestContext);
|
||||||
RenamingDelegatingContext targetContextWrapper = new RenamingDelegatingContext(
|
setContext(mockContext);
|
||||||
new MyMockContext(), // The context that most methods are delegated to
|
|
||||||
getContext(), // The context that file methods are delegated to
|
|
||||||
filenamePrefix);
|
|
||||||
Context context = new IsolatedContext(resolver, targetContextWrapper);
|
|
||||||
setContext(context);
|
|
||||||
mAms = new MyAccountManagerService(getContext(),
|
mAms = new MyAccountManagerService(getContext(),
|
||||||
new MyMockPackageManager(), new MockAccountAuthenticatorCache());
|
new MyMockPackageManager(), new MockAccountAuthenticatorCache(), realTestContext);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void tearDown() throws Exception {
|
||||||
|
new File(mAms.getCeDatabaseName(UserHandle.USER_SYSTEM)).delete();
|
||||||
|
new File(mAms.getDeDatabaseName(UserHandle.USER_SYSTEM)).delete();
|
||||||
|
new File(mAms.getPreNDatabaseName(UserHandle.USER_SYSTEM)).delete();
|
||||||
|
super.tearDown();
|
||||||
}
|
}
|
||||||
|
|
||||||
public class AccountSorter implements Comparator<Account> {
|
public class AccountSorter implements Comparator<Account> {
|
||||||
@@ -69,6 +88,7 @@ public class AccountManagerServiceTest extends AndroidTestCase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void testCheckAddAccount() throws Exception {
|
public void testCheckAddAccount() throws Exception {
|
||||||
|
unlockUser(UserHandle.USER_SYSTEM);
|
||||||
Account a11 = new Account("account1", "type1");
|
Account a11 = new Account("account1", "type1");
|
||||||
Account a21 = new Account("account2", "type1");
|
Account a21 = new Account("account2", "type1");
|
||||||
Account a31 = new Account("account3", "type1");
|
Account a31 = new Account("account3", "type1");
|
||||||
@@ -109,6 +129,7 @@ public class AccountManagerServiceTest extends AndroidTestCase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void testPasswords() throws Exception {
|
public void testPasswords() throws Exception {
|
||||||
|
unlockUser(UserHandle.USER_SYSTEM);
|
||||||
Account a11 = new Account("account1", "type1");
|
Account a11 = new Account("account1", "type1");
|
||||||
Account a12 = new Account("account1", "type2");
|
Account a12 = new Account("account1", "type2");
|
||||||
mAms.addAccountExplicitly(a11, "p11", null);
|
mAms.addAccountExplicitly(a11, "p11", null);
|
||||||
@@ -124,6 +145,7 @@ public class AccountManagerServiceTest extends AndroidTestCase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void testUserdata() throws Exception {
|
public void testUserdata() throws Exception {
|
||||||
|
unlockUser(UserHandle.USER_SYSTEM);
|
||||||
Account a11 = new Account("account1", "type1");
|
Account a11 = new Account("account1", "type1");
|
||||||
Bundle u11 = new Bundle();
|
Bundle u11 = new Bundle();
|
||||||
u11.putString("a", "a_a11");
|
u11.putString("a", "a_a11");
|
||||||
@@ -156,6 +178,7 @@ public class AccountManagerServiceTest extends AndroidTestCase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void testAuthtokens() throws Exception {
|
public void testAuthtokens() throws Exception {
|
||||||
|
unlockUser(UserHandle.USER_SYSTEM);
|
||||||
Account a11 = new Account("account1", "type1");
|
Account a11 = new Account("account1", "type1");
|
||||||
Account a12 = new Account("account1", "type2");
|
Account a12 = new Account("account1", "type2");
|
||||||
mAms.addAccountExplicitly(a11, "p11", null);
|
mAms.addAccountExplicitly(a11, "p11", null);
|
||||||
@@ -188,15 +211,21 @@ public class AccountManagerServiceTest extends AndroidTestCase {
|
|||||||
assertNull(mAms.peekAuthToken(a12, "att2"));
|
assertNull(mAms.peekAuthToken(a12, "att2"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void unlockUser(int userId) {
|
||||||
|
Intent intent = new Intent();
|
||||||
|
intent.putExtra(Intent.EXTRA_USER_HANDLE, userId);
|
||||||
|
mAms.onUserUnlocked(intent);
|
||||||
|
}
|
||||||
|
|
||||||
static public class MockAccountAuthenticatorCache implements IAccountAuthenticatorCache {
|
static public class MockAccountAuthenticatorCache implements IAccountAuthenticatorCache {
|
||||||
private ArrayList<ServiceInfo<AuthenticatorDescription>> mServices;
|
private ArrayList<ServiceInfo<AuthenticatorDescription>> mServices;
|
||||||
|
|
||||||
public MockAccountAuthenticatorCache() {
|
public MockAccountAuthenticatorCache() {
|
||||||
mServices = new ArrayList<ServiceInfo<AuthenticatorDescription>>();
|
mServices = new ArrayList<>();
|
||||||
AuthenticatorDescription d1 = new AuthenticatorDescription("type1", "p1", 0, 0, 0, 0);
|
AuthenticatorDescription d1 = new AuthenticatorDescription("type1", "p1", 0, 0, 0, 0);
|
||||||
AuthenticatorDescription d2 = new AuthenticatorDescription("type2", "p2", 0, 0, 0, 0);
|
AuthenticatorDescription d2 = new AuthenticatorDescription("type2", "p2", 0, 0, 0, 0);
|
||||||
mServices.add(new ServiceInfo<AuthenticatorDescription>(d1, null, null));
|
mServices.add(new ServiceInfo<>(d1, null, null));
|
||||||
mServices.add(new ServiceInfo<AuthenticatorDescription>(d2, null, null));
|
mServices.add(new ServiceInfo<>(d2, null, null));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -232,10 +261,68 @@ public class AccountManagerServiceTest extends AndroidTestCase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static public class MyMockContext extends MockContext {
|
static public class MyMockContext extends MockContext {
|
||||||
|
private Context mTestContext;
|
||||||
|
private AppOpsManager mAppOpsManager;
|
||||||
|
private UserManager mUserManager;
|
||||||
|
|
||||||
|
public MyMockContext(Context testContext) {
|
||||||
|
this.mTestContext = testContext;
|
||||||
|
this.mAppOpsManager = mock(AppOpsManager.class);
|
||||||
|
this.mUserManager = mock(UserManager.class);
|
||||||
|
final UserInfo ui = new UserInfo(UserHandle.USER_SYSTEM, "user0", 0);
|
||||||
|
when(mUserManager.getUserInfo(eq(ui.id))).thenReturn(ui);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int checkCallingOrSelfPermission(final String permission) {
|
public int checkCallingOrSelfPermission(final String permission) {
|
||||||
return PackageManager.PERMISSION_GRANTED;
|
return PackageManager.PERMISSION_GRANTED;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Object getSystemService(String name) {
|
||||||
|
if (Context.APP_OPS_SERVICE.equals(name)) {
|
||||||
|
return mAppOpsManager;
|
||||||
|
} else if( Context.USER_SERVICE.equals(name)) {
|
||||||
|
return mUserManager;
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getSystemServiceName(Class<?> serviceClass) {
|
||||||
|
if (AppOpsManager.class.equals(serviceClass)) {
|
||||||
|
return Context.APP_OPS_SERVICE;
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Intent registerReceiverAsUser(BroadcastReceiver receiver, UserHandle user,
|
||||||
|
IntentFilter filter, String broadcastPermission, Handler scheduler) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public SQLiteDatabase openOrCreateDatabase(String file, int mode,
|
||||||
|
SQLiteDatabase.CursorFactory factory, DatabaseErrorHandler errorHandler) {
|
||||||
|
Log.i(TAG, "openOrCreateDatabase " + file + " mode " + mode);
|
||||||
|
return mTestContext.openOrCreateDatabase(file, mode, factory,errorHandler);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void sendBroadcastAsUser(Intent intent, UserHandle user) {
|
||||||
|
Log.i(TAG, "sendBroadcastAsUser " + intent + " " + user);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getOpPackageName() {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static public class MyMockPackageManager extends MockPackageManager {
|
static public class MyMockPackageManager extends MockPackageManager {
|
||||||
@@ -246,9 +333,11 @@ public class AccountManagerServiceTest extends AndroidTestCase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static public class MyAccountManagerService extends AccountManagerService {
|
static public class MyAccountManagerService extends AccountManagerService {
|
||||||
|
private Context mRealTestContext;
|
||||||
public MyAccountManagerService(Context context, PackageManager packageManager,
|
public MyAccountManagerService(Context context, PackageManager packageManager,
|
||||||
IAccountAuthenticatorCache authenticatorCache) {
|
IAccountAuthenticatorCache authenticatorCache, Context realTestContext) {
|
||||||
super(context, packageManager, authenticatorCache);
|
super(context, packageManager, authenticatorCache);
|
||||||
|
this.mRealTestContext = realTestContext;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -258,5 +347,20 @@ public class AccountManagerServiceTest extends AndroidTestCase {
|
|||||||
@Override
|
@Override
|
||||||
protected void cancelNotification(final int id, UserHandle user) {
|
protected void cancelNotification(final int id, UserHandle user) {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected String getCeDatabaseName(int userId) {
|
||||||
|
return new File(mRealTestContext.getCacheDir(), CE_DB).getPath();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected String getDeDatabaseName(int userId) {
|
||||||
|
return new File(mRealTestContext.getCacheDir(), DE_DB).getPath();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
String getPreNDatabaseName(int userId) {
|
||||||
|
return new File(mRealTestContext.getCacheDir(), PREN_DB).getPath();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user