1/*
2 * Copyright (C) 2009 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 com.android.providers.calendar;
18
19import android.database.Cursor;
20import android.net.Uri;
21import android.text.format.Time;
22
23/**
24 * Calendar Sync instrumentation tests. Testing creation of new events, deleting events,
25 * editing events.
26 */
27public class SyncCalendarTest extends CalendarSyncTestingBase {
28    private EventInfo normalEvent =
29            new EventInfo("normal0", "2008-12-01T00:00:00", "2008-12-02T00:00:00", false);
30    private EventInfo dailyRecurringEvent = new EventInfo("dailyEvent",
31            "daily from 5/1/2008 12am to 1am", "2008-10-01T00:00:00", "2008-10-01T01:00:00",
32            "FREQ=DAILY;WKST=SU", false);
33
34    private static final long ONE_HOUR_IN_MILLIS = 3600000;
35
36    @Override
37    protected void setUp() throws Exception {
38        super.setUp();
39    }
40
41    public void testCreateNewEvent() throws Exception {
42        int countBeforeNewEvent = getEventsCount();
43        insertEvent(normalEvent);
44        assertTrue("No new event was added. ", getEventsCount() > countBeforeNewEvent);
45    }
46
47    public void testCreateAndDeleteNewRecurringEvent() throws Exception {
48        syncCalendar();
49        int countBeforeNewEvent = getEventsCount();
50        Uri insertUri = insertEvent(dailyRecurringEvent);
51
52        assertTrue("A daily recurring event should have been created.",
53                   getEventsCount() > countBeforeNewEvent);
54        deleteEvent(insertUri);
55        assertEquals("Daily recurring event should have been deleted.",
56                     countBeforeNewEvent, getEventsCount());
57    }
58
59    public void testCreateAllDayEvent() throws Exception {
60        Time time = new Time();
61        time.setToNow();
62        long dtStart = time.toMillis(false);
63        long dtEnd = time.toMillis(false) + ONE_HOUR_IN_MILLIS;
64        EventInfo allDayEvent = new EventInfo("allday0", dtStart, dtEnd, true);
65
66        int countBeforeNewEvent = getEventsCount();
67        insertEvent(allDayEvent);
68
69        assertTrue("An all-day event should have been created.",
70                   getEventsCount() > countBeforeNewEvent);
71    }
72
73    public void testEditEventTitle() throws Exception {
74        Cursor cursor;
75        cursor = mResolver.query(mEventsUri, null, null, null, null);
76
77        int countBeforeNewEvent = cursor.getCount();
78        cursor.moveToNext();
79        Time time = new Time();
80        time.setToNow();
81        String newTitle = cursor.getString(cursor.getColumnIndex("title")) + time.toString();
82        long dtStart = cursor.getLong(cursor.getColumnIndex("dtstart"));
83        long dtEnd = cursor.getLong(cursor.getColumnIndex("dtend"));
84
85        EventInfo event = new EventInfo(newTitle, dtStart, dtEnd, false);
86        long eventId = cursor.getLong(cursor.getColumnIndex("_id"));
87
88        editEvent(eventId, event);
89        cursor = mResolver.query(mEventsUri, null, null, null, null);
90        assertTrue("Events count should remain same.", getEventsCount() == countBeforeNewEvent);
91
92        while (cursor.moveToNext()) {
93            if (cursor.getLong(cursor.getColumnIndex("_id")) == eventId) {
94                assertEquals(cursor.getString(cursor.getColumnIndex("title")), newTitle);
95                break;
96            }
97        }
98        cursor.close();
99    }
100
101    public void testEditEventDate() throws Exception {
102        Cursor cursor;
103        cursor = mResolver.query(mEventsUri, null, null, null, null);
104
105        int countBeforeNewEvent = cursor.getCount();
106        cursor.moveToNext();
107        Time time = new Time();
108        String title = cursor.getString(cursor.getColumnIndex("title"));
109        long dtStart = cursor.getLong(cursor.getColumnIndex("dtstart"));
110        time.set(dtStart + 2 * ONE_HOUR_IN_MILLIS);
111        long newDtStart = time.toMillis(false);
112        time.set(dtStart + 3 * ONE_HOUR_IN_MILLIS);
113        long newDtEnd = time.toMillis(false);
114
115        EventInfo event = new EventInfo(title, newDtStart, newDtEnd, false);
116        long eventId = cursor.getLong(cursor.getColumnIndex("_id"));
117
118        editEvent(eventId,  event);
119
120        cursor = mResolver.query(mEventsUri, null, null, null, null);
121        int countAfterNewEvent = cursor.getCount();
122        assertTrue("Events count should remain same.", countAfterNewEvent == countBeforeNewEvent);
123
124        while (cursor.moveToNext()){
125          if (cursor.getLong(cursor.getColumnIndex("_id")) == eventId) {
126            assertEquals(cursor.getLong(cursor.getColumnIndex("dtstart")), newDtStart);
127            assertEquals(cursor.getLong(cursor.getColumnIndex("dtend")), newDtEnd);
128            break;
129          }
130        }
131        cursor.close();
132    }
133
134    public void testEditEventDescription() throws Exception {
135        Cursor cursor;
136        cursor = mResolver.query(mEventsUri, null, null, null, null);
137        int countBeforeNewEvent = cursor.getCount();
138        cursor.moveToNext();
139
140        String title = cursor.getString(cursor.getColumnIndex("title"));
141        long dtStart = cursor.getLong(cursor.getColumnIndex("dtstart"));
142        long dtEnd = cursor.getLong(cursor.getColumnIndex("dtend"));
143
144        String newDescription = "NEW Descrption";
145        EventInfo event = new EventInfo(title, dtStart, dtEnd, false, newDescription);
146
147        long eventId = cursor.getLong(cursor.getColumnIndex("_id"));
148        editEvent(eventId,  event);
149
150        cursor = mResolver.query(mEventsUri, null, null, null, null);
151        int countAfterNewEvent = cursor.getCount();
152        assertTrue("Events count should remain same.", countAfterNewEvent == countBeforeNewEvent);
153
154        while (cursor.moveToNext()){
155          if (cursor.getLong(cursor.getColumnIndex("_id")) == eventId) {
156            assertEquals(cursor.getString(cursor.getColumnIndex("description")), newDescription);
157            break;
158          }
159        }
160        cursor.close();
161    }
162}
163
164