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.
4
5package org.chromium.sync.notifier;
6
7import android.accounts.Account;
8import android.content.ComponentName;
9import android.content.Intent;
10import android.os.Bundle;
11
12import com.google.ipc.invalidation.external.client.types.ObjectId;
13
14import org.chromium.base.CollectionUtil;
15
16import java.util.ArrayList;
17import java.util.HashSet;
18import java.util.List;
19import java.util.Set;
20
21/**
22 * Subclass of {@link InvalidationService} that captures events and allows controlling
23 * whether or not Chrome is in the foreground and sync is enabled.
24 *
25 * @author dsmyers@google.com (Daniel Myers)
26 */
27public 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>>();
30
31    /** Object ids given to {@link #unregister}, one list element per call. */
32    final List<List<ObjectId>> mUnregistrations = new ArrayList<List<ObjectId>>();
33
34    /**
35     * Current registered based on the cumulative calls to {@link #register} and
36     * {@link #unregister}.
37     */
38    final Set<ObjectId> mCurrentRegistrations = new HashSet<ObjectId>();
39
40    /** Intents given to {@link #startService}. */
41    final List<Intent> mStartedServices = new ArrayList<Intent>();
42
43    /** Bundles given to {@link #requestSyncFromContentResolver}. */
44    final List<Bundle> mRequestedSyncs = new ArrayList<Bundle>();
45
46    final List<byte[]> mAcknowledgements = new ArrayList<byte[]>();
47
48    /** Whether Chrome is in the foreground. */
49    private boolean mIsChromeInForeground = false;
50
51    /** Whether sync is enabled. */
52    private boolean mIsSyncEnabled = false;
53
54    public TestableInvalidationService() {
55    }
56
57    @Override
58    public void acknowledge(byte[] ackHandle) {
59        mAcknowledgements.add(ackHandle);
60    }
61
62    @Override
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);
68    }
69
70    @Override
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);
76    }
77
78    @Override
79    public ComponentName startService(Intent intent) {
80        mStartedServices.add(intent);
81        return super.startService(intent);
82    }
83
84    @Override
85    public void requestSyncFromContentResolver(Bundle bundle, Account account,
86            String contractAuthority) {
87        mRequestedSyncs.add(bundle);
88        super.requestSyncFromContentResolver(bundle, account, contractAuthority);
89    }
90
91    @Override
92    boolean isChromeInForeground() {
93        return mIsChromeInForeground;
94    }
95
96    @Override
97    boolean isSyncEnabled() {
98        return mIsSyncEnabled;
99    }
100
101    /**
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
105     */
106    void setShouldRunStates(boolean isChromeInForeground, boolean isSyncEnabled) {
107        this.mIsChromeInForeground = isChromeInForeground;
108        this.mIsSyncEnabled = isSyncEnabled;
109    }
110}
111