blob: 58f0ded9b9a13fee3e62ac7157d118976103bff1 [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;
19import android.os.Bundle;
Geoffrey Pitsch0ffe7552016-08-29 10:07:40 -040020import android.support.test.runner.AndroidJUnit4;
Chris Wren92af3722014-05-27 16:37:02 -040021import android.test.suitebuilder.annotation.SmallTest;
22import android.text.SpannableString;
23
24import java.util.ArrayList;
25import java.util.Arrays;
26
Geoffrey Pitsch0ffe7552016-08-29 10:07:40 -040027import org.junit.Test;
28import org.junit.runner.RunWith;
Chris Wren92af3722014-05-27 16:37:02 -040029
Geoffrey Pitsch0ffe7552016-08-29 10:07:40 -040030import static org.junit.Assert.assertNull;
31import static org.junit.Assert.assertEquals;
32
Jason Monk74f5e362017-12-06 08:56:33 -050033import com.android.server.UiServiceTestCase;
34
Geoffrey Pitsch0ffe7552016-08-29 10:07:40 -040035@SmallTest
36@RunWith(AndroidJUnit4.class)
Jason Monk74f5e362017-12-06 08:56:33 -050037public class ValidateNotificationPeopleTest extends UiServiceTestCase {
Geoffrey Pitsch0ffe7552016-08-29 10:07:40 -040038
39 @Test
Chris Wren92af3722014-05-27 16:37:02 -040040 public void testNoExtra() throws Exception {
41 Bundle bundle = new Bundle();
42 String[] result = ValidateNotificationPeople.getExtraPeople(bundle);
43 assertNull("lack of extra should return null", result);
44 }
45
Geoffrey Pitsch0ffe7552016-08-29 10:07:40 -040046 @Test
Chris Wren92af3722014-05-27 16:37:02 -040047 public void testSingleString() throws Exception {
48 String[] expected = { "foobar" };
49 Bundle bundle = new Bundle();
50 bundle.putString(Notification.EXTRA_PEOPLE, expected[0]);
51 String[] result = ValidateNotificationPeople.getExtraPeople(bundle);
52 assertStringArrayEquals("string should be in result[0]", expected, result);
53 }
54
Geoffrey Pitsch0ffe7552016-08-29 10:07:40 -040055 @Test
Chris Wren92af3722014-05-27 16:37:02 -040056 public void testSingleCharArray() throws Exception {
57 String[] expected = { "foobar" };
58 Bundle bundle = new Bundle();
59 bundle.putCharArray(Notification.EXTRA_PEOPLE, expected[0].toCharArray());
60 String[] result = ValidateNotificationPeople.getExtraPeople(bundle);
61 assertStringArrayEquals("char[] should be in result[0]", expected, result);
62 }
63
Geoffrey Pitsch0ffe7552016-08-29 10:07:40 -040064 @Test
Chris Wren92af3722014-05-27 16:37:02 -040065 public void testSingleCharSequence() throws Exception {
66 String[] expected = { "foobar" };
67 Bundle bundle = new Bundle();
68 bundle.putCharSequence(Notification.EXTRA_PEOPLE, new SpannableString(expected[0]));
69 String[] result = ValidateNotificationPeople.getExtraPeople(bundle);
70 assertStringArrayEquals("charSequence should be in result[0]", expected, result);
71 }
72
Geoffrey Pitsch0ffe7552016-08-29 10:07:40 -040073 @Test
Chris Wren92af3722014-05-27 16:37:02 -040074 public void testStringArraySingle() throws Exception {
75 Bundle bundle = new Bundle();
76 String[] expected = { "foobar" };
77 bundle.putStringArray(Notification.EXTRA_PEOPLE, expected);
78 String[] result = ValidateNotificationPeople.getExtraPeople(bundle);
79 assertStringArrayEquals("wrapped string should be in result[0]", expected, result);
80 }
81
Geoffrey Pitsch0ffe7552016-08-29 10:07:40 -040082 @Test
Chris Wren92af3722014-05-27 16:37:02 -040083 public void testStringArrayMultiple() throws Exception {
84 Bundle bundle = new Bundle();
85 String[] expected = { "foo", "bar", "baz" };
86 bundle.putStringArray(Notification.EXTRA_PEOPLE, expected);
87 String[] result = ValidateNotificationPeople.getExtraPeople(bundle);
88 assertStringArrayEquals("testStringArrayMultiple", expected, result);
89 }
90
Geoffrey Pitsch0ffe7552016-08-29 10:07:40 -040091 @Test
Chris Wren92af3722014-05-27 16:37:02 -040092 public void testStringArrayNulls() throws Exception {
93 Bundle bundle = new Bundle();
94 String[] expected = { "foo", null, "baz" };
95 bundle.putStringArray(Notification.EXTRA_PEOPLE, expected);
96 String[] result = ValidateNotificationPeople.getExtraPeople(bundle);
97 assertStringArrayEquals("testStringArrayNulls", expected, result);
98 }
99
Geoffrey Pitsch0ffe7552016-08-29 10:07:40 -0400100 @Test
Chris Wren92af3722014-05-27 16:37:02 -0400101 public void testCharSequenceArrayMultiple() throws Exception {
102 Bundle bundle = new Bundle();
103 String[] expected = { "foo", "bar", "baz" };
104 CharSequence[] charSeqArray = new CharSequence[expected.length];
105 for (int i = 0; i < expected.length; i++) {
106 charSeqArray[i] = new SpannableString(expected[i]);
107 }
108 bundle.putCharSequenceArray(Notification.EXTRA_PEOPLE, charSeqArray);
109 String[] result = ValidateNotificationPeople.getExtraPeople(bundle);
110 assertStringArrayEquals("testCharSequenceArrayMultiple", expected, result);
111 }
112
Geoffrey Pitsch0ffe7552016-08-29 10:07:40 -0400113 @Test
Chris Wren92af3722014-05-27 16:37:02 -0400114 public void testMixedCharSequenceArrayList() throws Exception {
115 Bundle bundle = new Bundle();
116 String[] expected = { "foo", "bar", "baz" };
117 CharSequence[] charSeqArray = new CharSequence[expected.length];
118 for (int i = 0; i < expected.length; i++) {
119 if (i % 2 == 0) {
120 charSeqArray[i] = expected[i];
121 } else {
122 charSeqArray[i] = new SpannableString(expected[i]);
123 }
124 }
125 bundle.putCharSequenceArray(Notification.EXTRA_PEOPLE, charSeqArray);
126 String[] result = ValidateNotificationPeople.getExtraPeople(bundle);
127 assertStringArrayEquals("testMixedCharSequenceArrayList", expected, result);
128 }
129
Geoffrey Pitsch0ffe7552016-08-29 10:07:40 -0400130 @Test
Chris Wren92af3722014-05-27 16:37:02 -0400131 public void testStringArrayList() throws Exception {
132 Bundle bundle = new Bundle();
133 String[] expected = { "foo", null, "baz" };
134 final ArrayList<String> stringArrayList = new ArrayList<String>(expected.length);
135 for (int i = 0; i < expected.length; i++) {
136 stringArrayList.add(expected[i]);
137 }
138 bundle.putStringArrayList(Notification.EXTRA_PEOPLE, stringArrayList);
139 String[] result = ValidateNotificationPeople.getExtraPeople(bundle);
140 assertStringArrayEquals("testStringArrayList", expected, result);
141 }
142
Geoffrey Pitsch0ffe7552016-08-29 10:07:40 -0400143 @Test
Chris Wren92af3722014-05-27 16:37:02 -0400144 public void testCharSequenceArrayList() throws Exception {
145 Bundle bundle = new Bundle();
146 String[] expected = { "foo", "bar", "baz" };
147 final ArrayList<CharSequence> stringArrayList =
148 new ArrayList<CharSequence>(expected.length);
149 for (int i = 0; i < expected.length; i++) {
150 stringArrayList.add(new SpannableString(expected[i]));
151 }
152 bundle.putCharSequenceArrayList(Notification.EXTRA_PEOPLE, stringArrayList);
153 String[] result = ValidateNotificationPeople.getExtraPeople(bundle);
154 assertStringArrayEquals("testCharSequenceArrayList", expected, result);
155 }
156
157 private void assertStringArrayEquals(String message, String[] expected, String[] result) {
158 String expectedString = Arrays.toString(expected);
159 String resultString = Arrays.toString(result);
160 assertEquals(message + ": arrays differ", expectedString, resultString);
161 }
162}