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; |
| 19 | import android.os.Bundle; |
Geoffrey Pitsch | 0ffe755 | 2016-08-29 10:07:40 -0400 | [diff] [blame] | 20 | import android.support.test.runner.AndroidJUnit4; |
Chris Wren | 92af372 | 2014-05-27 16:37:02 -0400 | [diff] [blame] | 21 | import android.test.suitebuilder.annotation.SmallTest; |
| 22 | import android.text.SpannableString; |
| 23 | |
| 24 | import java.util.ArrayList; |
| 25 | import java.util.Arrays; |
| 26 | |
Geoffrey Pitsch | 0ffe755 | 2016-08-29 10:07:40 -0400 | [diff] [blame] | 27 | import org.junit.Test; |
| 28 | import org.junit.runner.RunWith; |
Chris Wren | 92af372 | 2014-05-27 16:37:02 -0400 | [diff] [blame] | 29 | |
Geoffrey Pitsch | 0ffe755 | 2016-08-29 10:07:40 -0400 | [diff] [blame] | 30 | import static org.junit.Assert.assertNull; |
| 31 | import static org.junit.Assert.assertEquals; |
| 32 | |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 33 | import com.android.server.UiServiceTestCase; |
| 34 | |
Geoffrey Pitsch | 0ffe755 | 2016-08-29 10:07:40 -0400 | [diff] [blame] | 35 | @SmallTest |
| 36 | @RunWith(AndroidJUnit4.class) |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 37 | public class ValidateNotificationPeopleTest extends UiServiceTestCase { |
Geoffrey Pitsch | 0ffe755 | 2016-08-29 10:07:40 -0400 | [diff] [blame] | 38 | |
| 39 | @Test |
Chris Wren | 92af372 | 2014-05-27 16:37:02 -0400 | [diff] [blame] | 40 | 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 Pitsch | 0ffe755 | 2016-08-29 10:07:40 -0400 | [diff] [blame] | 46 | @Test |
Chris Wren | 92af372 | 2014-05-27 16:37:02 -0400 | [diff] [blame] | 47 | 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 Pitsch | 0ffe755 | 2016-08-29 10:07:40 -0400 | [diff] [blame] | 55 | @Test |
Chris Wren | 92af372 | 2014-05-27 16:37:02 -0400 | [diff] [blame] | 56 | 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 Pitsch | 0ffe755 | 2016-08-29 10:07:40 -0400 | [diff] [blame] | 64 | @Test |
Chris Wren | 92af372 | 2014-05-27 16:37:02 -0400 | [diff] [blame] | 65 | 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 Pitsch | 0ffe755 | 2016-08-29 10:07:40 -0400 | [diff] [blame] | 73 | @Test |
Chris Wren | 92af372 | 2014-05-27 16:37:02 -0400 | [diff] [blame] | 74 | 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 Pitsch | 0ffe755 | 2016-08-29 10:07:40 -0400 | [diff] [blame] | 82 | @Test |
Chris Wren | 92af372 | 2014-05-27 16:37:02 -0400 | [diff] [blame] | 83 | 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 Pitsch | 0ffe755 | 2016-08-29 10:07:40 -0400 | [diff] [blame] | 91 | @Test |
Chris Wren | 92af372 | 2014-05-27 16:37:02 -0400 | [diff] [blame] | 92 | 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 Pitsch | 0ffe755 | 2016-08-29 10:07:40 -0400 | [diff] [blame] | 100 | @Test |
Chris Wren | 92af372 | 2014-05-27 16:37:02 -0400 | [diff] [blame] | 101 | 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 Pitsch | 0ffe755 | 2016-08-29 10:07:40 -0400 | [diff] [blame] | 113 | @Test |
Chris Wren | 92af372 | 2014-05-27 16:37:02 -0400 | [diff] [blame] | 114 | 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 Pitsch | 0ffe755 | 2016-08-29 10:07:40 -0400 | [diff] [blame] | 130 | @Test |
Chris Wren | 92af372 | 2014-05-27 16:37:02 -0400 | [diff] [blame] | 131 | 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 Pitsch | 0ffe755 | 2016-08-29 10:07:40 -0400 | [diff] [blame] | 143 | @Test |
Chris Wren | 92af372 | 2014-05-27 16:37:02 -0400 | [diff] [blame] | 144 | 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 | } |