| 1 | // Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | package org.chromium.sync.test.util; |
| 6 | |
| 7 | |
| 8 | import android.accounts.Account; |
| 9 | import android.content.ContentResolver; |
| 10 | import android.content.SyncStatusObserver; |
| 11 | import android.os.AsyncTask; |
| 12 | |
| 13 | import junit.framework.Assert; |
| 14 | |
| 15 | import org.chromium.base.ThreadUtils; |
| 16 | import org.chromium.sync.notifier.SyncContentResolverDelegate; |
| 17 | |
| 18 | import java.util.HashMap; |
| 19 | import java.util.HashSet; |
| 20 | import java.util.Map; |
| 21 | import java.util.Set; |
| 22 | import java.util.concurrent.Semaphore; |
| 23 | import java.util.concurrent.TimeUnit; |
| 24 | |
| 25 | |
| 26 | /** |
| 27 | * Mock implementation of the SyncContentResolverWrapper. |
| 28 | * |
| 29 | * This implementation only supports status change listeners for the type |
| 30 | * SYNC_OBSERVER_TYPE_SETTINGS. |
| 31 | */ |
| 32 | public class MockSyncContentResolverDelegate implements SyncContentResolverDelegate { |
| 33 | |
| 34 | private final Map<String, Boolean> mSyncAutomaticallyMap; |
| 35 | |
| 36 | private final Set<AsyncSyncStatusObserver> mObservers; |
| 37 | |
| 38 | private boolean mMasterSyncAutomatically; |
| 39 | |
| 40 | private Semaphore mPendingObserverCount; |
| 41 | |
| 42 | public MockSyncContentResolverDelegate() { |
| 43 | mSyncAutomaticallyMap = new HashMap<String, Boolean>(); |
| 44 | mObservers = new HashSet<AsyncSyncStatusObserver>(); |
| 45 | } |
| 46 | |
| 47 | @Override |
| 48 | public Object addStatusChangeListener(int mask, SyncStatusObserver callback) { |
| 49 | if (mask != ContentResolver.SYNC_OBSERVER_TYPE_SETTINGS) { |
| 50 | throw new IllegalArgumentException("This implementation only supports " |
| 51 | + "ContentResolver.SYNC_OBSERVER_TYPE_SETTINGS as the mask"); |
| 52 | } |
| 53 | AsyncSyncStatusObserver asyncSyncStatusObserver = new AsyncSyncStatusObserver(callback); |
| 54 | synchronized (mObservers) { |
| 55 | mObservers.add(asyncSyncStatusObserver); |
| 56 | } |
| 57 | return asyncSyncStatusObserver; |
| 58 | } |
| 59 | |
| 60 | @Override |
| 61 | public void removeStatusChangeListener(Object handle) { |
| 62 | synchronized (mObservers) { |
| 63 | mObservers.remove(handle); |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | @Override |
| 68 | public void setMasterSyncAutomatically(boolean sync) { |
| 69 | if (mMasterSyncAutomatically == sync) return; |
| 70 | |
| 71 | mMasterSyncAutomatically = sync; |
| 72 | notifyObservers(); |
| 73 | } |
| 74 | |
| 75 | @Override |
| 76 | public boolean getMasterSyncAutomatically() { |
| 77 | return mMasterSyncAutomatically; |
| 78 | } |
| 79 | |
| 80 | @Override |
| 81 | public boolean getSyncAutomatically(Account account, String authority) { |
| 82 | String key = createKey(account, authority); |
| 83 | synchronized (mSyncAutomaticallyMap) { |
| 84 | return mSyncAutomaticallyMap.containsKey(key) && mSyncAutomaticallyMap.get(key); |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | @Override |
| 89 | public void setSyncAutomatically(Account account, String authority, boolean sync) { |
| 90 | String key = createKey(account, authority); |
| 91 | synchronized (mSyncAutomaticallyMap) { |
| 92 | if (!mSyncAutomaticallyMap.containsKey(key)) { |
| 93 | throw new IllegalArgumentException("Account " + account + |
| 94 | " is not syncable for authority " + authority + |
| 95 | ". Can not set sync state to " + sync); |
| 96 | } |
| 97 | if (mSyncAutomaticallyMap.get(key) == sync) return; |
| 98 | mSyncAutomaticallyMap.put(key, sync); |
| 99 | } |
| 100 | notifyObservers(); |
| 101 | } |
| 102 | |
| 103 | @Override |
| 104 | public void setIsSyncable(Account account, String authority, int syncable) { |
| 105 | String key = createKey(account, authority); |
| 106 | |
| 107 | synchronized (mSyncAutomaticallyMap) { |
| 108 | switch (syncable) { |
| 109 | case 0: |
| 110 | if (!mSyncAutomaticallyMap.containsKey(key)) return; |
| 111 | |
| 112 | mSyncAutomaticallyMap.remove(key); |
| 113 | break; |
| 114 | case 1: |
| 115 | if (mSyncAutomaticallyMap.containsKey(key)) return; |
| 116 | |
| 117 | mSyncAutomaticallyMap.put(key, false); |
| 118 | break; |
| 119 | default: |
| 120 | throw new IllegalArgumentException("Unable to understand syncable argument: " + |
| 121 | syncable); |
| 122 | } |
| 123 | } |
| 124 | notifyObservers(); |
| 125 | } |
| 126 | |
| 127 | @Override |
| 128 | public int getIsSyncable(Account account, String authority) { |
| 129 | synchronized (mSyncAutomaticallyMap) { |
| 130 | final Boolean isSyncable = mSyncAutomaticallyMap.get(createKey(account, authority)); |
| 131 | if (isSyncable == null) { |
| 132 | return -1; |
| 133 | } |
| 134 | return isSyncable ? 1 : 0; |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | private static String createKey(Account account, String authority) { |
| 139 | return account.name + "@@@" + account.type + "@@@" + authority; |
| 140 | } |
| 141 | |
| 142 | private void notifyObservers() { |
| 143 | synchronized (mObservers) { |
| 144 | mPendingObserverCount = new Semaphore(1 - mObservers.size()); |
| 145 | for (AsyncSyncStatusObserver observer : mObservers) { |
| 146 | observer.notifyObserverAsync(mPendingObserverCount); |
| 147 | } |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | * Blocks until the last notification has been issued to all registered observers. |
| 153 | * Note that if an observer is removed while a notification is being handled this can |
| 154 | * fail to return correctly. |
| 155 | * |
| 156 | * @throws InterruptedException |
| 157 | */ |
| 158 | public void waitForLastNotificationCompleted() throws InterruptedException { |
| 159 | Assert.assertTrue("Timed out waiting for notifications to complete.", |
| 160 | mPendingObserverCount.tryAcquire(5, TimeUnit.SECONDS)); |
| 161 | } |
| 162 | |
| 163 | private static class AsyncSyncStatusObserver { |
| 164 | |
| 165 | private final SyncStatusObserver mSyncStatusObserver; |
| 166 | |
| 167 | private AsyncSyncStatusObserver(SyncStatusObserver syncStatusObserver) { |
| 168 | mSyncStatusObserver = syncStatusObserver; |
| 169 | } |
| 170 | |
| 171 | private void notifyObserverAsync(final Semaphore pendingObserverCount) { |
| 172 | if (ThreadUtils.runningOnUiThread()) { |
| 173 | new AsyncTask<Void, Void, Void>() { |
| 174 | @Override |
| 175 | protected Void doInBackground(Void... params) { |
| 176 | mSyncStatusObserver.onStatusChanged( |
| 177 | ContentResolver.SYNC_OBSERVER_TYPE_SETTINGS); |
| 178 | return null; |
| 179 | } |
| 180 | |
| 181 | @Override |
| 182 | protected void onPostExecute(Void result) { |
| 183 | pendingObserverCount.release(); |
| 184 | } |
| 185 | }.execute(); |
| 186 | } else { |
| 187 | mSyncStatusObserver.onStatusChanged( |
| 188 | ContentResolver.SYNC_OBSERVER_TYPE_SETTINGS); |
| 189 | pendingObserverCount.release(); |
| 190 | } |
| 191 | } |
| 192 | } |
| 193 | } |