Chris Wren | 92af372 | 2014-05-27 16:37:02 -0400 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package com.android.server.notification; |
| 17 | |
Brett Chabot | 84151d9 | 2019-02-27 15:37:59 -0800 | [diff] [blame] | 18 | import static org.junit.Assert.assertEquals; |
| 19 | import static org.junit.Assert.assertNull; |
| 20 | |
Chris Wren | 92af372 | 2014-05-27 16:37:02 -0400 | [diff] [blame] | 21 | import android.app.Notification; |
Selim Cinek | 9acd673 | 2018-03-23 16:39:02 -0700 | [diff] [blame] | 22 | import android.app.Person; |
Chris Wren | 92af372 | 2014-05-27 16:37:02 -0400 | [diff] [blame] | 23 | import android.os.Bundle; |
Chris Wren | 92af372 | 2014-05-27 16:37:02 -0400 | [diff] [blame] | 24 | import android.test.suitebuilder.annotation.SmallTest; |
| 25 | import android.text.SpannableString; |
| 26 | |
Brett Chabot | 84151d9 | 2019-02-27 15:37:59 -0800 | [diff] [blame] | 27 | import androidx.test.runner.AndroidJUnit4; |
| 28 | |
| 29 | import com.android.server.UiServiceTestCase; |
Chris Wren | 92af372 | 2014-05-27 16:37:02 -0400 | [diff] [blame] | 30 | |
Geoffrey Pitsch | 0ffe755 | 2016-08-29 10:07:40 -0400 | [diff] [blame] | 31 | import org.junit.Test; |
| 32 | import org.junit.runner.RunWith; |
Chris Wren | 92af372 | 2014-05-27 16:37:02 -0400 | [diff] [blame] | 33 | |
Brett Chabot | 84151d9 | 2019-02-27 15:37:59 -0800 | [diff] [blame] | 34 | import java.util.ArrayList; |
| 35 | import java.util.Arrays; |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 36 | |
Geoffrey Pitsch | 0ffe755 | 2016-08-29 10:07:40 -0400 | [diff] [blame] | 37 | @SmallTest |
| 38 | @RunWith(AndroidJUnit4.class) |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 39 | public class ValidateNotificationPeopleTest extends UiServiceTestCase { |
Geoffrey Pitsch | 0ffe755 | 2016-08-29 10:07:40 -0400 | [diff] [blame] | 40 | |
| 41 | @Test |
Chris Wren | 92af372 | 2014-05-27 16:37:02 -0400 | [diff] [blame] | 42 | 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 Pitsch | 0ffe755 | 2016-08-29 10:07:40 -0400 | [diff] [blame] | 48 | @Test |
Chris Wren | 92af372 | 2014-05-27 16:37:02 -0400 | [diff] [blame] | 49 | public void testSingleString() throws Exception { |
| 50 | String[] expected = { "foobar" }; |
| 51 | Bundle bundle = new Bundle(); |
Selim Cinek | e7238dd | 2017-12-14 17:48:32 -0800 | [diff] [blame] | 52 | bundle.putString(Notification.EXTRA_PEOPLE_LIST, expected[0]); |
Chris Wren | 92af372 | 2014-05-27 16:37:02 -0400 | [diff] [blame] | 53 | String[] result = ValidateNotificationPeople.getExtraPeople(bundle); |
| 54 | assertStringArrayEquals("string should be in result[0]", expected, result); |
| 55 | } |
| 56 | |
Geoffrey Pitsch | 0ffe755 | 2016-08-29 10:07:40 -0400 | [diff] [blame] | 57 | @Test |
Chris Wren | 92af372 | 2014-05-27 16:37:02 -0400 | [diff] [blame] | 58 | public void testSingleCharArray() throws Exception { |
| 59 | String[] expected = { "foobar" }; |
| 60 | Bundle bundle = new Bundle(); |
Selim Cinek | e7238dd | 2017-12-14 17:48:32 -0800 | [diff] [blame] | 61 | bundle.putCharArray(Notification.EXTRA_PEOPLE_LIST, expected[0].toCharArray()); |
Chris Wren | 92af372 | 2014-05-27 16:37:02 -0400 | [diff] [blame] | 62 | String[] result = ValidateNotificationPeople.getExtraPeople(bundle); |
| 63 | assertStringArrayEquals("char[] should be in result[0]", expected, result); |
| 64 | } |
| 65 | |
Geoffrey Pitsch | 0ffe755 | 2016-08-29 10:07:40 -0400 | [diff] [blame] | 66 | @Test |
Chris Wren | 92af372 | 2014-05-27 16:37:02 -0400 | [diff] [blame] | 67 | public void testSingleCharSequence() throws Exception { |
| 68 | String[] expected = { "foobar" }; |
| 69 | Bundle bundle = new Bundle(); |
Selim Cinek | e7238dd | 2017-12-14 17:48:32 -0800 | [diff] [blame] | 70 | bundle.putCharSequence(Notification.EXTRA_PEOPLE_LIST, new SpannableString(expected[0])); |
Chris Wren | 92af372 | 2014-05-27 16:37:02 -0400 | [diff] [blame] | 71 | String[] result = ValidateNotificationPeople.getExtraPeople(bundle); |
| 72 | assertStringArrayEquals("charSequence should be in result[0]", expected, result); |
| 73 | } |
| 74 | |
Geoffrey Pitsch | 0ffe755 | 2016-08-29 10:07:40 -0400 | [diff] [blame] | 75 | @Test |
Chris Wren | 92af372 | 2014-05-27 16:37:02 -0400 | [diff] [blame] | 76 | public void testStringArraySingle() throws Exception { |
| 77 | Bundle bundle = new Bundle(); |
| 78 | String[] expected = { "foobar" }; |
Selim Cinek | e7238dd | 2017-12-14 17:48:32 -0800 | [diff] [blame] | 79 | bundle.putStringArray(Notification.EXTRA_PEOPLE_LIST, expected); |
Chris Wren | 92af372 | 2014-05-27 16:37:02 -0400 | [diff] [blame] | 80 | String[] result = ValidateNotificationPeople.getExtraPeople(bundle); |
| 81 | assertStringArrayEquals("wrapped string should be in result[0]", expected, result); |
| 82 | } |
| 83 | |
Geoffrey Pitsch | 0ffe755 | 2016-08-29 10:07:40 -0400 | [diff] [blame] | 84 | @Test |
Chris Wren | 92af372 | 2014-05-27 16:37:02 -0400 | [diff] [blame] | 85 | public void testStringArrayMultiple() throws Exception { |
| 86 | Bundle bundle = new Bundle(); |
| 87 | String[] expected = { "foo", "bar", "baz" }; |
Selim Cinek | e7238dd | 2017-12-14 17:48:32 -0800 | [diff] [blame] | 88 | bundle.putStringArray(Notification.EXTRA_PEOPLE_LIST, expected); |
Chris Wren | 92af372 | 2014-05-27 16:37:02 -0400 | [diff] [blame] | 89 | String[] result = ValidateNotificationPeople.getExtraPeople(bundle); |
| 90 | assertStringArrayEquals("testStringArrayMultiple", expected, result); |
| 91 | } |
| 92 | |
Geoffrey Pitsch | 0ffe755 | 2016-08-29 10:07:40 -0400 | [diff] [blame] | 93 | @Test |
Chris Wren | 92af372 | 2014-05-27 16:37:02 -0400 | [diff] [blame] | 94 | public void testStringArrayNulls() throws Exception { |
| 95 | Bundle bundle = new Bundle(); |
| 96 | String[] expected = { "foo", null, "baz" }; |
Selim Cinek | e7238dd | 2017-12-14 17:48:32 -0800 | [diff] [blame] | 97 | bundle.putStringArray(Notification.EXTRA_PEOPLE_LIST, expected); |
Chris Wren | 92af372 | 2014-05-27 16:37:02 -0400 | [diff] [blame] | 98 | String[] result = ValidateNotificationPeople.getExtraPeople(bundle); |
| 99 | assertStringArrayEquals("testStringArrayNulls", expected, result); |
| 100 | } |
| 101 | |
Geoffrey Pitsch | 0ffe755 | 2016-08-29 10:07:40 -0400 | [diff] [blame] | 102 | @Test |
Chris Wren | 92af372 | 2014-05-27 16:37:02 -0400 | [diff] [blame] | 103 | 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 Cinek | e7238dd | 2017-12-14 17:48:32 -0800 | [diff] [blame] | 110 | bundle.putCharSequenceArray(Notification.EXTRA_PEOPLE_LIST, charSeqArray); |
Chris Wren | 92af372 | 2014-05-27 16:37:02 -0400 | [diff] [blame] | 111 | String[] result = ValidateNotificationPeople.getExtraPeople(bundle); |
| 112 | assertStringArrayEquals("testCharSequenceArrayMultiple", expected, result); |
| 113 | } |
| 114 | |
Geoffrey Pitsch | 0ffe755 | 2016-08-29 10:07:40 -0400 | [diff] [blame] | 115 | @Test |
Chris Wren | 92af372 | 2014-05-27 16:37:02 -0400 | [diff] [blame] | 116 | 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 Cinek | e7238dd | 2017-12-14 17:48:32 -0800 | [diff] [blame] | 127 | bundle.putCharSequenceArray(Notification.EXTRA_PEOPLE_LIST, charSeqArray); |
Chris Wren | 92af372 | 2014-05-27 16:37:02 -0400 | [diff] [blame] | 128 | String[] result = ValidateNotificationPeople.getExtraPeople(bundle); |
| 129 | assertStringArrayEquals("testMixedCharSequenceArrayList", expected, result); |
| 130 | } |
| 131 | |
Geoffrey Pitsch | 0ffe755 | 2016-08-29 10:07:40 -0400 | [diff] [blame] | 132 | @Test |
Chris Wren | 92af372 | 2014-05-27 16:37:02 -0400 | [diff] [blame] | 133 | 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 Cinek | e7238dd | 2017-12-14 17:48:32 -0800 | [diff] [blame] | 140 | bundle.putStringArrayList(Notification.EXTRA_PEOPLE_LIST, stringArrayList); |
Chris Wren | 92af372 | 2014-05-27 16:37:02 -0400 | [diff] [blame] | 141 | String[] result = ValidateNotificationPeople.getExtraPeople(bundle); |
| 142 | assertStringArrayEquals("testStringArrayList", expected, result); |
| 143 | } |
| 144 | |
Geoffrey Pitsch | 0ffe755 | 2016-08-29 10:07:40 -0400 | [diff] [blame] | 145 | @Test |
Chris Wren | 92af372 | 2014-05-27 16:37:02 -0400 | [diff] [blame] | 146 | 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 Cinek | e7238dd | 2017-12-14 17:48:32 -0800 | [diff] [blame] | 154 | bundle.putCharSequenceArrayList(Notification.EXTRA_PEOPLE_LIST, stringArrayList); |
Chris Wren | 92af372 | 2014-05-27 16:37:02 -0400 | [diff] [blame] | 155 | String[] result = ValidateNotificationPeople.getExtraPeople(bundle); |
| 156 | assertStringArrayEquals("testCharSequenceArrayList", expected, result); |
| 157 | } |
| 158 | |
Selim Cinek | e7238dd | 2017-12-14 17:48:32 -0800 | [diff] [blame] | 159 | @Test |
| 160 | public void testPeopleArrayList() throws Exception { |
| 161 | Bundle bundle = new Bundle(); |
| 162 | String[] expected = { "name:test" , "tel:1234" }; |
Selim Cinek | 9acd673 | 2018-03-23 16:39:02 -0700 | [diff] [blame] | 163 | final ArrayList<Person> arrayList = |
Selim Cinek | e7238dd | 2017-12-14 17:48:32 -0800 | [diff] [blame] | 164 | new ArrayList<>(expected.length); |
Selim Cinek | 9acd673 | 2018-03-23 16:39:02 -0700 | [diff] [blame] | 165 | arrayList.add(new Person.Builder().setName("test").build()); |
| 166 | arrayList.add(new Person.Builder().setUri(expected[1]).build()); |
Selim Cinek | e7238dd | 2017-12-14 17:48:32 -0800 | [diff] [blame] | 167 | bundle.putParcelableArrayList(Notification.EXTRA_PEOPLE_LIST, arrayList); |
| 168 | String[] result = ValidateNotificationPeople.getExtraPeople(bundle); |
| 169 | assertStringArrayEquals("testPeopleArrayList", expected, result); |
| 170 | } |
| 171 | |
Chris Wren | 92af372 | 2014-05-27 16:37:02 -0400 | [diff] [blame] | 172 | 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 | } |