SyncStorageEngineTest.java revision c5d1c6db61f208b206b260f897bb5bbc64be4d97
1/* 2 * Copyright (C) 2007 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package android.content; 18 19import android.test.AndroidTestCase; 20import android.test.RenamingDelegatingContext; 21import android.test.suitebuilder.annotation.SmallTest; 22import android.test.mock.MockContext; 23import android.test.mock.MockContentResolver; 24import android.accounts.Account; 25import android.os.Bundle; 26 27import java.util.List; 28import java.io.File; 29 30public class SyncStorageEngineTest extends AndroidTestCase { 31 32 /** 33 * Test that we handle the case of a history row being old enough to purge before the 34 * correcponding sync is finished. This can happen if the clock changes while we are syncing. 35 * 36 */ 37 @SmallTest 38 public void testPurgeActiveSync() throws Exception { 39 final Account account = new Account("a@example.com", "example.type"); 40 final String authority = "testprovider"; 41 42 MockContentResolver mockResolver = new MockContentResolver(); 43 44 SyncStorageEngine engine = SyncStorageEngine.newTestInstance( 45 new TestContext(mockResolver, getContext())); 46 47 long time0 = 1000; 48 long historyId = engine.insertStartSyncEvent( 49 account, authority, time0, SyncStorageEngine.SOURCE_LOCAL); 50 long time1 = time0 + SyncStorageEngine.MILLIS_IN_4WEEKS * 2; 51 engine.stopSyncEvent(historyId, new Bundle(), time1 - time0, "yay", 0, 0); 52 } 53 54 /** 55 * Test that we can create, remove and retrieve periodic syncs 56 */ 57 @SmallTest 58 public void testPeriodics() throws Exception { 59 final Account account1 = new Account("a@example.com", "example.type"); 60 final Account account2 = new Account("b@example.com", "example.type.2"); 61 final String authority = "testprovider"; 62 final Bundle extras1 = new Bundle(); 63 extras1.putString("a", "1"); 64 final Bundle extras2 = new Bundle(); 65 extras2.putString("a", "2"); 66 final int period1 = 200; 67 final int period2 = 1000; 68 69 PeriodicSync sync1 = new PeriodicSync(account1, authority, extras1, period1); 70 PeriodicSync sync2 = new PeriodicSync(account1, authority, extras2, period1); 71 PeriodicSync sync3 = new PeriodicSync(account1, authority, extras2, period2); 72 PeriodicSync sync4 = new PeriodicSync(account2, authority, extras2, period2); 73 74 MockContentResolver mockResolver = new MockContentResolver(); 75 76 SyncStorageEngine engine = SyncStorageEngine.newTestInstance( 77 new TestContext(mockResolver, getContext())); 78 79 removePeriodicSyncs(engine, account1, authority); 80 removePeriodicSyncs(engine, account2, authority); 81 82 // this should add two distinct periodic syncs for account1 and one for account2 83 engine.addPeriodicSync(sync1.account, sync1.authority, sync1.extras, sync1.period); 84 engine.addPeriodicSync(sync2.account, sync2.authority, sync2.extras, sync2.period); 85 engine.addPeriodicSync(sync3.account, sync3.authority, sync3.extras, sync3.period); 86 engine.addPeriodicSync(sync4.account, sync4.authority, sync4.extras, sync4.period); 87 88 List<PeriodicSync> syncs = engine.getPeriodicSyncs(account1, authority); 89 90 assertEquals(2, syncs.size()); 91 92 assertEquals(sync1, syncs.get(0)); 93 assertEquals(sync3, syncs.get(1)); 94 95 engine.removePeriodicSync(sync1.account, sync1.authority, sync1.extras); 96 97 syncs = engine.getPeriodicSyncs(account1, authority); 98 assertEquals(1, syncs.size()); 99 assertEquals(sync3, syncs.get(0)); 100 101 syncs = engine.getPeriodicSyncs(account2, authority); 102 assertEquals(1, syncs.size()); 103 assertEquals(sync4, syncs.get(0)); 104 } 105 106 private void removePeriodicSyncs(SyncStorageEngine engine, Account account, String authority) { 107 engine.setIsSyncable(account, authority, engine.getIsSyncable(account, authority)); 108 List<PeriodicSync> syncs = engine.getPeriodicSyncs(account, authority); 109 for (PeriodicSync sync : syncs) { 110 engine.removePeriodicSync(sync.account, sync.authority, sync.extras); 111 } 112 } 113 114 @SmallTest 115 public void testAuthorityPersistence() throws Exception { 116 final Account account1 = new Account("a@example.com", "example.type"); 117 final Account account2 = new Account("b@example.com", "example.type.2"); 118 final String authority1 = "testprovider1"; 119 final String authority2 = "testprovider2"; 120 final Bundle extras1 = new Bundle(); 121 extras1.putString("a", "1"); 122 final Bundle extras2 = new Bundle(); 123 extras2.putString("a", "2"); 124 extras2.putLong("b", 2); 125 extras2.putInt("c", 1); 126 extras2.putBoolean("d", true); 127 extras2.putDouble("e", 1.2); 128 extras2.putFloat("f", 4.5f); 129 extras2.putParcelable("g", account1); 130 final int period1 = 200; 131 final int period2 = 1000; 132 133 PeriodicSync sync1 = new PeriodicSync(account1, authority1, extras1, period1); 134 PeriodicSync sync2 = new PeriodicSync(account1, authority1, extras2, period1); 135 PeriodicSync sync3 = new PeriodicSync(account1, authority2, extras1, period1); 136 PeriodicSync sync4 = new PeriodicSync(account1, authority2, extras2, period2); 137 PeriodicSync sync5 = new PeriodicSync(account2, authority1, extras1, period1); 138 139 MockContentResolver mockResolver = new MockContentResolver(); 140 141 SyncStorageEngine engine = SyncStorageEngine.newTestInstance( 142 new TestContext(mockResolver, getContext())); 143 144 removePeriodicSyncs(engine, account1, authority1); 145 removePeriodicSyncs(engine, account2, authority1); 146 removePeriodicSyncs(engine, account1, authority2); 147 removePeriodicSyncs(engine, account2, authority2); 148 149 engine.setMasterSyncAutomatically(false); 150 151 engine.setIsSyncable(account1, authority1, 1); 152 engine.setSyncAutomatically(account1, authority1, true); 153 154 engine.setIsSyncable(account2, authority1, 1); 155 engine.setSyncAutomatically(account2, authority1, true); 156 157 engine.setIsSyncable(account1, authority2, 1); 158 engine.setSyncAutomatically(account1, authority2, false); 159 160 engine.setIsSyncable(account2, authority2, 0); 161 engine.setSyncAutomatically(account2, authority2, true); 162 163 engine.addPeriodicSync(sync1.account, sync1.authority, sync1.extras, sync1.period); 164 engine.addPeriodicSync(sync2.account, sync2.authority, sync2.extras, sync2.period); 165 engine.addPeriodicSync(sync3.account, sync3.authority, sync3.extras, sync3.period); 166 engine.addPeriodicSync(sync4.account, sync4.authority, sync4.extras, sync4.period); 167 engine.addPeriodicSync(sync5.account, sync5.authority, sync5.extras, sync5.period); 168 169 engine.writeAllState(); 170 engine.clearAndReadState(); 171 172 List<PeriodicSync> syncs = engine.getPeriodicSyncs(account1, authority1); 173 assertEquals(2, syncs.size()); 174 assertEquals(sync1, syncs.get(0)); 175 assertEquals(sync2, syncs.get(1)); 176 177 syncs = engine.getPeriodicSyncs(account1, authority2); 178 assertEquals(2, syncs.size()); 179 assertEquals(sync3, syncs.get(0)); 180 assertEquals(sync4, syncs.get(1)); 181 182 syncs = engine.getPeriodicSyncs(account2, authority1); 183 assertEquals(1, syncs.size()); 184 assertEquals(sync5, syncs.get(0)); 185 186 assertEquals(true, engine.getSyncAutomatically(account1, authority1)); 187 assertEquals(true, engine.getSyncAutomatically(account2, authority1)); 188 assertEquals(false, engine.getSyncAutomatically(account1, authority2)); 189 assertEquals(true, engine.getSyncAutomatically(account2, authority2)); 190 191 assertEquals(1, engine.getIsSyncable(account1, authority1)); 192 assertEquals(1, engine.getIsSyncable(account2, authority1)); 193 assertEquals(1, engine.getIsSyncable(account1, authority2)); 194 assertEquals(0, engine.getIsSyncable(account2, authority2)); 195 } 196} 197 198class TestContext extends ContextWrapper { 199 200 ContentResolver mResolver; 201 202 private final Context mRealContext; 203 204 public TestContext(ContentResolver resolver, Context realContext) { 205 super(new RenamingDelegatingContext(new MockContext(), realContext, "test.")); 206 mRealContext = realContext; 207 mResolver = resolver; 208 } 209 210 @Override 211 public File getFilesDir() { 212 return mRealContext.getFilesDir(); 213 } 214 215 @Override 216 public void enforceCallingOrSelfPermission(String permission, String message) { 217 } 218 219 @Override 220 public void sendBroadcast(Intent intent) { 221 } 222 223 @Override 224 public ContentResolver getContentResolver() { 225 return mResolver; 226 } 227} 228