1 /* 2 * Copyright (C) 2010 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 17 package com.android.server.content; 18 19 import android.accounts.Account; 20 import android.content.ContentResolver; 21 import android.os.Bundle; 22 import android.os.PersistableBundle; 23 import android.test.AndroidTestCase; 24 import android.test.suitebuilder.annotation.SmallTest; 25 26 /** 27 * Test for SyncOperation. 28 * 29 * atest ${ANDROID_BUILD_TOP}/frameworks/base/services/tests/servicestests/src/com/android/server/content/SyncOperationTest.java 30 */ 31 @SmallTest 32 public class SyncOperationTest extends AndroidTestCase { 33 34 Account mDummy; 35 /** Indicate an unimportant long that we're not testing. */ 36 long mUnimportantLong = 0L; 37 /** Empty bundle. */ 38 Bundle mEmpty; 39 /** Silly authority. */ 40 String mAuthority; 41 42 @Override setUp()43 public void setUp() { 44 mDummy = new Account("account1", "type1"); 45 mEmpty = new Bundle(); 46 mAuthority = "authority1"; 47 } 48 49 @SmallTest testToKey()50 public void testToKey() { 51 Account account1 = new Account("account1", "type1"); 52 Account account2 = new Account("account2", "type2"); 53 54 Bundle b1 = new Bundle(); 55 Bundle b2 = new Bundle(); 56 b2.putBoolean("b2", true); 57 58 SyncOperation op1 = new SyncOperation(account1, 0, 59 1, "foo", 0, 60 SyncOperation.REASON_PERIODIC, 61 "authority1", 62 b1, 63 false, 64 ContentResolver.SYNC_EXEMPTION_NONE); 65 66 // Same as op1 but different time infos 67 SyncOperation op2 = new SyncOperation(account1, 0, 68 1, "foo", 0, 69 SyncOperation.REASON_PERIODIC, 70 "authority1", 71 b1, 72 false, 73 ContentResolver.SYNC_EXEMPTION_NONE); 74 75 // Same as op1 but different authority 76 SyncOperation op3 = new SyncOperation(account1, 0, 77 1, "foo", 0, 78 SyncOperation.REASON_PERIODIC, 79 "authority2", 80 b1, 81 false, 82 ContentResolver.SYNC_EXEMPTION_NONE); 83 84 // Same as op1 but different account 85 SyncOperation op4 = new SyncOperation(account2, 0, 86 1, "foo", 0, 87 SyncOperation.REASON_PERIODIC, 88 "authority1", 89 b1, 90 false, 91 ContentResolver.SYNC_EXEMPTION_NONE); 92 93 // Same as op1 but different bundle 94 SyncOperation op5 = new SyncOperation(account1, 0, 95 1, "foo", 0, 96 SyncOperation.REASON_PERIODIC, 97 "authority1", 98 b2, 99 false, 100 ContentResolver.SYNC_EXEMPTION_NONE); 101 102 assertEquals(op1.key, op2.key); 103 assertNotSame(op1.key, op3.key); 104 assertNotSame(op1.key, op4.key); 105 assertNotSame(op1.key, op5.key); 106 } 107 108 @SmallTest testConversionToExtras()109 public void testConversionToExtras() { 110 Account account1 = new Account("account1", "type1"); 111 Bundle b1 = new Bundle(); 112 b1.putParcelable("acc", account1); 113 b1.putString("str", "String"); 114 115 SyncOperation op1 = new SyncOperation(account1, 0, 116 1, "foo", 0, 117 SyncOperation.REASON_PERIODIC, 118 "authority1", 119 b1, 120 false, 121 ContentResolver.SYNC_EXEMPTION_NONE); 122 123 PersistableBundle pb = op1.toJobInfoExtras(); 124 SyncOperation op2 = SyncOperation.maybeCreateFromJobExtras(pb); 125 126 assertTrue("Account fields in extras not persisted.", 127 account1.equals(op2.getClonedExtras().get("acc"))); 128 assertTrue("Fields in extras not persisted", "String".equals( 129 op2.getClonedExtras().getString("str"))); 130 } 131 132 @SmallTest testConversionFromExtras()133 public void testConversionFromExtras() { 134 PersistableBundle extras = new PersistableBundle(); 135 SyncOperation op = SyncOperation.maybeCreateFromJobExtras(extras); 136 assertTrue("Non sync operation bundle falsely converted to SyncOperation.", op == null); 137 } 138 139 /** 140 * Tests whether a failed periodic sync operation is converted correctly into a one time 141 * sync operation, and whether the periodic sync can be re-created from the one-time operation. 142 */ 143 @SmallTest testFailedPeriodicConversion()144 public void testFailedPeriodicConversion() { 145 SyncStorageEngine.EndPoint ep = new SyncStorageEngine.EndPoint(new Account("name", "type"), 146 "provider", 0); 147 Bundle extras = new Bundle(); 148 SyncOperation periodic = new SyncOperation(ep, 0, "package", 0, 0, extras, false, true, 149 SyncOperation.NO_JOB_ID, 60000, 10000, 150 ContentResolver.SYNC_EXEMPTION_NONE); 151 SyncOperation oneoff = periodic.createOneTimeSyncOperation(); 152 assertFalse("Conversion to oneoff sync failed.", oneoff.isPeriodic); 153 assertEquals("Period not restored", periodic.periodMillis, oneoff.periodMillis); 154 assertEquals("Flex not restored", periodic.flexMillis, oneoff.flexMillis); 155 } 156 157 @SmallTest testScheduleAsEjIsInExtras()158 public void testScheduleAsEjIsInExtras() { 159 Account account1 = new Account("account1", "type1"); 160 Bundle b1 = new Bundle(); 161 b1.putBoolean(ContentResolver.SYNC_EXTRAS_SCHEDULE_AS_EXPEDITED_JOB, true); 162 163 SyncOperation op1 = new SyncOperation(account1, 0, 1, "foo", 0, 164 SyncOperation.REASON_USER_START, "authority1", b1, false, 165 ContentResolver.SYNC_EXEMPTION_NONE); 166 assertTrue(op1.isScheduledAsExpeditedJob()); 167 168 PersistableBundle pb = op1.toJobInfoExtras(); 169 assertTrue("EJ extra not found in job extras", 170 ((PersistableBundle) pb.get("syncExtras")) 171 .containsKey(ContentResolver.SYNC_EXTRAS_SCHEDULE_AS_EXPEDITED_JOB)); 172 173 SyncOperation op2 = SyncOperation.maybeCreateFromJobExtras(pb); 174 assertTrue("EJ extra not found in extras", op2.getClonedExtras() 175 .getBoolean(ContentResolver.SYNC_EXTRAS_SCHEDULE_AS_EXPEDITED_JOB)); 176 } 177 } 178