Search in sources :

Example 61 with MockContentResolver

use of android.test.mock.MockContentResolver in project android_frameworks_base by ResurrectionRemix.

the class ModelTest method setupTestContext.

private void setupTestContext() {
    final MockContentResolver resolver = new MockContentResolver();
    context = new ContextWrapper(getContext()) {

        @Override
        public ContentResolver getContentResolver() {
            return resolver;
        }
    };
    provider = new TestContentProvider();
    resolver.addProvider(AUTHORITY, provider);
}
Also used : MockContentResolver(android.test.mock.MockContentResolver) ContextWrapper(android.content.ContextWrapper) ContentResolver(android.content.ContentResolver) MockContentResolver(android.test.mock.MockContentResolver)

Example 62 with MockContentResolver

use of android.test.mock.MockContentResolver in project android_frameworks_base by ResurrectionRemix.

the class TestContext method testSyncableMigration.

@SmallTest
public void testSyncableMigration() throws Exception {
    final Account account = new Account("acc", "type");
    MockContentResolver mockResolver = new MockContentResolver();
    final TestContext testContext = new TestContext(mockResolver, getContext());
    byte[] accountsFileData = ("<?xml version='1.0' encoding='utf-8' standalone='yes' ?>\n" + "<accounts>\n" + "<authority id=\"0\" account=\"acc\" authority=\"other1\" />\n" + "<authority id=\"1\" account=\"acc\" type=\"type\" authority=\"other2\" />\n" + "<authority id=\"2\" account=\"acc\" type=\"type\" syncable=\"false\"" + " authority=\"other3\" />\n" + "<authority id=\"3\" account=\"acc\" type=\"type\" syncable=\"true\"" + " authority=\"other4\" />\n" + "</accounts>\n").getBytes();
    File syncDir = new File(new File(testContext.getFilesDir(), "system"), "sync");
    syncDir.mkdirs();
    AtomicFile accountInfoFile = new AtomicFile(new File(syncDir, "accounts.xml"));
    FileOutputStream fos = accountInfoFile.startWrite();
    fos.write(accountsFileData);
    accountInfoFile.finishWrite(fos);
    SyncStorageEngine engine = SyncStorageEngine.newTestInstance(testContext);
    assertEquals(-1, engine.getIsSyncable(account, 0, "other1"));
    assertEquals(1, engine.getIsSyncable(account, 0, "other2"));
    assertEquals(0, engine.getIsSyncable(account, 0, "other3"));
    assertEquals(1, engine.getIsSyncable(account, 0, "other4"));
}
Also used : Account(android.accounts.Account) AtomicFile(com.android.internal.os.AtomicFile) FileOutputStream(java.io.FileOutputStream) MockContentResolver(android.test.mock.MockContentResolver) File(java.io.File) AtomicFile(com.android.internal.os.AtomicFile) SmallTest(android.test.suitebuilder.annotation.SmallTest)

Example 63 with MockContentResolver

use of android.test.mock.MockContentResolver in project android_frameworks_base by ResurrectionRemix.

the class ProviderTestCase2 method newResolverWithContentProviderFromSql.

/**
     * <p>
     *      Creates a new content provider of the same type as that passed to the test case class,
     *      with an authority name set to the authority parameter, and using an SQLite database as
     *      the underlying data source. The SQL statement parameter is used to create the database.
     *      This method also creates a new {@link MockContentResolver} and adds the provider to it.
     * </p>
     * <p>
     *      Both the new provider and the new resolver are put into an {@link IsolatedContext}
     *      that uses the targetContext parameter for file operations and a {@link MockContext}
     *      for everything else. The IsolatedContext prepends the filenamePrefix parameter to
     *      file, database, and directory names.
     * </p>
     * <p>
     *      This is a convenience method for creating a "mock" provider that can contain test data.
     * </p>
     *
     * @param targetContext The context to use as the basis of the IsolatedContext
     * @param filenamePrefix A string that is prepended to file, database, and directory names
     * @param providerClass The type of the provider being tested
     * @param authority The authority string to associated with the test provider
     * @param databaseName The name assigned to the database
     * @param databaseVersion The version assigned to the database
     * @param sql A string containing the SQL statements that are needed to create the desired
     * database and its tables. The format is the same as that generated by the
     * <a href="http://www.sqlite.org/sqlite.html">sqlite3</a> tool's <code>.dump</code> command.
     * @return ContentResolver A new {@link MockContentResolver} linked to the provider
     *
     * @throws IllegalAccessException
     * @throws InstantiationException
     */
public static <T extends ContentProvider> ContentResolver newResolverWithContentProviderFromSql(Context targetContext, String filenamePrefix, Class<T> providerClass, String authority, String databaseName, int databaseVersion, String sql) throws IllegalAccessException, InstantiationException {
    MockContentResolver resolver = new MockContentResolver();
    RenamingDelegatingContext targetContextWrapper = new RenamingDelegatingContext(// The context that most methods are delegated to
    new MockContext(), // The context that file methods are delegated to
    targetContext, filenamePrefix);
    Context context = new IsolatedContext(resolver, targetContextWrapper);
    DatabaseUtils.createDbFromSqlStatements(context, databaseName, databaseVersion, sql);
    T provider = createProviderForTest(context, providerClass, authority);
    resolver.addProvider(authority, provider);
    return resolver;
}
Also used : Context(android.content.Context) MockContext(android.test.mock.MockContext) MockContext(android.test.mock.MockContext) MockContentResolver(android.test.mock.MockContentResolver)

