summaryrefslogtreecommitdiff
path: root/InCallUI/tests/src/com/android/incallui/InCallContactInteractionsTest.java
blob: 625cda44837b93739edaca596a8b50afde39f7eb (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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
/*
 * Copyright (C) 2015 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.incallui;

import android.location.Address;
import android.test.AndroidTestCase;
import android.util.Pair;

import com.android.incallui.InCallContactInteractions.BusinessContextInfo;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.List;
import java.util.Locale;

/**
 * Tests for InCallContactInteractions class methods for formatting info for display.
 *
 * NOTE: tests assume system settings are set to 12hr time format and US locale. This means that
 * the output of InCallContactInteractions methods are compared against strings in 12hr time format
 * and US locale address formatting unless otherwise specified.
 */
public class InCallContactInteractionsTest extends AndroidTestCase {
    private InCallContactInteractions mInCallContactInteractions;
    private static final float TEST_DISTANCE = (float) 1234.56;

    @Override
    protected void setUp() {
        mInCallContactInteractions = new InCallContactInteractions(mContext, true /* isBusiness */);
    }

    public void testIsOpenNow_NowMatchesOpenTime() {
        assertEquals(mContext.getString(R.string.open_now),
                mInCallContactInteractions.constructHoursInfo(
                        getTestCalendarWithHour(8),
                        Arrays.asList(
                                Pair.create(
                                        getTestCalendarWithHour(8),
                                        getTestCalendarWithHour(20))))
                .heading);
    }

    public void testIsOpenNow_ClosingAfterMidnight() {
        assertEquals(mContext.getString(R.string.open_now),
                mInCallContactInteractions.constructHoursInfo(
                        getTestCalendarWithHour(10),
                        Arrays.asList(
                                Pair.create(
                                        getTestCalendarWithHour(8),
                                        getTestCalendarWithHourAndDaysFromToday(1, 1))))
                .heading);
    }

    public void testIsOpenNow_Open24Hours() {
        assertEquals(mContext.getString(R.string.open_now),
                mInCallContactInteractions.constructHoursInfo(
                        getTestCalendarWithHour(10),
                        Arrays.asList(
                                Pair.create(
                                        getTestCalendarWithHour(8),
                                        getTestCalendarWithHourAndDaysFromToday(8, 1))))
                .heading);
    }

    public void testIsOpenNow_AfterMiddayBreak() {
        assertEquals(mContext.getString(R.string.open_now),
                mInCallContactInteractions.constructHoursInfo(
                        getTestCalendarWithHour(13),
                        Arrays.asList(
                                Pair.create(
                                        getTestCalendarWithHour(8),
                                        getTestCalendarWithHour(10)),
                                Pair.create(
                                        getTestCalendarWithHour(12),
                                        getTestCalendarWithHour(15))))
                .heading);
    }

    public void testIsClosedNow_DuringMiddayBreak() {
        assertEquals(mContext.getString(R.string.closed_now),
                mInCallContactInteractions.constructHoursInfo(
                        getTestCalendarWithHour(11),
                        Arrays.asList(
                                Pair.create(
                                        getTestCalendarWithHour(8),
                                        getTestCalendarWithHour(10)),
                                Pair.create(
                                        getTestCalendarWithHour(12),
                                        getTestCalendarWithHour(15))))
                .heading);
    }

    public void testIsClosedNow_BeforeOpen() {
        assertEquals(mContext.getString(R.string.closed_now),
                mInCallContactInteractions.constructHoursInfo(
                        getTestCalendarWithHour(6),
                        Arrays.asList(
                                Pair.create(
                                        getTestCalendarWithHour(8),
                                        getTestCalendarWithHour(20))))
                .heading);
    }

    public void testIsClosedNow_NowMatchesClosedTime() {
        assertEquals(mContext.getString(R.string.closed_now),
                mInCallContactInteractions.constructHoursInfo(
                        getTestCalendarWithHour(20),
                        Arrays.asList(
                                Pair.create(
                                        getTestCalendarWithHour(8),
                                        getTestCalendarWithHour(20))))
                .heading);
    }

    public void testIsClosedNow_AfterClosed() {
        assertEquals(mContext.getString(R.string.closed_now),
                mInCallContactInteractions.constructHoursInfo(
                        getTestCalendarWithHour(21),
                        Arrays.asList(
                                Pair.create(
                                        getTestCalendarWithHour(8),
                                        getTestCalendarWithHour(20))))
                .heading);
    }

    public void testOpeningHours_SingleOpenRangeWhileOpen() {
        assertEquals("8:00 AM - 8:00 PM",
                mInCallContactInteractions.constructHoursInfo(
                        getTestCalendarWithHour(12),
                        Arrays.asList(
                                Pair.create(
                                        getTestCalendarWithHour(8),
                                        getTestCalendarWithHour(20))))
                .detail);
    }

    public void testOpeningHours_TwoOpenRangesWhileOpen() {
        assertEquals("8:00 AM - 10:00 AM, 12:00 PM - 3:00 PM",
                mInCallContactInteractions.constructHoursInfo(
                        getTestCalendarWithHour(12),
                        Arrays.asList(
                                Pair.create(
                                    getTestCalendarWithHour(8),
                                    getTestCalendarWithHour(10)),
                                Pair.create(
                                        getTestCalendarWithHour(12),
                                        getTestCalendarWithHour(15))))
                .detail);
    }

    public void testOpeningHours_AfterClosedNoTomorrow() {
        assertEquals("Closed today at 8:00 PM",
                mInCallContactInteractions.constructHoursInfo(
                        getTestCalendarWithHour(21),
                        Arrays.asList(
                                Pair.create(
                                        getTestCalendarWithHour(8),
                                        getTestCalendarWithHour(20))))
                .detail);
    }

    public void testOpeningHours_NotOpenTodayOpenTomorrow() {
        assertEquals("Opens tomorrow at 8:00 AM",
                mInCallContactInteractions.constructHoursInfo(
                        getTestCalendarWithHour(21),
                        Arrays.asList(
                                Pair.create(
                                        getTestCalendarWithHourAndDaysFromToday(8, 1),
                                        getTestCalendarWithHourAndDaysFromToday(10, 1))))
                .detail);
    }

    public void testMultipleOpenRanges_BeforeOpen() {
        assertEquals("Opens today at 8:00 AM",
                mInCallContactInteractions.constructHoursInfo(
                        getTestCalendarWithHour(7),
                        getMultipleOpeningHours())
                .detail);
    }

    public void testMultipleOpenRanges_DuringFirstRange() {
        assertEquals("Closes at 10:00 AM",
                mInCallContactInteractions.constructHoursInfo(
                        getTestCalendarWithHour(9),
                        getMultipleOpeningHours())
                .detail);
    }

    public void testMultipleOpenRanges_BeforeMiddleRange() {
        assertEquals("Opens today at 12:00 PM",
                mInCallContactInteractions.constructHoursInfo(
                        getTestCalendarWithHour(11),
                        getMultipleOpeningHours())
                .detail);
    }

    public void testMultipleOpeningHours_DuringLastRange() {
        assertEquals("Closes at 9:00 PM",
                mInCallContactInteractions.constructHoursInfo(
                        getTestCalendarWithHour(19),
                        getMultipleOpeningHours())
                .detail);
    }

    public void testMultipleOpeningHours_AfterClose() {
        assertEquals("Opens tomorrow at 8:00 AM",
                mInCallContactInteractions.constructHoursInfo(
                        getTestCalendarWithHour(22),
                        getMultipleOpeningHours())
                .detail);
    }

    public void testNotOpenTodayOrTomorrow() {
        assertEquals(null,
                mInCallContactInteractions.constructHoursInfo(
                        getTestCalendarWithHour(21),
                        new ArrayList<Pair<Calendar, Calendar>>()));
    }

    public void testLocationInfo_ForUS() {
        BusinessContextInfo info =
                mInCallContactInteractions.constructLocationInfo(
                        Locale.US,
                        getAddressForTest(),
                        TEST_DISTANCE);
        assertEquals("0.8 mi away", info.heading);
        assertEquals("Test address, Test locality", info.detail);
    }

    public void testLocationInfo_ForNotUS() {
        BusinessContextInfo info =
                mInCallContactInteractions.constructLocationInfo(
                        Locale.CANADA,
                        getAddressForTest(),
                        TEST_DISTANCE);
        assertEquals("1.2 km away", info.heading);
        assertEquals("Test address, Test locality", info.detail);
    }

    public void testLocationInfo_NoLocality() {
        Address address = getAddressForTest();
        address.setLocality(null);
        BusinessContextInfo info =
                mInCallContactInteractions.constructLocationInfo(
                        Locale.CANADA,
                        address,
                        TEST_DISTANCE);
        assertEquals("1.2 km away", info.heading);
        assertEquals("Test address", info.detail);
    }

    public void testLocationInfo_NoAddress() {
        BusinessContextInfo info =
                mInCallContactInteractions.constructLocationInfo(
                        Locale.CANADA,
                        null,
                        TEST_DISTANCE);
        assertEquals(null, info);
    }

    public void testLocationInfo_NoDistance() {
        BusinessContextInfo info =
                mInCallContactInteractions.constructLocationInfo(
                        Locale.US,
                        getAddressForTest(),
                        DistanceHelper.DISTANCE_NOT_FOUND);
        assertEquals(null, info.heading);
    }

    private Address getAddressForTest() {
        Address address = new Address(Locale.US);
        address.setAddressLine(0, "Test address");
        address.setLocality("Test locality");
        return address;
    }

    private Calendar getTestCalendarWithHour(int hour) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, hour);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar;
    }

    private Calendar getTestCalendarWithHourAndDaysFromToday(int hour, int daysFromToday) {
        Calendar calendar = getTestCalendarWithHour(hour);
        calendar.add(Calendar.DATE, daysFromToday);
        return calendar;
    }

    private List<Pair<Calendar, Calendar>> getMultipleOpeningHours() {
        return Arrays.asList(
                Pair.create(
                    getTestCalendarWithHour(8),
                    getTestCalendarWithHour(10)),
                Pair.create(
                        getTestCalendarWithHour(12),
                        getTestCalendarWithHour(15)),
                Pair.create(
                        getTestCalendarWithHour(17),
                        getTestCalendarWithHour(21)),
                Pair.create(
                        getTestCalendarWithHourAndDaysFromToday(8, 1),
                        getTestCalendarWithHourAndDaysFromToday(10, 1)),
                Pair.create(
                        getTestCalendarWithHourAndDaysFromToday(12, 1),
                        getTestCalendarWithHourAndDaysFromToday(8, 1)));
    }
}