blob: b1ee1209d83eb82c02cf86bdcda380acf1b89380 [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
Brett Chabot84151d92019-02-27 15:37:59 -080018import static org.junit.Assert.assertEquals;
19import static org.junit.Assert.assertNull;
20
Chris Wren92af3722014-05-27 16:37:02 -040021import android.app.Notification;
Selim Cinek9acd6732018-03-23 16:39:02 -070022import android.app.Person;
Chris Wren92af3722014-05-27 16:37:02 -040023import android.os.Bundle;
Chris Wren92af3722014-05-27 16:37:02 -040024import android.test.suitebuilder.annotation.SmallTest;
25import android.text.SpannableString;
26
Brett Chabot84151d92019-02-27 15:37:59 -080027import androidx.test.runner.AndroidJUnit4;
28
29import com.android.server.UiServiceTestCase;
Chris Wren92af3722014-05-27 16:37:02 -040030
Geoffrey Pitsch0ffe7552016-08-29 10:07:40 -040031import org.junit.Test;
32import org.junit.runner.RunWith;
Chris Wren92af3722014-05-27 16:37:02 -040033
Brett Chabot84151d92019-02-27 15:37:59 -080034import java.util.ArrayList;
35import java.util.Arrays;
Jason Monk74f5e362017-12-06 08:56:33 -050036
Geoffrey Pitsch0ffe7552016-08-29 10:07:40 -040037@SmallTest
38@RunWith(AndroidJUnit4.class)
Jason Monk74f5e362017-12-06 08:56:33 -050039public class ValidateNotificationPeopleTest extends UiServiceTestCase {
Geoffrey Pitsch0ffe7552016-08-29 10:07:40 -040040
41 @Test
Chris Wren92af3722014-05-27 16:37:02 -040042 public void testNoExtra() throws Exception {
43 Bundle bundle = new Bundle();
44 String[] result = ValidateNotificationPeople.getExtraPeople(bundle);
45 assertNull("lack of extra should return null", result);
46 }
47
Geoffrey Pitsch0ffe7552016-08-29 10:07:40 -040048 @Test
Chris Wren92af3722014-05-27 16:37:02 -040049 public void testSingleString() throws Exception {
50 String[] expected = { "foobar" };
51 Bundle bundle = new Bundle();
Selim Cineke7238dd2017-12-14 17:48:32 -080052 bundle.putString(Notification.EXTRA_PEOPLE_LIST, expected[0]);
Chris Wren92af3722014-05-27 16:37:02 -040053 String[] result = ValidateNotificationPeople.getExtraPeople(bundle);
54 assertStringArrayEquals("string should be in result[0]", expected, result);
55 }
56
Geoffrey Pitsch0ffe7552016-08-29 10:07:40 -040057 @Test
Chris Wren92af3722014-05-27 16:37:02 -040058 public void testSingleCharArray() throws Exception {
59 String[] expected = { "foobar" };
60 Bundle bundle = new Bundle();
Selim Cineke7238dd2017-12-14 17:48:32 -080061 bundle.putCharArray(Notification.EXTRA_PEOPLE_LIST, expected[0].toCharArray());
Chris Wren92af3722014-05-27 16:37:02 -040062 String[] result = ValidateNotificationPeople.getExtraPeople(bundle);
63 assertStringArrayEquals("char[] should be in result[0]", expected, result);
64 }
65
Geoffrey Pitsch0ffe7552016-08-29 10:07:40 -040066 @Test
Chris Wren92af3722014-05-27 16:37:02 -040067 public void testSingleCharSequence() throws Exception {
68 String[] expected = { "foobar" };
69 Bundle bundle = new Bundle();
Selim Cineke7238dd2017-12-14 17:48:32 -080070 bundle.putCharSequence(Notification.EXTRA_PEOPLE_LIST, new SpannableString(expected[0]));
Chris Wren92af3722014-05-27 16:37:02 -040071 String[] result = ValidateNotificationPeople.getExtraPeople(bundle);
72 assertStringArrayEquals("charSequence should be in result[0]", expected, result);
73 }
74
Geoffrey Pitsch0ffe7552016-08-29 10:07:40 -040075 @Test
Chris Wren92af3722014-05-27 16:37:02 -040076 public void testStringArraySingle() throws Exception {
77 Bundle bundle = new Bundle();
78 String[] expected = { "foobar" };
Selim Cineke7238dd2017-12-14 17:48:32 -080079 bundle.putStringArray(Notification.EXTRA_PEOPLE_LIST, expected);
Chris Wren92af3722014-05-27 16:37:02 -040080 String[] result = ValidateNotificationPeople.getExtraPeople(bundle);
81 assertStringArrayEquals("wrapped string should be in result[0]", expected, result);
82 }
83
Geoffrey Pitsch0ffe7552016-08-29 10:07:40 -040084 @Test
Chris Wren92af3722014-05-27 16:37:02 -040085 public void testStringArrayMultiple() throws Exception {
86 Bundle bundle = new Bundle();
87 String[] expected = { "foo", "bar", "baz" };
Selim Cineke7238dd2017-12-14 17:48:32 -080088 bundle.putStringArray(Notification.EXTRA_PEOPLE_LIST, expected);
Chris Wren92af3722014-05-27 16:37:02 -040089 String[] result = ValidateNotificationPeople.getExtraPeople(bundle);
90 assertStringArrayEquals("testStringArrayMultiple", expected, result);
91 }
92
Geoffrey Pitsch0ffe7552016-08-29 10:07:40 -040093 @Test
Chris Wren92af3722014-05-27 16:37:02 -040094 public void testStringArrayNulls() throws Exception {
95 Bundle bundle = new Bundle();
96 String[] expected = { "foo", null, "baz" };
Selim Cineke7238dd2017-12-14 17:48:32 -080097 bundle.putStringArray(Notification.EXTRA_PEOPLE_LIST, expected);
Chris Wren92af3722014-05-27 16:37:02 -040098 String[] result = ValidateNotificationPeople.getExtraPeople(bundle);
99 assertStringArrayEquals("testStringArrayNulls", expected, result);
100 }
101
Geoffrey Pitsch0ffe7552016-08-29 10:07:40 -0400102 @Test
Chris Wren92af3722014-05-27 16:37:02 -0400103 public void testCharSequenceArrayMultiple() throws Exception {
104 Bundle bundle = new Bundle();
105 String[] expected = { "foo", "bar", "baz" };
106 CharSequence[] charSeqArray = new CharSequence[expected.length];
107 for (int i = 0; i < expected.length; i++) {
108 charSeqArray[i] = new SpannableString(expected[i]);
109 }
Selim Cineke7238dd2017-12-14 17:48:32 -0800110 bundle.putCharSequenceArray(Notification.EXTRA_PEOPLE_LIST, charSeqArray);
Chris Wren92af3722014-05-27 16:37:02 -0400111 String[] result = ValidateNotificationPeople.getExtraPeople(bundle);
112 assertStringArrayEquals("testCharSequenceArrayMultiple", expected, result);
113 }
114
Geoffrey Pitsch0ffe7552016-08-29 10:07:40 -0400115 @Test
Chris Wren92af3722014-05-27 16:37:02 -0400116 public void testMixedCharSequenceArrayList() throws Exception {
117 Bundle bundle = new Bundle();
118 String[] expected = { "foo", "bar", "baz" };
119 CharSequence[] charSeqArray = new CharSequence[expected.length];
120 for (int i = 0; i < expected.length; i++) {
121 if (i % 2 == 0) {
122 charSeqArray[i] = expected[i];
123 } else {
124 charSeqArray[i] = new SpannableString(expected[i]);
125 }
126 }
Selim Cineke7238dd2017-12-14 17:48:32 -0800127 bundle.putCharSequenceArray(Notification.EXTRA_PEOPLE_LIST, charSeqArray);
Chris Wren92af3722014-05-27 16:37:02 -0400128 String[] result = ValidateNotificationPeople.getExtraPeople(bundle);
129 assertStringArrayEquals("testMixedCharSequenceArrayList", expected, result);
130 }
131
Geoffrey Pitsch0ffe7552016-08-29 10:07:40 -0400132 @Test
Chris Wren92af3722014-05-27 16:37:02 -0400133 public void testStringArrayList() throws Exception {
134 Bundle bundle = new Bundle();
135 String[] expected = { "foo", null, "baz" };
136 final ArrayList<String> stringArrayList = new ArrayList<String>(expected.length);
137 for (int i = 0; i < expected.length; i++) {
138 stringArrayList.add(expected[i]);
139 }
Selim Cineke7238dd2017-12-14 17:48:32 -0800140 bundle.putStringArrayList(Notification.EXTRA_PEOPLE_LIST, stringArrayList);
Chris Wren92af3722014-05-27 16:37:02 -0400141 String[] result = ValidateNotificationPeople.getExtraPeople(bundle);
142 assertStringArrayEquals("testStringArrayList", expected, result);
143 }
144
Geoffrey Pitsch0ffe7552016-08-29 10:07:40 -0400145 @Test
Chris Wren92af3722014-05-27 16:37:02 -0400146 public void testCharSequenceArrayList() throws Exception {
147 Bundle bundle = new Bundle();
148 String[] expected = { "foo", "bar", "baz" };
149 final ArrayList<CharSequence> stringArrayList =
150 new ArrayList<CharSequence>(expected.length);
151 for (int i = 0; i < expected.length; i++) {
152 stringArrayList.add(new SpannableString(expected[i]));
153 }
Selim Cineke7238dd2017-12-14 17:48:32 -0800154 bundle.putCharSequenceArrayList(Notification.EXTRA_PEOPLE_LIST, stringArrayList);
Chris Wren92af3722014-05-27 16:37:02 -0400155 String[] result = ValidateNotificationPeople.getExtraPeople(bundle);
156 assertStringArrayEquals("testCharSequenceArrayList", expected, result);
157 }
158
Selim Cineke7238dd2017-12-14 17:48:32 -0800159 @Test
160 public void testPeopleArrayList() throws Exception {
161 Bundle bundle = new Bundle();
162 String[] expected = { "name:test" , "tel:1234" };
Selim Cinek9acd6732018-03-23 16:39:02 -0700163 final ArrayList<Person> arrayList =
Selim Cineke7238dd2017-12-14 17:48:32 -0800164 new ArrayList<>(expected.length);
Selim Cinek9acd6732018-03-23 16:39:02 -0700165 arrayList.add(new Person.Builder().setName("test").build());
166 arrayList.add(new Person.Builder().setUri(expected[1]).build());
Selim Cineke7238dd2017-12-14 17:48:32 -0800167 bundle.putParcelableArrayList(Notification.EXTRA_PEOPLE_LIST, arrayList);
168 String[] result = ValidateNotificationPeople.getExtraPeople(bundle);
169 assertStringArrayEquals("testPeopleArrayList", expected, result);
170 }
171
Chris Wren92af3722014-05-27 16:37:02 -0400172 private void assertStringArrayEquals(String message, String[] expected, String[] result) {
173 String expectedString = Arrays.toString(expected);
174 String resultString = Arrays.toString(result);
175 assertEquals(message + ": arrays differ", expectedString, resultString);
176 }
177}