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