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.settings.notification;
18 
19 import android.app.NotificationManager;
20 import android.app.NotificationManager.Policy;
21 import android.content.Context;
22 import android.support.v7.preference.Preference;
23 
24 import com.android.settings.R;
25 import com.android.settings.notification.NotificationBackend.AppRow;
26 import com.android.settings.SettingsRobolectricTestRunner;
27 import com.android.settings.TestConfig;
28 import org.robolectric.annotation.Config;
29 
30 import org.junit.Before;
31 import org.junit.Test;
32 import org.junit.runner.RunWith;
33 import org.mockito.Mock;
34 
35 import static junit.framework.Assert.assertEquals;
36 import static junit.framework.Assert.assertTrue;
37 import static junit.framework.Assert.assertFalse;
38 import static junit.framework.Assert.assertNull;
39 
40 
41 @RunWith(SettingsRobolectricTestRunner.class)
42 @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
43 public class NotificationBackendTest {
44 
45     @Test
testMarkAppRow_unblockablePackage()46     public void testMarkAppRow_unblockablePackage() {
47         AppRow appRow = new AppRow();
48         String packageName = "foo.bar.unblockable";
49         appRow.pkg = packageName;
50         String[] nonBlockablePkgs = new String[2];
51         nonBlockablePkgs[0] = packageName;
52         nonBlockablePkgs[1] = "some.other.package";
53         NotificationBackend.markAppRowWithBlockables(nonBlockablePkgs, appRow, packageName);
54 
55         // This package has a package lock but no locked channels
56         assertTrue(appRow.lockedImportance);
57         assertNull(appRow.lockedChannelId);
58     }
59 
60     @Test
testMarkAppRow_unblockableChannelOrPkg()61     public void testMarkAppRow_unblockableChannelOrPkg() {
62         String channelBlockName = "foo.bar.pkgWithChannel";
63         String pkgBlockName = "foo.bar.pkgBlock";
64         String[] nonBlockablePkgs = new String[2];
65         nonBlockablePkgs[0] = pkgBlockName;
66         nonBlockablePkgs[1] = channelBlockName + ":SpecificChannel";
67 
68         // This package has a channel level lock but no full package lock
69         AppRow channelBlockApp = new AppRow();
70         channelBlockApp.pkg = channelBlockName;
71         NotificationBackend.markAppRowWithBlockables(nonBlockablePkgs, channelBlockApp,
72                 channelBlockName);
73         assertFalse(channelBlockApp.lockedImportance);
74         assertEquals("SpecificChannel", channelBlockApp.lockedChannelId);
75 
76         // This other package has the reverse
77         AppRow pkgBlock = new AppRow();
78         pkgBlock.pkg = pkgBlockName;
79         NotificationBackend.markAppRowWithBlockables(nonBlockablePkgs, pkgBlock, pkgBlockName);
80         assertTrue(pkgBlock.lockedImportance);
81         assertNull(pkgBlock.lockedChannelId);
82 
83         // This third package has no locks at all
84         AppRow otherAppRow = new AppRow();
85         otherAppRow.pkg ="foo.bar.nothingBlocked";
86         NotificationBackend.markAppRowWithBlockables(nonBlockablePkgs, otherAppRow,
87                 "foo.bar.nothingBlocked");
88         assertFalse(otherAppRow.lockedImportance);
89         assertNull(otherAppRow.lockedChannelId);
90     }
91 
92     @Test
testMarkAppRow_unblockableChannelAndPkg()93     public void testMarkAppRow_unblockableChannelAndPkg() {
94         AppRow appRow = new AppRow();
95         String packageName = "foo.bar.unblockable";
96         appRow.pkg = packageName;
97         String[] nonBlockablePkgs = new String[2];
98         nonBlockablePkgs[0] = "foo.bar.unblockable";
99         nonBlockablePkgs[1] = "foo.bar.unblockable:SpecificChannel";
100         NotificationBackend.markAppRowWithBlockables(nonBlockablePkgs, appRow, packageName);
101 
102         // This package has both a channel lock and a package lock
103         assertTrue(appRow.lockedImportance);
104         assertEquals("SpecificChannel", appRow.lockedChannelId);
105     }
106 
107     @Test
testMarkAppRow_channelNameWithColons()108     public void testMarkAppRow_channelNameWithColons() {
109         AppRow appRow = new AppRow();
110         String packageName = "foo.bar.unblockable";
111         String channelName = "SpecificChannel:1234:abc:defg";
112         appRow.pkg = packageName;
113         String[] nonBlockablePkgs = new String[1];
114         nonBlockablePkgs[0] = packageName + ":" + channelName;
115         NotificationBackend.markAppRowWithBlockables(nonBlockablePkgs, appRow, packageName);
116 
117         assertEquals(channelName, appRow.lockedChannelId);
118     }
119 
120 
121     @Test
testMarkAppRow_blocklistWithNullEntries()122     public void testMarkAppRow_blocklistWithNullEntries() {
123         AppRow appRow = new AppRow();
124         String packageName = "foo.bar.unblockable";
125         appRow.pkg = packageName;
126         String[] nonBlockablePkgs = new String[6]; // extra long list with some entries left null
127         nonBlockablePkgs[2] = "foo.bar.unblockable";
128         nonBlockablePkgs[4] = "foo.bar.unblockable:SpecificChannel";
129         NotificationBackend.markAppRowWithBlockables(nonBlockablePkgs, appRow, packageName);
130 
131         assertTrue(appRow.lockedImportance);
132         assertEquals("SpecificChannel", appRow.lockedChannelId);
133     }
134 }
135