-
Notifications
You must be signed in to change notification settings - Fork 0
/
NotificationAdjustmentExtractorTest.java
131 lines (107 loc) · 5.45 KB
/
NotificationAdjustmentExtractorTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/*
* Copyright (C) 2017 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.server.notification;
import static android.app.NotificationManager.IMPORTANCE_LOW;
import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertFalse;
import static junit.framework.Assert.assertNull;
import static junit.framework.Assert.assertTrue;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.PendingIntent;
import android.content.Intent;
import android.graphics.drawable.Icon;
import android.os.Bundle;
import android.os.UserHandle;
import android.service.notification.Adjustment;
import android.service.notification.SnoozeCriterion;
import android.service.notification.StatusBarNotification;
import com.android.server.UiServiceTestCase;
import org.junit.Test;
import java.util.ArrayList;
import java.util.Objects;
public class NotificationAdjustmentExtractorTest extends UiServiceTestCase {
@Test
public void testExtractsAdjustment() {
NotificationAdjustmentExtractor extractor = new NotificationAdjustmentExtractor();
NotificationRecord r = generateRecord();
Bundle signals = new Bundle();
signals.putString(Adjustment.KEY_GROUP_KEY, GroupHelper.AUTOGROUP_KEY);
ArrayList<SnoozeCriterion> snoozeCriteria = new ArrayList<>();
snoozeCriteria.add(new SnoozeCriterion("n", "n", "n"));
signals.putParcelableArrayList(Adjustment.KEY_SNOOZE_CRITERIA, snoozeCriteria);
ArrayList<String> people = new ArrayList<>();
people.add("you");
signals.putStringArrayList(Adjustment.KEY_PEOPLE, people);
ArrayList<Notification.Action> smartActions = new ArrayList<>();
smartActions.add(createAction());
signals.putParcelableArrayList(Adjustment.KEY_CONTEXTUAL_ACTIONS, smartActions);
Adjustment adjustment = new Adjustment("pkg", r.getKey(), signals, "", 0);
r.addAdjustment(adjustment);
assertFalse(r.getGroupKey().contains(GroupHelper.AUTOGROUP_KEY));
assertFalse(Objects.equals(people, r.getPeopleOverride()));
assertFalse(Objects.equals(snoozeCriteria, r.getSnoozeCriteria()));
assertNull(extractor.process(r));
assertTrue(r.getGroupKey().contains(GroupHelper.AUTOGROUP_KEY));
assertEquals(people, r.getPeopleOverride());
assertEquals(snoozeCriteria, r.getSnoozeCriteria());
assertEquals(smartActions, r.getSystemGeneratedSmartActions());
}
@Test
public void testExtractsAdjustments() {
NotificationAdjustmentExtractor extractor = new NotificationAdjustmentExtractor();
NotificationRecord r = generateRecord();
Bundle pSignals = new Bundle();
ArrayList<String> people = new ArrayList<>();
people.add("you");
pSignals.putStringArrayList(Adjustment.KEY_PEOPLE, people);
Adjustment pAdjustment = new Adjustment("pkg", r.getKey(), pSignals, "", 0);
r.addAdjustment(pAdjustment);
Bundle sSignals = new Bundle();
ArrayList<SnoozeCriterion> snoozeCriteria = new ArrayList<>();
snoozeCriteria.add(new SnoozeCriterion("n", "n", "n"));
sSignals.putParcelableArrayList(Adjustment.KEY_SNOOZE_CRITERIA, snoozeCriteria);
Adjustment sAdjustment = new Adjustment("pkg", r.getKey(), sSignals, "", 0);
r.addAdjustment(sAdjustment);
Bundle gSignals = new Bundle();
gSignals.putString(Adjustment.KEY_GROUP_KEY, GroupHelper.AUTOGROUP_KEY);
Adjustment gAdjustment = new Adjustment("pkg", r.getKey(), gSignals, "", 0);
r.addAdjustment(gAdjustment);
assertFalse(r.getGroupKey().contains(GroupHelper.AUTOGROUP_KEY));
assertFalse(Objects.equals(people, r.getPeopleOverride()));
assertFalse(Objects.equals(snoozeCriteria, r.getSnoozeCriteria()));
assertNull(extractor.process(r));
assertTrue(r.getGroupKey().contains(GroupHelper.AUTOGROUP_KEY));
assertEquals(people, r.getPeopleOverride());
assertEquals(snoozeCriteria, r.getSnoozeCriteria());
}
private NotificationRecord generateRecord() {
NotificationChannel channel = new NotificationChannel("a", "a", IMPORTANCE_LOW);
final Notification.Builder builder = new Notification.Builder(getContext())
.setContentTitle("foo")
.setSmallIcon(android.R.drawable.sym_def_app_icon);
Notification n = builder.build();
StatusBarNotification sbn = new StatusBarNotification("", "", 0, "", 0,
0, n, UserHandle.ALL, null, System.currentTimeMillis());
return new NotificationRecord(getContext(), sbn, channel);
}
private Notification.Action createAction() {
return new Notification.Action.Builder(
Icon.createWithResource(getContext(), android.R.drawable.sym_def_app_icon),
"action",
PendingIntent.getBroadcast(getContext(), 0, new Intent("Action"), 0)).build();
}
}