blob: ca473c64991f69cc2bcc69372af7d8f110d637b4 [file] [log] [blame]
Julia Reynoldsf7321592017-05-24 16:09:19 -04001/*
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;
Julia Reynoldsaca215a2018-06-20 10:20:31 -040022import static junit.framework.Assert.assertNull;
Julia Reynoldsf7321592017-05-24 16:09:19 -040023
24import android.app.NotificationChannel;
Julia Reynoldsaca215a2018-06-20 10:20:31 -040025import android.net.Uri;
Julia Reynoldsf7321592017-05-24 16:09:19 -040026import android.os.Parcel;
27import android.support.test.runner.AndroidJUnit4;
28import android.test.suitebuilder.annotation.SmallTest;
Julia Reynoldsaca215a2018-06-20 10:20:31 -040029import android.util.Xml;
Julia Reynoldsf7321592017-05-24 16:09:19 -040030
Julia Reynolds0f170002017-07-10 10:51:01 -040031import com.android.internal.util.FastXmlSerializer;
Jason Monk74f5e362017-12-06 08:56:33 -050032import com.android.server.UiServiceTestCase;
Julia Reynolds0f170002017-07-10 10:51:01 -040033
Julia Reynoldsf7321592017-05-24 16:09:19 -040034import org.junit.Test;
35import org.junit.runner.RunWith;
Julia Reynoldsaca215a2018-06-20 10:20:31 -040036import org.xmlpull.v1.XmlPullParser;
Julia Reynolds0f170002017-07-10 10:51:01 -040037import org.xmlpull.v1.XmlSerializer;
38
Julia Reynoldsaca215a2018-06-20 10:20:31 -040039import java.io.BufferedInputStream;
Julia Reynolds0f170002017-07-10 10:51:01 -040040import java.io.BufferedOutputStream;
Julia Reynoldsaca215a2018-06-20 10:20:31 -040041import java.io.ByteArrayInputStream;
Julia Reynolds0f170002017-07-10 10:51:01 -040042import java.io.ByteArrayOutputStream;
Julia Reynoldsf7321592017-05-24 16:09:19 -040043
44@SmallTest
45@RunWith(AndroidJUnit4.class)
Jason Monk74f5e362017-12-06 08:56:33 -050046public class NotificationChannelTest extends UiServiceTestCase {
Julia Reynoldsf7321592017-05-24 16:09:19 -040047
48 @Test
49 public void testWriteToParcel() {
50 NotificationChannel channel =
51 new NotificationChannel("1", "one", IMPORTANCE_DEFAULT);
52 Parcel parcel = Parcel.obtain();
53 channel.writeToParcel(parcel, 0);
54 parcel.setDataPosition(0);
55 NotificationChannel channel1 = NotificationChannel.CREATOR.createFromParcel(parcel);
56 assertEquals(channel, channel1);
57 }
58
59 @Test
60 public void testSystemBlockable() {
61 NotificationChannel channel = new NotificationChannel("a", "ab", IMPORTANCE_DEFAULT);
62 assertEquals(false, channel.isBlockableSystem());
63 channel.setBlockableSystem(true);
64 assertEquals(true, channel.isBlockableSystem());
65 }
Julia Reynolds0f170002017-07-10 10:51:01 -040066
67 @Test
68 public void testEmptyVibration_noException() throws Exception {
69 NotificationChannel channel = new NotificationChannel("a", "ab", IMPORTANCE_DEFAULT);
70 channel.setVibrationPattern(new long[0]);
71
72 XmlSerializer serializer = new FastXmlSerializer();
73 ByteArrayOutputStream baos = new ByteArrayOutputStream();
74 serializer.setOutput(new BufferedOutputStream(baos), "utf-8");
75 channel.writeXml(serializer);
76 }
Julia Reynoldsaca215a2018-06-20 10:20:31 -040077
78 @Test
79 public void testBackupEmptySound() throws Exception {
80 NotificationChannel channel = new NotificationChannel("a", "ab", IMPORTANCE_DEFAULT);
81 channel.setSound(Uri.EMPTY, null);
82
83 XmlSerializer serializer = new FastXmlSerializer();
84 ByteArrayOutputStream baos = new ByteArrayOutputStream();
85 serializer.setOutput(new BufferedOutputStream(baos), "utf-8");
86 channel.writeXmlForBackup(serializer, getContext());
87
88 XmlPullParser parser = Xml.newPullParser();
89 parser.setInput(new BufferedInputStream(
90 new ByteArrayInputStream(baos.toByteArray())), null);
91 NotificationChannel restored = new NotificationChannel("a", "ab", IMPORTANCE_DEFAULT);
92 restored.populateFromXmlForRestore(parser, getContext());
93
94 assertNull(restored.getSound());
95 }
Julia Reynoldsf7321592017-05-24 16:09:19 -040096}