summaryrefslogtreecommitdiffstats
path: root/tests/src/com/android/contacts/interactions/ContactInteractionUtilTest.java
blob: 05ad9b59478b73ef79b7ebb0e03e8c2128db35af (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*
 * Copyright (C) 2014 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.contacts.interactions;

import com.android.contacts.common.R;

import android.content.res.Configuration;
import android.content.res.Resources;
import android.test.AndroidTestCase;
import android.text.format.DateUtils;

import java.util.Calendar;
import java.util.Locale;

/**
 * Tests for utility functions in {@link ContactInteractionUtil}
 */
public class ContactInteractionUtilTest extends AndroidTestCase {

    private Locale mOriginalLocale;
    private Calendar calendar;

    @Override
    protected void setUp() throws Exception {
        super.setUp();
        calendar = Calendar.getInstance();

        // Time/Date utilities rely on specific locales. Forace US and set back in tearDown()
        mOriginalLocale = Locale.getDefault();
        setLocale(Locale.US);
    }

    @Override
    protected void tearDown() throws Exception {
        setLocale(mOriginalLocale);
        super.tearDown();
    }

    public void testOneQuestionMark() {
        assertEquals("(?)", ContactInteractionUtil.questionMarks(1));
    }

    public void testTwoQuestionMarks() {
        assertEquals("(?,?)", ContactInteractionUtil.questionMarks(2));
    }

    public void testFiveQuestionMarks() {
        assertEquals("(?,?,?,?,?)", ContactInteractionUtil.questionMarks(5));
    }

    public void testFormatDateStringFromTimestamp_todaySingleMinuteAm() {
        // Test today scenario (time shown)
        // Single digit minute & AM
        calendar.set(Calendar.HOUR_OF_DAY, 8);
        calendar.set(Calendar.MINUTE, 8);
        long todayTimestamp = calendar.getTimeInMillis();
        assertEquals("8:08 AM", ContactInteractionUtil.formatDateStringFromTimestamp(
                calendar.getTimeInMillis(), getContext()));
    }

    public void testFormatDateStringFromTimestamp_todayDoubleMinutePm() {
        // Double digit minute & PM
        calendar.set(Calendar.HOUR_OF_DAY, 22);
        calendar.set(Calendar.MINUTE, 18);
        assertEquals("10:18 PM",
                ContactInteractionUtil.formatDateStringFromTimestamp(calendar.getTimeInMillis(),
                        getContext()));
    }

    public void testFormatDateStringFromTimestamp_yesterday() {
        // Test yesterday and tomorrow (Yesterday or Tomorrow shown)
        calendar.add(Calendar.DAY_OF_YEAR, -1);
        assertEquals(getContext().getResources().getString(R.string.timestamp_string_yesterday),
                ContactInteractionUtil.formatDateStringFromTimestamp(calendar.getTimeInMillis(),
                        getContext()));
    }

    public void testFormatDateStringFromTimestamp_yesterdayLastYear() {
        // Set to non leap year
        calendar.set(Calendar.YEAR, 1999);
        calendar.set(Calendar.DAY_OF_YEAR, 365);
        long lastYear = calendar.getTimeInMillis();
        calendar.add(Calendar.DAY_OF_YEAR, 1);

        assertEquals(getContext().getResources().getString(R.string.timestamp_string_yesterday),
                ContactInteractionUtil.formatDateStringFromTimestamp(lastYear,
                        getContext(), calendar));
    }

    public void testFormatDateStringFromTimestamp_tomorrow() {
        calendar.add(Calendar.DAY_OF_YEAR, 1);
        assertEquals(getContext().getResources().getString(R.string.timestamp_string_tomorrow),
                ContactInteractionUtil.formatDateStringFromTimestamp(calendar.getTimeInMillis(),
                        getContext()));
    }

    public void testFormatDateStringFromTimestamp_tomorrowNewYear() {
        calendar.set(Calendar.DAY_OF_YEAR, 1);
        long thisYear = calendar.getTimeInMillis();
        calendar.add(Calendar.DAY_OF_YEAR, -1);

        assertEquals(getContext().getResources().getString(R.string.timestamp_string_tomorrow),
                ContactInteractionUtil.formatDateStringFromTimestamp(thisYear,
                        getContext(), calendar));
    }

    public void testFormatDateStringFromTimestamp_other() {
        // Test other (Month Date)
        calendar.set(
                /* year = */ 1991,
                /* month = */ Calendar.MONTH,
                /* day = */ 11);
        assertEquals("March 11",
                ContactInteractionUtil.formatDateStringFromTimestamp(calendar.getTimeInMillis(),
                        getContext()));
    }

    private void setLocale(Locale locale) {
        Locale.setDefault(locale);
        Resources res = getContext().getResources();
        Configuration config = res.getConfiguration();
        config.locale = locale;
        res.updateConfiguration(config, res.getDisplayMetrics());
    }
}