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; |
Fred Quintana | d9d2f11 | 2009-04-23 13:36:27 -0700 | [diff] [blame] | 25 | import android.accounts.Account; |
| 26 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 27 | /** |
| 28 | * If you would like to test sync a single provider with an |
| 29 | * {@link InstrumentationTestCase}, this provides some of the boiler plate in {@link #setUp} and |
| 30 | * {@link #tearDown}. |
| 31 | */ |
| 32 | public class SyncBaseInstrumentation extends InstrumentationTestCase { |
| 33 | private Context mTargetContext; |
| 34 | ContentResolver mContentResolver; |
| 35 | private static final int MAX_TIME_FOR_SYNC_IN_MINS = 20; |
| 36 | |
| 37 | @Override |
| 38 | protected void setUp() throws Exception { |
| 39 | super.setUp(); |
| 40 | mTargetContext = getInstrumentation().getTargetContext(); |
| 41 | mContentResolver = mTargetContext.getContentResolver(); |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Syncs the specified provider. |
| 46 | * @throws Exception |
| 47 | */ |
| 48 | protected void syncProvider(Uri uri, String account, String authority) throws Exception { |
| 49 | Bundle extras = new Bundle(); |
| 50 | extras.putBoolean(ContentResolver.SYNC_EXTRAS_FORCE, true); |
Fred Quintana | d9d2f11 | 2009-04-23 13:36:27 -0700 | [diff] [blame] | 51 | Account account1 = new Account(account, "com.google.GAIA"); |
| 52 | extras.putParcelable(ContentResolver.SYNC_EXTRAS_ACCOUNT, account1); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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 | |
Fred Quintana | d9d2f11 | 2009-04-23 13:36:27 -0700 | [diff] [blame] | 69 | if (isSyncActive(account1, authority)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 70 | counter = 0; |
| 71 | continue; |
| 72 | } |
| 73 | counter++; |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | protected void cancelSyncsandDisableAutoSync() { |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 78 | try { |
| 79 | ContentResolver.getContentService().setListenForNetworkTickles(false); |
| 80 | } catch (RemoteException e) { |
| 81 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 82 | mContentResolver.cancelSync(null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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 | */ |
Fred Quintana | d9d2f11 | 2009-04-23 13:36:27 -0700 | [diff] [blame] | 90 | private boolean isSyncActive(Account account, String authority) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 91 | try { |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 92 | return ContentResolver.getContentService().isSyncActive(account, |
| 93 | authority); |
| 94 | } catch (RemoteException e) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 95 | return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 96 | } |
| 97 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 98 | } |