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
3 // found in the LICENSE file.
5 package org.chromium.sync.notifier;
7 import android.accounts.Account;
8 import android.content.ComponentName;
9 import android.content.Intent;
10 import android.os.Bundle;
12 import com.google.ipc.invalidation.external.client.types.ObjectId;
14 import org.chromium.base.CollectionUtil;
16 import java.util.ArrayList;
17 import java.util.HashSet;
18 import java.util.List;
22 * Subclass of {@link InvalidationService} that captures events and allows controlling
23 * whether or not Chrome is in the foreground and sync is enabled.
25 * @author dsmyers@google.com (Daniel Myers)
27 public class TestableInvalidationService extends InvalidationService {
28 /** Object ids given to {@link #register}, one list element per call. */
29 final List<List<ObjectId>> mRegistrations = new ArrayList<List<ObjectId>>();
31 /** Object ids given to {@link #unregister}, one list element per call. */
32 final List<List<ObjectId>> mUnregistrations = new ArrayList<List<ObjectId>>();
35 * Current registered based on the cumulative calls to {@link #register} and
36 * {@link #unregister}.
38 final Set<ObjectId> mCurrentRegistrations = new HashSet<ObjectId>();
40 /** Intents given to {@link #startService}. */
41 final List<Intent> mStartedServices = new ArrayList<Intent>();
43 /** Bundles given to {@link #requestSyncFromContentResolver}. */
44 final List<Bundle> mRequestedSyncs = new ArrayList<Bundle>();
46 final List<byte[]> mAcknowledgements = new ArrayList<byte[]>();
48 /** Whether Chrome is in the foreground. */
49 private boolean mIsChromeInForeground = false;
51 /** Whether sync is enabled. */
52 private boolean mIsSyncEnabled = false;
54 public TestableInvalidationService() {
58 public void acknowledge(byte[] ackHandle) {
59 mAcknowledgements.add(ackHandle);
63 public void register(byte[] clientId, Iterable<ObjectId> objectIds) {
64 List<ObjectId> objectIdList = CollectionUtil.newArrayList(objectIds);
65 mRegistrations.add(objectIdList);
66 mCurrentRegistrations.addAll(objectIdList);
67 super.register(clientId, objectIds);
71 public void unregister(byte[] clientId, Iterable<ObjectId> objectIds) {
72 List<ObjectId> objectIdList = CollectionUtil.newArrayList(objectIds);
73 mUnregistrations.add(objectIdList);
74 mCurrentRegistrations.removeAll(objectIdList);
75 super.unregister(clientId, objectIds);
79 public ComponentName startService(Intent intent) {
80 mStartedServices.add(intent);
81 return super.startService(intent);
85 public void requestSyncFromContentResolver(Bundle bundle, Account account,
86 String contractAuthority) {
87 mRequestedSyncs.add(bundle);
88 super.requestSyncFromContentResolver(bundle, account, contractAuthority);
92 boolean isChromeInForeground() {
93 return mIsChromeInForeground;
97 boolean isSyncEnabled() {
98 return mIsSyncEnabled;
102 * Sets the variables used to control whether or not a notification client should be running.
103 * @param isChromeInForeground whether Chrome is in the foreground
104 * @param isSyncEnabled whether sync is enabled
106 void setShouldRunStates(boolean isChromeInForeground, boolean isSyncEnabled) {
107 this.mIsChromeInForeground = isChromeInForeground;
108 this.mIsSyncEnabled = isSyncEnabled;