Julia Reynolds | f732159 | 2017-05-24 16:09:19 -0400 | [diff] [blame] | 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 | |
| 17 | package com.android.server.notification; |
| 18 | |
| 19 | import static android.app.NotificationManager.IMPORTANCE_DEFAULT; |
| 20 | |
| 21 | import static junit.framework.Assert.assertEquals; |
Julia Reynolds | aca215a | 2018-06-20 10:20:31 -0400 | [diff] [blame] | 22 | import static junit.framework.Assert.assertNull; |
Julia Reynolds | f732159 | 2017-05-24 16:09:19 -0400 | [diff] [blame] | 23 | |
| 24 | import android.app.NotificationChannel; |
Julia Reynolds | aca215a | 2018-06-20 10:20:31 -0400 | [diff] [blame] | 25 | import android.net.Uri; |
Julia Reynolds | f732159 | 2017-05-24 16:09:19 -0400 | [diff] [blame] | 26 | import android.os.Parcel; |
| 27 | import android.support.test.runner.AndroidJUnit4; |
| 28 | import android.test.suitebuilder.annotation.SmallTest; |
Julia Reynolds | aca215a | 2018-06-20 10:20:31 -0400 | [diff] [blame] | 29 | import android.util.Xml; |
Julia Reynolds | f732159 | 2017-05-24 16:09:19 -0400 | [diff] [blame] | 30 | |
Julia Reynolds | 0f17000 | 2017-07-10 10:51:01 -0400 | [diff] [blame] | 31 | import com.android.internal.util.FastXmlSerializer; |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 32 | import com.android.server.UiServiceTestCase; |
Julia Reynolds | 0f17000 | 2017-07-10 10:51:01 -0400 | [diff] [blame] | 33 | |
Julia Reynolds | f732159 | 2017-05-24 16:09:19 -0400 | [diff] [blame] | 34 | import org.junit.Test; |
| 35 | import org.junit.runner.RunWith; |
Julia Reynolds | aca215a | 2018-06-20 10:20:31 -0400 | [diff] [blame] | 36 | import org.xmlpull.v1.XmlPullParser; |
Julia Reynolds | 0f17000 | 2017-07-10 10:51:01 -0400 | [diff] [blame] | 37 | import org.xmlpull.v1.XmlSerializer; |
| 38 | |
Julia Reynolds | aca215a | 2018-06-20 10:20:31 -0400 | [diff] [blame] | 39 | import java.io.BufferedInputStream; |
Julia Reynolds | 0f17000 | 2017-07-10 10:51:01 -0400 | [diff] [blame] | 40 | import java.io.BufferedOutputStream; |
Julia Reynolds | aca215a | 2018-06-20 10:20:31 -0400 | [diff] [blame] | 41 | import java.io.ByteArrayInputStream; |
Julia Reynolds | 0f17000 | 2017-07-10 10:51:01 -0400 | [diff] [blame] | 42 | import java.io.ByteArrayOutputStream; |
Julia Reynolds | f732159 | 2017-05-24 16:09:19 -0400 | [diff] [blame] | 43 | |
| 44 | @SmallTest |
| 45 | @RunWith(AndroidJUnit4.class) |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 46 | public class NotificationChannelTest extends UiServiceTestCase { |
Julia Reynolds | f732159 | 2017-05-24 16:09:19 -0400 | [diff] [blame] | 47 | |
| 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 Reynolds | 0f17000 | 2017-07-10 10:51:01 -0400 | [diff] [blame] | 66 | |
| 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 Reynolds | aca215a | 2018-06-20 10:20:31 -0400 | [diff] [blame] | 77 | |
| 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 Reynolds | f732159 | 2017-05-24 16:09:19 -0400 | [diff] [blame] | 96 | } |