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