• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 static com.google.common.truth.Truth.assertThat;
20 
21 import static org.junit.Assert.assertFalse;
22 import static org.junit.Assert.assertTrue;
23 import static org.mockito.ArgumentMatchers.anyString;
24 import static org.mockito.Mockito.mock;
25 import static org.mockito.Mockito.when;
26 
27 import android.app.role.RoleManager;
28 import android.app.usage.UsageEvents;
29 import android.content.pm.ApplicationInfo;
30 import android.content.pm.PackageInfo;
31 import android.content.pm.PackageManager;
32 import android.os.Parcel;
33 
34 import com.android.settings.notification.NotificationBackend.AppRow;
35 
36 import org.junit.Test;
37 import org.junit.runner.RunWith;
38 import org.robolectric.RobolectricTestRunner;
39 import org.robolectric.RuntimeEnvironment;
40 
41 import java.util.ArrayList;
42 import java.util.List;
43 
44 @RunWith(RobolectricTestRunner.class)
45 public class NotificationBackendTest {
46 
47     @Test
testMarkAppRow_unblockablePackage()48     public void testMarkAppRow_unblockablePackage() {
49         AppRow appRow = new AppRow();
50         String packageName = "foo.bar.unblockable";
51         appRow.pkg = packageName;
52         String[] nonBlockablePkgs = new String[2];
53         nonBlockablePkgs[0] = packageName;
54         nonBlockablePkgs[1] = "some.other.package";
55         NotificationBackend.markAppRowWithBlockables(nonBlockablePkgs, appRow, packageName);
56 
57         // This package has a package lock but no locked channels
58         assertTrue(appRow.lockedImportance);
59     }
60 
61     @Test
testMarkAppRow_defaultPackage()62     public void testMarkAppRow_defaultPackage() {
63         PackageInfo pi = new PackageInfo();
64         pi.packageName = "test";
65         pi.applicationInfo = new ApplicationInfo();
66         pi.applicationInfo.packageName = "test";
67         List<String> roles = new ArrayList<>();
68         roles.add(RoleManager.ROLE_DIALER);
69         RoleManager rm = mock(RoleManager.class);
70         when(rm.getHeldRolesFromController(anyString())).thenReturn(roles);
71 
72         AppRow appRow = new NotificationBackend().loadAppRow(RuntimeEnvironment.application,
73                 mock(PackageManager.class), rm, pi);
74 
75         assertTrue(appRow.systemApp);
76     }
77 
78     @Test
testMarkAppRow_notDefaultPackage()79     public void testMarkAppRow_notDefaultPackage() {
80         PackageInfo pi = new PackageInfo();
81         pi.packageName = "test";
82         pi.applicationInfo = new ApplicationInfo();
83         pi.applicationInfo.packageName = "test";
84         List<String> roles = new ArrayList<>();
85         roles.add(RoleManager.ROLE_HOME);
86         RoleManager rm = mock(RoleManager.class);
87         when(rm.getHeldRolesFromController(anyString())).thenReturn(roles);
88 
89         AppRow appRow = new NotificationBackend().loadAppRow(RuntimeEnvironment.application,
90                 mock(PackageManager.class), rm, pi);
91 
92         assertFalse(appRow.systemApp);
93     }
94 
95     @Test
testGetAggregatedUsageEvents_multipleEventsAgg()96     public void testGetAggregatedUsageEvents_multipleEventsAgg() {
97         List<UsageEvents.Event> events = new ArrayList<>();
98         UsageEvents.Event good = new UsageEvents.Event();
99         good.mEventType = UsageEvents.Event.NOTIFICATION_INTERRUPTION;
100         good.mPackage = "pkg";
101         good.mNotificationChannelId = "channel1";
102         good.mTimeStamp = 2;
103         events.add(good);
104         UsageEvents.Event good2 = new UsageEvents.Event();
105         good2.mEventType = UsageEvents.Event.NOTIFICATION_INTERRUPTION;
106         good2.mPackage = "pkg";
107         good2.mNotificationChannelId = "channel2";
108         good2.mTimeStamp = 3;
109         events.add(good2);
110         UsageEvents.Event good1 = new UsageEvents.Event();
111         good1.mEventType = UsageEvents.Event.NOTIFICATION_INTERRUPTION;
112         good1.mPackage = "pkg";
113         good1.mNotificationChannelId = "channel1";
114         good1.mTimeStamp = 6;
115         events.add(good1);
116         NotificationBackend backend = new NotificationBackend();
117 
118         AppRow appRow = new AppRow();
119         appRow.pkg = "pkg";
120         backend.recordAggregatedUsageEvents(getUsageEvents(events), appRow);
121 
122         assertThat(appRow.sentByChannel.get("channel1").sentCount).isEqualTo(2);
123         assertThat(appRow.sentByChannel.get("channel1").lastSent).isEqualTo(6);
124         assertThat(appRow.sentByChannel.get("channel1").avgSentWeekly).isEqualTo(2);
125         assertThat(appRow.sentByChannel.get("channel2").sentCount).isEqualTo(1);
126         assertThat(appRow.sentByChannel.get("channel2").lastSent).isEqualTo(3);
127         assertThat(appRow.sentByChannel.get("channel2").avgSentWeekly).isEqualTo(1);
128         assertThat(appRow.sentByApp.sentCount).isEqualTo(3);
129         assertThat(appRow.sentByApp.lastSent).isEqualTo(6);
130         assertThat(appRow.sentByApp.avgSentWeekly).isEqualTo(3);
131     }
132 
getUsageEvents(List<UsageEvents.Event> events)133     private UsageEvents getUsageEvents(List<UsageEvents.Event> events) {
134         UsageEvents usageEvents = new UsageEvents(events, new String[] {"pkg"});
135         Parcel parcel = Parcel.obtain();
136         parcel.setDataPosition(0);
137         usageEvents.writeToParcel(parcel, 0);
138         parcel.setDataPosition(0);
139         return UsageEvents.CREATOR.createFromParcel(parcel);
140     }
141 }
142