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 */
16package com.android.providers.calendar;
17
18import android.database.sqlite.SQLiteOpenHelper;
19import android.test.AndroidTestCase;
20
21/**
22 * Run various tests on CalendarCache
23 *
24 * Use the following command line:
25 *
26 * adb shell am instrument -e debug false -w -e class com.android.providers.calendar.CalendarCacheTest com.android.providers.calendar.tests/android.test.InstrumentationTestRunner
27 *
28 */
29public class CalendarCacheTest extends AndroidTestCase {
30
31    private SQLiteOpenHelper mDbHelper;
32    private CalendarCache mCalendarCache;
33    private static final String TIMEZONE_DB_2011A = "2011a";
34
35    private static final String TIMEZONE_AMERICA_LOS_ANGELES = "America/Los_Angeles";
36    private static final String TIMEZONE_AMERICA_DENVER = "America/Denver";
37
38    @Override
39    public void setUp() {
40        mDbHelper = CalendarDatabaseHelper.getInstance(getContext());
41        mCalendarCache = new CalendarCache(mDbHelper);
42    }
43
44    @Override
45    public void tearDown() {
46        if (mDbHelper != null) {
47            mDbHelper.close();
48            mDbHelper = null;
49            mCalendarCache = null;
50        }
51    }
52
53    public void testGenerateCacheException() {
54        boolean hasException = false;
55        try {
56            String value = mCalendarCache.readData(null);
57        } catch (CalendarCache.CacheException e) {
58            hasException = true;
59        }
60        assertTrue(hasException);
61    }
62
63    public void testWriteAndReadTimezoneDatabaseVersion() throws CalendarCache.CacheException {
64        mCalendarCache.writeTimezoneDatabaseVersion(TIMEZONE_DB_2011A);
65        assertEquals(TIMEZONE_DB_2011A, mCalendarCache.readTimezoneDatabaseVersion());
66    }
67
68    public void testWriteAndReadTimezone() throws CalendarCache.CacheException {
69        mCalendarCache.writeTimezoneInstances(TIMEZONE_AMERICA_DENVER);
70        assertEquals(TIMEZONE_AMERICA_DENVER, mCalendarCache.readTimezoneInstances());
71    }
72
73    public void testWriteAndReadTimezonePrevious() throws CalendarCache.CacheException {
74        mCalendarCache.writeTimezoneInstancesPrevious(TIMEZONE_AMERICA_LOS_ANGELES);
75        assertEquals(TIMEZONE_AMERICA_LOS_ANGELES, mCalendarCache.readTimezoneInstancesPrevious());
76    }
77
78    public void testWriteAndReadTimezoneType() throws CalendarCache.CacheException {
79        mCalendarCache.writeTimezoneType(CalendarCache.TIMEZONE_TYPE_AUTO);
80        assertEquals(CalendarCache.TIMEZONE_TYPE_AUTO, mCalendarCache.readTimezoneType());
81
82        mCalendarCache.writeTimezoneType(CalendarCache.TIMEZONE_TYPE_HOME);
83        assertEquals(CalendarCache.TIMEZONE_TYPE_HOME, mCalendarCache.readTimezoneType());
84    }
85}
86