Example 64 with MockContentResolver

use of android.test.mock.MockContentResolver in project android_frameworks_base by crdroidandroid.

the class ProviderTestCase2 method newResolverWithContentProviderFromSql.

/**
     * <p>
     *      Creates a new content provider of the same type as that passed to the test case class,
     *      with an authority name set to the authority parameter, and using an SQLite database as
     *      the underlying data source. The SQL statement parameter is used to create the database.
     *      This method also creates a new {@link MockContentResolver} and adds the provider to it.
     * </p>
     * <p>
     *      Both the new provider and the new resolver are put into an {@link IsolatedContext}
     *      that uses the targetContext parameter for file operations and a {@link MockContext}
     *      for everything else. The IsolatedContext prepends the filenamePrefix parameter to
     *      file, database, and directory names.
     * </p>
     * <p>
     *      This is a convenience method for creating a "mock" provider that can contain test data.
     * </p>
     *
     * @param targetContext The context to use as the basis of the IsolatedContext
     * @param filenamePrefix A string that is prepended to file, database, and directory names
     * @param providerClass The type of the provider being tested
     * @param authority The authority string to associated with the test provider
     * @param databaseName The name assigned to the database
     * @param databaseVersion The version assigned to the database
     * @param sql A string containing the SQL statements that are needed to create the desired
     * database and its tables. The format is the same as that generated by the
     * <a href="http://www.sqlite.org/sqlite.html">sqlite3</a> tool's <code>.dump</code> command.
     * @return ContentResolver A new {@link MockContentResolver} linked to the provider
     *
     * @throws IllegalAccessException
     * @throws InstantiationException
     */
public static <T extends ContentProvider> ContentResolver newResolverWithContentProviderFromSql(Context targetContext, String filenamePrefix, Class<T> providerClass, String authority, String databaseName, int databaseVersion, String sql) throws IllegalAccessException, InstantiationException {
    MockContentResolver resolver = new MockContentResolver();
    RenamingDelegatingContext targetContextWrapper = new RenamingDelegatingContext(// The context that most methods are delegated to
    new MockContext(), // The context that file methods are delegated to
    targetContext, filenamePrefix);
    Context context = new IsolatedContext(resolver, targetContextWrapper);
    DatabaseUtils.createDbFromSqlStatements(context, databaseName, databaseVersion, sql);
    T provider = createProviderForTest(context, providerClass, authority);
    resolver.addProvider(authority, provider);
    return resolver;
}
Also used : Context(android.content.Context) MockContext(android.test.mock.MockContext) MockContext(android.test.mock.MockContext) MockContentResolver(android.test.mock.MockContentResolver)

Example 65 with MockContentResolver

use of android.test.mock.MockContentResolver in project android_frameworks_base by crdroidandroid.

the class ProviderTestCase2 method setUp.

/**
     * Sets up the environment for the test fixture.
     * <p>
     * Creates a new
     * {@link android.test.mock.MockContentResolver}, a new IsolatedContext
     * that isolates the provider's file operations, and a new instance of
     * the provider under test within the isolated environment.
     * </p>
     *
     * @throws Exception
     */
@Override
protected void setUp() throws Exception {
    super.setUp();
    mResolver = new MockContentResolver();
    final String filenamePrefix = "test.";
    RenamingDelegatingContext targetContextWrapper = new RenamingDelegatingContext(// The context that most methods are
    new MockContext2(), // The context that file methods are delegated to
    getContext(), filenamePrefix);
    mProviderContext = new IsolatedContext(mResolver, targetContextWrapper);
    mProvider = createProviderForTest(mProviderContext, mProviderClass, mProviderAuthority);
    mResolver.addProvider(mProviderAuthority, getProvider());
}
Also used : MockContentResolver(android.test.mock.MockContentResolver)

Aggregations

MockContentResolver (android.test.mock.MockContentResolver)79 Account (android.accounts.Account)22 MockContext (android.test.mock.MockContext)17 AtomicFile (com.android.internal.os.AtomicFile)16 File (java.io.File)16 FileOutputStream (java.io.FileOutputStream)16 Context (android.content.Context)13 MediumTest (android.test.suitebuilder.annotation.MediumTest)12 ContextWrapper (android.content.ContextWrapper)10 Bundle (android.os.Bundle)8 Before (org.junit.Before)8 ContentResolver (android.content.ContentResolver)5 SmallTest (android.test.suitebuilder.annotation.SmallTest)5 ComponentName (android.content.ComponentName)4 MockContentProvider (com.android.contacts.common.test.mocks.MockContentProvider)4 PeriodicSync (android.content.PeriodicSync)3 ProviderInfo (android.content.pm.ProviderInfo)3 IsolatedContext (android.test.IsolatedContext)3 Uri (android.net.Uri)2 RemoteException (android.os.RemoteException)2