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