diff options
Diffstat (limited to 'services/tests/servicestests/src')
-rw-r--r-- | services/tests/servicestests/src/com/android/server/content/SyncOperationTest.java | 44 | ||||
-rw-r--r-- | services/tests/servicestests/src/com/android/server/content/SyncStorageEngineTest.java | 276 |
2 files changed, 294 insertions, 26 deletions
diff --git a/services/tests/servicestests/src/com/android/server/content/SyncOperationTest.java b/services/tests/servicestests/src/com/android/server/content/SyncOperationTest.java index f2772c8..37176d6 100644 --- a/services/tests/servicestests/src/com/android/server/content/SyncOperationTest.java +++ b/services/tests/servicestests/src/com/android/server/content/SyncOperationTest.java @@ -14,9 +14,10 @@ * limitations under the License. */ -package com.android.server; +package com.android.server.content; import android.accounts.Account; +import android.content.ContentResolver; import android.os.Bundle; import android.test.AndroidTestCase; import android.test.suitebuilder.annotation.SmallTest; @@ -48,7 +49,8 @@ public class SyncOperationTest extends AndroidTestCase { SyncOperation.REASON_PERIODIC, "authority1", b1, - 100, + 100, /* run time from now*/ + 10, /* flex */ 1000, 10000, false); @@ -60,6 +62,7 @@ public class SyncOperationTest extends AndroidTestCase { "authority1", b1, 200, + 20, 2000, 20000, false); @@ -71,6 +74,7 @@ public class SyncOperationTest extends AndroidTestCase { "authority2", b1, 100, + 10, 1000, 10000, false); @@ -82,6 +86,7 @@ public class SyncOperationTest extends AndroidTestCase { "authority1", b1, 100, + 10, 1000, 10000, false); @@ -93,6 +98,7 @@ public class SyncOperationTest extends AndroidTestCase { "authority1", b2, 100, + 10, 1000, 10000, false); @@ -102,4 +108,38 @@ public class SyncOperationTest extends AndroidTestCase { assertNotSame(op1.key, op4.key); assertNotSame(op1.key, op5.key); } + + @SmallTest + public void testCompareTo() { + Account dummy = new Account("account1", "type1"); + Bundle b1 = new Bundle(); + final long unimportant = 0L; + long soon = 1000; + long soonFlex = 50; + long after = 1500; + long afterFlex = 100; + SyncOperation op1 = new SyncOperation(dummy, 0, 0, SyncOperation.REASON_PERIODIC, + "authority1", b1, soon, soonFlex, unimportant, unimportant, true); + + // Interval disjoint from and after op1. + SyncOperation op2 = new SyncOperation(dummy, 0, 0, SyncOperation.REASON_PERIODIC, + "authority1", b1, after, afterFlex, unimportant, unimportant, true); + + // Interval equivalent to op1, but expedited. + Bundle b2 = new Bundle(); + b2.putBoolean(ContentResolver.SYNC_EXTRAS_EXPEDITED, true); + SyncOperation op3 = new SyncOperation(dummy, 0, 0, 0, + "authority1", b2, soon, soonFlex, unimportant, unimportant, true); + + // Interval overlaps but not equivalent to op1. + SyncOperation op4 = new SyncOperation(dummy, 0, 0, SyncOperation.REASON_PERIODIC, + "authority1", b1, soon + 100, soonFlex + 100, unimportant, unimportant, true); + + assertTrue(op1.compareTo(op2) == -1); + assertTrue("less than not transitive.", op2.compareTo(op1) == 1); + assertTrue(op1.compareTo(op3) == 1); + assertTrue("greater than not transitive. ", op3.compareTo(op1) == -1); + assertTrue("overlapping intervals not the same.", op1.compareTo(op4) == 0); + assertTrue("equality not transitive.", op4.compareTo(op1) == 0); + } } diff --git a/services/tests/servicestests/src/com/android/server/content/SyncStorageEngineTest.java b/services/tests/servicestests/src/com/android/server/content/SyncStorageEngineTest.java index 8b00f2c..dff6661 100644 --- a/services/tests/servicestests/src/com/android/server/content/SyncStorageEngineTest.java +++ b/services/tests/servicestests/src/com/android/server/content/SyncStorageEngineTest.java @@ -22,6 +22,7 @@ import android.content.Context; import android.content.ContextWrapper; import android.content.Intent; import android.content.PeriodicSync; +import android.content.res.Resources; import android.os.Bundle; import android.test.AndroidTestCase; import android.test.RenamingDelegatingContext; @@ -39,10 +40,31 @@ import java.util.List; public class SyncStorageEngineTest extends AndroidTestCase { + protected Account account1; + protected String authority1 = "testprovider"; + protected Bundle defaultBundle; + protected final int DEFAULT_USER = 0; + + MockContentResolver mockResolver; + SyncStorageEngine engine; + private File getSyncDir() { return new File(new File(getContext().getFilesDir(), "system"), "sync"); } + @Override + public void setUp() { + account1 = new Account("a@example.com", "example.type"); + // 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())); + } + /** * 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. @@ -68,7 +90,25 @@ public class SyncStorageEngineTest extends AndroidTestCase { } /** - * Test that we can create, remove and retrieve periodic syncs + * Test persistence of pending operations. + */ + @MediumTest + public void testPending() throws Exception { + SyncStorageEngine.PendingOperation pop = + new SyncStorageEngine.PendingOperation(account1, DEFAULT_USER, + SyncOperation.REASON_PERIODIC, SyncStorageEngine.SOURCE_LOCAL, + authority1, defaultBundle, false); + + engine.insertIntoPending(pop); + // Force engine to read from disk. + engine.clearAndReadState(); + + assert(engine.getPendingOperationCount() == 1); + } + + /** + * Test that we can create, remove and retrieve periodic syncs. Backwards compatibility - + * periodic syncs with no flex time are no longer used. */ @MediumTest public void testPeriodics() throws Exception { @@ -87,6 +127,64 @@ public class SyncStorageEngineTest extends AndroidTestCase { PeriodicSync sync3 = new PeriodicSync(account1, authority, extras2, period2); PeriodicSync sync4 = new PeriodicSync(account2, authority, extras2, period2); + + + 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, 0); + engine.addPeriodicSync(sync2, 0); + engine.addPeriodicSync(sync3, 0); + engine.addPeriodicSync(sync4, 0); + // add a second user + engine.addPeriodicSync(sync2, 1); + + 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, 0); + + 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)); + } + + /** + * Test that we can create, remove and retrieve periodic syncs with a provided flex time. + */ + @MediumTest + public void testPeriodicsV2() 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; + final int flex1 = 10; + final int flex2 = 100; + + PeriodicSync sync1 = new PeriodicSync(account1, authority, extras1, period1, flex1); + PeriodicSync sync2 = new PeriodicSync(account1, authority, extras2, period1, flex1); + PeriodicSync sync3 = new PeriodicSync(account1, authority, extras2, period2, flex2); + PeriodicSync sync4 = new PeriodicSync(account2, authority, extras2, period2, flex2); + MockContentResolver mockResolver = new MockContentResolver(); SyncStorageEngine engine = SyncStorageEngine.newTestInstance( @@ -96,13 +194,13 @@ public class SyncStorageEngineTest extends AndroidTestCase { 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); + // This should add two distinct periodic syncs for account1 and one for account2 + engine.addPeriodicSync(sync1, 0); + engine.addPeriodicSync(sync2, 0); + engine.addPeriodicSync(sync3, 0); // Should edit sync2 and update the period. + engine.addPeriodicSync(sync4, 0); // add a second user - engine.addPeriodicSync(sync2.account, 1, sync2.authority, sync2.extras, sync2.period); + engine.addPeriodicSync(sync2, 1); List<PeriodicSync> syncs = engine.getPeriodicSyncs(account1, 0, authority); @@ -111,7 +209,7 @@ public class SyncStorageEngineTest extends AndroidTestCase { assertEquals(sync1, syncs.get(0)); assertEquals(sync3, syncs.get(1)); - engine.removePeriodicSync(sync1.account, 0, sync1.authority, sync1.extras); + engine.removePeriodicSync(sync1, 0); syncs = engine.getPeriodicSyncs(account1, 0, authority); assertEquals(1, syncs.size()); @@ -126,13 +224,11 @@ public class SyncStorageEngineTest extends AndroidTestCase { assertEquals(sync2, syncs.get(0)); } - private void removePeriodicSyncs(SyncStorageEngine engine, Account account, int userId, - String authority) { - engine.setIsSyncable(account, userId, authority, - engine.getIsSyncable(account, 0, authority)); + private void removePeriodicSyncs(SyncStorageEngine engine, Account account, int userId, String authority) { + engine.setIsSyncable(account, userId, authority, engine.getIsSyncable(account, 0, authority)); List<PeriodicSync> syncs = engine.getPeriodicSyncs(account, userId, authority); for (PeriodicSync sync : syncs) { - engine.removePeriodicSync(sync.account, userId, sync.authority, sync.extras); + engine.removePeriodicSync(sync, userId); } } @@ -154,12 +250,14 @@ public class SyncStorageEngineTest extends AndroidTestCase { extras2.putParcelable("g", account1); final int period1 = 200; final int period2 = 1000; + final int flex1 = 10; + final int flex2 = 100; - PeriodicSync sync1 = new PeriodicSync(account1, authority1, extras1, period1); - PeriodicSync sync2 = new PeriodicSync(account1, authority1, extras2, period1); - PeriodicSync sync3 = new PeriodicSync(account1, authority2, extras1, period1); - PeriodicSync sync4 = new PeriodicSync(account1, authority2, extras2, period2); - PeriodicSync sync5 = new PeriodicSync(account2, authority1, extras1, period1); + PeriodicSync sync1 = new PeriodicSync(account1, authority1, extras1, period1, flex1); + PeriodicSync sync2 = new PeriodicSync(account1, authority1, extras2, period1, flex1); + PeriodicSync sync3 = new PeriodicSync(account1, authority2, extras1, period1, flex1); + PeriodicSync sync4 = new PeriodicSync(account1, authority2, extras2, period2, flex2); + PeriodicSync sync5 = new PeriodicSync(account2, authority1, extras1, period1, flex1); MockContentResolver mockResolver = new MockContentResolver(); @@ -185,11 +283,11 @@ public class SyncStorageEngineTest extends AndroidTestCase { engine.setIsSyncable(account2, 0, authority2, 0); engine.setSyncAutomatically(account2, 0, authority2, true); - 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); - engine.addPeriodicSync(sync5.account, 0, sync5.authority, sync5.extras, sync5.period); + engine.addPeriodicSync(sync1, 0); + engine.addPeriodicSync(sync2, 0); + engine.addPeriodicSync(sync3, 0); + engine.addPeriodicSync(sync4, 0); + engine.addPeriodicSync(sync5, 0); engine.writeAllState(); engine.clearAndReadState(); @@ -220,6 +318,131 @@ public class SyncStorageEngineTest extends AndroidTestCase { } @MediumTest + /** + * V2 introduces flex time as well as service components. + * @throws Exception + */ + public void testAuthorityParsingV2() throws Exception { + final Account account = new Account("account1", "type1"); + final String authority1 = "auth1"; + final String authority2 = "auth2"; + final String authority3 = "auth3"; + + final long dayPoll = (60 * 60 * 24); + final long dayFuzz = 60; + final long thousandSecs = 1000; + final long thousandSecsFuzz = 100; + final Bundle extras = new Bundle(); + PeriodicSync sync1 = new PeriodicSync(account, authority1, extras, dayPoll, dayFuzz); + PeriodicSync sync2 = new PeriodicSync(account, authority2, extras, dayPoll, dayFuzz); + PeriodicSync sync3 = new PeriodicSync(account, authority3, extras, dayPoll, dayFuzz); + PeriodicSync sync1s = new PeriodicSync(account, authority1, extras, thousandSecs, thousandSecsFuzz); + PeriodicSync sync2s = new PeriodicSync(account, authority2, extras, thousandSecs, thousandSecsFuzz); + PeriodicSync sync3s = new PeriodicSync(account, authority3, extras, thousandSecs, thousandSecsFuzz); + MockContentResolver mockResolver = new MockContentResolver(); + + final TestContext testContext = new TestContext(mockResolver, getContext()); + + byte[] accountsFileData = ("<?xml version='1.0' encoding='utf-8' standalone='yes' ?>\n" + + "<accounts version=\"2\" >\n" + + "<authority id=\"0\" user=\"0\" account=\"account1\" type=\"type1\" authority=\"auth1\" >" + + "\n<periodicSync period=\"" + dayPoll + "\" flex=\"" + dayFuzz + "\"/>" + + "\n</authority>" + + "<authority id=\"1\" user=\"0\" account=\"account1\" type=\"type1\" authority=\"auth2\" >" + + "\n<periodicSync period=\"" + dayPoll + "\" flex=\"" + dayFuzz + "\"/>" + + "\n</authority>" + // No user defaults to user 0 - all users. + + "<authority id=\"2\" account=\"account1\" type=\"type1\" authority=\"auth3\" >" + + "\n<periodicSync period=\"" + dayPoll + "\" flex=\"" + dayFuzz + "\"/>" + + "\n</authority>" + + "<authority id=\"3\" user=\"1\" account=\"account1\" type=\"type1\" authority=\"auth3\" >" + + "\n<periodicSync period=\"" + dayPoll + "\" flex=\"" + dayFuzz + "\"/>" + + "\n</authority>" + + "</accounts>").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("Got incorrect # of syncs", 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)); + + // Test empty periodic data. + 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)); + } + + @MediumTest public void testAuthorityParsing() throws Exception { final Account account = new Account("account1", "type1"); final String authority1 = "auth1"; @@ -256,7 +479,7 @@ public class SyncStorageEngineTest extends AndroidTestCase { List<PeriodicSync> syncs = engine.getPeriodicSyncs(account, 0, authority1); assertEquals(1, syncs.size()); - assertEquals(sync1, syncs.get(0)); + assertEquals("expected sync1: " + sync1.toString() + " == sync 2" + syncs.get(0).toString(), sync1, syncs.get(0)); syncs = engine.getPeriodicSyncs(account, 0, authority2); assertEquals(1, syncs.size()); @@ -451,6 +674,11 @@ class TestContext extends ContextWrapper { } @Override + public Resources getResources() { + return mRealContext.getResources(); + } + + @Override public File getFilesDir() { return mRealContext.getFilesDir(); } |