blob: f1c9980f12c4cb62030a6ba49d0f1cbecdf9fdfe [file] [log] [blame]
Dan Sandler8e032e12017-01-25 13:41:38 -05001/*
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
15package com.android.systemui.util;
16
17import static org.junit.Assert.assertEquals;
Dan Sandler8e032e12017-01-25 13:41:38 -050018import static org.junit.Assert.assertTrue;
Dan Sandler8e032e12017-01-25 13:41:38 -050019import static org.mockito.Mockito.mock;
20import static org.mockito.Mockito.verify;
Dan Sandler8e032e12017-01-25 13:41:38 -050021
Dan Sandler8e032e12017-01-25 13:41:38 -050022import android.app.NotificationChannel;
23import android.app.NotificationManager;
24import android.content.Context;
Alison Cichowlas08abf6d2018-01-22 20:46:38 -050025import android.provider.Settings;
Dan Sandler8e032e12017-01-25 13:41:38 -050026import android.test.suitebuilder.annotation.SmallTest;
27import android.util.ArraySet;
Brett Chabot84151d92019-02-27 15:37:59 -080028
29import androidx.test.runner.AndroidJUnit4;
30
Dan Sandler8e032e12017-01-25 13:41:38 -050031import com.android.systemui.SysuiTestCase;
32import com.android.systemui.util.NotificationChannels;
Alison Cichowlas08abf6d2018-01-22 20:46:38 -050033
Dan Sandler8e032e12017-01-25 13:41:38 -050034import org.junit.Before;
35import org.junit.Test;
36import org.junit.runner.RunWith;
37import org.mockito.ArgumentCaptor;
38
39import java.util.Arrays;
40import java.util.List;
41import java.util.Set;
42
43@SmallTest
44@RunWith(AndroidJUnit4.class)
45public 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 Cichowlas08abf6d2018-01-22 20:46:38 -050057 NotificationChannels.SCREENSHOTS_HEADSUP,
Geoffrey Pitsch1dc93bc2017-01-31 16:38:11 -050058 NotificationChannels.STORAGE,
Beverly334bc5f2017-07-31 10:37:17 -040059 NotificationChannels.GENERAL,
Makoto Onuki52c62952018-03-22 10:43:03 -070060 NotificationChannels.BATTERY,
61 NotificationChannels.HINTS
Dan Sandler8e032e12017-01-25 13:41:38 -050062 ));
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 Cichowlas08abf6d2018-01-22 20:46:38 -050070
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 Reynoldsaca215a2018-06-20 10:20:31 -0400103 assertEquals(null, newChannel.getSound());
Alison Cichowlas08abf6d2018-01-22 20:46:38 -0500104 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 Reynoldsaca215a2018-06-20 10:20:31 -0400113 assertEquals(null, newChannel.getSound());
Alison Cichowlas08abf6d2018-01-22 20:46:38 -0500114 assertEquals("newName", newChannel.getName());
115 assertEquals(NotificationManager.IMPORTANCE_HIGH, newChannel.getImportance());
116 }
117
Dan Sandler8e032e12017-01-25 13:41:38 -0500118}