1 /*
2  * Copyright (C) 2015 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 
17 package com.android.incallui;
18 
19 import android.location.Address;
20 import android.test.AndroidTestCase;
21 import android.util.Pair;
22 
23 import com.android.incallui.InCallContactInteractions.BusinessContextInfo;
24 
25 import java.util.ArrayList;
26 import java.util.Arrays;
27 import java.util.Calendar;
28 import java.util.List;
29 import java.util.Locale;
30 
31 /**
32  * Tests for InCallContactInteractions class methods for formatting info for display.
33  *
34  * NOTE: tests assume system settings are set to 12hr time format and US locale. This means that
35  * the output of InCallContactInteractions methods are compared against strings in 12hr time format
36  * and US locale address formatting unless otherwise specified.
37  */
38 public class InCallContactInteractionsTest extends AndroidTestCase {
39     private InCallContactInteractions mInCallContactInteractions;
40     private static final float TEST_DISTANCE = (float) 1234.56;
41 
42     @Override
setUp()43     protected void setUp() {
44         mInCallContactInteractions = new InCallContactInteractions(mContext, true /* isBusiness */);
45     }
46 
testIsOpenNow_NowMatchesOpenTime()47     public void testIsOpenNow_NowMatchesOpenTime() {
48         assertEquals(mContext.getString(R.string.open_now),
49                 mInCallContactInteractions.constructHoursInfo(
50                         getTestCalendarWithHour(8),
51                         Arrays.asList(
52                                 Pair.create(
53                                         getTestCalendarWithHour(8),
54                                         getTestCalendarWithHour(20))))
55                 .heading);
56     }
57 
testIsOpenNow_ClosingAfterMidnight()58     public void testIsOpenNow_ClosingAfterMidnight() {
59         assertEquals(mContext.getString(R.string.open_now),
60                 mInCallContactInteractions.constructHoursInfo(
61                         getTestCalendarWithHour(10),
62                         Arrays.asList(
63                                 Pair.create(
64                                         getTestCalendarWithHour(8),
65                                         getTestCalendarWithHourAndDaysFromToday(1, 1))))
66                 .heading);
67     }
68 
testIsOpenNow_Open24Hours()69     public void testIsOpenNow_Open24Hours() {
70         assertEquals(mContext.getString(R.string.open_now),
71                 mInCallContactInteractions.constructHoursInfo(
72                         getTestCalendarWithHour(10),
73                         Arrays.asList(
74                                 Pair.create(
75                                         getTestCalendarWithHour(8),
76                                         getTestCalendarWithHourAndDaysFromToday(8, 1))))
77                 .heading);
78     }
79 
testIsOpenNow_AfterMiddayBreak()80     public void testIsOpenNow_AfterMiddayBreak() {
81         assertEquals(mContext.getString(R.string.open_now),
82                 mInCallContactInteractions.constructHoursInfo(
83                         getTestCalendarWithHour(13),
84                         Arrays.asList(
85                                 Pair.create(
86                                         getTestCalendarWithHour(8),
87                                         getTestCalendarWithHour(10)),
88                                 Pair.create(
89                                         getTestCalendarWithHour(12),
90                                         getTestCalendarWithHour(15))))
91                 .heading);
92     }
93 
testIsClosedNow_DuringMiddayBreak()94     public void testIsClosedNow_DuringMiddayBreak() {
95         assertEquals(mContext.getString(R.string.closed_now),
96                 mInCallContactInteractions.constructHoursInfo(
97                         getTestCalendarWithHour(11),
98                         Arrays.asList(
99                                 Pair.create(
100                                         getTestCalendarWithHour(8),
101                                         getTestCalendarWithHour(10)),
102                                 Pair.create(
103                                         getTestCalendarWithHour(12),
104                                         getTestCalendarWithHour(15))))
105                 .heading);
106     }
107 
testIsClosedNow_BeforeOpen()108     public void testIsClosedNow_BeforeOpen() {
109         assertEquals(mContext.getString(R.string.closed_now),
110                 mInCallContactInteractions.constructHoursInfo(
111                         getTestCalendarWithHour(6),
112                         Arrays.asList(
113                                 Pair.create(
114                                         getTestCalendarWithHour(8),
115                                         getTestCalendarWithHour(20))))
116                 .heading);
117     }
118 
testIsClosedNow_NowMatchesClosedTime()119     public void testIsClosedNow_NowMatchesClosedTime() {
120         assertEquals(mContext.getString(R.string.closed_now),
121                 mInCallContactInteractions.constructHoursInfo(
122                         getTestCalendarWithHour(20),
123                         Arrays.asList(
124                                 Pair.create(
125                                         getTestCalendarWithHour(8),
126                                         getTestCalendarWithHour(20))))
127                 .heading);
128     }
129 
testIsClosedNow_AfterClosed()130     public void testIsClosedNow_AfterClosed() {
131         assertEquals(mContext.getString(R.string.closed_now),
132                 mInCallContactInteractions.constructHoursInfo(
133                         getTestCalendarWithHour(21),
134                         Arrays.asList(
135                                 Pair.create(
136                                         getTestCalendarWithHour(8),
137                                         getTestCalendarWithHour(20))))
138                 .heading);
139     }
140 
testOpeningHours_SingleOpenRangeWhileOpen()141     public void testOpeningHours_SingleOpenRangeWhileOpen() {
142         assertEquals("8:00 AM - 8:00 PM",
143                 mInCallContactInteractions.constructHoursInfo(
144                         getTestCalendarWithHour(12),
145                         Arrays.asList(
146                                 Pair.create(
147                                         getTestCalendarWithHour(8),
148                                         getTestCalendarWithHour(20))))
149                 .detail);
150     }
151 
testOpeningHours_TwoOpenRangesWhileOpen()152     public void testOpeningHours_TwoOpenRangesWhileOpen() {
153         assertEquals("8:00 AM - 10:00 AM, 12:00 PM - 3:00 PM",
154                 mInCallContactInteractions.constructHoursInfo(
155                         getTestCalendarWithHour(12),
156                         Arrays.asList(
157                                 Pair.create(
158                                     getTestCalendarWithHour(8),
159                                     getTestCalendarWithHour(10)),
160                                 Pair.create(
161                                         getTestCalendarWithHour(12),
162                                         getTestCalendarWithHour(15))))
163                 .detail);
164     }
165 
testOpeningHours_AfterClosedNoTomorrow()166     public void testOpeningHours_AfterClosedNoTomorrow() {
167         assertEquals("Closed today at 8:00 PM",
168                 mInCallContactInteractions.constructHoursInfo(
169                         getTestCalendarWithHour(21),
170                         Arrays.asList(
171                                 Pair.create(
172                                         getTestCalendarWithHour(8),
173                                         getTestCalendarWithHour(20))))
174                 .detail);
175     }
176 
testOpeningHours_NotOpenTodayOpenTomorrow()177     public void testOpeningHours_NotOpenTodayOpenTomorrow() {
178         assertEquals("Opens tomorrow at 8:00 AM",
179                 mInCallContactInteractions.constructHoursInfo(
180                         getTestCalendarWithHour(21),
181                         Arrays.asList(
182                                 Pair.create(
183                                         getTestCalendarWithHourAndDaysFromToday(8, 1),
184                                         getTestCalendarWithHourAndDaysFromToday(10, 1))))
185                 .detail);
186     }
187 
testMultipleOpenRanges_BeforeOpen()188     public void testMultipleOpenRanges_BeforeOpen() {
189         assertEquals("Opens today at 8:00 AM",
190                 mInCallContactInteractions.constructHoursInfo(
191                         getTestCalendarWithHour(7),
192                         getMultipleOpeningHours())
193                 .detail);
194     }
195 
testMultipleOpenRanges_DuringFirstRange()196     public void testMultipleOpenRanges_DuringFirstRange() {
197         assertEquals("Closes at 10:00 AM",
198                 mInCallContactInteractions.constructHoursInfo(
199                         getTestCalendarWithHour(9),
200                         getMultipleOpeningHours())
201                 .detail);
202     }
203 
testMultipleOpenRanges_BeforeMiddleRange()204     public void testMultipleOpenRanges_BeforeMiddleRange() {
205         assertEquals("Opens today at 12:00 PM",
206                 mInCallContactInteractions.constructHoursInfo(
207                         getTestCalendarWithHour(11),
208                         getMultipleOpeningHours())
209                 .detail);
210     }
211 
testMultipleOpeningHours_DuringLastRange()212     public void testMultipleOpeningHours_DuringLastRange() {
213         assertEquals("Closes at 9:00 PM",
214                 mInCallContactInteractions.constructHoursInfo(
215                         getTestCalendarWithHour(19),
216                         getMultipleOpeningHours())
217                 .detail);
218     }
219 
testMultipleOpeningHours_AfterClose()220     public void testMultipleOpeningHours_AfterClose() {
221         assertEquals("Opens tomorrow at 8:00 AM",
222                 mInCallContactInteractions.constructHoursInfo(
223                         getTestCalendarWithHour(22),
224                         getMultipleOpeningHours())
225                 .detail);
226     }
227 
testNotOpenTodayOrTomorrow()228     public void testNotOpenTodayOrTomorrow() {
229         assertEquals(null,
230                 mInCallContactInteractions.constructHoursInfo(
231                         getTestCalendarWithHour(21),
232                         new ArrayList<Pair<Calendar, Calendar>>()));
233     }
234 
testLocationInfo_ForUS()235     public void testLocationInfo_ForUS() {
236         BusinessContextInfo info =
237                 mInCallContactInteractions.constructLocationInfo(
238                         Locale.US,
239                         getAddressForTest(),
240                         TEST_DISTANCE);
241         assertEquals("0.8 mi away", info.heading);
242         assertEquals("Test address, Test locality", info.detail);
243     }
244 
testLocationInfo_ForNotUS()245     public void testLocationInfo_ForNotUS() {
246         BusinessContextInfo info =
247                 mInCallContactInteractions.constructLocationInfo(
248                         Locale.CANADA,
249                         getAddressForTest(),
250                         TEST_DISTANCE);
251         assertEquals("1.2 km away", info.heading);
252         assertEquals("Test address, Test locality", info.detail);
253     }
254 
testLocationInfo_NoLocality()255     public void testLocationInfo_NoLocality() {
256         Address address = getAddressForTest();
257         address.setLocality(null);
258         BusinessContextInfo info =
259                 mInCallContactInteractions.constructLocationInfo(
260                         Locale.CANADA,
261                         address,
262                         TEST_DISTANCE);
263         assertEquals("1.2 km away", info.heading);
264         assertEquals("Test address", info.detail);
265     }
266 
testLocationInfo_NoAddress()267     public void testLocationInfo_NoAddress() {
268         BusinessContextInfo info =
269                 mInCallContactInteractions.constructLocationInfo(
270                         Locale.CANADA,
271                         null,
272                         TEST_DISTANCE);
273         assertEquals(null, info);
274     }
275 
testLocationInfo_NoDistance()276     public void testLocationInfo_NoDistance() {
277         BusinessContextInfo info =
278                 mInCallContactInteractions.constructLocationInfo(
279                         Locale.US,
280                         getAddressForTest(),
281                         DistanceHelper.DISTANCE_NOT_FOUND);
282         assertEquals(null, info.heading);
283     }
284 
getAddressForTest()285     private Address getAddressForTest() {
286         Address address = new Address(Locale.US);
287         address.setAddressLine(0, "Test address");
288         address.setLocality("Test locality");
289         return address;
290     }
291 
getTestCalendarWithHour(int hour)292     private Calendar getTestCalendarWithHour(int hour) {
293         Calendar calendar = Calendar.getInstance();
294         calendar.set(Calendar.HOUR_OF_DAY, hour);
295         calendar.set(Calendar.MINUTE, 0);
296         calendar.set(Calendar.SECOND, 0);
297         calendar.set(Calendar.MILLISECOND, 0);
298         return calendar;
299     }
300 
getTestCalendarWithHourAndDaysFromToday(int hour, int daysFromToday)301     private Calendar getTestCalendarWithHourAndDaysFromToday(int hour, int daysFromToday) {
302         Calendar calendar = getTestCalendarWithHour(hour);
303         calendar.add(Calendar.DATE, daysFromToday);
304         return calendar;
305     }
306 
getMultipleOpeningHours()307     private List<Pair<Calendar, Calendar>> getMultipleOpeningHours() {
308         return Arrays.asList(
309                 Pair.create(
310                     getTestCalendarWithHour(8),
311                     getTestCalendarWithHour(10)),
312                 Pair.create(
313                         getTestCalendarWithHour(12),
314                         getTestCalendarWithHour(15)),
315                 Pair.create(
316                         getTestCalendarWithHour(17),
317                         getTestCalendarWithHour(21)),
318                 Pair.create(
319                         getTestCalendarWithHourAndDaysFromToday(8, 1),
320                         getTestCalendarWithHourAndDaysFromToday(10, 1)),
321                 Pair.create(
322                         getTestCalendarWithHourAndDaysFromToday(12, 1),
323                         getTestCalendarWithHourAndDaysFromToday(8, 1)));
324     }
325 }
326