diff options
author | Sara Ting <sarating@google.com> | 2012-04-09 15:37:10 -0700 |
---|---|---|
committer | Sara Ting <sarating@google.com> | 2012-04-18 16:55:15 -0700 |
commit | 75f53668f94c3ced9d3cc8583d7e45ce725ff9de (patch) | |
tree | 56ca9d102b8e6acd70e592d0624949e9871cee1c /tests/src/com | |
parent | 5a50a9cb1a668f64225b859885babb9ef9c76801 (diff) | |
download | android_packages_apps_Calendar-75f53668f94c3ced9d3cc8583d7e45ce725ff9de.tar.gz android_packages_apps_Calendar-75f53668f94c3ced9d3cc8583d7e45ce725ff9de.tar.bz2 android_packages_apps_Calendar-75f53668f94c3ced9d3cc8583d7e45ce725ff9de.zip |
Shorten date/time info in event info to display in one line.
1) Don't show the year if it is the current year, 2) shorten day/month names for multiday events, 3) display "Today" or "Tomorrow" when possible, 4) use short timezone string. (b/6285801)
Change-Id: Idc4646c15f90ea6803cdff7cde985550501f62cf
Diffstat (limited to 'tests/src/com')
-rw-r--r-- | tests/src/com/android/calendar/DbTestUtils.java | 196 | ||||
-rw-r--r-- | tests/src/com/android/calendar/FakeSharedPreferences.java | 166 | ||||
-rw-r--r-- | tests/src/com/android/calendar/UtilsTests.java | 206 |
3 files changed, 566 insertions, 2 deletions
diff --git a/tests/src/com/android/calendar/DbTestUtils.java b/tests/src/com/android/calendar/DbTestUtils.java new file mode 100644 index 00000000..ed9b5e91 --- /dev/null +++ b/tests/src/com/android/calendar/DbTestUtils.java @@ -0,0 +1,196 @@ +/* + * Copyright (C) 2012 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ + +package com.android.calendar; + +import android.content.ContentResolver; +import android.content.ContentValues; +import android.content.Context; +import android.content.SharedPreferences; +import android.content.res.Resources; +import android.database.Cursor; +import android.net.Uri; +import android.os.Bundle; +import android.test.mock.MockContentProvider; +import android.test.mock.MockContentResolver; +import android.test.mock.MockContext; +import android.test.mock.MockCursor; + +import java.util.ArrayList; +import java.util.List; + +/** + * A helper class for creating and wiring fake implementations of db classes, like + * Context, ContentResolver, ContentProvider, etc. Typical setup will look something like: + * DbUtils dbUtils = new DbUtils(mockResources); + * dbUtils.getContentResolver().addProvider("settings", dbUtils.getContentProvider()); + * dbUtils.getContentResolver().addProvider(CalendarCache.URI.getAuthority(), + * dbUtils.getContentProvider()); + */ +class DbTestUtils { + private final MockContentResolver contentResolver; + private final FakeContext context; + private final FakeSharedPreferences sharedPreferences; + private final FakeContentProvider contentProvider; + + class FakeContext extends MockContext { + private ContentResolver contentResolver; + private Resources resources; + private SharedPreferences sharedPreferences; + + FakeContext(ContentResolver contentResolver, Resources resources) { + this.contentResolver = contentResolver; + this.resources = resources; + } + + @Override + public ContentResolver getContentResolver() { + return contentResolver; + } + + @Override + public Resources getResources() { + return resources; + } + + public void setSharedPreferences(SharedPreferences sharedPreferences) { + this.sharedPreferences = sharedPreferences; + } + + @Override + public SharedPreferences getSharedPreferences(String name, int mode) { + if (sharedPreferences != null) { + return sharedPreferences; + } else { + return super.getSharedPreferences(name, mode); + } + } + } + + // TODO: finish fake implementation. + static class FakeCursor extends MockCursor { + private List<String> queryResult; + int mCurrentPosition = -1; + + FakeCursor(List<String> queryResult) { + this.queryResult = queryResult; + } + + @Override + public int getCount() { + return queryResult.size(); + } + + @Override + public boolean moveToFirst() { + mCurrentPosition = 0; + return true; + } + + @Override + public boolean moveToNext() { + if (queryResult.size() > 0 && mCurrentPosition < queryResult.size()) { + mCurrentPosition++; + return true; + } else { + return false; + } + } + + @Override + public boolean isBeforeFirst() { + return mCurrentPosition < 0; + } + + @Override + public String getString(int columnIndex) { + return queryResult.get(columnIndex); + } + + @Override + public void close() { + } + } + + // TODO: finish implementation, perhaps using an in-memory table + static class FakeContentProvider extends MockContentProvider { + private ArrayList<String> queryResult = null; + + public FakeContentProvider(Context context) { + super(context); + } + + @Override + public Bundle call(String method, String request, Bundle args) { + return null; + } + + @Override + public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) { + // TODO: not currently implemented + return 1; + } + + /** + * Set the mocked results to return from a query call. + */ + public void setQueryResult(ArrayList<String> result) { + this.queryResult = result; + } + + @Override + public final Cursor query(Uri uri, String[] projection, String selection, + String[] selectionArgs, String orderBy) { + ArrayList<String> result = (queryResult == null) ? + new ArrayList<String>() : queryResult; + return new FakeCursor(result); + } + + @Override + public String getType(Uri uri) { + return null; + } + + @Override + public boolean onCreate() { + return false; + } + } + + public DbTestUtils(Resources resources) { + this.contentResolver = new MockContentResolver(); + this.context = new FakeContext(contentResolver, resources); + this.sharedPreferences = new FakeSharedPreferences(); + this.contentProvider = new FakeContentProvider(context); + context.setSharedPreferences(sharedPreferences); + } + + public MockContentResolver getContentResolver() { + return contentResolver; + } + + public FakeContext getContext() { + return context; + } + + public FakeContentProvider getContentProvider() { + return contentProvider; + } + + public FakeSharedPreferences getMockSharedPreferences() { + return sharedPreferences; + } +} diff --git a/tests/src/com/android/calendar/FakeSharedPreferences.java b/tests/src/com/android/calendar/FakeSharedPreferences.java new file mode 100644 index 00000000..b906b888 --- /dev/null +++ b/tests/src/com/android/calendar/FakeSharedPreferences.java @@ -0,0 +1,166 @@ +/* + * Copyright (C) 2012 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.calendar; + +import android.content.SharedPreferences; +import android.content.SharedPreferences.Editor; +import android.content.SharedPreferences.OnSharedPreferenceChangeListener; + +import java.util.HashMap; +import java.util.Map; +import java.util.Set; + +class FakeSharedPreferences implements SharedPreferences, SharedPreferences.Editor { + + private HashMap<String, Object> mValues = new HashMap<String, Object>(); + private HashMap<String, Object> mTempValues = new HashMap<String, Object>(); + + @Override + public Editor edit() { + return this; + } + + @Override + public boolean contains(String key) { + return mValues.containsKey(key); + } + + @Override + public Map<String, ?> getAll() { + return new HashMap<String, Object>(mValues); + } + + @Override + public boolean getBoolean(String key, boolean defValue) { + if (mValues.containsKey(key)) { + return ((Boolean)mValues.get(key)).booleanValue(); + } + return defValue; + } + + @Override + public float getFloat(String key, float defValue) { + if (mValues.containsKey(key)) { + return ((Float)mValues.get(key)).floatValue(); + } + return defValue; + } + + @Override + public int getInt(String key, int defValue) { + if (mValues.containsKey(key)) { + return ((Integer)mValues.get(key)).intValue(); + } + return defValue; + } + + @Override + public long getLong(String key, long defValue) { + if (mValues.containsKey(key)) { + return ((Long)mValues.get(key)).longValue(); + } + return defValue; + } + + @Override + public String getString(String key, String defValue) { + if (mValues.containsKey(key)) + return (String)mValues.get(key); + return defValue; + } + + @SuppressWarnings("unchecked") + @Override + public Set<String> getStringSet(String key, Set<String> defValues) { + if (mValues.containsKey(key)) { + return (Set<String>) mValues.get(key); + } + return defValues; + } + + @Override + public void registerOnSharedPreferenceChangeListener( + OnSharedPreferenceChangeListener listener) { + throw new UnsupportedOperationException(); + } + + @Override + public void unregisterOnSharedPreferenceChangeListener( + OnSharedPreferenceChangeListener listener) { + throw new UnsupportedOperationException(); + } + + @Override + public Editor putBoolean(String key, boolean value) { + mTempValues.put(key, Boolean.valueOf(value)); + return this; + } + + @Override + public Editor putFloat(String key, float value) { + mTempValues.put(key, value); + return this; + } + + @Override + public Editor putInt(String key, int value) { + mTempValues.put(key, value); + return this; + } + + @Override + public Editor putLong(String key, long value) { + mTempValues.put(key, value); + return this; + } + + @Override + public Editor putString(String key, String value) { + mTempValues.put(key, value); + return this; + } + + @Override + public Editor putStringSet(String key, Set<String> values) { + mTempValues.put(key, values); + return this; + } + + @Override + public Editor remove(String key) { + mTempValues.remove(key); + return this; + } + + @Override + public Editor clear() { + mTempValues.clear(); + return this; + } + + @SuppressWarnings("unchecked") + @Override + public boolean commit() { + mValues = (HashMap<String, Object>)mTempValues.clone(); + return true; + } + + @Override + public void apply() { + commit(); + } +}
\ No newline at end of file diff --git a/tests/src/com/android/calendar/UtilsTests.java b/tests/src/com/android/calendar/UtilsTests.java index d587f1e9..11181b60 100644 --- a/tests/src/com/android/calendar/UtilsTests.java +++ b/tests/src/com/android/calendar/UtilsTests.java @@ -16,15 +16,18 @@ package com.android.calendar; +import com.android.calendar.CalendarUtils.TimeZoneUtils; +import android.content.res.Configuration; import android.database.MatrixCursor; +import android.provider.CalendarContract.CalendarCache; +import android.test.mock.MockResources; import android.test.suitebuilder.annotation.SmallTest; import android.test.suitebuilder.annotation.Smoke; import android.text.format.Time; -import android.util.Log; -import java.util.ArrayList; import java.util.HashMap; +import java.util.Locale; import junit.framework.TestCase; @@ -39,6 +42,8 @@ public class UtilsTests extends TestCase { HashMap<String, Boolean> mIsDuplicateName; HashMap<String, Boolean> mIsDuplicateNameExpected; MatrixCursor mDuplicateNameCursor; + private DbTestUtils dbUtils; + private final TimeZoneUtils timezoneUtils = new TimeZoneUtils(Utils.SHARED_PREFS_NAME); private static final int NAME_COLUMN = 0; private static final String[] DUPLICATE_NAME_COLUMNS = new String[] { "name" }; @@ -65,6 +70,53 @@ public class UtilsTests extends TestCase { private static final int[] WEEKS_FOR_JULIAN_MONDAYS = {1, 2}; private static final int[] EXPECTED_JULIAN_MONDAYS = {2440592, 2440599}; + private static final int NOW_MONTH = 3; // April + private static final int NOW_DAY = 10; + private static final int NOW_YEAR = 2012; + private static final long NOW_TIME = createTimeInMillis(5, 5, 5, NOW_DAY, NOW_MONTH, NOW_YEAR); + private static final String DEFAULT_TIMEZONE = Time.getCurrentTimezone(); + + /** + * Mock resources. Add translation strings for test here. + */ + private static class ResourcesForTest extends MockResources { + @Override + public String getString(int id) { + if (id == R.string.today) { + return "Today"; + } + if (id == R.string.tomorrow) { + return "Tomorrow"; + } + throw new IllegalArgumentException("unexpected resource ID: " + id); + } + + @Override + public Configuration getConfiguration() { + Configuration config = new Configuration(); + config.locale = Locale.getDefault(); + return config; + } + } + + private static long createTimeInMillis(int second, int minute, int hour, int monthDay, + int month, int year) { + return createTimeInMillis(second, minute, hour, monthDay, month, year, + Time.getCurrentTimezone()); + } + + private static long createTimeInMillis(int second, int minute, int hour, int monthDay, + int month, int year, String timezone) { + Time t = new Time(timezone); + t.set(second, minute, hour, monthDay, month, year); + t.normalize(false); + return t.toMillis(false); + } + + private void setTimezone(String tz) { + timezoneUtils.setTimeZone(dbUtils.getContext(), tz); + } + @Override public void setUp() { mIsDuplicateName = new HashMap<String, Boolean> (); @@ -79,11 +131,22 @@ public class UtilsTests extends TestCase { mIsDuplicateNameExpected.put("Peter Parker", false); mIsDuplicateNameExpected.put("Silver Surfer", false); mIsDuplicateNameExpected.put("John Jameson", true); + + // Set up fake db. + dbUtils = new DbTestUtils(new ResourcesForTest()); + dbUtils.getContentResolver().addProvider("settings", dbUtils.getContentProvider()); + dbUtils.getContentResolver().addProvider(CalendarCache.URI.getAuthority(), + dbUtils.getContentProvider()); + setTimezone(DEFAULT_TIMEZONE); } @Override public void tearDown() { mDuplicateNameCursor.close(); + + // Must reset the timezone here, because even though the fake provider will be + // recreated/cleared, TimeZoneUtils statically holds on to a cached value. + setTimezone(Time.getCurrentTimezone()); } @Smoke @@ -299,4 +362,143 @@ public class UtilsTests extends TestCase { assertEquals(matches[i], seq); } } + + @SmallTest + public void testGetDisplayedDatetime_differentYear() { + // 4/12/2000 5pm - 4/12/2000 6pm + long start = createTimeInMillis(0, 0, 17, 12, 3, 2000); + long end = createTimeInMillis(0, 0, 18, 12, 3, 2000); + String result = Utils.getDisplayedDatetime(start, end, NOW_TIME, DEFAULT_TIMEZONE, + DEFAULT_TIMEZONE, false, dbUtils.getContext()); + assertEquals("Wednesday, April 12, 2000, 5:00pm \u2013 6:00pm", result); + + // 12/31/2012 5pm - 1/1/2013 6pm + start = createTimeInMillis(0, 0, 17, 31, 11, 2012); + end = createTimeInMillis(0, 0, 18, 1, 0, 2013); + result = Utils.getDisplayedDatetime(start, end, NOW_TIME, DEFAULT_TIMEZONE, + DEFAULT_TIMEZONE, false, dbUtils.getContext()); + assertEquals("Mon, Dec 31, 2012, 5:00pm – Tue, Jan 1, 2013, 6:00pm", result); + } + + @SmallTest + public void testGetDisplayedDatetime_sameYear() { + // 4/12/2012 5pm - 4/12/2012 6pm + long start = createTimeInMillis(0, 0, 17, 12, 3, 2012); + long end = createTimeInMillis(0, 0, 18, 12, 3, 2012); + String result = Utils.getDisplayedDatetime(start, end, NOW_TIME, DEFAULT_TIMEZONE, + DEFAULT_TIMEZONE, false, dbUtils.getContext()); + assertEquals("Thursday, April 12, 5:00pm \u2013 6:00pm", result); + } + + @SmallTest + public void testGetDisplayedDatetime_today() { + // 4/10/2012 5pm - 4/10/2012 6pm + long start = createTimeInMillis(0, 0, 17, NOW_DAY, NOW_MONTH, NOW_YEAR); + long end = createTimeInMillis(0, 0, 18, NOW_DAY, NOW_MONTH, NOW_YEAR); + String result = Utils.getDisplayedDatetime(start, end, NOW_TIME, DEFAULT_TIMEZONE, + DEFAULT_TIMEZONE, false, dbUtils.getContext()); + assertEquals("Today, 5:00pm \u2013 6:00pm", result); + } + + @SmallTest + public void testGetDisplayedDatetime_todayMidnight() { + // 4/10/2012 5pm - 4/11/2012 12am + long start = createTimeInMillis(0, 0, 17, NOW_DAY, NOW_MONTH, NOW_YEAR); + long end = createTimeInMillis(0, 0, 0, NOW_DAY + 1, NOW_MONTH, NOW_YEAR); + String result = Utils.getDisplayedDatetime(start, end, NOW_TIME, DEFAULT_TIMEZONE, + DEFAULT_TIMEZONE, false, dbUtils.getContext()); + assertEquals("Today, 5:00pm \u2013 midnight", result); + } + + @SmallTest + public void testGetDisplayedDatetime_tomorrow() { + // 4/11/2012 12:01am - 4/11/2012 11:59pm + long start = createTimeInMillis(0, 1, 0, NOW_DAY + 1, NOW_MONTH, NOW_YEAR); + long end = createTimeInMillis(0, 59, 23, NOW_DAY + 1, NOW_MONTH, NOW_YEAR); + String result = Utils.getDisplayedDatetime(start, end, NOW_TIME, DEFAULT_TIMEZONE, + DEFAULT_TIMEZONE, false, dbUtils.getContext()); + assertEquals("Tomorrow, 12:01am \u2013 11:59pm", result); + } + + @SmallTest + public void testGetDisplayedDatetime_yesterday() { + // 4/9/2012 5pm - 4/9/2012 6pm + long start = createTimeInMillis(0, 0, 17, 9, 3, 2012); + long end = createTimeInMillis(0, 0, 18, 9, 3, 2012); + String result = Utils.getDisplayedDatetime(start, end, NOW_TIME, DEFAULT_TIMEZONE, + DEFAULT_TIMEZONE, false, dbUtils.getContext()); + assertEquals("Monday, April 9, 5:00pm \u2013 6:00pm", result); + } + + @SmallTest + public void testGetDisplayedDatetime_multiDay() { + // 4/10/2012 12:01am - 4/11/2012 12:01am + long start = createTimeInMillis(0, 1, 0, NOW_DAY, NOW_MONTH, NOW_YEAR); + long end = createTimeInMillis(0, 1, 0, NOW_DAY + 1, NOW_MONTH, NOW_YEAR); + String result = Utils.getDisplayedDatetime(start, end, NOW_TIME, DEFAULT_TIMEZONE, + DEFAULT_TIMEZONE, false, dbUtils.getContext()); + assertEquals("Tue, Apr 10, 12:01am \u2013 Wed, Apr 11, 12:01am", result); + } + + @SmallTest + public void testGetDisplayedDatetime_allDay() { + // 4/2/2012 12:00am - 4/3/2012 12:00am + long start = createTimeInMillis(0, 0, 0, 2, 3, NOW_YEAR, Time.TIMEZONE_UTC); + long end = createTimeInMillis(0, 0, 0, 3, 3, NOW_YEAR, Time.TIMEZONE_UTC); + String result = Utils.getDisplayedDatetime(start, end, NOW_TIME, DEFAULT_TIMEZONE, + DEFAULT_TIMEZONE, true, dbUtils.getContext()); + assertEquals("Monday, April 2", result); + } + + @SmallTest + public void testGetDisplayedDatetime_allDayToday() { + // 4/10/2012 12:00am - 4/11/2012 12:00am + long start = createTimeInMillis(0, 0, 0, NOW_DAY, NOW_MONTH, NOW_YEAR, Time.TIMEZONE_UTC); + long end = createTimeInMillis(0, 0, 0, NOW_DAY + 1, NOW_MONTH, NOW_YEAR, Time.TIMEZONE_UTC); + String result = Utils.getDisplayedDatetime(start, end, NOW_TIME, DEFAULT_TIMEZONE, + DEFAULT_TIMEZONE, true, dbUtils.getContext()); + assertEquals("Today", result); + } + + @SmallTest + public void testGetDisplayedDatetime_differentTimezone() { + String localTz = "America/New_York"; + String eventTz = "America/Los_Angeles"; + setTimezone(localTz); + + // 4/12/2012 5pm - 4/12/2012 6pm (Pacific) + long start = createTimeInMillis(0, 0, 17, 12, 3, 2012, eventTz); + long end = createTimeInMillis(0, 0, 18, 12, 3, 2012, eventTz); + String result = Utils.getDisplayedDatetime(start, end, NOW_TIME, localTz, eventTz, false, + dbUtils.getContext()); + assertEquals("Thursday, April 12, 8:00pm \u2013 9:00pm (EDT)", result); + } + + @SmallTest + public void testGetDisplayedDatetime_allDayDiffTimezone() { + String localTz = "America/New_York"; + setTimezone(localTz); + + // 4/2/2012 12:00am - 4/3/2012 12:00am + long start = createTimeInMillis(0, 0, 0, 2, 3, NOW_YEAR, Time.TIMEZONE_UTC); + long end = createTimeInMillis(0, 0, 0, 3, 3, NOW_YEAR, Time.TIMEZONE_UTC); + String result = Utils.getDisplayedDatetime(start, end, NOW_TIME, localTz, + Time.TIMEZONE_UTC, true, dbUtils.getContext()); + assertEquals("Monday, April 2", result); + } + + @SmallTest + public void testGetDisplayedDatetime_allDayTomorrowDiffTimezone() { + String localTz = "America/New_York"; + setTimezone(localTz); + + // 4/2/2012 12:00am - 4/3/2012 12:00am + long start = createTimeInMillis(0, 0, 0, NOW_DAY + 1, NOW_MONTH, NOW_YEAR, + Time.TIMEZONE_UTC); + long end = createTimeInMillis(0, 0, 0, NOW_DAY + 2, NOW_MONTH, NOW_YEAR, + Time.TIMEZONE_UTC); + String result = Utils.getDisplayedDatetime(start, end, NOW_TIME, localTz, + Time.TIMEZONE_UTC, true, dbUtils.getContext()); + assertEquals("Tomorrow", result); + } } |