1 /*
2  * Copyright (C) 2010 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package android.content.cts;
18 
19 import android.accounts.Account;
20 import android.content.ContentResolver;
21 import android.content.ISyncAdapter;
22 import android.content.ISyncContext;
23 import android.os.Bundle;
24 import android.os.RemoteException;
25 
26 import java.util.ArrayList;
27 import java.util.concurrent.CountDownLatch;
28 
29 public class MockSyncAdapter extends ISyncAdapter.Stub {
30 
31     private static MockSyncAdapter sSyncAdapter = null;
32 
33     private ArrayList<Account> mAccounts = new ArrayList<Account>();
34     private String mAuthority;
35     private Bundle mExtras;
36     private boolean mInitialized;
37     private boolean mStartSync;
38     private boolean mCancelSync;
39     private CountDownLatch mLatch;
40 
getAccounts()41     public ArrayList<Account> getAccounts() {
42         return mAccounts;
43     }
44 
getAuthority()45     public String getAuthority() {
46         return mAuthority;
47     }
48 
getExtras()49     public Bundle getExtras() {
50         return mExtras;
51     }
52 
isInitialized()53     public boolean isInitialized() {
54         return mInitialized;
55     }
56 
isStartSync()57     public boolean isStartSync() {
58         return mStartSync;
59     }
60 
isCancelSync()61     public boolean isCancelSync() {
62         return mCancelSync;
63     }
64 
clearData()65     public void clearData() {
66         mAccounts.clear();
67         mAuthority = null;
68         mExtras = null;
69         mInitialized = false;
70         mStartSync = false;
71         mCancelSync = false;
72         mLatch = null;
73     }
74 
setLatch(CountDownLatch mLatch)75     public void setLatch(CountDownLatch mLatch) {
76         this.mLatch = mLatch;
77     }
78 
startSync(ISyncContext syncContext, String authority, Account account, Bundle extras)79     public void startSync(ISyncContext syncContext, String authority, Account account,
80             Bundle extras) throws RemoteException {
81 
82         mAccounts.add(account);
83         mAuthority = authority;
84         mExtras = extras;
85 
86         if (null != extras && extras.getBoolean(ContentResolver.SYNC_EXTRAS_INITIALIZE)) {
87             mInitialized = true;
88             mStartSync = false;
89             mCancelSync = false;
90         } else {
91             mInitialized = false;
92             mStartSync = true;
93             mCancelSync = false;
94         }
95 
96         if (null != mLatch) {
97             mLatch.countDown();
98         }
99     }
100 
cancelSync(ISyncContext syncContext)101     public void cancelSync(ISyncContext syncContext) throws RemoteException {
102         mAccounts.clear();
103         mAuthority = null;
104         mExtras = null;
105 
106         mInitialized = false;
107         mStartSync = false;
108         mCancelSync = true;
109 
110         if (null != mLatch) {
111             mLatch.countDown();
112         }
113     }
114 
initialize(android.accounts.Account account, java.lang.String authority)115     public void initialize(android.accounts.Account account, java.lang.String authority)
116             throws android.os.RemoteException {
117 
118         mAccounts.add(account);
119         mAuthority = authority;
120 
121         mInitialized = true;
122         mStartSync = false;
123         mCancelSync = false;
124 
125         if (null != mLatch) {
126             mLatch.countDown();
127         }
128     }
129 
getMockSyncAdapter()130     public static MockSyncAdapter getMockSyncAdapter() {
131         if (null == sSyncAdapter) {
132             sSyncAdapter = new MockSyncAdapter();
133         }
134         return sSyncAdapter;
135     }
136 }
137