| 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.notifier; |
| 6 | |
| 7 | import android.accounts.Account; |
| 8 | import android.content.ComponentName; |
| 9 | import android.content.Intent; |
| 10 | import android.os.Bundle; |
| 11 | |
| 12 | import com.google.ipc.invalidation.external.client.types.ObjectId; |
| 13 | |
| 14 | import org.chromium.base.CollectionUtil; |
| 15 | |
| 16 | import java.util.ArrayList; |
| 17 | import java.util.List; |
| 18 | |
| 19 | /** |
| 20 | * Subclass of {@link InvalidationService} that captures events and allows controlling |
| 21 | * whether or not Chrome is in the foreground and sync is enabled. |
| 22 | * |
| 23 | * @author dsmyers@google.com (Daniel Myers) |
| 24 | */ |
| 25 | public class TestableInvalidationService extends InvalidationService { |
| 26 | /** Object ids given to {@link #register}, one list element per call. */ |
| 27 | final List<List<ObjectId>> mRegistrations = new ArrayList<List<ObjectId>>(); |
| 28 | |
| 29 | /** Object ids given to {@link #unregister}, one list element per call. */ |
| 30 | final List<List<ObjectId>> mUnregistrations = new ArrayList<List<ObjectId>>(); |
| 31 | |
| 32 | /** Intents given to {@link #startService}. */ |
| 33 | final List<Intent> mStartedServices = new ArrayList<Intent>(); |
| 34 | |
| 35 | /** Bundles given to {@link #requestSyncFromContentResolver}. */ |
| 36 | final List<Bundle> mRequestedSyncs = new ArrayList<Bundle>(); |
| 37 | |
| 38 | final List<byte[]> mAcknowledgements = new ArrayList<byte[]>(); |
| 39 | |
| 40 | /** Whether Chrome is in the foreground. */ |
| 41 | private boolean mIsChromeInForeground = false; |
| 42 | |
| 43 | /** Whether sync is enabled. */ |
| 44 | private boolean mIsSyncEnabled = false; |
| 45 | |
| 46 | public TestableInvalidationService() { |
| 47 | } |
| 48 | |
| 49 | @Override |
| 50 | public void acknowledge(byte[] ackHandle) { |
| 51 | mAcknowledgements.add(ackHandle); |
| 52 | } |
| 53 | |
| 54 | @Override |
| 55 | public void register(byte[] clientId, Iterable<ObjectId> objectIds) { |
| 56 | mRegistrations.add(CollectionUtil.newArrayList(objectIds)); |
| 57 | super.register(clientId, objectIds); |
| 58 | } |
| 59 | |
| 60 | @Override |
| 61 | public void unregister(byte[] clientId, Iterable<ObjectId> objectIds) { |
| 62 | mUnregistrations.add(CollectionUtil.newArrayList(objectIds)); |
| 63 | super.unregister(clientId, objectIds); |
| 64 | } |
| 65 | |
| 66 | @Override |
| 67 | public ComponentName startService(Intent intent) { |
| 68 | mStartedServices.add(intent); |
| 69 | return super.startService(intent); |
| 70 | } |
| 71 | |
| 72 | @Override |
| 73 | public void requestSyncFromContentResolver(Bundle bundle, Account account, |
| 74 | String contractAuthority) { |
| 75 | mRequestedSyncs.add(bundle); |
| 76 | super.requestSyncFromContentResolver(bundle, account, contractAuthority); |
| 77 | } |
| 78 | |
| 79 | @Override |
| 80 | boolean isChromeInForeground() { |
| 81 | return mIsChromeInForeground; |
| 82 | } |
| 83 | |
| 84 | @Override |
| 85 | boolean isSyncEnabled() { |
| 86 | return mIsSyncEnabled; |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Sets the variables used to control whether or not a notification client should be running. |
| 91 | * @param isChromeInForeground whether Chrome is in the foreground |
| 92 | * @param isSyncEnabled whether sync is enabled |
| 93 | */ |
| 94 | void setShouldRunStates(boolean isChromeInForeground, boolean isSyncEnabled) { |
| 95 | this.mIsChromeInForeground = isChromeInForeground; |
| 96 | this.mIsSyncEnabled = isSyncEnabled; |
| 97 | } |
| 98 | } |