OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package org.chromium.sync.test.util; | 5 package org.chromium.components.sync.test.util; |
6 | |
7 | 6 |
8 import android.accounts.Account; | 7 import android.accounts.Account; |
9 import android.content.ContentResolver; | 8 import android.content.ContentResolver; |
10 import android.content.SyncStatusObserver; | 9 import android.content.SyncStatusObserver; |
11 import android.os.AsyncTask; | 10 import android.os.AsyncTask; |
12 import android.os.Bundle; | 11 import android.os.Bundle; |
13 | 12 |
14 import junit.framework.Assert; | 13 import junit.framework.Assert; |
15 | 14 |
16 import org.chromium.base.ThreadUtils; | 15 import org.chromium.base.ThreadUtils; |
17 import org.chromium.base.VisibleForTesting; | 16 import org.chromium.base.VisibleForTesting; |
18 import org.chromium.sync.SyncContentResolverDelegate; | 17 import org.chromium.components.sync.SyncContentResolverDelegate; |
19 | 18 |
20 import java.util.HashMap; | 19 import java.util.HashMap; |
21 import java.util.HashSet; | 20 import java.util.HashSet; |
22 import java.util.Map; | 21 import java.util.Map; |
23 import java.util.Set; | 22 import java.util.Set; |
24 import java.util.concurrent.Semaphore; | 23 import java.util.concurrent.Semaphore; |
25 import java.util.concurrent.TimeUnit; | 24 import java.util.concurrent.TimeUnit; |
26 | 25 |
27 | |
28 /** | 26 /** |
29 * Mock implementation of the {@link SyncContentResolverDelegate}. | 27 * Mock implementation of the {@link SyncContentResolverDelegate}. |
30 * | 28 * |
31 * This implementation only supports status change listeners for the type | 29 * This implementation only supports status change listeners for the type |
32 * SYNC_OBSERVER_TYPE_SETTINGS. | 30 * SYNC_OBSERVER_TYPE_SETTINGS. |
33 */ | 31 */ |
34 public class MockSyncContentResolverDelegate implements SyncContentResolverDeleg
ate { | 32 public class MockSyncContentResolverDelegate implements SyncContentResolverDeleg
ate { |
35 | |
36 private final Set<String> mSyncAutomaticallySet; | 33 private final Set<String> mSyncAutomaticallySet; |
37 private final Map<String, Boolean> mIsSyncableMap; | 34 private final Map<String, Boolean> mIsSyncableMap; |
38 private final Object mSyncableMapLock = new Object(); | 35 private final Object mSyncableMapLock = new Object(); |
39 | 36 |
40 private final Set<AsyncSyncStatusObserver> mObservers; | 37 private final Set<AsyncSyncStatusObserver> mObservers; |
41 | 38 |
42 private boolean mMasterSyncAutomatically; | 39 private boolean mMasterSyncAutomatically; |
43 private boolean mDisableObserverNotifications; | 40 private boolean mDisableObserverNotifications; |
44 | 41 |
45 private Semaphore mPendingObserverCount; | 42 private Semaphore mPendingObserverCount; |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
121 break; | 118 break; |
122 case 1: | 119 case 1: |
123 mIsSyncableMap.put(key, true); | 120 mIsSyncableMap.put(key, true); |
124 break; | 121 break; |
125 case -1: | 122 case -1: |
126 if (mIsSyncableMap.containsKey(key)) { | 123 if (mIsSyncableMap.containsKey(key)) { |
127 mIsSyncableMap.remove(key); | 124 mIsSyncableMap.remove(key); |
128 } | 125 } |
129 break; | 126 break; |
130 default: | 127 default: |
131 throw new IllegalArgumentException("Unable to understand syn
cable argument: " | 128 throw new IllegalArgumentException( |
132 + syncable); | 129 "Unable to understand syncable argument: " + syncabl
e); |
133 } | 130 } |
134 } | 131 } |
135 notifyObservers(); | 132 notifyObservers(); |
136 } | 133 } |
137 | 134 |
138 @Override | 135 @Override |
139 public int getIsSyncable(Account account, String authority) { | 136 public int getIsSyncable(Account account, String authority) { |
140 String key = createKey(account, authority); | 137 String key = createKey(account, authority); |
141 synchronized (mSyncableMapLock) { | 138 synchronized (mSyncableMapLock) { |
142 if (mIsSyncableMap.containsKey(key)) { | 139 if (mIsSyncableMap.containsKey(key)) { |
143 return mIsSyncableMap.get(key) ? 1 : 0; | 140 return mIsSyncableMap.get(key) ? 1 : 0; |
144 } else { | 141 } else { |
145 return -1; | 142 return -1; |
146 } | 143 } |
147 } | 144 } |
148 } | 145 } |
149 | 146 |
150 @Override | 147 @Override |
151 public void removePeriodicSync(Account account, String authority, Bundle ext
ras) { | 148 public void removePeriodicSync(Account account, String authority, Bundle ext
ras) {} |
152 } | |
153 | 149 |
154 private static String createKey(Account account, String authority) { | 150 private static String createKey(Account account, String authority) { |
155 return account.name + "@@@" + account.type + "@@@" + authority; | 151 return account.name + "@@@" + account.type + "@@@" + authority; |
156 } | 152 } |
157 | 153 |
158 private void notifyObservers() { | 154 private void notifyObservers() { |
159 if (mDisableObserverNotifications) return; | 155 if (mDisableObserverNotifications) return; |
160 synchronized (mObservers) { | 156 synchronized (mObservers) { |
161 mPendingObserverCount = new Semaphore(1 - mObservers.size()); | 157 mPendingObserverCount = new Semaphore(1 - mObservers.size()); |
162 for (AsyncSyncStatusObserver observer : mObservers) { | 158 for (AsyncSyncStatusObserver observer : mObservers) { |
(...skipping 12 matching lines...) Expand all Loading... |
175 @VisibleForTesting | 171 @VisibleForTesting |
176 public void waitForLastNotificationCompleted() throws InterruptedException { | 172 public void waitForLastNotificationCompleted() throws InterruptedException { |
177 Assert.assertTrue("Timed out waiting for notifications to complete.", | 173 Assert.assertTrue("Timed out waiting for notifications to complete.", |
178 mPendingObserverCount.tryAcquire(5, TimeUnit.SECONDS)); | 174 mPendingObserverCount.tryAcquire(5, TimeUnit.SECONDS)); |
179 } | 175 } |
180 | 176 |
181 public void disableObserverNotifications() { | 177 public void disableObserverNotifications() { |
182 mDisableObserverNotifications = true; | 178 mDisableObserverNotifications = true; |
183 } | 179 } |
184 | 180 |
185 /** | 181 /** |
186 * Simulate an account rename, which copies settings to the new account. | 182 * Simulate an account rename, which copies settings to the new account. |
187 */ | 183 */ |
188 public void renameAccounts(Account oldAccount, Account newAccount, String au
thority) { | 184 public void renameAccounts(Account oldAccount, Account newAccount, String au
thority) { |
189 int oldIsSyncable = getIsSyncable(oldAccount, authority); | 185 int oldIsSyncable = getIsSyncable(oldAccount, authority); |
190 setIsSyncable(newAccount, authority, oldIsSyncable); | 186 setIsSyncable(newAccount, authority, oldIsSyncable); |
191 if (oldIsSyncable == 1) { | 187 if (oldIsSyncable == 1) { |
192 setSyncAutomatically( | 188 setSyncAutomatically( |
193 newAccount, authority, getSyncAutomatically(oldAccount, auth
ority)); | 189 newAccount, authority, getSyncAutomatically(oldAccount, auth
ority)); |
194 } | 190 } |
195 } | 191 } |
196 | 192 |
197 private static class AsyncSyncStatusObserver { | 193 private static class AsyncSyncStatusObserver { |
198 | |
199 private final SyncStatusObserver mSyncStatusObserver; | 194 private final SyncStatusObserver mSyncStatusObserver; |
200 | 195 |
201 private AsyncSyncStatusObserver(SyncStatusObserver syncStatusObserver) { | 196 private AsyncSyncStatusObserver(SyncStatusObserver syncStatusObserver) { |
202 mSyncStatusObserver = syncStatusObserver; | 197 mSyncStatusObserver = syncStatusObserver; |
203 } | 198 } |
204 | 199 |
205 private void notifyObserverAsync(final Semaphore pendingObserverCount) { | 200 private void notifyObserverAsync(final Semaphore pendingObserverCount) { |
206 if (ThreadUtils.runningOnUiThread()) { | 201 if (ThreadUtils.runningOnUiThread()) { |
207 new AsyncTask<Void, Void, Void>() { | 202 new AsyncTask<Void, Void, Void>() { |
208 @Override | 203 @Override |
209 protected Void doInBackground(Void... params) { | 204 protected Void doInBackground(Void... params) { |
210 mSyncStatusObserver.onStatusChanged( | 205 mSyncStatusObserver.onStatusChanged( |
211 ContentResolver.SYNC_OBSERVER_TYPE_SETTINGS); | 206 ContentResolver.SYNC_OBSERVER_TYPE_SETTINGS); |
212 return null; | 207 return null; |
213 } | 208 } |
214 | 209 |
215 @Override | 210 @Override |
216 protected void onPostExecute(Void result) { | 211 protected void onPostExecute(Void result) { |
217 pendingObserverCount.release(); | 212 pendingObserverCount.release(); |
218 } | 213 } |
219 }.execute(); | 214 }.execute(); |
220 } else { | 215 } else { |
221 mSyncStatusObserver.onStatusChanged( | 216 mSyncStatusObserver.onStatusChanged(ContentResolver.SYNC_OBSERVE
R_TYPE_SETTINGS); |
222 ContentResolver.SYNC_OBSERVER_TYPE_SETTINGS); | |
223 pendingObserverCount.release(); | 217 pendingObserverCount.release(); |
224 } | 218 } |
225 } | 219 } |
226 } | 220 } |
227 } | 221 } |
OLD | NEW |