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 import com.android.calendar.Utils; 23 24 import android.content.Context; 25 import android.database.MatrixCursor; 26 import android.test.AndroidTestCase; 27 import android.test.suitebuilder.annotation.SmallTest; 28 import android.test.suitebuilder.annotation.Suppress; 29 import android.text.format.DateUtils; 30 import android.text.format.Time; 31 import android.util.Log; 32 import android.view.View; 33 34 import java.util.TimeZone; 35 36 // adb shell am instrument -w -e class com.android.calendar.widget.CalendarAppWidgetServiceTest 37 // com.google.android.calendar.tests/android.test.InstrumentationTestRunner 38 39 40 public class CalendarAppWidgetServiceTest extends AndroidTestCase { 41 private static final String TAG = "CalendarAppWidgetService"; 42 43 private static final String DEFAULT_TIMEZONE = "America/Los_Angeles"; 44 long now; 45 final long ONE_MINUTE = 60000; 46 final long ONE_HOUR = 60 * ONE_MINUTE; 47 final long HALF_HOUR = ONE_HOUR / 2; 48 final long TWO_HOURS = ONE_HOUR * 2; 49 50 final String title = "Title"; 51 final String location = "Location"; 52 53 54 55 // TODO Disabled test since this CalendarAppWidgetModel is not used for the no event case 56 // 57 // @SmallTest 58 // public void testGetAppWidgetModel_noEvents() throws Exception { 59 // // Input 60 // MatrixCursor cursor = new MatrixCursor(CalendarAppWidgetService.EVENT_PROJECTION, 0); 61 // 62 // // Expected Output 63 // CalendarAppWidgetModel expected = new CalendarAppWidgetModel(); 64 // expected.visibNoEvents = View.VISIBLE; 65 // 66 // // Test 67 // long now = 1270000000000L; 68 // MarkedEvents events = CalendarAppWidgetService.buildMarkedEvents(cursor, null, now); 69 // CalendarAppWidgetModel actual = CalendarAppWidgetService.getAppWidgetModel( 70 // getTestContext(), cursor, events, now); 71 // 72 // assertEquals(expected.toString(), actual.toString()); 73 // } 74 75 @Override setUp()76 protected void setUp() throws Exception { 77 super.setUp(); 78 // we want to run these tests in a predictable timezone 79 TimeZone.setDefault(TimeZone.getTimeZone(DEFAULT_TIMEZONE)); 80 81 // Set the "current time" to 2am tomorrow. 82 Time time = new Time(); 83 time.setToNow(); 84 time.monthDay += 1; 85 time.hour = 2; 86 time.minute = 0; 87 time.second = 0; 88 now = time.normalize(false); 89 } 90 91 @Override tearDown()92 protected void tearDown() throws Exception { 93 super.tearDown(); 94 // this restores the previous default timezone 95 TimeZone.setDefault(null); 96 } 97 98 @SmallTest testGetAppWidgetModel_1Event()99 public void testGetAppWidgetModel_1Event() throws Exception { 100 CalendarAppWidgetModel expected = new CalendarAppWidgetModel(getContext(), Time 101 .getCurrentTimezone()); 102 MatrixCursor cursor = new MatrixCursor(CalendarAppWidgetService.EVENT_PROJECTION, 0); 103 104 105 // Input 106 // allDay, begin, end, title, location, eventId 107 cursor.addRow(getRow(0, now + ONE_HOUR, now + TWO_HOURS, title, location, 0)); 108 109 // Expected Output 110 EventInfo eventInfo = new EventInfo(); 111 eventInfo.visibWhen = View.VISIBLE; 112 eventInfo.visibWhere = View.VISIBLE; 113 eventInfo.visibTitle = View.VISIBLE; 114 eventInfo.when = Utils.formatDateRange(getContext(), now + ONE_HOUR, now + TWO_HOURS, 115 DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_ABBREV_ALL); 116 eventInfo.where = location; 117 eventInfo.title = title; 118 expected.mEventInfos.add(eventInfo); 119 120 // Test 121 CalendarAppWidgetModel actual = CalendarFactory.buildAppWidgetModel( 122 getContext(), cursor, Time.getCurrentTimezone()); 123 124 assertEquals(expected.toString(), actual.toString()); 125 } 126 127 @SmallTest testGetAppWidgetModel_AllDayEventLater()128 public void testGetAppWidgetModel_AllDayEventLater() throws Exception { 129 Context context = getContext(); 130 CalendarAppWidgetModel expected = new CalendarAppWidgetModel(getContext(), Time 131 .getCurrentTimezone()); 132 MatrixCursor cursor = new MatrixCursor(CalendarAppWidgetService.EVENT_PROJECTION, 0); 133 134 int i = 0; 135 136 // Expected Output 137 EventInfo eventInfo = new EventInfo(); 138 eventInfo.visibWhen = View.VISIBLE; 139 eventInfo.visibWhere = View.VISIBLE; 140 eventInfo.visibTitle = View.VISIBLE; 141 eventInfo.when = Utils.formatDateRange(context, now + ONE_HOUR, now + TWO_HOURS, 142 DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_ABBREV_ALL); 143 eventInfo.where = location + i; 144 eventInfo.title = title + i; 145 expected.mEventInfos.add(eventInfo); 146 cursor.addRow(getRow(0, now + ONE_HOUR, now + TWO_HOURS, title + i, location + i, 0)); 147 148 i++; 149 // Set the start time to 5 days from now at midnight UTC. 150 Time time = new Time(); 151 time.set(now); 152 time.monthDay += 5; 153 time.hour = 0; 154 time.timezone = Time.TIMEZONE_UTC; 155 long start = time.normalize(false); 156 time.monthDay += 1; 157 long end = time.normalize(false); 158 159 eventInfo = new EventInfo(); 160 eventInfo.visibWhen = View.VISIBLE; 161 eventInfo.visibWhere = View.VISIBLE; 162 eventInfo.visibTitle = View.VISIBLE; 163 eventInfo.when = DateUtils.formatDateTime(context, end, 164 DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_ABBREV_ALL); 165 eventInfo.where = location + i; 166 eventInfo.title = title + i; 167 cursor.addRow(getRow(1, start, end, title + i, location + i, 0)); 168 169 // Test 170 CalendarAppWidgetModel actual = CalendarAppWidgetService.CalendarFactory.buildAppWidgetModel( 171 context, cursor, Time.getCurrentTimezone()); 172 173 Log.e("Test", " expected: " + expected.toString() 174 + " actual: " + actual.toString()); 175 assertEquals(expected.toString(), actual.toString()); 176 } 177 getRow(int allDay, long begin, long end, String title, String location, long eventId)178 private Object[] getRow(int allDay, long begin, long end, String title, String location, 179 long eventId) { 180 Object[] row = new Object[CalendarAppWidgetService.EVENT_PROJECTION.length]; 181 row[CalendarAppWidgetService.INDEX_ALL_DAY] = new Integer(allDay); 182 row[CalendarAppWidgetService.INDEX_BEGIN] = new Long(begin); 183 row[CalendarAppWidgetService.INDEX_END] = new Long(end); 184 row[CalendarAppWidgetService.INDEX_TITLE] = new String(title); 185 row[CalendarAppWidgetService.INDEX_EVENT_LOCATION] = new String(location); 186 row[CalendarAppWidgetService.INDEX_EVENT_ID] = new Long(eventId); 187 return row; 188 } 189 } 190