blob: fd674f0c3858fbde51188ff1cfeb97bba94ff839 [file] [log] [blame]
Julia Reynoldseb3dca72017-07-11 10:39:58 -04001/*
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
17package com.android.server.notification;
18
19import static android.app.NotificationManager.IMPORTANCE_LOW;
20
21import static junit.framework.Assert.assertEquals;
22import static junit.framework.Assert.assertFalse;
23import static junit.framework.Assert.assertNull;
24import static junit.framework.Assert.assertTrue;
25
26import android.app.Notification;
27import android.app.NotificationChannel;
28import android.os.Bundle;
29import android.os.UserHandle;
30import android.service.notification.Adjustment;
31import android.service.notification.SnoozeCriterion;
32import android.service.notification.StatusBarNotification;
33
Jason Monk74f5e362017-12-06 08:56:33 -050034import com.android.server.UiServiceTestCase;
35
Julia Reynoldseb3dca72017-07-11 10:39:58 -040036import org.junit.Test;
37
38import java.util.ArrayList;
39import java.util.Objects;
40
Jason Monk74f5e362017-12-06 08:56:33 -050041public class NotificationAdjustmentExtractorTest extends UiServiceTestCase {
Julia Reynoldseb3dca72017-07-11 10:39:58 -040042
43 @Test
44 public void testExtractsAdjustment() {
45 NotificationAdjustmentExtractor extractor = new NotificationAdjustmentExtractor();
46
47 NotificationRecord r = generateRecord();
48
49 Bundle signals = new Bundle();
50 signals.putString(Adjustment.KEY_GROUP_KEY, GroupHelper.AUTOGROUP_KEY);
51 ArrayList<SnoozeCriterion> snoozeCriteria = new ArrayList<>();
52 snoozeCriteria.add(new SnoozeCriterion("n", "n", "n"));
53 signals.putParcelableArrayList(Adjustment.KEY_SNOOZE_CRITERIA, snoozeCriteria);
54 ArrayList<String> people = new ArrayList<>();
55 people.add("you");
56 signals.putStringArrayList(Adjustment.KEY_PEOPLE, people);
57 Adjustment adjustment = new Adjustment("pkg", r.getKey(), signals, "", 0);
58 r.addAdjustment(adjustment);
59
60 assertFalse(r.getGroupKey().contains(GroupHelper.AUTOGROUP_KEY));
61 assertFalse(Objects.equals(people, r.getPeopleOverride()));
62 assertFalse(Objects.equals(snoozeCriteria, r.getSnoozeCriteria()));
63
64 assertNull(extractor.process(r));
65
66 assertTrue(r.getGroupKey().contains(GroupHelper.AUTOGROUP_KEY));
67 assertEquals(people, r.getPeopleOverride());
68 assertEquals(snoozeCriteria, r.getSnoozeCriteria());
69 }
70
71 @Test
72 public void testExtractsAdjustments() {
73 NotificationAdjustmentExtractor extractor = new NotificationAdjustmentExtractor();
74
75 NotificationRecord r = generateRecord();
76
77 Bundle pSignals = new Bundle();
78 ArrayList<String> people = new ArrayList<>();
79 people.add("you");
80 pSignals.putStringArrayList(Adjustment.KEY_PEOPLE, people);
81 Adjustment pAdjustment = new Adjustment("pkg", r.getKey(), pSignals, "", 0);
82 r.addAdjustment(pAdjustment);
83
84 Bundle sSignals = new Bundle();
85 ArrayList<SnoozeCriterion> snoozeCriteria = new ArrayList<>();
86 snoozeCriteria.add(new SnoozeCriterion("n", "n", "n"));
87 sSignals.putParcelableArrayList(Adjustment.KEY_SNOOZE_CRITERIA, snoozeCriteria);
88 Adjustment sAdjustment = new Adjustment("pkg", r.getKey(), sSignals, "", 0);
89 r.addAdjustment(sAdjustment);
90
91 Bundle gSignals = new Bundle();
92 gSignals.putString(Adjustment.KEY_GROUP_KEY, GroupHelper.AUTOGROUP_KEY);
93 Adjustment gAdjustment = new Adjustment("pkg", r.getKey(), gSignals, "", 0);
94 r.addAdjustment(gAdjustment);
95
96 assertFalse(r.getGroupKey().contains(GroupHelper.AUTOGROUP_KEY));
97 assertFalse(Objects.equals(people, r.getPeopleOverride()));
98 assertFalse(Objects.equals(snoozeCriteria, r.getSnoozeCriteria()));
99
100 assertNull(extractor.process(r));
101
102 assertTrue(r.getGroupKey().contains(GroupHelper.AUTOGROUP_KEY));
103 assertEquals(people, r.getPeopleOverride());
104 assertEquals(snoozeCriteria, r.getSnoozeCriteria());
105 }
106
107 private NotificationRecord generateRecord() {
108 NotificationChannel channel = new NotificationChannel("a", "a", IMPORTANCE_LOW);
109 final Notification.Builder builder = new Notification.Builder(getContext())
110 .setContentTitle("foo")
111 .setSmallIcon(android.R.drawable.sym_def_app_icon);
112 Notification n = builder.build();
113 StatusBarNotification sbn = new StatusBarNotification("", "", 0, "", 0,
114 0, n, UserHandle.ALL, null, System.currentTimeMillis());
115 return new NotificationRecord(getContext(), sbn, channel);
116 }
117}