blob: 7d418f09ead96fcd119e643cf2413d3eaac55bc9 [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;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080021import android.os.Bundle;
22import android.os.SystemClock;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080023import android.net.Uri;
Fred Quintanad9d2f112009-04-23 13:36:27 -070024import android.accounts.Account;
25
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080026/**
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 */
31public 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 */
Fred Quintanaac9385e2009-06-22 18:00:59 -070047 protected void syncProvider(Uri uri, String accountName, String authority) throws Exception {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080048 Bundle extras = new Bundle();
Fred Quintana53bd2522010-02-05 15:28:12 -080049 extras.putBoolean(ContentResolver.SYNC_EXTRAS_IGNORE_SETTINGS, true);
Costin Manolache3348f142009-09-29 18:58:36 -070050 Account account = new Account(accountName, "com.google");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080051
Fred Quintanaac9385e2009-06-22 18:00:59 -070052 ContentResolver.requestSync(account, authority, extras);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080053 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
Fred Quintanaac9385e2009-06-22 18:00:59 -070067 if (ContentResolver.isSyncActive(account, authority)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080068 counter = 0;
69 continue;
70 }
71 counter++;
72 }
73 }
74
75 protected void cancelSyncsandDisableAutoSync() {
Fred Quintanaac9385e2009-06-22 18:00:59 -070076 ContentResolver.setMasterSyncAutomatically(false);
77 ContentResolver.cancelSync(null /* all accounts */, null /* all authorities */);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080078 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080079}