Fabrice Di Meglio | 7e118b5 | 2010-11-23 17:06:45 -0800 | [diff] [blame] | 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 | |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 17 | package com.android.server.content; |
Fabrice Di Meglio | 7e118b5 | 2010-11-23 17:06:45 -0800 | [diff] [blame] | 18 | |
| 19 | import android.accounts.Account; |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 20 | import android.content.ContentResolver; |
Matthew Williams | 6428046 | 2014-01-09 10:49:23 -0800 | [diff] [blame] | 21 | import android.content.Context; |
Fabrice Di Meglio | 7e118b5 | 2010-11-23 17:06:45 -0800 | [diff] [blame] | 22 | import android.os.Bundle; |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 23 | import android.os.PersistableBundle; |
Matthew Williams | 6428046 | 2014-01-09 10:49:23 -0800 | [diff] [blame] | 24 | import android.os.SystemClock; |
| 25 | import android.provider.Settings; |
Fabrice Di Meglio | 7e118b5 | 2010-11-23 17:06:45 -0800 | [diff] [blame] | 26 | import android.test.AndroidTestCase; |
| 27 | import android.test.suitebuilder.annotation.SmallTest; |
| 28 | |
| 29 | /** |
| 30 | * You can run those tests with: |
| 31 | * |
| 32 | * adb shell am instrument |
| 33 | * -e debug false |
| 34 | * -w |
| 35 | * -e class android.content.SyncOperationTest com.android.frameworks.coretests/android.test.InstrumentationTestRunner |
| 36 | */ |
| 37 | |
| 38 | public class SyncOperationTest extends AndroidTestCase { |
| 39 | |
Matthew Williams | 6428046 | 2014-01-09 10:49:23 -0800 | [diff] [blame] | 40 | Account mDummy; |
| 41 | /** Indicate an unimportant long that we're not testing. */ |
| 42 | long mUnimportantLong = 0L; |
| 43 | /** Empty bundle. */ |
| 44 | Bundle mEmpty; |
| 45 | /** Silly authority. */ |
| 46 | String mAuthority; |
| 47 | |
| 48 | @Override |
| 49 | public void setUp() { |
| 50 | mDummy = new Account("account1", "type1"); |
| 51 | mEmpty = new Bundle(); |
| 52 | mAuthority = "authority1"; |
| 53 | } |
| 54 | |
Fabrice Di Meglio | 7e118b5 | 2010-11-23 17:06:45 -0800 | [diff] [blame] | 55 | @SmallTest |
| 56 | public void testToKey() { |
| 57 | Account account1 = new Account("account1", "type1"); |
| 58 | Account account2 = new Account("account2", "type2"); |
| 59 | |
| 60 | Bundle b1 = new Bundle(); |
| 61 | Bundle b2 = new Bundle(); |
| 62 | b2.putBoolean("b2", true); |
| 63 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 64 | SyncOperation op1 = new SyncOperation(account1, 0, |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 65 | 1, "foo", 0, |
Alon Albert | 57286f9 | 2012-10-09 14:21:38 -0700 | [diff] [blame] | 66 | SyncOperation.REASON_PERIODIC, |
Fabrice Di Meglio | 7e118b5 | 2010-11-23 17:06:45 -0800 | [diff] [blame] | 67 | "authority1", |
| 68 | b1, |
Fred Quintana | 0c4d04a | 2010-11-03 17:02:55 -0700 | [diff] [blame] | 69 | false); |
Fabrice Di Meglio | 7e118b5 | 2010-11-23 17:06:45 -0800 | [diff] [blame] | 70 | |
| 71 | // Same as op1 but different time infos |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 72 | SyncOperation op2 = new SyncOperation(account1, 0, |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 73 | 1, "foo", 0, |
Alon Albert | 57286f9 | 2012-10-09 14:21:38 -0700 | [diff] [blame] | 74 | SyncOperation.REASON_PERIODIC, |
Fabrice Di Meglio | 7e118b5 | 2010-11-23 17:06:45 -0800 | [diff] [blame] | 75 | "authority1", |
| 76 | b1, |
Fred Quintana | 0c4d04a | 2010-11-03 17:02:55 -0700 | [diff] [blame] | 77 | false); |
Fabrice Di Meglio | 7e118b5 | 2010-11-23 17:06:45 -0800 | [diff] [blame] | 78 | |
| 79 | // Same as op1 but different authority |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 80 | SyncOperation op3 = new SyncOperation(account1, 0, |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 81 | 1, "foo", 0, |
Alon Albert | 57286f9 | 2012-10-09 14:21:38 -0700 | [diff] [blame] | 82 | SyncOperation.REASON_PERIODIC, |
Fabrice Di Meglio | 7e118b5 | 2010-11-23 17:06:45 -0800 | [diff] [blame] | 83 | "authority2", |
| 84 | b1, |
Fred Quintana | 0c4d04a | 2010-11-03 17:02:55 -0700 | [diff] [blame] | 85 | false); |
Fabrice Di Meglio | 7e118b5 | 2010-11-23 17:06:45 -0800 | [diff] [blame] | 86 | |
| 87 | // Same as op1 but different account |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 88 | SyncOperation op4 = new SyncOperation(account2, 0, |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 89 | 1, "foo", 0, |
Alon Albert | 57286f9 | 2012-10-09 14:21:38 -0700 | [diff] [blame] | 90 | SyncOperation.REASON_PERIODIC, |
Fabrice Di Meglio | 7e118b5 | 2010-11-23 17:06:45 -0800 | [diff] [blame] | 91 | "authority1", |
| 92 | b1, |
Fred Quintana | 0c4d04a | 2010-11-03 17:02:55 -0700 | [diff] [blame] | 93 | false); |
Fabrice Di Meglio | 7e118b5 | 2010-11-23 17:06:45 -0800 | [diff] [blame] | 94 | |
| 95 | // Same as op1 but different bundle |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 96 | SyncOperation op5 = new SyncOperation(account1, 0, |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 97 | 1, "foo", 0, |
Alon Albert | 57286f9 | 2012-10-09 14:21:38 -0700 | [diff] [blame] | 98 | SyncOperation.REASON_PERIODIC, |
Fabrice Di Meglio | 7e118b5 | 2010-11-23 17:06:45 -0800 | [diff] [blame] | 99 | "authority1", |
| 100 | b2, |
Fred Quintana | 0c4d04a | 2010-11-03 17:02:55 -0700 | [diff] [blame] | 101 | false); |
Fabrice Di Meglio | 7e118b5 | 2010-11-23 17:06:45 -0800 | [diff] [blame] | 102 | |
| 103 | assertEquals(op1.key, op2.key); |
| 104 | assertNotSame(op1.key, op3.key); |
| 105 | assertNotSame(op1.key, op4.key); |
| 106 | assertNotSame(op1.key, op5.key); |
| 107 | } |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 108 | |
| 109 | @SmallTest |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 110 | public void testConversionToExtras() { |
| 111 | Account account1 = new Account("account1", "type1"); |
| 112 | Bundle b1 = new Bundle(); |
| 113 | b1.putParcelable("acc", account1); |
| 114 | b1.putString("str", "String"); |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 115 | |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 116 | SyncOperation op1 = new SyncOperation(account1, 0, |
| 117 | 1, "foo", 0, |
| 118 | SyncOperation.REASON_PERIODIC, |
| 119 | "authority1", |
| 120 | b1, |
| 121 | false); |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 122 | |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 123 | PersistableBundle pb = op1.toJobInfoExtras(); |
| 124 | SyncOperation op2 = SyncOperation.maybeCreateFromJobExtras(pb); |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 125 | |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 126 | assertTrue("Account fields in extras not persisted.", |
| 127 | account1.equals(op2.extras.get("acc"))); |
| 128 | assertTrue("Fields in extras not persisted", "String".equals(op2.extras.getString("str"))); |
Matthew Williams | 6428046 | 2014-01-09 10:49:23 -0800 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | @SmallTest |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 132 | public void testConversionFromExtras() { |
| 133 | PersistableBundle extras = new PersistableBundle(); |
| 134 | SyncOperation op = SyncOperation.maybeCreateFromJobExtras(extras); |
| 135 | assertTrue("Non sync operation bundle falsely converted to SyncOperation.", op == null); |
| 136 | } |
Matthew Williams | 6428046 | 2014-01-09 10:49:23 -0800 | [diff] [blame] | 137 | |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 138 | /** |
| 139 | * Tests whether a failed periodic sync operation is converted correctly into a one time |
| 140 | * sync operation, and whether the periodic sync can be re-created from the one-time operation. |
| 141 | */ |
| 142 | @SmallTest |
| 143 | public void testFailedPeriodicConversion() { |
| 144 | SyncStorageEngine.EndPoint ep = new SyncStorageEngine.EndPoint(new Account("name", "type"), |
| 145 | "provider", 0); |
| 146 | Bundle extras = new Bundle(); |
| 147 | SyncOperation periodic = new SyncOperation(ep, 0, "package", 0, 0, extras, false, true, |
Shreyas Basarge | 1675400 | 2016-02-08 23:52:27 +0000 | [diff] [blame] | 148 | SyncOperation.NO_JOB_ID, 60000, 10000); |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 149 | SyncOperation oneoff = periodic.createOneTimeSyncOperation(); |
| 150 | assertFalse("Conversion to oneoff sync failed.", oneoff.isPeriodic); |
Shreyas Basarge | 1675400 | 2016-02-08 23:52:27 +0000 | [diff] [blame] | 151 | assertEquals("Period not restored", periodic.periodMillis, oneoff.periodMillis); |
| 152 | assertEquals("Flex not restored", periodic.flexMillis, oneoff.flexMillis); |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 153 | } |
Fabrice Di Meglio | 7e118b5 | 2010-11-23 17:06:45 -0800 | [diff] [blame] | 154 | } |