• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2 **
3 ** Copyright 2010, The Android Open Source Project
4 **
5 ** Licensed under the Apache License, Version 2.0 (the "License");
6 ** you may not use this file except in compliance with the License.
7 ** You may obtain a copy of the License at
8 **
9 **     http://www.apache.org/licenses/LICENSE-2.0
10 **
11 ** Unless required by applicable law or agreed to in writing, software
12 ** distributed under the License is distributed on an "AS IS" BASIS,
13 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 ** See the License for the specific language governing permissions and
15 ** limitations under the License.
16 */
17 
18 package com.android.calendar.widget;
19 
20 import com.android.calendar.widget.CalendarAppWidgetModel.EventInfo;
21 import com.android.calendar.widget.CalendarAppWidgetService.CalendarFactory;
22 
23 import android.database.MatrixCursor;
24 import android.test.AndroidTestCase;
25 import android.test.suitebuilder.annotation.SmallTest;
26 import android.test.suitebuilder.annotation.Suppress;
27 import android.text.format.DateUtils;
28 import android.text.format.Time;
29 import android.view.View;
30 
31 import java.util.TimeZone;
32 
33 // adb shell am instrument -w -e class com.android.calendar.widget.CalendarAppWidgetServiceTest
34 //   com.google.android.calendar.tests/android.test.InstrumentationTestRunner
35 
36 
37 public class CalendarAppWidgetServiceTest extends AndroidTestCase {
38     private static final String TAG = "CalendarAppWidgetService";
39 
40     private static final String DEFAULT_TIMEZONE = "America/Los_Angeles";
41 
42     final long now = 1262340000000L; // Fri Jan 01 2010 02:00:00 GMT-0800 (PST)
43     final long ONE_MINUTE = 60000;
44     final long ONE_HOUR = 60 * ONE_MINUTE;
45     final long HALF_HOUR = ONE_HOUR / 2;
46     final long TWO_HOURS = ONE_HOUR * 2;
47 
48     final String title = "Title";
49     final String location = "Location";
50 
51 
52 
53 //    TODO Disabled test since this CalendarAppWidgetModel is not used for the no event case
54 //
55 //    @SmallTest
56 //    public void testGetAppWidgetModel_noEvents() throws Exception {
57 //        // Input
58 //        MatrixCursor cursor = new MatrixCursor(CalendarAppWidgetService.EVENT_PROJECTION, 0);
59 //
60 //        // Expected Output
61 //        CalendarAppWidgetModel expected = new CalendarAppWidgetModel();
62 //        expected.visibNoEvents = View.VISIBLE;
63 //
64 //        // Test
65 //        long now = 1270000000000L;
66 //        MarkedEvents events = CalendarAppWidgetService.buildMarkedEvents(cursor, null, now);
67 //        CalendarAppWidgetModel actual = CalendarAppWidgetService.getAppWidgetModel(
68 //                getTestContext(), cursor, events, now);
69 //
70 //        assertEquals(expected.toString(), actual.toString());
71 //    }
72 
73     @Override
setUp()74     protected void setUp() throws Exception {
75         super.setUp();
76         // we want to run these tests in a predictable timezone
77         TimeZone.setDefault(TimeZone.getTimeZone(DEFAULT_TIMEZONE));
78     }
79 
80     @Override
tearDown()81     protected void tearDown() throws Exception {
82         super.tearDown();
83         // this restores the previous default timezone
84         TimeZone.setDefault(null);
85     }
86 
87     @SmallTest
testGetAppWidgetModel_1Event()88     public void testGetAppWidgetModel_1Event() throws Exception {
89         CalendarAppWidgetModel expected = new CalendarAppWidgetModel(getContext(), Time
90                 .getCurrentTimezone());
91         MatrixCursor cursor = new MatrixCursor(CalendarAppWidgetService.EVENT_PROJECTION, 0);
92 
93 
94         // Input
95         // allDay, begin, end, title, location, eventId
96         cursor.addRow(getRow(0, now + ONE_HOUR, now + TWO_HOURS, title, location, 0));
97 
98         // Expected Output
99         EventInfo eventInfo = new EventInfo();
100         eventInfo.visibWhen = View.VISIBLE;
101         eventInfo.visibWhere = View.VISIBLE;
102         eventInfo.visibTitle = View.VISIBLE;
103         eventInfo.when = "3am";
104         eventInfo.where = location;
105         eventInfo.title = title;
106         expected.mEventInfos.add(eventInfo);
107 
108         // Test
109         CalendarAppWidgetModel actual = CalendarFactory.buildAppWidgetModel(
110                 getContext(), cursor, Time.getCurrentTimezone());
111 
112         assertEquals(expected.toString(), actual.toString());
113     }
114 
115  // TODO re-enable this test when our widget behavior is finalized
116     @Suppress @SmallTest
testGetAppWidgetModel_2StaggeredEvents()117     public void testGetAppWidgetModel_2StaggeredEvents() throws Exception {
118         CalendarAppWidgetModel expected = new CalendarAppWidgetModel(getContext(), Time
119                 .getCurrentTimezone());
120         MatrixCursor cursor = new MatrixCursor(CalendarAppWidgetService.EVENT_PROJECTION, 0);
121 
122         int i = 0;
123         long tomorrow = now + DateUtils.DAY_IN_MILLIS;
124         long sunday = tomorrow + DateUtils.DAY_IN_MILLIS;
125 
126         // Expected Output
127         EventInfo eventInfo = new EventInfo();
128         eventInfo.visibWhen = View.VISIBLE;
129         eventInfo.visibWhere = View.VISIBLE;
130         eventInfo.visibTitle = View.VISIBLE;
131         eventInfo.when = "2am, Tomorrow";
132         eventInfo.where = location + i;
133         eventInfo.title = title + i;
134         expected.mEventInfos.add(eventInfo);
135 
136         ++i;
137         eventInfo = new EventInfo();
138         eventInfo.visibWhen = View.VISIBLE;
139         eventInfo.visibWhere = View.VISIBLE;
140         eventInfo.visibTitle = View.VISIBLE;
141         eventInfo.when = "2am, Sun";
142         eventInfo.where = location + i;
143         eventInfo.title = title + i;
144         expected.mEventInfos.add(eventInfo);
145 
146         // Input
147         // allDay, begin, end, title, location, eventId
148         i = 0;
149         cursor.addRow(getRow(0, tomorrow, tomorrow + TWO_HOURS, title + i, location + i, 0));
150         ++i;
151         cursor.addRow(getRow(0, sunday, sunday + TWO_HOURS, title + i, location + i, 0));
152         ++i;
153 
154         // Test
155         CalendarAppWidgetModel actual = CalendarFactory.buildAppWidgetModel(
156                 getContext(), cursor, Time.getCurrentTimezone());
157 
158         assertEquals(expected.toString(), actual.toString());
159     }
160 
161     @SmallTest
testGetAppWidgetModel_AllDayEventToday()162     public void testGetAppWidgetModel_AllDayEventToday() throws Exception {
163         final long now = 1262340000000L; // Fri Jan 01 2010 01:00:00 GMT-0700 (PDT)
164         CalendarAppWidgetModel expected = new CalendarAppWidgetModel(getContext(), Time
165                 .getCurrentTimezone());
166         MatrixCursor cursor = new MatrixCursor(CalendarAppWidgetService.EVENT_PROJECTION, 0);
167 
168         int i = 0;
169 
170         // Expected Output
171         EventInfo eventInfo = new EventInfo();
172         eventInfo.visibWhen = View.VISIBLE;
173         eventInfo.visibWhere = View.VISIBLE;
174         eventInfo.visibTitle = View.VISIBLE;
175         eventInfo.when = "Today";
176         eventInfo.where = location + i;
177         eventInfo.title = title + i;
178         expected.mEventInfos.add(eventInfo);
179 
180         i++;
181         eventInfo = new EventInfo();
182         eventInfo.visibWhen = View.VISIBLE;
183         eventInfo.visibWhere = View.VISIBLE;
184         eventInfo.visibTitle = View.VISIBLE;
185         eventInfo.when = "3am";
186         eventInfo.where = location + i;
187         eventInfo.title = title + i;
188         expected.mEventInfos.add(eventInfo);
189 
190         i = 0;
191         cursor.addRow(getRow(1, 1262304000000L, 1262390400000L, title + i, location + i, 0));
192         ++i;
193         cursor.addRow(getRow(0, now + ONE_HOUR, now + TWO_HOURS, title + i, location + i, 0));
194 
195         // Test
196         CalendarAppWidgetModel actual = CalendarFactory.buildAppWidgetModel(
197                 getContext(), cursor, Time.getCurrentTimezone());
198 
199         assertEquals(expected.toString(), actual.toString());
200     }
201 
202     @SmallTest
testGetAppWidgetModel_AllDayEventTomorrow()203     public void testGetAppWidgetModel_AllDayEventTomorrow() throws Exception {
204         final long now = 1262340000000L; // Fri Jan 01 2010 01:00:00 GMT-0700 (PDT)
205         CalendarAppWidgetModel expected = new CalendarAppWidgetModel(getContext(), Time
206                 .getCurrentTimezone());
207         MatrixCursor cursor = new MatrixCursor(CalendarAppWidgetService.EVENT_PROJECTION, 0);
208 
209         int i = 0;
210 
211         // Expected Output
212         EventInfo eventInfo = new EventInfo();
213         eventInfo.visibWhen = View.VISIBLE;
214         eventInfo.visibWhere = View.VISIBLE;
215         eventInfo.visibTitle = View.VISIBLE;
216         eventInfo.when = "3am";
217         eventInfo.where = location + i;
218         eventInfo.title = title + i;
219         expected.mEventInfos.add(eventInfo);
220 
221         i++;
222         eventInfo = new EventInfo();
223         eventInfo.visibWhen = View.VISIBLE;
224         eventInfo.visibWhere = View.VISIBLE;
225         eventInfo.visibTitle = View.VISIBLE;
226         eventInfo.when = "Tomorrow";
227         eventInfo.where = location + i;
228         eventInfo.title = title + i;
229         expected.mEventInfos.add(eventInfo);
230 
231         i = 0;
232         cursor.addRow(getRow(0, now + ONE_HOUR, now + TWO_HOURS, title + i, location + i, 0));
233         ++i;
234         cursor.addRow(getRow(1, 1262390400000L, 1262476800000L, title + i, location + i, 0));
235 
236         // Test
237         CalendarAppWidgetModel actual = CalendarFactory.buildAppWidgetModel(
238                 getContext(), cursor, Time.getCurrentTimezone());
239 
240         assertEquals(expected.toString(), actual.toString());
241     }
242 
243     @SmallTest
testGetAppWidgetModel_AllDayEventLater()244     public void testGetAppWidgetModel_AllDayEventLater() throws Exception {
245         final long now = 1262340000000L; // Fri Jan 01 2010 01:00:00 GMT-0700 (PDT)
246         CalendarAppWidgetModel expected = new CalendarAppWidgetModel(getContext(), Time
247                 .getCurrentTimezone());
248         MatrixCursor cursor = new MatrixCursor(CalendarAppWidgetService.EVENT_PROJECTION, 0);
249 
250         int i = 0;
251 
252         // Expected Output
253         EventInfo eventInfo = new EventInfo();
254         eventInfo.visibWhen = View.VISIBLE;
255         eventInfo.visibWhere = View.VISIBLE;
256         eventInfo.visibTitle = View.VISIBLE;
257         eventInfo.when = "3am";
258         eventInfo.where = location + i;
259         eventInfo.title = title + i;
260         expected.mEventInfos.add(eventInfo);
261 
262         i++;
263         eventInfo = new EventInfo();
264         eventInfo.visibWhen = View.VISIBLE;
265         eventInfo.visibWhere = View.VISIBLE;
266         eventInfo.visibTitle = View.VISIBLE;
267         eventInfo.when = "Sun";
268         eventInfo.where = location + i;
269         eventInfo.title = title + i;
270         expected.mEventInfos.add(eventInfo);
271 
272         i = 0;
273         cursor.addRow(getRow(0, now + ONE_HOUR, now + TWO_HOURS, title + i, location + i, 0));
274         ++i;
275         cursor.addRow(getRow(1, 1262476800000L, 1262563200000L, title + i, location + i, 0));
276 
277         // Test
278         CalendarAppWidgetModel actual = CalendarAppWidgetService.CalendarFactory.buildAppWidgetModel(
279                 getContext(), cursor, Time.getCurrentTimezone());
280 
281         assertEquals(expected.toString(), actual.toString());
282     }
283 
getRow(int allDay, long begin, long end, String title, String location, long eventId)284     private Object[] getRow(int allDay, long begin, long end, String title, String location,
285             long eventId) {
286         Object[] row = new Object[CalendarAppWidgetService.EVENT_PROJECTION.length];
287         row[CalendarAppWidgetService.INDEX_ALL_DAY] = new Integer(allDay);
288         row[CalendarAppWidgetService.INDEX_BEGIN] = new Long(begin);
289         row[CalendarAppWidgetService.INDEX_END] = new Long(end);
290         row[CalendarAppWidgetService.INDEX_TITLE] = new String(title);
291         row[CalendarAppWidgetService.INDEX_EVENT_LOCATION] = new String(location);
292         row[CalendarAppWidgetService.INDEX_EVENT_ID] = new Long(eventId);
293         return row;
294     }
295 }
296