The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [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; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 21 | import android.os.Bundle; |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame^] | 22 | import android.os.RemoteException; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 23 | import android.os.SystemClock; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 24 | import android.net.Uri; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 25 | |
| 26 | /** |
| 27 | * If you would like to test sync a single provider with an |
| 28 | * {@link InstrumentationTestCase}, this provides some of the boiler plate in {@link #setUp} and |
| 29 | * {@link #tearDown}. |
| 30 | */ |
| 31 | public class SyncBaseInstrumentation extends InstrumentationTestCase { |
| 32 | private Context mTargetContext; |
| 33 | ContentResolver mContentResolver; |
| 34 | private static final int MAX_TIME_FOR_SYNC_IN_MINS = 20; |
| 35 | |
| 36 | @Override |
| 37 | protected void setUp() throws Exception { |
| 38 | super.setUp(); |
| 39 | mTargetContext = getInstrumentation().getTargetContext(); |
| 40 | mContentResolver = mTargetContext.getContentResolver(); |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * Syncs the specified provider. |
| 45 | * @throws Exception |
| 46 | */ |
| 47 | protected void syncProvider(Uri uri, String account, String authority) throws Exception { |
| 48 | Bundle extras = new Bundle(); |
| 49 | extras.putBoolean(ContentResolver.SYNC_EXTRAS_FORCE, true); |
| 50 | extras.putString(ContentResolver.SYNC_EXTRAS_ACCOUNT, account); |
| 51 | |
| 52 | mContentResolver.startSync(uri, extras); |
| 53 | long startTimeInMillis = SystemClock.elapsedRealtime(); |
| 54 | long endTimeInMillis = startTimeInMillis + MAX_TIME_FOR_SYNC_IN_MINS * 60000; |
| 55 | |
| 56 | int counter = 0; |
| 57 | // Making sure race condition does not occur when en entry have been removed from pending |
| 58 | // and active tables and loaded in memory (therefore sync might be still in progress) |
| 59 | while (counter < 2) { |
| 60 | // Sleep for 1 second. |
| 61 | Thread.sleep(1000); |
| 62 | // Finish test if time to sync has exceeded max time. |
| 63 | if (SystemClock.elapsedRealtime() > endTimeInMillis) { |
| 64 | break; |
| 65 | } |
| 66 | |
| 67 | if (isSyncActive(account, authority)) { |
| 68 | counter = 0; |
| 69 | continue; |
| 70 | } |
| 71 | counter++; |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | protected void cancelSyncsandDisableAutoSync() { |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame^] | 76 | try { |
| 77 | ContentResolver.getContentService().setListenForNetworkTickles(false); |
| 78 | } catch (RemoteException e) { |
| 79 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 80 | mContentResolver.cancelSync(null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | /** |
| 84 | * This method tests if any sync is active or not. Sync is considered to be active if the |
| 85 | * entry is in either the Pending or Active tables. |
| 86 | * @return |
| 87 | */ |
| 88 | private boolean isSyncActive(String account, String authority) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 89 | try { |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame^] | 90 | return ContentResolver.getContentService().isSyncActive(account, |
| 91 | authority); |
| 92 | } catch (RemoteException e) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 93 | return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 94 | } |
| 95 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 96 | } |