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; |
Dan Sandler | 8e032e1 | 2017-01-25 13:41:38 -0500 | [diff] [blame] | 18 | import static org.junit.Assert.assertTrue; |
Dan Sandler | 8e032e1 | 2017-01-25 13:41:38 -0500 | [diff] [blame] | 19 | import static org.mockito.Mockito.mock; |
| 20 | import static org.mockito.Mockito.verify; |
Dan Sandler | 8e032e1 | 2017-01-25 13:41:38 -0500 | [diff] [blame] | 21 | |
Dan Sandler | 8e032e1 | 2017-01-25 13:41:38 -0500 | [diff] [blame] | 22 | import android.app.NotificationChannel; |
| 23 | import android.app.NotificationManager; |
| 24 | import android.content.Context; |
Alison Cichowlas | 08abf6d | 2018-01-22 20:46:38 -0500 | [diff] [blame] | 25 | import android.provider.Settings; |
Dan Sandler | 8e032e1 | 2017-01-25 13:41:38 -0500 | [diff] [blame] | 26 | import android.test.suitebuilder.annotation.SmallTest; |
| 27 | import android.util.ArraySet; |
Brett Chabot | 84151d9 | 2019-02-27 15:37:59 -0800 | [diff] [blame] | 28 | |
| 29 | import androidx.test.runner.AndroidJUnit4; |
| 30 | |
Dan Sandler | 8e032e1 | 2017-01-25 13:41:38 -0500 | [diff] [blame] | 31 | import com.android.systemui.SysuiTestCase; |
| 32 | import com.android.systemui.util.NotificationChannels; |
Alison Cichowlas | 08abf6d | 2018-01-22 20:46:38 -0500 | [diff] [blame] | 33 | |
Dan Sandler | 8e032e1 | 2017-01-25 13:41:38 -0500 | [diff] [blame] | 34 | import org.junit.Before; |
| 35 | import org.junit.Test; |
| 36 | import org.junit.runner.RunWith; |
| 37 | import org.mockito.ArgumentCaptor; |
| 38 | |
| 39 | import java.util.Arrays; |
| 40 | import java.util.List; |
| 41 | import java.util.Set; |
| 42 | |
| 43 | @SmallTest |
| 44 | @RunWith(AndroidJUnit4.class) |
| 45 | public class ChannelsTest extends SysuiTestCase { |
| 46 | private final NotificationManager mMockNotificationManager = mock(NotificationManager.class); |
| 47 | |
| 48 | @Before |
| 49 | public void setup() throws Exception { |
| 50 | mContext.addMockSystemService(Context.NOTIFICATION_SERVICE, mMockNotificationManager); |
| 51 | } |
| 52 | |
| 53 | @Test |
| 54 | public void testChannelSetup() { |
| 55 | Set<String> ALL_CHANNELS = new ArraySet<>(Arrays.asList( |
| 56 | NotificationChannels.ALERTS, |
Alison Cichowlas | 08abf6d | 2018-01-22 20:46:38 -0500 | [diff] [blame] | 57 | NotificationChannels.SCREENSHOTS_HEADSUP, |
Geoffrey Pitsch | 1dc93bc | 2017-01-31 16:38:11 -0500 | [diff] [blame] | 58 | NotificationChannels.STORAGE, |
Beverly | 334bc5f | 2017-07-31 10:37:17 -0400 | [diff] [blame] | 59 | NotificationChannels.GENERAL, |
Makoto Onuki | 52c6295 | 2018-03-22 10:43:03 -0700 | [diff] [blame] | 60 | NotificationChannels.BATTERY, |
| 61 | NotificationChannels.HINTS |
Dan Sandler | 8e032e1 | 2017-01-25 13:41:38 -0500 | [diff] [blame] | 62 | )); |
| 63 | NotificationChannels.createAll(mContext); |
| 64 | ArgumentCaptor<List> captor = ArgumentCaptor.forClass(List.class); |
| 65 | verify(mMockNotificationManager).createNotificationChannels(captor.capture()); |
| 66 | final List<NotificationChannel> list = captor.getValue(); |
| 67 | assertEquals(ALL_CHANNELS.size(), list.size()); |
| 68 | list.forEach((chan) -> assertTrue(ALL_CHANNELS.contains(chan.getId()))); |
| 69 | } |
Alison Cichowlas | 08abf6d | 2018-01-22 20:46:38 -0500 | [diff] [blame] | 70 | |
| 71 | @Test |
| 72 | public void testChannelSetup_noLegacyScreenshot() { |
| 73 | // Assert old channel cleaned up. |
| 74 | // TODO: remove that code + this test after P. |
| 75 | NotificationChannels.createAll(mContext); |
| 76 | ArgumentCaptor<List> captor = ArgumentCaptor.forClass(List.class); |
| 77 | verify(mMockNotificationManager).deleteNotificationChannel( |
| 78 | NotificationChannels.SCREENSHOTS_LEGACY); |
| 79 | } |
| 80 | |
| 81 | @Test |
| 82 | public void testInheritFromLegacy_keepsUserLockedLegacySettings() { |
| 83 | NotificationChannel legacyChannel = new NotificationChannel("id", "oldName", |
| 84 | NotificationManager.IMPORTANCE_MIN); |
| 85 | legacyChannel.setImportance(NotificationManager.IMPORTANCE_NONE);; |
| 86 | legacyChannel.setSound(Settings.System.DEFAULT_NOTIFICATION_URI, |
| 87 | legacyChannel.getAudioAttributes()); |
| 88 | legacyChannel.lockFields(NotificationChannel.USER_LOCKED_IMPORTANCE | |
| 89 | NotificationChannel.USER_LOCKED_SOUND); |
| 90 | NotificationChannel newChannel = |
| 91 | NotificationChannels.createScreenshotChannel("newName", legacyChannel); |
| 92 | // NONE importance user locked, so don't use HIGH for new channel. |
| 93 | assertEquals(NotificationManager.IMPORTANCE_NONE, newChannel.getImportance()); |
| 94 | assertEquals(Settings.System.DEFAULT_NOTIFICATION_URI, newChannel.getSound()); |
| 95 | } |
| 96 | |
| 97 | @Test |
| 98 | public void testInheritFromLegacy_dropsUnlockedLegacySettings() { |
| 99 | NotificationChannel legacyChannel = new NotificationChannel("id", "oldName", |
| 100 | NotificationManager.IMPORTANCE_MIN); |
| 101 | NotificationChannel newChannel = |
| 102 | NotificationChannels.createScreenshotChannel("newName", legacyChannel); |
Julia Reynolds | aca215a | 2018-06-20 10:20:31 -0400 | [diff] [blame] | 103 | assertEquals(null, newChannel.getSound()); |
Alison Cichowlas | 08abf6d | 2018-01-22 20:46:38 -0500 | [diff] [blame] | 104 | assertEquals("newName", newChannel.getName()); |
| 105 | // MIN importance not user locked, so HIGH wins out. |
| 106 | assertEquals(NotificationManager.IMPORTANCE_HIGH, newChannel.getImportance()); |
| 107 | } |
| 108 | |
| 109 | @Test |
| 110 | public void testInheritFromLegacy_noLegacyExists() { |
| 111 | NotificationChannel newChannel = |
| 112 | NotificationChannels.createScreenshotChannel("newName", null); |
Julia Reynolds | aca215a | 2018-06-20 10:20:31 -0400 | [diff] [blame] | 113 | assertEquals(null, newChannel.getSound()); |
Alison Cichowlas | 08abf6d | 2018-01-22 20:46:38 -0500 | [diff] [blame] | 114 | assertEquals("newName", newChannel.getName()); |
| 115 | assertEquals(NotificationManager.IMPORTANCE_HIGH, newChannel.getImportance()); |
| 116 | } |
| 117 | |
Dan Sandler | 8e032e1 | 2017-01-25 13:41:38 -0500 | [diff] [blame] | 118 | } |