1 /*
2  * Copyright (C) 2016 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 com.android.cts.content;
18 
19 import static org.mockito.Mockito.mock;
20 import static org.mockito.Mockito.when;
21 
22 import android.accounts.Account;
23 import android.content.AbstractThreadedSyncAdapter;
24 import android.content.ContentProviderClient;
25 import android.content.Context;
26 import android.content.SyncResult;
27 import android.os.Bundle;
28 
29 public class SyncAdapter extends AbstractThreadedSyncAdapter {
30     private final Object mLock = new Object();
31     private AbstractThreadedSyncAdapter mDelegate;
32 
setNewDelegate()33     public AbstractThreadedSyncAdapter setNewDelegate() {
34         AbstractThreadedSyncAdapter delegate = mock(AbstractThreadedSyncAdapter.class);
35         when(delegate.onUnsyncableAccount()).thenCallRealMethod();
36 
37         synchronized (mLock) {
38             mDelegate = delegate;
39         }
40 
41         return delegate;
42     }
43 
SyncAdapter(Context context, boolean autoInitialize)44     public SyncAdapter(Context context, boolean autoInitialize) {
45         super(context, autoInitialize);
46     }
47 
getCopyOfDelegate()48     private AbstractThreadedSyncAdapter getCopyOfDelegate() {
49         synchronized (mLock) {
50             return mDelegate;
51         }
52     }
53 
54     @Override
onPerformSync(Account account, Bundle extras, String authority, ContentProviderClient provider, SyncResult syncResult)55     public void onPerformSync(Account account, Bundle extras, String authority,
56             ContentProviderClient provider, SyncResult syncResult) {
57         AbstractThreadedSyncAdapter delegate = getCopyOfDelegate();
58 
59         if (delegate != null) {
60             delegate.onPerformSync(account, extras, authority, provider, syncResult);
61         }
62     }
63 
64     @Override
onUnsyncableAccount()65     public boolean onUnsyncableAccount() {
66         AbstractThreadedSyncAdapter delegate = getCopyOfDelegate();
67 
68         if (delegate == null) {
69             return true;
70         } else {
71             return delegate.onUnsyncableAccount();
72         }
73     }
74 }
75