1 /* 2 * Copyright (C) 2014 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 package com.android.contacts.interactions; 17 18 import com.android.contacts.common.R; 19 20 import android.content.res.Configuration; 21 import android.content.res.Resources; 22 import android.test.AndroidTestCase; 23 import android.text.format.DateUtils; 24 25 import java.util.Calendar; 26 import java.util.Locale; 27 28 /** 29 * Tests for utility functions in {@link ContactInteractionUtil} 30 */ 31 public class ContactInteractionUtilTest extends AndroidTestCase { 32 33 private Locale mOriginalLocale; 34 private Calendar calendar; 35 36 @Override setUp()37 protected void setUp() throws Exception { 38 super.setUp(); 39 calendar = Calendar.getInstance(); 40 41 // Time/Date utilities rely on specific locales. Forace US and set back in tearDown() 42 mOriginalLocale = Locale.getDefault(); 43 setLocale(Locale.US); 44 } 45 46 @Override tearDown()47 protected void tearDown() throws Exception { 48 setLocale(mOriginalLocale); 49 super.tearDown(); 50 } 51 testOneQuestionMark()52 public void testOneQuestionMark() { 53 assertEquals("(?)", ContactInteractionUtil.questionMarks(1)); 54 } 55 testTwoQuestionMarks()56 public void testTwoQuestionMarks() { 57 assertEquals("(?,?)", ContactInteractionUtil.questionMarks(2)); 58 } 59 testFiveQuestionMarks()60 public void testFiveQuestionMarks() { 61 assertEquals("(?,?,?,?,?)", ContactInteractionUtil.questionMarks(5)); 62 } 63 testFormatDateStringFromTimestamp_todaySingleMinuteAm()64 public void testFormatDateStringFromTimestamp_todaySingleMinuteAm() { 65 // Test today scenario (time shown) 66 // Single digit minute & AM 67 calendar.set(Calendar.HOUR_OF_DAY, 8); 68 calendar.set(Calendar.MINUTE, 8); 69 long todayTimestamp = calendar.getTimeInMillis(); 70 assertEquals("8:08 AM", ContactInteractionUtil.formatDateStringFromTimestamp( 71 calendar.getTimeInMillis(), getContext())); 72 } 73 testFormatDateStringFromTimestamp_todayDoubleMinutePm()74 public void testFormatDateStringFromTimestamp_todayDoubleMinutePm() { 75 // Double digit minute & PM 76 calendar.set(Calendar.HOUR_OF_DAY, 22); 77 calendar.set(Calendar.MINUTE, 18); 78 assertEquals("10:18 PM", 79 ContactInteractionUtil.formatDateStringFromTimestamp(calendar.getTimeInMillis(), 80 getContext())); 81 } 82 testFormatDateStringFromTimestamp_yesterday()83 public void testFormatDateStringFromTimestamp_yesterday() { 84 // Test yesterday and tomorrow (Yesterday or Tomorrow shown) 85 calendar.add(Calendar.DAY_OF_YEAR, -1); 86 assertEquals(getContext().getResources().getString(R.string.yesterday), 87 ContactInteractionUtil.formatDateStringFromTimestamp(calendar.getTimeInMillis(), 88 getContext())); 89 } 90 testFormatDateStringFromTimestamp_yesterdayLastYear()91 public void testFormatDateStringFromTimestamp_yesterdayLastYear() { 92 // Set to non leap year 93 calendar.set(Calendar.YEAR, 1999); 94 calendar.set(Calendar.DAY_OF_YEAR, 365); 95 long lastYear = calendar.getTimeInMillis(); 96 calendar.add(Calendar.DAY_OF_YEAR, 1); 97 98 assertEquals(getContext().getResources().getString(R.string.yesterday), 99 ContactInteractionUtil.formatDateStringFromTimestamp(lastYear, 100 getContext(), calendar)); 101 } 102 testFormatDateStringFromTimestamp_tomorrow()103 public void testFormatDateStringFromTimestamp_tomorrow() { 104 calendar.add(Calendar.DAY_OF_YEAR, 1); 105 assertEquals(getContext().getResources().getString(R.string.tomorrow), 106 ContactInteractionUtil.formatDateStringFromTimestamp(calendar.getTimeInMillis(), 107 getContext())); 108 } 109 testFormatDateStringFromTimestamp_tomorrowNewYear()110 public void testFormatDateStringFromTimestamp_tomorrowNewYear() { 111 calendar.set(Calendar.DAY_OF_YEAR, 1); 112 long thisYear = calendar.getTimeInMillis(); 113 calendar.add(Calendar.DAY_OF_YEAR, -1); 114 115 assertEquals(getContext().getResources().getString(R.string.tomorrow), 116 ContactInteractionUtil.formatDateStringFromTimestamp(thisYear, 117 getContext(), calendar)); 118 } 119 testFormatDateStringFromTimestamp_other()120 public void testFormatDateStringFromTimestamp_other() { 121 // Test other (Month Date) 122 calendar.set( 123 /* year = */ 1991, 124 /* month = */ Calendar.MONTH, 125 /* day = */ 11); 126 assertEquals("March 11", 127 ContactInteractionUtil.formatDateStringFromTimestamp(calendar.getTimeInMillis(), 128 getContext())); 129 } 130 setLocale(Locale locale)131 private void setLocale(Locale locale) { 132 Locale.setDefault(locale); 133 Resources res = getContext().getResources(); 134 Configuration config = res.getConfiguration(); 135 config.locale = locale; 136 res.updateConfiguration(config, res.getDisplayMetrics()); 137 } 138 }