Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 9ad66541 authored by Dianne Hackborn's avatar Dianne Hackborn Committed by Android (Google) Code Review
Browse files

Merge "New ContentProvider initialization for testing." into jb-mr2-dev

parents be1a8cda 334d9aeb
Loading
Loading
Loading
Loading
+16 −10
Original line number Original line Diff line number Diff line
@@ -16,6 +16,7 @@


package android.content;
package android.content;


import static android.content.pm.PackageManager.GET_PROVIDERS;
import static android.content.pm.PackageManager.PERMISSION_GRANTED;
import static android.content.pm.PackageManager.PERMISSION_GRANTED;


import android.app.AppOpsManager;
import android.app.AppOpsManager;
@@ -1168,6 +1169,15 @@ public abstract class ContentProvider implements ComponentCallbacks2 {
        return mTransport;
        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
     * After being instantiated, this is called to tell the content provider
     * about itself.
     * about itself.
@@ -1176,12 +1186,18 @@ public abstract class ContentProvider implements ComponentCallbacks2 {
     * @param info Registered information about this content provider
     * @param info Registered information about this content provider
     */
     */
    public void attachInfo(Context context, ProviderInfo info) {
    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
         * We may be using AsyncTask from binder threads.  Make it init here
         * so its static handler is on the main thread.
         * so its static handler is on the main thread.
         */
         */
        AsyncTask.init();
        AsyncTask.init();


        mNoPerms = testing;

        /*
        /*
         * Only allow it to be set once, so after the content service gives
         * Only allow it to be set once, so after the content service gives
         * this to us clients can't change it.
         * this to us clients can't change it.
@@ -1194,16 +1210,6 @@ public abstract class ContentProvider implements ComponentCallbacks2 {
                setWritePermission(info.writePermission);
                setWritePermission(info.writePermission);
                setPathPermissions(info.pathPermissions);
                setPathPermissions(info.pathPermissions);
                mExported = info.exported;
                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();
            ContentProvider.this.onCreate();
        }
        }
+2 −2
Original line number Original line Diff line number Diff line
@@ -68,7 +68,7 @@ public abstract class ProviderTestCase<T extends ContentProvider>
        mProviderContext = new IsolatedContext(mResolver, targetContextWrapper);
        mProviderContext = new IsolatedContext(mResolver, targetContextWrapper);


        mProvider = mProviderClass.newInstance();
        mProvider = mProviderClass.newInstance();
        mProvider.attachInfo(mProviderContext, null);
        mProvider.attachInfoForTesting(mProviderContext, null);
        assertNotNull(mProvider);
        assertNotNull(mProvider);
        mResolver.addProvider(mProviderAuthority, getProvider());
        mResolver.addProvider(mProviderAuthority, getProvider());
    }
    }
@@ -108,7 +108,7 @@ public abstract class ProviderTestCase<T extends ContentProvider>
        DatabaseUtils.createDbFromSqlStatements(context, databaseName, databaseVersion, sql);
        DatabaseUtils.createDbFromSqlStatements(context, databaseName, databaseVersion, sql);


        T provider = providerClass.newInstance();
        T provider = providerClass.newInstance();
        provider.attachInfo(context, null);
        provider.attachInfoForTesting(context, null);
        resolver.addProvider(authority, provider);
        resolver.addProvider(authority, provider);


        return resolver;
        return resolver;
+2 −2
Original line number Original line Diff line number Diff line
@@ -140,7 +140,7 @@ public abstract class ProviderTestCase2<T extends ContentProvider> extends Andro
        mProviderContext = new IsolatedContext(mResolver, targetContextWrapper);
        mProviderContext = new IsolatedContext(mResolver, targetContextWrapper);


        mProvider = mProviderClass.newInstance();
        mProvider = mProviderClass.newInstance();
        mProvider.attachInfo(mProviderContext, null);
        mProvider.attachInfoForTesting(mProviderContext, null);
        assertNotNull(mProvider);
        assertNotNull(mProvider);
        mResolver.addProvider(mProviderAuthority, getProvider());
        mResolver.addProvider(mProviderAuthority, getProvider());
    }
    }
@@ -219,7 +219,7 @@ public abstract class ProviderTestCase2<T extends ContentProvider> extends Andro
        DatabaseUtils.createDbFromSqlStatements(context, databaseName, databaseVersion, sql);
        DatabaseUtils.createDbFromSqlStatements(context, databaseName, databaseVersion, sql);


        T provider = providerClass.newInstance();
        T provider = providerClass.newInstance();
        provider.attachInfo(context, null);
        provider.attachInfoForTesting(context, null);
        resolver.addProvider(authority, provider);
        resolver.addProvider(authority, provider);


        return resolver;
        return resolver;
+1 −1
Original line number Original line Diff line number Diff line
@@ -63,7 +63,7 @@ public class RenamingDelegatingContext extends ContextWrapper {
        if (allowAccessToExistingFilesAndDbs) {
        if (allowAccessToExistingFilesAndDbs) {
            mContext.makeExistingFilesAndDbsAccessible();
            mContext.makeExistingFilesAndDbsAccessible();
        }
        }
        mProvider.attachInfo(mContext, null);
        mProvider.attachInfoForTesting(mContext, null);
        return mProvider;
        return mProvider;
    }
    }