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