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