UtilsTests.java revision 059117da0141f5e3c6ce479dd87c6f8d8365e7e1
1/*
2 * Copyright (C) 2010 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.calendar;
18
19import com.android.calendar.CalendarUtils.TimeZoneUtils;
20
21import android.content.res.Configuration;
22import android.content.res.Resources.NotFoundException;
23import android.database.MatrixCursor;
24import android.provider.CalendarContract.CalendarCache;
25import android.test.mock.MockResources;
26import android.test.suitebuilder.annotation.SmallTest;
27import android.test.suitebuilder.annotation.Smoke;
28import android.text.format.Time;
29
30import java.util.HashMap;
31import java.util.Locale;
32
33import junit.framework.TestCase;
34
35/**
36 * Test class for verifying helper functions in Calendar's Utils
37 *
38 * You can run these tests with the following command:
39 * "adb shell am instrument -w -e class com.android.calendar.UtilsTests
40 *          com.android.calendar.tests/android.test.InstrumentationTestRunner"
41 */
42public class UtilsTests extends TestCase {
43    HashMap<String, Boolean> mIsDuplicateName;
44    HashMap<String, Boolean> mIsDuplicateNameExpected;
45    MatrixCursor mDuplicateNameCursor;
46    private DbTestUtils dbUtils;
47    private final TimeZoneUtils timezoneUtils = new TimeZoneUtils(Utils.SHARED_PREFS_NAME);
48
49    private static final int NAME_COLUMN = 0;
50    private static final String[] DUPLICATE_NAME_COLUMNS = new String[] { "name" };
51    private static final String[][] DUPLICATE_NAMES = new String[][] {
52        {"Pepper Pots"},
53        {"Green Goblin"},
54        {"Pepper Pots"},
55        {"Peter Parker"},
56        {"Silver Surfer"},
57        {"John Jameson"},
58        {"John Jameson"},
59        {"Pepper Pots"}
60    };
61    // First date is Thursday, Jan 1st, 1970.
62    private static final int[] JULIAN_DAYS = {2440588, 2440589, 2440590, 2440591, 2440592, 2440593,
63            2440594, 2440595, 2440596, 2440597, 2440598, 2440599, 2440600, 2440601
64    };
65    private static final int[] EXPECTED_WEEK_MONDAY_START = {
66            0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2 };
67    private static final int[] EXPECTED_WEEK_SUNDAY_START = {
68            0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2 };
69    private static final int[] EXPECTED_WEEK_SATURDAY_START = {
70            0, 0, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2 };
71    private static final int[] WEEKS_FOR_JULIAN_MONDAYS = {1, 2};
72    private static final int[] EXPECTED_JULIAN_MONDAYS = {2440592, 2440599};
73
74    private static final int NOW_MONTH = 3; // April
75    private static final int NOW_DAY = 10;
76    private static final int NOW_YEAR = 2012;
77    private static final long NOW_TIME = createTimeInMillis(5, 5, 5, NOW_DAY, NOW_MONTH, NOW_YEAR);
78    private static final String DEFAULT_TIMEZONE = Time.getCurrentTimezone();
79
80    /**
81     * Mock resources.  Add translation strings for test here.
82     */
83    private static class ResourcesForTest extends MockResources {
84        @Override
85        public String getString(int id) {
86            if (id == R.string.today) {
87                return "Today";
88            }
89            if (id == R.string.tomorrow) {
90                return "Tomorrow";
91            }
92            throw new IllegalArgumentException("unexpected resource ID: " + id);
93        }
94
95        @Override
96        public String getString(int id, Object... formatArgs) {
97            if (id == R.string.today_at_time_fmt) {
98                return String.format("Today at %s", formatArgs);
99            }
100            if (id == R.string.tomorrow_at_time_fmt) {
101                return String.format("Tomorrow at %s", formatArgs);
102            }
103            if (id == R.string.date_time_fmt) {
104                return String.format("%s, %s", formatArgs);
105            }
106            throw new IllegalArgumentException("unexpected resource ID: " + id);
107        }
108
109        @Override
110        public Configuration getConfiguration() {
111            Configuration config = new Configuration();
112            config.locale = Locale.getDefault();
113            return config;
114        }
115    }
116
117    private static long createTimeInMillis(int second, int minute, int hour, int monthDay,
118            int month, int year) {
119        return createTimeInMillis(second, minute, hour, monthDay, month, year,
120                Time.getCurrentTimezone());
121    }
122
123    private static long createTimeInMillis(int second, int minute, int hour, int monthDay,
124            int month, int year, String timezone) {
125        Time t = new Time(timezone);
126        t.set(second, minute, hour, monthDay, month, year);
127        t.normalize(false);
128        return t.toMillis(false);
129    }
130
131    private void setTimezone(String tz) {
132        timezoneUtils.setTimeZone(dbUtils.getContext(), tz);
133    }
134
135    @Override
136    public void setUp() {
137        mIsDuplicateName = new HashMap<String, Boolean> ();
138        mDuplicateNameCursor = new MatrixCursor(DUPLICATE_NAME_COLUMNS);
139        for (int i = 0; i < DUPLICATE_NAMES.length; i++) {
140            mDuplicateNameCursor.addRow(DUPLICATE_NAMES[i]);
141        }
142
143        mIsDuplicateNameExpected = new HashMap<String, Boolean> ();
144        mIsDuplicateNameExpected.put("Pepper Pots", true);
145        mIsDuplicateNameExpected.put("Green Goblin", false);
146        mIsDuplicateNameExpected.put("Peter Parker", false);
147        mIsDuplicateNameExpected.put("Silver Surfer", false);
148        mIsDuplicateNameExpected.put("John Jameson", true);
149
150        // Set up fake db.
151        dbUtils = new DbTestUtils(new ResourcesForTest());
152        dbUtils.getContentResolver().addProvider("settings", dbUtils.getContentProvider());
153        dbUtils.getContentResolver().addProvider(CalendarCache.URI.getAuthority(),
154                dbUtils.getContentProvider());
155        setTimezone(DEFAULT_TIMEZONE);
156    }
157
158    @Override
159    public void tearDown() {
160        mDuplicateNameCursor.close();
161
162        // Must reset the timezone here, because even though the fake provider will be
163        // recreated/cleared, TimeZoneUtils statically holds on to a cached value.
164        setTimezone(Time.getCurrentTimezone());
165    }
166
167    @Smoke
168    @SmallTest
169    public void testCheckForDuplicateNames() {
170        Utils.checkForDuplicateNames(mIsDuplicateName, mDuplicateNameCursor, NAME_COLUMN);
171        assertEquals(mIsDuplicateName, mIsDuplicateNameExpected);
172    }
173
174    @Smoke
175    @SmallTest
176    public void testGetWeeksSinceEpochFromJulianDay() {
177        for (int i = 0; i < JULIAN_DAYS.length; i++) {
178            assertEquals(EXPECTED_WEEK_MONDAY_START[i],
179                    Utils.getWeeksSinceEpochFromJulianDay(JULIAN_DAYS[i], Time.MONDAY));
180            assertEquals(EXPECTED_WEEK_SUNDAY_START[i],
181                    Utils.getWeeksSinceEpochFromJulianDay(JULIAN_DAYS[i], Time.SUNDAY));
182            assertEquals(EXPECTED_WEEK_SATURDAY_START[i],
183                    Utils.getWeeksSinceEpochFromJulianDay(JULIAN_DAYS[i], Time.SATURDAY));
184        }
185    }
186
187    @Smoke
188    @SmallTest
189    public void testGetJulianMondayFromWeeksSinceEpoch() {
190        for (int i = 0; i < WEEKS_FOR_JULIAN_MONDAYS.length; i++) {
191            assertEquals(EXPECTED_JULIAN_MONDAYS[i],
192                    Utils.getJulianMondayFromWeeksSinceEpoch(WEEKS_FOR_JULIAN_MONDAYS[i]));
193        }
194    }
195
196    @Smoke
197    @SmallTest
198    public void testEquals() {
199        assertTrue(Utils.equals(null, null));
200        assertFalse(Utils.equals("", null));
201        assertFalse(Utils.equals(null, ""));
202        assertTrue(Utils.equals("",""));
203
204        Integer int1 = new Integer(1);
205        Integer int2 = new Integer(1);
206        assertTrue(Utils.equals(int1, int2));
207    }
208
209
210    // Helper function to create test events for BusyBits testing
211    Event buildTestEvent(int startTime, int endTime, boolean allDay, int startDay, int endDay) {
212        Event e = new Event();
213        e.startTime = startTime;
214        e.endTime = endTime;
215        e.allDay = allDay;
216        e.startDay = startDay;
217        e.endDay = endDay;
218        e.startMillis = e.startDay * 1000L * 3600L * 24L + e.startTime * 60L * 1000L;
219        e.endMillis = e.endDay * 1000L * 3600L * 24L + e.endTime * 60L * 1000L;
220        return e;
221    }
222
223    @Smoke
224    @SmallTest
225    public void testCreateBusyBitSegments() {
226
227  /*      ArrayList<Event> events = new ArrayList<Event>();
228
229        // Test cases that should return null
230        // Empty events list
231        assertEquals(null, Utils.createBusyBitSegments(10, 30, 100, 200, 0, events));
232        // No events list
233        assertEquals(null, Utils.createBusyBitSegments(10, 30, 100, 200, 0, null));
234
235        events.add(buildTestEvent(100, 130, false, 1, 1));
236        events.add(buildTestEvent(1000, 1030, false, 1, 1));
237        // Illegal pixel positions
238        assertEquals(null, Utils.createBusyBitSegments(30, 10, 100, 200, 1, events));
239        // Illegal start and end times
240        assertEquals(null, Utils.createBusyBitSegments(10, 30, 200, 100, 1, events));
241        assertEquals(null, Utils.createBusyBitSegments(10, 30, -10, 100, 1, events));
242        assertEquals(null, Utils.createBusyBitSegments(10, 30, 24 * 60 + 100, 24 * 60 + 200, 1,
243                events));
244        assertEquals(null, Utils.createBusyBitSegments(10, 30, 200, 24 * 60 + 100, 1, events));
245        assertEquals(null, Utils.createBusyBitSegments(10, 30, 200, -100, 1, events));
246        // No Events in time frame
247        assertEquals(null, Utils.createBusyBitSegments(10, 30, 500, 900, 1, events));
248
249        // Test event that spans over the day
250        events.clear();
251        events.add(buildTestEvent(100, 300, false, 1, 5));
252        ArrayList<BusyBitsSegment> segments = new ArrayList<BusyBitsSegment>();
253        assertEquals(null, Utils.createBusyBitSegments(0, 250, 200, 1200, 3, events));
254
255        // test zero times events, events that are partially in the time span
256        // and all day events
257        events.clear();
258        events.add(buildTestEvent(100, 300, false, 1, 1));
259        events.add(buildTestEvent(1100, 1300, false, 1, 1));
260        events.add(buildTestEvent(500, 600, true, 1, 1));
261        events.add(buildTestEvent(700, 700, false, 1, 1));
262        segments.clear();
263        segments.add(new BusyBitsSegment(0, 10, false));
264        segments.add(new BusyBitsSegment(90, 100, false));
265        assertEquals(segments, Utils.createBusyBitSegments(0, 100, 200, 1200, 1, events));
266
267        // Test event that spans over 2 days but start and end time do not
268        // overlap fully with tested time span
269
270        events.clear();
271        events.add(buildTestEvent(23 * 60, 120, false, 1, 2));
272        segments.clear();
273        segments.add(new BusyBitsSegment(0, 120, false));
274        assertEquals(segments, Utils.createBusyBitSegments(0, 240, 60, 180, 2, events));
275
276        // Test overlapped events (two draw sizes)
277        events.clear();
278        events.add(buildTestEvent(10, 200, false, 1, 1));
279        events.add(buildTestEvent(150, 250, false, 1, 1));
280        events.add(buildTestEvent(150, 250, false, 1, 1));
281        events.add(buildTestEvent(200, 400, false, 1, 1));
282        events.add(buildTestEvent(500, 700, false, 1, 1));
283        events.add(buildTestEvent(550, 600, false, 1, 1));
284        events.add(buildTestEvent(550, 580, false, 1, 1));
285        events.add(buildTestEvent(560, 570, false, 1, 1));
286        events.add(buildTestEvent(600, 700, false, 1, 1));
287        events.add(buildTestEvent(620, 700, false, 1, 1));
288        events.add(buildTestEvent(650, 700, false, 1, 1));
289        events.add(buildTestEvent(800, 900, false, 1, 1));
290        events.add(buildTestEvent(800, 900, false, 1, 1));
291        events.add(buildTestEvent(800, 850, false, 1, 1));
292        events.add(buildTestEvent(1000, 1200, false, 1, 1));
293        events.add(buildTestEvent(1000, 1200, false, 1, 1));
294        segments.clear();
295        segments.add(new BusyBitsSegment(100, 149, false));
296        segments.add(new BusyBitsSegment(150, 250, true));
297        segments.add(new BusyBitsSegment(251, 400, false));
298        segments.add(new BusyBitsSegment(500, 549, false));
299        segments.add(new BusyBitsSegment(550, 700, true));
300        segments.add(new BusyBitsSegment(800, 900, true));
301        segments.add(new BusyBitsSegment(1000, 1100, true));
302        assertEquals(segments, Utils.createBusyBitSegments(100, 1100, 100, 1100, 1, events));
303        segments.clear();
304        segments.add(new BusyBitsSegment(100, 111, false));
305        segments.add(new BusyBitsSegment(112, 137, true));
306        segments.add(new BusyBitsSegment(138, 175, false));
307        segments.add(new BusyBitsSegment(200, 211, false));
308        segments.add(new BusyBitsSegment(212, 250, true));
309        segments.add(new BusyBitsSegment(275, 300, true));
310        segments.add(new BusyBitsSegment(325, 350, true));
311        assertEquals(segments, Utils.createBusyBitSegments(100, 350, 100, 1100, 1, events));
312*/
313    }
314
315    /**
316     * Tests the findNanpPhoneNumbers function.
317     */
318    @SmallTest
319    public void testFindNanpPhoneNumber() {
320        final String[] NO_NUMBERS = new String[] {};
321
322        findPhoneNumber("", NO_NUMBERS);
323        findPhoneNumber("               ", NO_NUMBERS);
324        findPhoneNumber("123", NO_NUMBERS);
325        findPhoneNumber("how much wood", NO_NUMBERS);
326        findPhoneNumber("abc1-650-555-1212", NO_NUMBERS);
327        findPhoneNumber("abc 5551212 def", new String[] { "5551212" });
328        findPhoneNumber("1234567", NO_NUMBERS);
329        findPhoneNumber(" 2345678 ", new String[] { "2345678" });
330        findPhoneNumber("1234567890", NO_NUMBERS);
331        findPhoneNumber("12345678901", new String[] { "12345678901" });
332        findPhoneNumber("123456789012", NO_NUMBERS);
333        findPhoneNumber("+1-555-1212", NO_NUMBERS);
334        findPhoneNumber("+1 (650) 555-1212", new String[] { "+1 (650) 555-1212" });
335        findPhoneNumber("(650) 555-1212, (650) 555-1213",
336                new String[] { "(650) 555-1212", "(650) 555-1213" });
337        findPhoneNumber("Call 555-1212, 555-1213 and also 555-1214.",
338                new String[] { "555-1212", "555-1213", "555-1214." });
339        findPhoneNumber("555-1212,555-1213,555-1214", new String[] { "555-1212" });
340        findPhoneNumber("123 (650) 555-1212", new String[] { "(650) 555-1212" });
341        findPhoneNumber("1-650-555-1212", new String[] { "1-650-555-1212" });
342        findPhoneNumber("1650-555-1212", new String[] { "1650-555-1212" });
343        findPhoneNumber("1650 555-1212", new String[] { "1650 555-1212" });
344        findPhoneNumber("1650/555-1212", NO_NUMBERS);
345        findPhoneNumber("1650-555 1212", NO_NUMBERS);
346        findPhoneNumber("8-650-555-1212", NO_NUMBERS);
347        findPhoneNumber("8 650-555-1212", new String[] { "650-555-1212" });
348        findPhoneNumber("650.555.1212", new String[] { "650.555.1212" });
349        findPhoneNumber(" *#650.555.1212#*!", new String[] { "*#650.555.1212#*" });
350        findPhoneNumber("555.1212", new String[] { "555.1212" });
351        findPhoneNumber("6505551212 x123, 555-1212", new String[] { "6505551212", "555-1212" });
352        findPhoneNumber("6505551212x123", new String[] { "6505551212" });
353        findPhoneNumber("http://example.com/6505551212/", NO_NUMBERS);
354        findPhoneNumber("Mountain View, CA 94043 (650) 555-1212", new String[]{ "(650) 555-1212" });
355        findPhoneNumber("New York, NY 10001-0001", NO_NUMBERS);
356    }
357
358    /**
359     * Finds the numbers in a block of text, and checks to see if the positions of the numbers
360     * match the expected values.
361     *
362     * @param text The text to search.
363     * @param matches Pairs of start/end positions.
364     */
365    private static void findPhoneNumber(String text, String[] matches) {
366        int[] results = EventInfoFragment.findNanpPhoneNumbers(text);
367
368        assertEquals(results.length % 2, 0);
369
370        if (results.length / 2 != matches.length) {
371            fail("Text '" + text + "': expected " + matches.length
372                    + " matches, found " + results.length / 2);
373        }
374
375        for (int i = 0; i < results.length / 2; i++) {
376            CharSequence seq = text.subSequence(results[i*2], results[i*2 + 1]);
377            assertEquals(matches[i], seq);
378        }
379    }
380
381    @SmallTest
382    public void testGetDisplayedDatetime_differentYear() {
383        // 4/12/2000 5pm - 4/12/2000 6pm
384        long start = createTimeInMillis(0, 0, 17, 12, 3, 2000);
385        long end = createTimeInMillis(0, 0, 18, 12, 3, 2000);
386        String result = Utils.getDisplayedDatetime(start, end, NOW_TIME, DEFAULT_TIMEZONE,
387                false, dbUtils.getContext());
388        assertEquals("Wednesday, April 12, 2000, 5:00pm \u2013 6:00pm", result);
389
390        // 12/31/2012 5pm - 1/1/2013 6pm
391        start = createTimeInMillis(0, 0, 17, 31, 11, 2012);
392        end = createTimeInMillis(0, 0, 18, 1, 0, 2013);
393        result = Utils.getDisplayedDatetime(start, end, NOW_TIME, DEFAULT_TIMEZONE,
394                false, dbUtils.getContext());
395        assertEquals("Mon, Dec 31, 2012, 5:00pm – Tue, Jan 1, 2013, 6:00pm", result);
396    }
397
398    @SmallTest
399    public void testGetDisplayedDatetime_sameYear() {
400        // 4/12/2012 5pm - 4/12/2012 6pm
401        long start = createTimeInMillis(0, 0, 17, 12, 3, 2012);
402        long end = createTimeInMillis(0, 0, 18, 12, 3, 2012);
403        String result = Utils.getDisplayedDatetime(start, end, NOW_TIME, DEFAULT_TIMEZONE,
404                false, dbUtils.getContext());
405        assertEquals("Thursday, April 12, 5:00pm \u2013 6:00pm", result);
406    }
407
408    @SmallTest
409    public void testGetDisplayedDatetime_today() {
410        // 4/10/2012 5pm - 4/10/2012 6pm
411        long start = createTimeInMillis(0, 0, 17, NOW_DAY, NOW_MONTH, NOW_YEAR);
412        long end = createTimeInMillis(0, 0, 18, NOW_DAY, NOW_MONTH, NOW_YEAR);
413        String result = Utils.getDisplayedDatetime(start, end, NOW_TIME, DEFAULT_TIMEZONE,
414                false, dbUtils.getContext());
415        assertEquals("Today at 5:00pm \u2013 6:00pm", result);
416    }
417
418    @SmallTest
419    public void testGetDisplayedDatetime_todayMidnight() {
420        // 4/10/2012 5pm - 4/11/2012 12am
421        long start = createTimeInMillis(0, 0, 17, NOW_DAY, NOW_MONTH, NOW_YEAR);
422        long end = createTimeInMillis(0, 0, 0, NOW_DAY + 1, NOW_MONTH, NOW_YEAR);
423        String result = Utils.getDisplayedDatetime(start, end, NOW_TIME, DEFAULT_TIMEZONE,
424                false, dbUtils.getContext());
425        assertEquals("Today at 5:00pm \u2013 midnight", result);
426    }
427
428    @SmallTest
429    public void testGetDisplayedDatetime_tomorrow() {
430        // 4/11/2012 12:01am - 4/11/2012 11:59pm
431        long start = createTimeInMillis(0, 1, 0, NOW_DAY + 1, NOW_MONTH, NOW_YEAR);
432        long end = createTimeInMillis(0, 59, 23, NOW_DAY + 1, NOW_MONTH, NOW_YEAR);
433        String result = Utils.getDisplayedDatetime(start, end, NOW_TIME, DEFAULT_TIMEZONE,
434                false, dbUtils.getContext());
435        assertEquals("Tomorrow at 12:01am \u2013 11:59pm", result);
436    }
437
438    @SmallTest
439    public void testGetDisplayedDatetime_yesterday() {
440        // 4/9/2012 5pm - 4/9/2012 6pm
441        long start = createTimeInMillis(0, 0, 17, 9, 3, 2012);
442        long end = createTimeInMillis(0, 0, 18, 9, 3, 2012);
443        String result = Utils.getDisplayedDatetime(start, end, NOW_TIME, DEFAULT_TIMEZONE,
444                false, dbUtils.getContext());
445        assertEquals("Monday, April 9, 5:00pm \u2013 6:00pm", result);
446    }
447
448    @SmallTest
449    public void testGetDisplayedDatetime_multiDay() {
450        // 4/10/2012 12:01am - 4/11/2012 12:01am
451        long start = createTimeInMillis(0, 1, 0, NOW_DAY, NOW_MONTH, NOW_YEAR);
452        long end = createTimeInMillis(0, 1, 0, NOW_DAY + 1, NOW_MONTH, NOW_YEAR);
453        String result = Utils.getDisplayedDatetime(start, end, NOW_TIME, DEFAULT_TIMEZONE,
454                false, dbUtils.getContext());
455        assertEquals("Tue, Apr 10, 12:01am \u2013 Wed, Apr 11, 12:01am", result);
456    }
457
458    @SmallTest
459    public void testGetDisplayedDatetime_allDay() {
460        // 4/2/2012 12:00am - 4/3/2012 12:00am
461        long start = createTimeInMillis(0, 0, 0, 2, 3, NOW_YEAR, Time.TIMEZONE_UTC);
462        long end = createTimeInMillis(0, 0, 0, 3, 3, NOW_YEAR, Time.TIMEZONE_UTC);
463        String result = Utils.getDisplayedDatetime(start, end, NOW_TIME, DEFAULT_TIMEZONE,
464                true, dbUtils.getContext());
465        assertEquals("Monday, April 2", result);
466    }
467
468    @SmallTest
469    public void testGetDisplayedDatetime_allDayToday() {
470        // 4/10/2012 12:00am - 4/11/2012 12:00am
471        long start = createTimeInMillis(0, 0, 0, NOW_DAY, NOW_MONTH, NOW_YEAR, Time.TIMEZONE_UTC);
472        long end = createTimeInMillis(0, 0, 0, NOW_DAY + 1, NOW_MONTH, NOW_YEAR, Time.TIMEZONE_UTC);
473        String result = Utils.getDisplayedDatetime(start, end, NOW_TIME, DEFAULT_TIMEZONE,
474                true, dbUtils.getContext());
475        assertEquals("Today", result);
476    }
477
478    @SmallTest
479    public void testGetDisplayedDatetime_allDayMultiday() {
480        // 4/10/2012 12:00am - 4/13/2012 12:00am
481        long start = createTimeInMillis(0, 0, 0, NOW_DAY, NOW_MONTH, NOW_YEAR, Time.TIMEZONE_UTC);
482        long end = createTimeInMillis(0, 0, 0, NOW_DAY + 3, NOW_MONTH, NOW_YEAR, Time.TIMEZONE_UTC);
483        String result = Utils.getDisplayedDatetime(start, end, NOW_TIME, DEFAULT_TIMEZONE,
484                true, dbUtils.getContext());
485        assertEquals("Tuesday, April 10 \u2013 Thursday, April 12", result);
486    }
487
488    @SmallTest
489    public void testGetDisplayedDatetime_differentTimezone() {
490        String localTz = "America/New_York";
491        String eventTz = "America/Los_Angeles";
492        setTimezone(localTz);
493
494        // 4/12/2012 5pm - 4/12/2012 6pm (Pacific)
495        long start = createTimeInMillis(0, 0, 17, 12, 3, 2012, eventTz);
496        long end = createTimeInMillis(0, 0, 18, 12, 3, 2012, eventTz);
497        String result = Utils.getDisplayedDatetime(start, end, NOW_TIME, localTz, false,
498                dbUtils.getContext());
499        assertEquals("Thursday, April 12, 8:00pm \u2013 9:00pm", result);
500    }
501
502    @SmallTest
503    public void testGetDisplayedDatetime_allDayDiffTimezone() {
504        String localTz = "America/New_York";
505        setTimezone(localTz);
506
507        // 4/2/2012 12:00am - 4/3/2012 12:00am
508        long start = createTimeInMillis(0, 0, 0, 2, 3, NOW_YEAR, Time.TIMEZONE_UTC);
509        long end = createTimeInMillis(0, 0, 0, 3, 3, NOW_YEAR, Time.TIMEZONE_UTC);
510        String result = Utils.getDisplayedDatetime(start, end, NOW_TIME, localTz, true,
511                dbUtils.getContext());
512        assertEquals("Monday, April 2", result);
513    }
514
515    @SmallTest
516    public void testGetDisplayedDatetime_allDayTomorrowDiffTimezone() {
517        String localTz = "America/New_York";
518        setTimezone(localTz);
519
520        // 4/2/2012 12:00am - 4/3/2012 12:00am
521        long start = createTimeInMillis(0, 0, 0, NOW_DAY + 1, NOW_MONTH, NOW_YEAR,
522                Time.TIMEZONE_UTC);
523        long end = createTimeInMillis(0, 0, 0, NOW_DAY + 2, NOW_MONTH, NOW_YEAR,
524                Time.TIMEZONE_UTC);
525        String result = Utils.getDisplayedDatetime(start, end, NOW_TIME, localTz, true,
526                dbUtils.getContext());
527        assertEquals("Tomorrow", result);
528    }
529
530    // TODO: add tests for army time.
531
532    @SmallTest
533    public void testGetDisplayedTimezone_sameTimezone() {
534        String localTz = "America/New_York";
535        setTimezone(localTz);
536
537        // 4/12/2012 5pm
538        long start = createTimeInMillis(0, 0, 17, 12, 3, 2012, localTz);
539        assertNull(Utils.getDisplayedTimezone(start, localTz, localTz));
540    }
541
542    @SmallTest
543    public void testGetDisplayedTimezone_differentTimezone() {
544        String localTz = "America/New_York";
545        String eventTz = "America/Los_Angeles";
546        setTimezone(localTz);
547
548        // 1/12/2012 5pm (not daylight savings)
549        long start = createTimeInMillis(0, 0, 17, 12, 0, 2012, eventTz);
550        assertEquals("EST", Utils.getDisplayedTimezone(start, localTz, eventTz));
551    }
552
553    @SmallTest
554    public void testGetDisplayedTimezone_differentTimezoneDst() {
555        String localTz = "America/New_York";
556        String eventTz = "America/Los_Angeles";
557        setTimezone(localTz);
558
559        // 4/12/2012 5pm (daylight savings)
560        long start = createTimeInMillis(0, 0, 17, 12, 3, 2012, eventTz);
561        assertEquals("EDT", Utils.getDisplayedTimezone(start, localTz, eventTz));
562    }
563}
564