1/*
2 * Copyright (C) 2017 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
17package com.android.server.notification;
18
19import static android.app.NotificationManager.IMPORTANCE_DEFAULT;
20
21import static junit.framework.Assert.assertEquals;
22
23import android.app.NotificationChannel;
24import android.os.Parcel;
25import android.support.test.runner.AndroidJUnit4;
26import android.test.suitebuilder.annotation.SmallTest;
27
28import com.android.internal.util.FastXmlSerializer;
29import com.android.server.UiServiceTestCase;
30
31import org.junit.Test;
32import org.junit.runner.RunWith;
33import org.xmlpull.v1.XmlSerializer;
34
35import java.io.BufferedOutputStream;
36import java.io.ByteArrayOutputStream;
37
38@SmallTest
39@RunWith(AndroidJUnit4.class)
40public class NotificationChannelTest extends UiServiceTestCase {
41
42    @Test
43    public void testWriteToParcel() {
44        NotificationChannel channel =
45                new NotificationChannel("1", "one", IMPORTANCE_DEFAULT);
46        Parcel parcel = Parcel.obtain();
47        channel.writeToParcel(parcel, 0);
48        parcel.setDataPosition(0);
49        NotificationChannel channel1 = NotificationChannel.CREATOR.createFromParcel(parcel);
50        assertEquals(channel, channel1);
51    }
52
53    @Test
54    public void testSystemBlockable() {
55        NotificationChannel channel = new NotificationChannel("a", "ab", IMPORTANCE_DEFAULT);
56        assertEquals(false, channel.isBlockableSystem());
57        channel.setBlockableSystem(true);
58        assertEquals(true, channel.isBlockableSystem());
59    }
60
61    @Test
62    public void testEmptyVibration_noException() throws Exception {
63        NotificationChannel channel = new NotificationChannel("a", "ab", IMPORTANCE_DEFAULT);
64        channel.setVibrationPattern(new long[0]);
65
66        XmlSerializer serializer = new FastXmlSerializer();
67        ByteArrayOutputStream baos = new ByteArrayOutputStream();
68        serializer.setOutput(new BufferedOutputStream(baos), "utf-8");
69        channel.writeXml(serializer);
70    }
71}
72