summaryrefslogtreecommitdiffstats
path: root/tests/src/com/android/providers/calendar/CalendarSyncTestingBase.java
blob: b5e1b76f922cac39013d0e4feca448dfdfbaa0d6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
/*
 * Copyright (C) 2009 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.providers.calendar;

import android.accounts.Account;
import android.accounts.AccountManager;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.os.SystemClock;
import android.provider.CalendarContract;
import android.test.SyncBaseInstrumentation;
import android.text.format.DateUtils;
import android.text.format.Time;
import android.util.Log;

import com.google.android.collect.Maps;

import java.util.HashSet;
import java.util.Map;
import java.util.Set;

public class CalendarSyncTestingBase extends SyncBaseInstrumentation {
    protected AccountManager mAccountManager;
    protected Context mTargetContext;
    protected String mAccount;
    protected ContentResolver mResolver;
    protected Uri mEventsUri = CalendarContract.Events.CONTENT_URI;

    static final String TAG = "calendar";
    static final String DEFAULT_TIMEZONE = "America/Los_Angeles";
    static final Set<String> EVENT_COLUMNS_TO_SKIP = new HashSet<String>();
    static final Set<String> ATTENDEES_COLUMNS_TO_SKIP = new HashSet<String>();
    static final Set<String> CALENDARS_COLUMNS_TO_SKIP = new HashSet<String>();
    static final Set<String> INSTANCES_COLUMNS_TO_SKIP = new HashSet<String>();

    static {
        EVENT_COLUMNS_TO_SKIP.add(CalendarContract.Events._ID);
        EVENT_COLUMNS_TO_SKIP.add(CalendarContract.Events.SYNC_DATA5);
        EVENT_COLUMNS_TO_SKIP.add(CalendarContract.Events.SYNC_DATA4);
        EVENT_COLUMNS_TO_SKIP.add(CalendarContract.Events.SYNC_DATA2);
        EVENT_COLUMNS_TO_SKIP.add(CalendarContract.Events.DIRTY);
        EVENT_COLUMNS_TO_SKIP.add(CalendarContract.Events.SYNC_DATA8);
        ATTENDEES_COLUMNS_TO_SKIP.add(CalendarContract.Attendees._ID);
        CALENDARS_COLUMNS_TO_SKIP.add(CalendarContract.Calendars._ID);
        CALENDARS_COLUMNS_TO_SKIP.add(CalendarContract.Calendars.CAL_SYNC8);
        CALENDARS_COLUMNS_TO_SKIP.add(CalendarContract.Calendars.CAL_SYNC7);
        CALENDARS_COLUMNS_TO_SKIP.add(CalendarContract.Calendars.DIRTY);
        CALENDARS_COLUMNS_TO_SKIP.add(CalendarContract.Calendars.CAL_SYNC6);
        INSTANCES_COLUMNS_TO_SKIP.add(CalendarContract.Instances._ID);
    }

    @Override
    protected void setUp() throws Exception {
        super.setUp();
        mTargetContext = getInstrumentation().getTargetContext();

        mAccountManager = AccountManager.get(mTargetContext);
        mAccount = getAccount();
        mResolver = mTargetContext.getContentResolver();
    }

    /**
     * A simple method that syncs the calendar provider.
     * @throws Exception
     */
    protected void syncCalendar() throws Exception {
        cancelSyncsandDisableAutoSync();
        syncProvider(CalendarContract.CONTENT_URI, mAccount, CalendarContract.AUTHORITY);
    }

    /**
     * Creates a new event in the default calendar.
     * @param event Event to be created.
     * @return Uri of the created event.
     * @throws Exception
     */
    protected Uri insertEvent(EventInfo event) throws Exception {
        return insertEvent(getDefaultCalendarId(), event);
    }

    /**
     * Creates a new event in the given calendarId.
     * @param calendarId Calendar to be used.
     * @param event Event to be created.
     * @return Uri of the event created.
     * @throws Exception
     */
    protected Uri insertEvent(int calendarId, EventInfo event) throws Exception{
        ContentValues m = new ContentValues();
        m.put(CalendarContract.Events.CALENDAR_ID, calendarId);
        m.put(CalendarContract.Events.TITLE, event.mTitle);
        m.put(CalendarContract.Events.DTSTART, event.mDtstart);
        m.put(CalendarContract.Events.ALL_DAY, event.mAllDay ? 1 : 0);

        if (event.mRrule == null) {
            // This is a normal event
            m.put(CalendarContract.Events.DTEND, event.mDtend);
        } else {
            // This is a repeating event
            m.put(CalendarContract.Events.RRULE, event.mRrule);
            m.put(CalendarContract.Events.DURATION, event.mDuration);
        }

        if (event.mDescription != null) {
            m.put(CalendarContract.Events.DESCRIPTION, event.mDescription);
        }
        if (event.mTimezone != null) {
            m.put(CalendarContract.Events.EVENT_TIMEZONE, event.mTimezone);
        }

        Uri url = mResolver.insert(mEventsUri, m);
        syncCalendar();
        return url;
    }

    /**
     * Edits the given event.
     * @param eventId EventID of the event to be edited.
     * @param event Edited event details.
     * @throws Exception
     */
    protected void editEvent(long eventId, EventInfo event) throws Exception {
        ContentValues values = new ContentValues();
        values.put(CalendarContract.Events.TITLE, event.mTitle);
        values.put(CalendarContract.Events.DTSTART, event.mDtstart);
        values.put(CalendarContract.Events.DTEND, event.mDtend);
        values.put(CalendarContract.Events.ALL_DAY, event.mAllDay ? 1 : 0);

        if (event.mDescription != null) {
            values.put(CalendarContract.Events.DESCRIPTION, event.mDescription);
        }

        Uri uri = ContentUris.withAppendedId(CalendarContract.Events.CONTENT_URI, eventId);
        mResolver.update(uri, values, null, null);
        syncCalendar();
    }

    /**
     * Deletes a given event.
     * @param uri
     * @throws Exception
     */
    protected void deleteEvent(Uri uri) throws Exception {
        mResolver.delete(uri, null, null);
        syncCalendar();
    }

    /**
     * Inserts a new calendar.
     * @param name
     * @param timezone
     * @param calendarUrl
     * @throws Exception
     */
    protected void insertCalendar(String name, String timezone, String calendarUrl)
            throws Exception {
        ContentValues values = new ContentValues();

        values.put(CalendarContract.Calendars.ACCOUNT_NAME, getAccount());
        values.put(CalendarContract.Calendars.CAL_SYNC1, calendarUrl);
        values.put(CalendarContract.Calendars.NAME, name);
        values.put(CalendarContract.Calendars.CALENDAR_DISPLAY_NAME, name);

        values.put(CalendarContract.Calendars.SYNC_EVENTS, 1);
        values.put(CalendarContract.Calendars.VISIBLE, 1);
        values.put(CalendarContract.Calendars.CALENDAR_COLOR, -14069085 /* blue */);
        values.put(CalendarContract.Calendars.CALENDAR_ACCESS_LEVEL,
                CalendarContract.Calendars.CAL_ACCESS_OWNER);

        values.put(CalendarContract.Calendars.CALENDAR_COLOR, "0xff123456");
        values.put(CalendarContract.Calendars.CALENDAR_TIME_ZONE, timezone);
        mResolver.insert(CalendarContract.Calendars.CONTENT_URI, values);
        syncCalendar();
    }

    /**
     * Returns a fresh count of events.
     * @return
     */
    protected int getEventsCount() {
        Cursor cursor;
        cursor = mResolver.query(mEventsUri, null, null, null, null);
        return cursor.getCount();
    }

    /**
     * Returns the ID of the default calendar.
     * @return
     */
    protected int getDefaultCalendarId() {
        Cursor calendarsCursor;
        calendarsCursor = mResolver.query(CalendarContract.Calendars.CONTENT_URI, null, null, null,
                null);
        calendarsCursor.moveToNext();
        return calendarsCursor.getInt(calendarsCursor.getColumnIndex("_id"));
    }

    /**
     * This class stores all the useful information about an event.
     */
    protected class EventInfo {
        String mTitle;
        String mDescription;
        String mTimezone;
        boolean mAllDay;
        long mDtstart;
        long mDtend;
        String mRrule;
        String mDuration;
        String mOriginalTitle;
        long mOriginalInstance;
        int mSyncId;

        // Constructor for normal events, using the default timezone
        public EventInfo(String title, String startDate, String endDate,
                boolean allDay) {
            init(title, startDate, endDate, allDay, DEFAULT_TIMEZONE);
        }

        public EventInfo(String title, long startDate, long endDate,
                boolean allDay) {
            mTitle = title;
            mTimezone = DEFAULT_TIMEZONE;
            mDtstart = startDate;
            mDtend = endDate;
            mDuration = null;
            mRrule = null;
            mAllDay = allDay;
        }

        public EventInfo(String title, long startDate, long endDate,
                boolean allDay, String description) {
            mTitle = title;
            mTimezone = DEFAULT_TIMEZONE;
            mDtstart = startDate;
            mDtend = endDate;
            mDuration = null;
            mRrule = null;
            mAllDay = allDay;
            mDescription = description;
        }

        // Constructor for normal events, specifying the timezone
        public EventInfo(String title, String startDate, String endDate,
                boolean allDay, String timezone) {
            init(title, startDate, endDate, allDay, timezone);
        }

        public void init(String title, String startDate, String endDate,
                boolean allDay, String timezone) {
            mTitle = title;
            Time time = new Time();
            if (allDay) {
                time.timezone = Time.TIMEZONE_UTC;
            } else if (timezone != null) {
                time.timezone = timezone;
            }
            mTimezone = time.timezone;
            time.parse3339(startDate);
            mDtstart = time.toMillis(false /* use isDst */);
            time.parse3339(endDate);
            mDtend = time.toMillis(false /* use isDst */);
            mDuration = null;
            mRrule = null;
            mAllDay = allDay;
        }

        // Constructor for repeating events, using the default timezone
        public EventInfo(String title, String description, String startDate, String endDate,
                String rrule, boolean allDay) {
            init(title, description, startDate, endDate, rrule, allDay, DEFAULT_TIMEZONE);
        }

        // Constructor for repeating events, specifying the timezone
        public EventInfo(String title, String description, String startDate, String endDate,
                String rrule, boolean allDay, String timezone) {
            init(title, description, startDate, endDate, rrule, allDay, timezone);
        }

        public void init(String title, String description, String startDate, String endDate,
                String rrule, boolean allDay, String timezone) {
            mTitle = title;
            mDescription = description;
            Time time = new Time();
            if (allDay) {
                time.timezone = Time.TIMEZONE_UTC;
            } else if (timezone != null) {
                time.timezone = timezone;
            }
            mTimezone = time.timezone;
            time.parse3339(startDate);
            mDtstart = time.toMillis(false /* use isDst */);
            if (endDate != null) {
                time.parse3339(endDate);
                mDtend = time.toMillis(false /* use isDst */);
            }
            if (allDay) {
                long days = 1;
                if (endDate != null) {
                    days = (mDtend - mDtstart) / DateUtils.DAY_IN_MILLIS;
                }
                mDuration = "P" + days + "D";
            } else {
                long seconds = (mDtend - mDtstart) / DateUtils.SECOND_IN_MILLIS;
                mDuration = "P" + seconds + "S";
            }
            mRrule = rrule;
            mAllDay = allDay;
        }

        // Constructor for recurrence exceptions, using the default timezone
        public EventInfo(String originalTitle, String originalInstance, String title,
                String description, String startDate, String endDate, boolean allDay) {
            init(originalTitle, originalInstance,
                    title, description, startDate, endDate, allDay, DEFAULT_TIMEZONE);
        }

        public void init(String originalTitle, String originalInstance,
                String title, String description, String startDate, String endDate,
                boolean allDay, String timezone) {
            mOriginalTitle = originalTitle;
            Time time = new Time(timezone);
            time.parse3339(originalInstance);
            mOriginalInstance = time.toMillis(false /* use isDst */);
            init(title, description, startDate, endDate, null /* rrule */, allDay, timezone);
        }
    }

    /**
     * Returns the default account on the device.
     * @return
     */
    protected String getAccount() {
        Account[] accounts = mAccountManager.getAccountsByType("com.google");

        assertTrue("Didn't find any Google accounts", accounts.length > 0);

        Account account = accounts[accounts.length - 1];
        Log.v(TAG, "Found " + accounts.length + " accounts; using the last one, " + account.name);
        return account.name;
    }

    /**
     * Compares two cursors
     */
    protected void compareCursors(Cursor cursor1, Cursor cursor2,
                                  Set<String> columnsToSkip, String tableName) {
        String[] cols = cursor1.getColumnNames();
        int length = cols.length;

        assertEquals(tableName + " count failed to match", cursor1.getCount(),
                cursor2.getCount());
        Map<String, String> row = Maps.newHashMap();
        while (cursor1.moveToNext() && cursor2.moveToNext()) {
            for (int i = 0; i < length; i++) {
                String col = cols[i];
                if (columnsToSkip != null && columnsToSkip.contains(col)) {
                    continue;
                }
                row.put(col, cursor1.getString(i));

                assertEquals("Row: " + row + " Table: " + tableName + ": " + cols[i] +
                        " failed to match", cursor1.getString(i),
                        cursor2.getString(i));
            }
        }
    }
}