Dan Sandler | 8e032e1 | 2017-01-25 13:41:38 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file |
| 5 | * except in compliance with the License. You may obtain a copy of the License at |
| 6 | * |
| 7 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | * |
| 9 | * Unless required by applicable law or agreed to in writing, software distributed under the |
| 10 | * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 11 | * KIND, either express or implied. See the License for the specific language governing |
| 12 | * permissions and limitations under the License. |
| 13 | */ |
| 14 | |
| 15 | package com.android.systemui.util; |
| 16 | |
| 17 | import static org.junit.Assert.assertEquals; |
| 18 | import static org.junit.Assert.assertNull; |
| 19 | import static org.junit.Assert.assertTrue; |
| 20 | import static org.mockito.Matchers.eq; |
| 21 | import static org.mockito.Mockito.mock; |
| 22 | import static org.mockito.Mockito.verify; |
| 23 | import static org.mockito.Mockito.when; |
| 24 | |
| 25 | import android.app.Notification; |
| 26 | import android.app.NotificationChannel; |
| 27 | import android.app.NotificationManager; |
| 28 | import android.content.Context; |
Alison Cichowlas | 08abf6d | 2018-01-22 20:46:38 -0500 | [diff] [blame] | 29 | import android.net.Uri; |
| 30 | import android.provider.Settings; |
Dan Sandler | 8e032e1 | 2017-01-25 13:41:38 -0500 | [diff] [blame] | 31 | import android.support.test.runner.AndroidJUnit4; |
| 32 | import android.test.suitebuilder.annotation.SmallTest; |
| 33 | import android.util.ArraySet; |
| 34 | import com.android.systemui.SysuiTestCase; |
| 35 | import com.android.systemui.util.NotificationChannels; |
Alison Cichowlas | 08abf6d | 2018-01-22 20:46:38 -0500 | [diff] [blame] | 36 | |
Dan Sandler | 8e032e1 | 2017-01-25 13:41:38 -0500 | [diff] [blame] | 37 | import org.junit.Before; |
| 38 | import org.junit.Test; |
| 39 | import org.junit.runner.RunWith; |
| 40 | import org.mockito.ArgumentCaptor; |
| 41 | |
| 42 | import java.util.Arrays; |
| 43 | import java.util.List; |
| 44 | import java.util.Set; |
| 45 | |
| 46 | @SmallTest |
| 47 | @RunWith(AndroidJUnit4.class) |
| 48 | public class ChannelsTest extends SysuiTestCase { |
| 49 | private final NotificationManager mMockNotificationManager = mock(NotificationManager.class); |
| 50 | |
| 51 | @Before |
| 52 | public void setup() throws Exception { |
| 53 | mContext.addMockSystemService(Context.NOTIFICATION_SERVICE, mMockNotificationManager); |
| 54 | } |
| 55 | |
| 56 | @Test |
| 57 | public void testChannelSetup() { |
| 58 | Set<String> ALL_CHANNELS = new ArraySet<>(Arrays.asList( |
| 59 | NotificationChannels.ALERTS, |
Alison Cichowlas | 08abf6d | 2018-01-22 20:46:38 -0500 | [diff] [blame] | 60 | NotificationChannels.SCREENSHOTS_HEADSUP, |
Geoffrey Pitsch | 1dc93bc | 2017-01-31 16:38:11 -0500 | [diff] [blame] | 61 | NotificationChannels.STORAGE, |
Beverly | 334bc5f | 2017-07-31 10:37:17 -0400 | [diff] [blame] | 62 | NotificationChannels.GENERAL, |
Makoto Onuki | 52c6295 | 2018-03-22 10:43:03 -0700 | [diff] [blame] | 63 | NotificationChannels.BATTERY, |
| 64 | NotificationChannels.HINTS |
Dan Sandler | 8e032e1 | 2017-01-25 13:41:38 -0500 | [diff] [blame] | 65 | )); |
| 66 | NotificationChannels.createAll(mContext); |
| 67 | ArgumentCaptor<List> captor = ArgumentCaptor.forClass(List.class); |
| 68 | verify(mMockNotificationManager).createNotificationChannels(captor.capture()); |
| 69 | final List<NotificationChannel> list = captor.getValue(); |
| 70 | assertEquals(ALL_CHANNELS.size(), list.size()); |
| 71 | list.forEach((chan) -> assertTrue(ALL_CHANNELS.contains(chan.getId()))); |
| 72 | } |
Alison Cichowlas | 08abf6d | 2018-01-22 20:46:38 -0500 | [diff] [blame] | 73 | |
| 74 | @Test |
| 75 | public void testChannelSetup_noLegacyScreenshot() { |
| 76 | // Assert old channel cleaned up. |
| 77 | // TODO: remove that code + this test after P. |
| 78 | NotificationChannels.createAll(mContext); |
| 79 | ArgumentCaptor<List> captor = ArgumentCaptor.forClass(List.class); |
| 80 | verify(mMockNotificationManager).deleteNotificationChannel( |
| 81 | NotificationChannels.SCREENSHOTS_LEGACY); |
| 82 | } |
| 83 | |
| 84 | @Test |
| 85 | public void testInheritFromLegacy_keepsUserLockedLegacySettings() { |
| 86 | NotificationChannel legacyChannel = new NotificationChannel("id", "oldName", |
| 87 | NotificationManager.IMPORTANCE_MIN); |
| 88 | legacyChannel.setImportance(NotificationManager.IMPORTANCE_NONE);; |
| 89 | legacyChannel.setSound(Settings.System.DEFAULT_NOTIFICATION_URI, |
| 90 | legacyChannel.getAudioAttributes()); |
| 91 | legacyChannel.lockFields(NotificationChannel.USER_LOCKED_IMPORTANCE | |
| 92 | NotificationChannel.USER_LOCKED_SOUND); |
| 93 | NotificationChannel newChannel = |
| 94 | NotificationChannels.createScreenshotChannel("newName", legacyChannel); |
| 95 | // NONE importance user locked, so don't use HIGH for new channel. |
| 96 | assertEquals(NotificationManager.IMPORTANCE_NONE, newChannel.getImportance()); |
| 97 | assertEquals(Settings.System.DEFAULT_NOTIFICATION_URI, newChannel.getSound()); |
| 98 | } |
| 99 | |
| 100 | @Test |
| 101 | public void testInheritFromLegacy_dropsUnlockedLegacySettings() { |
| 102 | NotificationChannel legacyChannel = new NotificationChannel("id", "oldName", |
| 103 | NotificationManager.IMPORTANCE_MIN); |
| 104 | NotificationChannel newChannel = |
| 105 | NotificationChannels.createScreenshotChannel("newName", legacyChannel); |
Julia Reynolds | aca215a | 2018-06-20 10:20:31 -0400 | [diff] [blame] | 106 | assertEquals(null, newChannel.getSound()); |
Alison Cichowlas | 08abf6d | 2018-01-22 20:46:38 -0500 | [diff] [blame] | 107 | assertEquals("newName", newChannel.getName()); |
| 108 | // MIN importance not user locked, so HIGH wins out. |
| 109 | assertEquals(NotificationManager.IMPORTANCE_HIGH, newChannel.getImportance()); |
| 110 | } |
| 111 | |
| 112 | @Test |
| 113 | public void testInheritFromLegacy_noLegacyExists() { |
| 114 | NotificationChannel newChannel = |
| 115 | NotificationChannels.createScreenshotChannel("newName", null); |
Julia Reynolds | aca215a | 2018-06-20 10:20:31 -0400 | [diff] [blame] | 116 | assertEquals(null, newChannel.getSound()); |
Alison Cichowlas | 08abf6d | 2018-01-22 20:46:38 -0500 | [diff] [blame] | 117 | assertEquals("newName", newChannel.getName()); |
| 118 | assertEquals(NotificationManager.IMPORTANCE_HIGH, newChannel.getImportance()); |
| 119 | } |
| 120 | |
Dan Sandler | 8e032e1 | 2017-01-25 13:41:38 -0500 | [diff] [blame] | 121 | } |