android.test.IsolatedContext Java Examples

The following examples show how to use android.test.IsolatedContext. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: LocationProviderTestCase.java    From background-geolocation-android with Apache License 2.0 6 votes vote down vote up
@Override
protected void setUp() throws Exception {
    super.setUp();

    mResolver = new MockContentResolver();
    RenamingDelegatingContext targetContextWrapper = new RenamingDelegatingContext(
            new DelegatedMockContext(getContext()), // The context that most methods are delegated to
            getContext(), // The context that file methods are delegated to
            filenamePrefix);
    mProviderContext = new IsolatedContext(mResolver, targetContextWrapper);

    ProviderInfo providerInfo = new ProviderInfo();
    providerInfo.authority = mProviderAuthority;

    mProvider = mProviderClass.newInstance();
    mProvider.attachInfo(mProviderContext, providerInfo);
    assertNotNull(mProvider);
    mResolver.addProvider(mProviderAuthority, mProvider);
}
 
Example #2
Source File: UniversalSearchContractTest.java    From android-tv-leanback with Apache License 2.0 6 votes vote down vote up
private SQLiteDatabase createTables(final boolean delete, final UniversalSearchContract.Table... tables) {
    SQLiteDatabase db = null;
    final String filenamePrefix = "test.";
    RenamingDelegatingContext targetContextWrapper = new
            RenamingDelegatingContext(
            new MockContext2(), // The context that most methods are
            //delegated to
            getContext(), // The context that file methods are delegated to
            filenamePrefix);
    final Context context = new IsolatedContext(super.getContext().getContentResolver(), targetContextWrapper);
    try {
        db = context.openOrCreateDatabase(DATABASE, 1, null);
        for (final UniversalSearchContract.Table table : tables) {
            table.onCreate(db);
            log("Table " + table + " onCreate successful");
        }
        return db;
    } finally {
        if (null != db && delete) {
            db.close();
            context.deleteDatabase(DATABASE);
        }
    }
}
 
Example #3
Source File: LocationProviderTestCase.java    From background-geolocation-android with Apache License 2.0 4 votes vote down vote up
public IsolatedContext getMockContext() {
    return mProviderContext;
}
 
Example #4
Source File: TrayProviderHelperTest.java    From tray with Apache License 2.0 4 votes vote down vote up
public void testClearBut() throws Exception {
    // We need a package name in this test, thus creating our own mock context
    final IsolatedContext context = getProviderMockContext();

    assertTrue(mProviderHelper.persist(MODULE_A, KEY_A, STRING_A));
    assertTrue(mProviderHelper.persist(MODULE_A, KEY_B, STRING_B));
    assertTrue(mProviderHelper.persist(MODULE_B, KEY_A, STRING_A));
    assertTrue(mProviderHelper.persist(MODULE_B, KEY_B, STRING_B));
    assertTrue(mProviderHelper.persist(MODULE_C, KEY_A, STRING_A));
    assertTrue(mProviderHelper.persist(MODULE_C, KEY_B, STRING_B));
    assertTrue(mProviderHelper.persist(context.getPackageName(), KEY_A, STRING_A));
    assertTrue(mProviderHelper.persist(context.getPackageName(), KEY_B, STRING_B));
    assertUserDatabaseSize(8);

    mProviderHelper.clearBut(new AppPreferences(context),
            new TestTrayModulePreferences(context, MODULE_A),
            new TestTrayModulePreferences(context, MODULE_B));
    assertUserDatabaseSize(6);

    mProviderHelper.clearBut(new TestTrayModulePreferences(context, MODULE_A),
            new TestTrayModulePreferences(context, MODULE_B));
    assertUserDatabaseSize(4);

    mProviderHelper.clearBut(new TestTrayModulePreferences(context, MODULE_A));
    assertUserDatabaseSize(2);

    mProviderHelper.clearBut((TrayPreferences) null);
    assertUserDatabaseSize(0);

    assertTrue(mProviderHelper.persist(MODULE_A, KEY_A, STRING_A));
    assertTrue(mProviderHelper.persist(MODULE_A, KEY_B, STRING_B));
    assertTrue(mProviderHelper.persist(context.getPackageName(), KEY_A, STRING_A));
    assertTrue(mProviderHelper.persist(context.getPackageName(), KEY_B, STRING_B));
    mProviderHelper.clearBut(new AppPreferences(context));
    assertUserDatabaseSize(2);

    // Also test empty values (= clear everything)
    assertTrue(mProviderHelper.persist(MODULE_A, KEY_A, STRING_A));
    assertTrue(mProviderHelper.persist(MODULE_A, KEY_B, STRING_B));

    mProviderHelper.clearBut((TrayPreferences) null);
    assertUserDatabaseSize(0);
}