use of android.test.mock.MockContentResolver in project android_frameworks_base by ParanoidAndroid.
the class TestContext method testPurgeActiveSync.
/**
* Test that we handle the case of a history row being old enough to purge before the
* correcponding sync is finished. This can happen if the clock changes while we are syncing.
*
*/
// TODO: this test causes AidlTest to fail. Omit for now
// @SmallTest
public void testPurgeActiveSync() throws Exception {
final Account account = new Account("a@example.com", "example.type");
final String authority = "testprovider";
MockContentResolver mockResolver = new MockContentResolver();
SyncStorageEngine engine = SyncStorageEngine.newTestInstance(new TestContext(mockResolver, getContext()));
long time0 = 1000;
long historyId = engine.insertStartSyncEvent(account, 0, SyncOperation.REASON_PERIODIC, authority, time0, SyncStorageEngine.SOURCE_LOCAL, false, /* initialization */
null);
long time1 = time0 + SyncStorageEngine.MILLIS_IN_4WEEKS * 2;
engine.stopSyncEvent(historyId, time1 - time0, "yay", 0, 0);
}
use of android.test.mock.MockContentResolver in project android_frameworks_base by ParanoidAndroid.
the class TestContext method testPeriodics.
/**
* Test that we can create, remove and retrieve periodic syncs
*/
@MediumTest
public void testPeriodics() throws Exception {
final Account account1 = new Account("a@example.com", "example.type");
final Account account2 = new Account("b@example.com", "example.type.2");
final String authority = "testprovider";
final Bundle extras1 = new Bundle();
extras1.putString("a", "1");
final Bundle extras2 = new Bundle();
extras2.putString("a", "2");
final int period1 = 200;
final int period2 = 1000;
PeriodicSync sync1 = new PeriodicSync(account1, authority, extras1, period1);
PeriodicSync sync2 = new PeriodicSync(account1, authority, extras2, period1);
PeriodicSync sync3 = new PeriodicSync(account1, authority, extras2, period2);
PeriodicSync sync4 = new PeriodicSync(account2, authority, extras2, period2);
MockContentResolver mockResolver = new MockContentResolver();
SyncStorageEngine engine = SyncStorageEngine.newTestInstance(new TestContext(mockResolver, getContext()));
removePeriodicSyncs(engine, account1, 0, authority);
removePeriodicSyncs(engine, account2, 0, authority);
removePeriodicSyncs(engine, account1, 1, authority);
// this should add two distinct periodic syncs for account1 and one for account2
engine.addPeriodicSync(sync1.account, 0, sync1.authority, sync1.extras, sync1.period);
engine.addPeriodicSync(sync2.account, 0, sync2.authority, sync2.extras, sync2.period);
engine.addPeriodicSync(sync3.account, 0, sync3.authority, sync3.extras, sync3.period);
engine.addPeriodicSync(sync4.account, 0, sync4.authority, sync4.extras, sync4.period);
// add a second user
engine.addPeriodicSync(sync2.account, 1, sync2.authority, sync2.extras, sync2.period);
List<PeriodicSync> syncs = engine.getPeriodicSyncs(account1, 0, authority);
assertEquals(2, syncs.size());
assertEquals(sync1, syncs.get(0));
assertEquals(sync3, syncs.get(1));
engine.removePeriodicSync(sync1.account, 0, sync1.authority, sync1.extras);
syncs = engine.getPeriodicSyncs(account1, 0, authority);
assertEquals(1, syncs.size());
assertEquals(sync3, syncs.get(0));
syncs = engine.getPeriodicSyncs(account2, 0, authority);
assertEquals(1, syncs.size());
assertEquals(sync4, syncs.get(0));
syncs = engine.getPeriodicSyncs(sync2.account, 1, sync2.authority);
assertEquals(1, syncs.size());
assertEquals(sync2, syncs.get(0));
}
use of android.test.mock.MockContentResolver in project android_frameworks_base by ParanoidAndroid.
the class TestContext method testAuthorityParsing.
@MediumTest
public void testAuthorityParsing() throws Exception {
final Account account = new Account("account1", "type1");
final String authority1 = "auth1";
final String authority2 = "auth2";
final String authority3 = "auth3";
final Bundle extras = new Bundle();
PeriodicSync sync1 = new PeriodicSync(account, authority1, extras, (long) (60 * 60 * 24));
PeriodicSync sync2 = new PeriodicSync(account, authority2, extras, (long) (60 * 60 * 24));
PeriodicSync sync3 = new PeriodicSync(account, authority3, extras, (long) (60 * 60 * 24));
PeriodicSync sync1s = new PeriodicSync(account, authority1, extras, 1000);
PeriodicSync sync2s = new PeriodicSync(account, authority2, extras, 1000);
PeriodicSync sync3s = new PeriodicSync(account, authority3, extras, 1000);
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\" user=\"0\" account=\"account1\" type=\"type1\" authority=\"auth1\" />\n" + "<authority id=\"1\" user=\"0\" account=\"account1\" type=\"type1\" authority=\"auth2\" />\n" + "<authority id=\"2\" account=\"account1\" type=\"type1\" authority=\"auth3\" />\n" + "<authority id=\"3\" user=\"1\" account=\"account1\" type=\"type1\" authority=\"auth3\" />\n" + "</accounts>\n").getBytes();
File syncDir = getSyncDir();
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);
List<PeriodicSync> syncs = engine.getPeriodicSyncs(account, 0, authority1);
assertEquals(1, syncs.size());
assertEquals(sync1, syncs.get(0));
syncs = engine.getPeriodicSyncs(account, 0, authority2);
assertEquals(1, syncs.size());
assertEquals(sync2, syncs.get(0));
syncs = engine.getPeriodicSyncs(account, 0, authority3);
assertEquals(1, syncs.size());
assertEquals(sync3, syncs.get(0));
syncs = engine.getPeriodicSyncs(account, 1, authority3);
assertEquals(1, syncs.size());
assertEquals(sync3, syncs.get(0));
accountsFileData = ("<?xml version='1.0' encoding='utf-8' standalone='yes' ?>\n" + "<accounts version=\"2\">\n" + "<authority id=\"0\" account=\"account1\" type=\"type1\" authority=\"auth1\" />\n" + "<authority id=\"1\" account=\"account1\" type=\"type1\" authority=\"auth2\" />\n" + "<authority id=\"2\" account=\"account1\" type=\"type1\" authority=\"auth3\" />\n" + "</accounts>\n").getBytes();
accountInfoFile = new AtomicFile(new File(syncDir, "accounts.xml"));
fos = accountInfoFile.startWrite();
fos.write(accountsFileData);
accountInfoFile.finishWrite(fos);
engine.clearAndReadState();
syncs = engine.getPeriodicSyncs(account, 0, authority1);
assertEquals(0, syncs.size());
syncs = engine.getPeriodicSyncs(account, 0, authority2);
assertEquals(0, syncs.size());
syncs = engine.getPeriodicSyncs(account, 0, authority3);
assertEquals(0, syncs.size());
accountsFileData = ("<?xml version='1.0' encoding='utf-8' standalone='yes' ?>\n" + "<accounts version=\"2\">\n" + "<authority id=\"0\" account=\"account1\" type=\"type1\" authority=\"auth1\">\n" + "<periodicSync period=\"1000\" />\n" + "</authority>" + "<authority id=\"1\" account=\"account1\" type=\"type1\" authority=\"auth2\">\n" + "<periodicSync period=\"1000\" />\n" + "</authority>" + "<authority id=\"2\" account=\"account1\" type=\"type1\" authority=\"auth3\">\n" + "<periodicSync period=\"1000\" />\n" + "</authority>" + "</accounts>\n").getBytes();
accountInfoFile = new AtomicFile(new File(syncDir, "accounts.xml"));
fos = accountInfoFile.startWrite();
fos.write(accountsFileData);
accountInfoFile.finishWrite(fos);
engine.clearAndReadState();
syncs = engine.getPeriodicSyncs(account, 0, authority1);
assertEquals(1, syncs.size());
assertEquals(sync1s, syncs.get(0));
syncs = engine.getPeriodicSyncs(account, 0, authority2);
assertEquals(1, syncs.size());
assertEquals(sync2s, syncs.get(0));
syncs = engine.getPeriodicSyncs(account, 0, authority3);
assertEquals(1, syncs.size());
assertEquals(sync3s, syncs.get(0));
}
use of android.test.mock.MockContentResolver in project platform_frameworks_base by android.
the class TestContext method createStorageTestContext.
/**
* Returns a Context configured with test provider for authority.
*/
static Context createStorageTestContext(Context context, String authority) {
final MockContentResolver testResolver = new MockContentResolver();
TestContentProvider provider = new TestContentProvider();
testResolver.addProvider(authority, provider);
return new ContextWrapper(context) {
@Override
public ContentResolver getContentResolver() {
return testResolver;
}
};
}
use of android.test.mock.MockContentResolver in project platform_frameworks_base by android.
the class TestContext method setUp.
@Override
public void setUp() {
account1 = new Account("a@example.com", "example.type");
account2 = new Account("b@example.com", "example.type");
syncService1 = new ComponentName("com.example", "SyncService");
// Default bundle.
defaultBundle = new Bundle();
defaultBundle.putInt("int_key", 0);
defaultBundle.putString("string_key", "hello");
// Set up storage engine.
mockResolver = new MockContentResolver();
engine = SyncStorageEngine.newTestInstance(new TestContext(mockResolver, getContext()));
}
Aggregations