New ContentProvider initialization for testing.
Using this turns off app ops checks. Change-Id: If29d4ca2fe9ddf1a1663d3a824b2f0afe7375862
This commit is contained in:
@@ -16,6 +16,7 @@
|
||||
|
||||
package android.content;
|
||||
|
||||
import static android.content.pm.PackageManager.GET_PROVIDERS;
|
||||
import static android.content.pm.PackageManager.PERMISSION_GRANTED;
|
||||
|
||||
import android.app.AppOpsManager;
|
||||
@@ -1168,6 +1169,15 @@ public abstract class ContentProvider implements ComponentCallbacks2 {
|
||||
return mTransport;
|
||||
}
|
||||
|
||||
/**
|
||||
* Like {@link #attachInfo(Context, android.content.pm.ProviderInfo)}, but for use
|
||||
* when directly instantiating the provider for testing.
|
||||
* @hide
|
||||
*/
|
||||
public void attachInfoForTesting(Context context, ProviderInfo info) {
|
||||
attachInfo(context, info, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* After being instantiated, this is called to tell the content provider
|
||||
* about itself.
|
||||
@@ -1176,12 +1186,18 @@ public abstract class ContentProvider implements ComponentCallbacks2 {
|
||||
* @param info Registered information about this content provider
|
||||
*/
|
||||
public void attachInfo(Context context, ProviderInfo info) {
|
||||
attachInfo(context, info, false);
|
||||
}
|
||||
|
||||
private void attachInfo(Context context, ProviderInfo info, boolean testing) {
|
||||
/*
|
||||
* We may be using AsyncTask from binder threads. Make it init here
|
||||
* so its static handler is on the main thread.
|
||||
*/
|
||||
AsyncTask.init();
|
||||
|
||||
mNoPerms = testing;
|
||||
|
||||
/*
|
||||
* Only allow it to be set once, so after the content service gives
|
||||
* this to us clients can't change it.
|
||||
@@ -1194,16 +1210,6 @@ public abstract class ContentProvider implements ComponentCallbacks2 {
|
||||
setWritePermission(info.writePermission);
|
||||
setPathPermissions(info.pathPermissions);
|
||||
mExported = info.exported;
|
||||
mNoPerms = false;
|
||||
} else {
|
||||
// We enter here because the content provider is being instantiated
|
||||
// as a mock. We don't have any information about the provider (such
|
||||
// as its required permissions), and also want to avoid doing app op
|
||||
// checks since these aren't real calls coming in and we may not be
|
||||
// able to get the app ops service at all (if the test is using something
|
||||
// like the IsolatedProvider). So set this to true, to prevent us
|
||||
// from enabling app ops on this object.
|
||||
mNoPerms = true;
|
||||
}
|
||||
ContentProvider.this.onCreate();
|
||||
}
|
||||
|
||||
@@ -68,7 +68,7 @@ public abstract class ProviderTestCase<T extends ContentProvider>
|
||||
mProviderContext = new IsolatedContext(mResolver, targetContextWrapper);
|
||||
|
||||
mProvider = mProviderClass.newInstance();
|
||||
mProvider.attachInfo(mProviderContext, null);
|
||||
mProvider.attachInfoForTesting(mProviderContext, null);
|
||||
assertNotNull(mProvider);
|
||||
mResolver.addProvider(mProviderAuthority, getProvider());
|
||||
}
|
||||
@@ -108,7 +108,7 @@ public abstract class ProviderTestCase<T extends ContentProvider>
|
||||
DatabaseUtils.createDbFromSqlStatements(context, databaseName, databaseVersion, sql);
|
||||
|
||||
T provider = providerClass.newInstance();
|
||||
provider.attachInfo(context, null);
|
||||
provider.attachInfoForTesting(context, null);
|
||||
resolver.addProvider(authority, provider);
|
||||
|
||||
return resolver;
|
||||
|
||||
@@ -140,7 +140,7 @@ public abstract class ProviderTestCase2<T extends ContentProvider> extends Andro
|
||||
mProviderContext = new IsolatedContext(mResolver, targetContextWrapper);
|
||||
|
||||
mProvider = mProviderClass.newInstance();
|
||||
mProvider.attachInfo(mProviderContext, null);
|
||||
mProvider.attachInfoForTesting(mProviderContext, null);
|
||||
assertNotNull(mProvider);
|
||||
mResolver.addProvider(mProviderAuthority, getProvider());
|
||||
}
|
||||
@@ -219,7 +219,7 @@ public abstract class ProviderTestCase2<T extends ContentProvider> extends Andro
|
||||
DatabaseUtils.createDbFromSqlStatements(context, databaseName, databaseVersion, sql);
|
||||
|
||||
T provider = providerClass.newInstance();
|
||||
provider.attachInfo(context, null);
|
||||
provider.attachInfoForTesting(context, null);
|
||||
resolver.addProvider(authority, provider);
|
||||
|
||||
return resolver;
|
||||
|
||||
@@ -63,7 +63,7 @@ public class RenamingDelegatingContext extends ContextWrapper {
|
||||
if (allowAccessToExistingFilesAndDbs) {
|
||||
mContext.makeExistingFilesAndDbsAccessible();
|
||||
}
|
||||
mProvider.attachInfo(mContext, null);
|
||||
mProvider.attachInfoForTesting(mContext, null);
|
||||
return mProvider;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user