blob: 0d398bef42338d1833fd8c3071759ee62db7c9db [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;
18import static org.junit.Assert.assertNull;
19import static org.junit.Assert.assertTrue;
20import static org.mockito.Matchers.eq;
21import static org.mockito.Mockito.mock;
22import static org.mockito.Mockito.verify;
23import static org.mockito.Mockito.when;
24
25import android.app.Notification;
26import android.app.NotificationChannel;
27import android.app.NotificationManager;
28import android.content.Context;
Alison Cichowlas08abf6d2018-01-22 20:46:38 -050029import android.net.Uri;
30import android.provider.Settings;
Dan Sandler8e032e12017-01-25 13:41:38 -050031import android.support.test.runner.AndroidJUnit4;
32import android.test.suitebuilder.annotation.SmallTest;
33import android.util.ArraySet;
34import com.android.systemui.SysuiTestCase;
35import com.android.systemui.util.NotificationChannels;
Alison Cichowlas08abf6d2018-01-22 20:46:38 -050036
Dan Sandler8e032e12017-01-25 13:41:38 -050037import org.junit.Before;
38import org.junit.Test;
39import org.junit.runner.RunWith;
40import org.mockito.ArgumentCaptor;
41
42import java.util.Arrays;
43import java.util.List;
44import java.util.Set;
45
46@SmallTest
47@RunWith(AndroidJUnit4.class)
48public 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 Cichowlas08abf6d2018-01-22 20:46:38 -050060 NotificationChannels.SCREENSHOTS_HEADSUP,
Geoffrey Pitsch1dc93bc2017-01-31 16:38:11 -050061 NotificationChannels.STORAGE,
Beverly334bc5f2017-07-31 10:37:17 -040062 NotificationChannels.GENERAL,
Makoto Onuki52c62952018-03-22 10:43:03 -070063 NotificationChannels.BATTERY,
64 NotificationChannels.HINTS
Dan Sandler8e032e12017-01-25 13:41:38 -050065 ));
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 Cichowlas08abf6d2018-01-22 20:46:38 -050073
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 Reynoldsaca215a2018-06-20 10:20:31 -0400106 assertEquals(null, newChannel.getSound());
Alison Cichowlas08abf6d2018-01-22 20:46:38 -0500107 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 Reynoldsaca215a2018-06-20 10:20:31 -0400116 assertEquals(null, newChannel.getSound());
Alison Cichowlas08abf6d2018-01-22 20:46:38 -0500117 assertEquals("newName", newChannel.getName());
118 assertEquals(NotificationManager.IMPORTANCE_HIGH, newChannel.getImportance());
119 }
120
Dan Sandler8e032e12017-01-25 13:41:38 -0500121}