blob: 025b11a704143f7d855bb1f585567f5a023c8643 [file] [log] [blame]
Chris Wren92af3722014-05-27 16:37:02 -04001/*
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 */
16package com.android.server.notification;
17
18import android.app.Notification;
Selim Cinek9acd6732018-03-23 16:39:02 -070019import android.app.Person;
Chris Wren92af3722014-05-27 16:37:02 -040020import android.os.Bundle;
Geoffrey Pitsch0ffe7552016-08-29 10:07:40 -040021import android.support.test.runner.AndroidJUnit4;
Chris Wren92af3722014-05-27 16:37:02 -040022import android.test.suitebuilder.annotation.SmallTest;
23import android.text.SpannableString;
24
25import java.util.ArrayList;
26import java.util.Arrays;
27
Geoffrey Pitsch0ffe7552016-08-29 10:07:40 -040028import org.junit.Test;
29import org.junit.runner.RunWith;
Chris Wren92af3722014-05-27 16:37:02 -040030
Geoffrey Pitsch0ffe7552016-08-29 10:07:40 -040031import static org.junit.Assert.assertNull;
32import static org.junit.Assert.assertEquals;
33
Jason Monk74f5e362017-12-06 08:56:33 -050034import com.android.server.UiServiceTestCase;
35
Geoffrey Pitsch0ffe7552016-08-29 10:07:40 -040036@SmallTest
37@RunWith(AndroidJUnit4.class)
Jason Monk74f5e362017-12-06 08:56:33 -050038public class ValidateNotificationPeopleTest extends UiServiceTestCase {
Geoffrey Pitsch0ffe7552016-08-29 10:07:40 -040039
40 @Test
Chris Wren92af3722014-05-27 16:37:02 -040041 public void testNoExtra() throws Exception {
42 Bundle bundle = new Bundle();
43 String[] result = ValidateNotificationPeople.getExtraPeople(bundle);
44 assertNull("lack of extra should return null", result);
45 }
46
Geoffrey Pitsch0ffe7552016-08-29 10:07:40 -040047 @Test
Chris Wren92af3722014-05-27 16:37:02 -040048 public void testSingleString() throws Exception {
49 String[] expected = { "foobar" };
50 Bundle bundle = new Bundle();
Selim Cineke7238dd2017-12-14 17:48:32 -080051 bundle.putString(Notification.EXTRA_PEOPLE_LIST, expected[0]);
Chris Wren92af3722014-05-27 16:37:02 -040052 String[] result = ValidateNotificationPeople.getExtraPeople(bundle);
53 assertStringArrayEquals("string should be in result[0]", expected, result);
54 }
55
Geoffrey Pitsch0ffe7552016-08-29 10:07:40 -040056 @Test
Chris Wren92af3722014-05-27 16:37:02 -040057 public void testSingleCharArray() throws Exception {
58 String[] expected = { "foobar" };
59 Bundle bundle = new Bundle();
Selim Cineke7238dd2017-12-14 17:48:32 -080060 bundle.putCharArray(Notification.EXTRA_PEOPLE_LIST, expected[0].toCharArray());
Chris Wren92af3722014-05-27 16:37:02 -040061 String[] result = ValidateNotificationPeople.getExtraPeople(bundle);
62 assertStringArrayEquals("char[] should be in result[0]", expected, result);
63 }
64
Geoffrey Pitsch0ffe7552016-08-29 10:07:40 -040065 @Test
Chris Wren92af3722014-05-27 16:37:02 -040066 public void testSingleCharSequence() throws Exception {
67 String[] expected = { "foobar" };
68 Bundle bundle = new Bundle();
Selim Cineke7238dd2017-12-14 17:48:32 -080069 bundle.putCharSequence(Notification.EXTRA_PEOPLE_LIST, new SpannableString(expected[0]));
Chris Wren92af3722014-05-27 16:37:02 -040070 String[] result = ValidateNotificationPeople.getExtraPeople(bundle);
71 assertStringArrayEquals("charSequence should be in result[0]", expected, result);
72 }
73
Geoffrey Pitsch0ffe7552016-08-29 10:07:40 -040074 @Test
Chris Wren92af3722014-05-27 16:37:02 -040075 public void testStringArraySingle() throws Exception {
76 Bundle bundle = new Bundle();
77 String[] expected = { "foobar" };
Selim Cineke7238dd2017-12-14 17:48:32 -080078 bundle.putStringArray(Notification.EXTRA_PEOPLE_LIST, expected);
Chris Wren92af3722014-05-27 16:37:02 -040079 String[] result = ValidateNotificationPeople.getExtraPeople(bundle);
80 assertStringArrayEquals("wrapped string should be in result[0]", expected, result);
81 }
82
Geoffrey Pitsch0ffe7552016-08-29 10:07:40 -040083 @Test
Chris Wren92af3722014-05-27 16:37:02 -040084 public void testStringArrayMultiple() throws Exception {
85 Bundle bundle = new Bundle();
86 String[] expected = { "foo", "bar", "baz" };
Selim Cineke7238dd2017-12-14 17:48:32 -080087 bundle.putStringArray(Notification.EXTRA_PEOPLE_LIST, expected);
Chris Wren92af3722014-05-27 16:37:02 -040088 String[] result = ValidateNotificationPeople.getExtraPeople(bundle);
89 assertStringArrayEquals("testStringArrayMultiple", expected, result);
90 }
91
Geoffrey Pitsch0ffe7552016-08-29 10:07:40 -040092 @Test
Chris Wren92af3722014-05-27 16:37:02 -040093 public void testStringArrayNulls() throws Exception {
94 Bundle bundle = new Bundle();
95 String[] expected = { "foo", null, "baz" };
Selim Cineke7238dd2017-12-14 17:48:32 -080096 bundle.putStringArray(Notification.EXTRA_PEOPLE_LIST, expected);
Chris Wren92af3722014-05-27 16:37:02 -040097 String[] result = ValidateNotificationPeople.getExtraPeople(bundle);
98 assertStringArrayEquals("testStringArrayNulls", expected, result);
99 }
100
Geoffrey Pitsch0ffe7552016-08-29 10:07:40 -0400101 @Test
Chris Wren92af3722014-05-27 16:37:02 -0400102 public void testCharSequenceArrayMultiple() throws Exception {
103 Bundle bundle = new Bundle();
104 String[] expected = { "foo", "bar", "baz" };
105 CharSequence[] charSeqArray = new CharSequence[expected.length];
106 for (int i = 0; i < expected.length; i++) {
107 charSeqArray[i] = new SpannableString(expected[i]);
108 }
Selim Cineke7238dd2017-12-14 17:48:32 -0800109 bundle.putCharSequenceArray(Notification.EXTRA_PEOPLE_LIST, charSeqArray);
Chris Wren92af3722014-05-27 16:37:02 -0400110 String[] result = ValidateNotificationPeople.getExtraPeople(bundle);
111 assertStringArrayEquals("testCharSequenceArrayMultiple", expected, result);
112 }
113
Geoffrey Pitsch0ffe7552016-08-29 10:07:40 -0400114 @Test
Chris Wren92af3722014-05-27 16:37:02 -0400115 public void testMixedCharSequenceArrayList() throws Exception {
116 Bundle bundle = new Bundle();
117 String[] expected = { "foo", "bar", "baz" };
118 CharSequence[] charSeqArray = new CharSequence[expected.length];
119 for (int i = 0; i < expected.length; i++) {
120 if (i % 2 == 0) {
121 charSeqArray[i] = expected[i];
122 } else {
123 charSeqArray[i] = new SpannableString(expected[i]);
124 }
125 }
Selim Cineke7238dd2017-12-14 17:48:32 -0800126 bundle.putCharSequenceArray(Notification.EXTRA_PEOPLE_LIST, charSeqArray);
Chris Wren92af3722014-05-27 16:37:02 -0400127 String[] result = ValidateNotificationPeople.getExtraPeople(bundle);
128 assertStringArrayEquals("testMixedCharSequenceArrayList", expected, result);
129 }
130
Geoffrey Pitsch0ffe7552016-08-29 10:07:40 -0400131 @Test
Chris Wren92af3722014-05-27 16:37:02 -0400132 public void testStringArrayList() throws Exception {
133 Bundle bundle = new Bundle();
134 String[] expected = { "foo", null, "baz" };
135 final ArrayList<String> stringArrayList = new ArrayList<String>(expected.length);
136 for (int i = 0; i < expected.length; i++) {
137 stringArrayList.add(expected[i]);
138 }
Selim Cineke7238dd2017-12-14 17:48:32 -0800139 bundle.putStringArrayList(Notification.EXTRA_PEOPLE_LIST, stringArrayList);
Chris Wren92af3722014-05-27 16:37:02 -0400140 String[] result = ValidateNotificationPeople.getExtraPeople(bundle);
141 assertStringArrayEquals("testStringArrayList", expected, result);
142 }
143
Geoffrey Pitsch0ffe7552016-08-29 10:07:40 -0400144 @Test
Chris Wren92af3722014-05-27 16:37:02 -0400145 public void testCharSequenceArrayList() throws Exception {
146 Bundle bundle = new Bundle();
147 String[] expected = { "foo", "bar", "baz" };
148 final ArrayList<CharSequence> stringArrayList =
149 new ArrayList<CharSequence>(expected.length);
150 for (int i = 0; i < expected.length; i++) {
151 stringArrayList.add(new SpannableString(expected[i]));
152 }
Selim Cineke7238dd2017-12-14 17:48:32 -0800153 bundle.putCharSequenceArrayList(Notification.EXTRA_PEOPLE_LIST, stringArrayList);
Chris Wren92af3722014-05-27 16:37:02 -0400154 String[] result = ValidateNotificationPeople.getExtraPeople(bundle);
155 assertStringArrayEquals("testCharSequenceArrayList", expected, result);
156 }
157
Selim Cineke7238dd2017-12-14 17:48:32 -0800158 @Test
159 public void testPeopleArrayList() throws Exception {
160 Bundle bundle = new Bundle();
161 String[] expected = { "name:test" , "tel:1234" };
Selim Cinek9acd6732018-03-23 16:39:02 -0700162 final ArrayList<Person> arrayList =
Selim Cineke7238dd2017-12-14 17:48:32 -0800163 new ArrayList<>(expected.length);
Selim Cinek9acd6732018-03-23 16:39:02 -0700164 arrayList.add(new Person.Builder().setName("test").build());
165 arrayList.add(new Person.Builder().setUri(expected[1]).build());
Selim Cineke7238dd2017-12-14 17:48:32 -0800166 bundle.putParcelableArrayList(Notification.EXTRA_PEOPLE_LIST, arrayList);
167 String[] result = ValidateNotificationPeople.getExtraPeople(bundle);
168 assertStringArrayEquals("testPeopleArrayList", expected, result);
169 }
170
Chris Wren92af3722014-05-27 16:37:02 -0400171 private void assertStringArrayEquals(String message, String[] expected, String[] result) {
172 String expectedString = Arrays.toString(expected);
173 String resultString = Arrays.toString(result);
174 assertEquals(message + ": arrays differ", expectedString, resultString);
175 }
176}