blob: 0b4bfed3e9cc36dbbd730916a35ab21ff4280242 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 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
17package android.test;
18
19import android.content.ContentResolver;
20import android.content.Context;
21import android.content.ContentValues;
22import android.os.Bundle;
23import android.os.SystemClock;
24import android.provider.Sync;
25import android.net.Uri;
Fred Quintanad9d2f112009-04-23 13:36:27 -070026import android.accounts.Account;
27
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080028import java.util.Map;
29
30/**
31 * If you would like to test sync a single provider with an
32 * {@link InstrumentationTestCase}, this provides some of the boiler plate in {@link #setUp} and
33 * {@link #tearDown}.
34 */
35public class SyncBaseInstrumentation extends InstrumentationTestCase {
36 private Context mTargetContext;
37 ContentResolver mContentResolver;
38 private static final int MAX_TIME_FOR_SYNC_IN_MINS = 20;
39
40 @Override
41 protected void setUp() throws Exception {
42 super.setUp();
43 mTargetContext = getInstrumentation().getTargetContext();
44 mContentResolver = mTargetContext.getContentResolver();
45 }
46
47 /**
48 * Syncs the specified provider.
49 * @throws Exception
50 */
51 protected void syncProvider(Uri uri, String account, String authority) throws Exception {
52 Bundle extras = new Bundle();
53 extras.putBoolean(ContentResolver.SYNC_EXTRAS_FORCE, true);
Fred Quintanad9d2f112009-04-23 13:36:27 -070054 Account account1 = new Account(account, "com.google.GAIA");
55 extras.putParcelable(ContentResolver.SYNC_EXTRAS_ACCOUNT, account1);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080056
57 mContentResolver.startSync(uri, extras);
58 long startTimeInMillis = SystemClock.elapsedRealtime();
59 long endTimeInMillis = startTimeInMillis + MAX_TIME_FOR_SYNC_IN_MINS * 60000;
60
61 int counter = 0;
62 // Making sure race condition does not occur when en entry have been removed from pending
63 // and active tables and loaded in memory (therefore sync might be still in progress)
64 while (counter < 2) {
65 // Sleep for 1 second.
66 Thread.sleep(1000);
67 // Finish test if time to sync has exceeded max time.
68 if (SystemClock.elapsedRealtime() > endTimeInMillis) {
69 break;
70 }
71
Fred Quintanad9d2f112009-04-23 13:36:27 -070072 if (isSyncActive(account1, authority)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080073 counter = 0;
74 continue;
75 }
76 counter++;
77 }
78 }
79
80 protected void cancelSyncsandDisableAutoSync() {
81 Sync.Settings.QueryMap mSyncSettings =
82 new Sync.Settings.QueryMap(mContentResolver, true, null);
83 mSyncSettings.setListenForNetworkTickles(false);
84 mContentResolver.cancelSync(null);
85 mSyncSettings.close();
86 }
87
88 /**
89 * This method tests if any sync is active or not. Sync is considered to be active if the
90 * entry is in either the Pending or Active tables.
91 * @return
92 */
Fred Quintanad9d2f112009-04-23 13:36:27 -070093 private boolean isSyncActive(Account account, String authority) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080094 Sync.Pending.QueryMap pendingQueryMap = null;
95 Sync.Active.QueryMap activeQueryMap = null;
96 try {
97 pendingQueryMap = new Sync.Pending.QueryMap(mContentResolver, false, null);
98 activeQueryMap = new Sync.Active.QueryMap(mContentResolver, false, null);
99
100 if (pendingQueryMap.isPending(account, authority)) {
101 return true;
102 }
103 if (isActiveInActiveQueryMap(activeQueryMap, account, authority)) {
104 return true;
105 }
106 return false;
107 } finally {
108 activeQueryMap.close();
109 pendingQueryMap.close();
110 }
111 }
112
Fred Quintanad9d2f112009-04-23 13:36:27 -0700113 private boolean isActiveInActiveQueryMap(Sync.Active.QueryMap activemap, Account account,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800114 String authority) {
115 Map<String, ContentValues> rows = activemap.getRows();
116 for (ContentValues values : rows.values()) {
Fred Quintanad9d2f112009-04-23 13:36:27 -0700117 if (values.getAsString("account").equals(account.mName)
118 && values.getAsString("account_type").equals(account.mType)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800119 && values.getAsString("authority").equals(authority)) {
120 return true;
121 }
122 }
123 return false;
124 }
125}