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 */
16package com.android.server.notification;
17
18import android.app.Notification;
19import android.os.Bundle;
20import android.test.AndroidTestCase;
21import android.test.suitebuilder.annotation.SmallTest;
22import android.text.SpannableString;
23
24import java.util.ArrayList;
25import java.util.Arrays;
26
27public class ValidateNotificationPeopleTest extends AndroidTestCase {
28
29    @SmallTest
30    public void testNoExtra() throws Exception {
31        Bundle bundle = new Bundle();
32        String[] result = ValidateNotificationPeople.getExtraPeople(bundle);
33        assertNull("lack of extra should return null", result);
34    }
35
36    @SmallTest
37    public void testSingleString() throws Exception {
38        String[] expected = { "foobar" };
39        Bundle bundle = new Bundle();
40        bundle.putString(Notification.EXTRA_PEOPLE, expected[0]);
41        String[] result = ValidateNotificationPeople.getExtraPeople(bundle);
42        assertStringArrayEquals("string should be in result[0]", expected, result);
43    }
44
45    @SmallTest
46    public void testSingleCharArray() throws Exception {
47        String[] expected = { "foobar" };
48        Bundle bundle = new Bundle();
49        bundle.putCharArray(Notification.EXTRA_PEOPLE, expected[0].toCharArray());
50        String[] result = ValidateNotificationPeople.getExtraPeople(bundle);
51        assertStringArrayEquals("char[] should be in result[0]", expected, result);
52    }
53
54    @SmallTest
55    public void testSingleCharSequence() throws Exception {
56        String[] expected = { "foobar" };
57        Bundle bundle = new Bundle();
58        bundle.putCharSequence(Notification.EXTRA_PEOPLE, new SpannableString(expected[0]));
59        String[] result = ValidateNotificationPeople.getExtraPeople(bundle);
60        assertStringArrayEquals("charSequence should be in result[0]", expected, result);
61    }
62
63    @SmallTest
64    public void testStringArraySingle() throws Exception {
65        Bundle bundle = new Bundle();
66        String[] expected = { "foobar" };
67        bundle.putStringArray(Notification.EXTRA_PEOPLE, expected);
68        String[] result = ValidateNotificationPeople.getExtraPeople(bundle);
69        assertStringArrayEquals("wrapped string should be in result[0]", expected, result);
70    }
71
72    @SmallTest
73    public void testStringArrayMultiple() throws Exception {
74        Bundle bundle = new Bundle();
75        String[] expected = { "foo", "bar", "baz" };
76        bundle.putStringArray(Notification.EXTRA_PEOPLE, expected);
77        String[] result = ValidateNotificationPeople.getExtraPeople(bundle);
78        assertStringArrayEquals("testStringArrayMultiple", expected, result);
79    }
80
81    @SmallTest
82    public void testStringArrayNulls() throws Exception {
83        Bundle bundle = new Bundle();
84        String[] expected = { "foo", null, "baz" };
85        bundle.putStringArray(Notification.EXTRA_PEOPLE, expected);
86        String[] result = ValidateNotificationPeople.getExtraPeople(bundle);
87        assertStringArrayEquals("testStringArrayNulls", expected, result);
88    }
89
90    @SmallTest
91    public void testCharSequenceArrayMultiple() throws Exception {
92        Bundle bundle = new Bundle();
93        String[] expected = { "foo", "bar", "baz" };
94        CharSequence[] charSeqArray = new CharSequence[expected.length];
95        for (int i = 0; i < expected.length; i++) {
96            charSeqArray[i] = new SpannableString(expected[i]);
97        }
98        bundle.putCharSequenceArray(Notification.EXTRA_PEOPLE, charSeqArray);
99        String[] result = ValidateNotificationPeople.getExtraPeople(bundle);
100        assertStringArrayEquals("testCharSequenceArrayMultiple", expected, result);
101    }
102
103    @SmallTest
104    public void testMixedCharSequenceArrayList() throws Exception {
105        Bundle bundle = new Bundle();
106        String[] expected = { "foo", "bar", "baz" };
107        CharSequence[] charSeqArray = new CharSequence[expected.length];
108        for (int i = 0; i < expected.length; i++) {
109            if (i % 2 == 0) {
110                charSeqArray[i] = expected[i];
111            } else {
112                charSeqArray[i] = new SpannableString(expected[i]);
113            }
114        }
115        bundle.putCharSequenceArray(Notification.EXTRA_PEOPLE, charSeqArray);
116        String[] result = ValidateNotificationPeople.getExtraPeople(bundle);
117        assertStringArrayEquals("testMixedCharSequenceArrayList", expected, result);
118    }
119
120    @SmallTest
121    public void testStringArrayList() throws Exception {
122        Bundle bundle = new Bundle();
123        String[] expected = { "foo", null, "baz" };
124        final ArrayList<String> stringArrayList = new ArrayList<String>(expected.length);
125        for (int i = 0; i < expected.length; i++) {
126            stringArrayList.add(expected[i]);
127        }
128        bundle.putStringArrayList(Notification.EXTRA_PEOPLE, stringArrayList);
129        String[] result = ValidateNotificationPeople.getExtraPeople(bundle);
130        assertStringArrayEquals("testStringArrayList", expected, result);
131    }
132
133    @SmallTest
134    public void testCharSequenceArrayList() throws Exception {
135        Bundle bundle = new Bundle();
136        String[] expected = { "foo", "bar", "baz" };
137        final ArrayList<CharSequence> stringArrayList =
138                new ArrayList<CharSequence>(expected.length);
139        for (int i = 0; i < expected.length; i++) {
140            stringArrayList.add(new SpannableString(expected[i]));
141        }
142        bundle.putCharSequenceArrayList(Notification.EXTRA_PEOPLE, stringArrayList);
143        String[] result = ValidateNotificationPeople.getExtraPeople(bundle);
144        assertStringArrayEquals("testCharSequenceArrayList", expected, result);
145    }
146
147    private void assertStringArrayEquals(String message, String[] expected, String[] result) {
148        String expectedString = Arrays.toString(expected);
149        String resultString = Arrays.toString(result);
150        assertEquals(message + ": arrays differ", expectedString, resultString);
151    }
152}
153