Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 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.server.notification; |
| 18 | |
| 19 | import static android.app.NotificationManager.IMPORTANCE_LOW; |
| 20 | |
| 21 | import static junit.framework.Assert.assertEquals; |
| 22 | import static junit.framework.Assert.assertFalse; |
| 23 | import static junit.framework.Assert.assertNull; |
| 24 | import static junit.framework.Assert.assertTrue; |
| 25 | |
| 26 | import android.app.Notification; |
| 27 | import android.app.NotificationChannel; |
| 28 | import android.os.Bundle; |
| 29 | import android.os.UserHandle; |
| 30 | import android.service.notification.Adjustment; |
| 31 | import android.service.notification.SnoozeCriterion; |
| 32 | import android.service.notification.StatusBarNotification; |
| 33 | |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 34 | import com.android.server.UiServiceTestCase; |
| 35 | |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 36 | import org.junit.Test; |
| 37 | |
| 38 | import java.util.ArrayList; |
| 39 | import java.util.Objects; |
| 40 | |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 41 | public class NotificationAdjustmentExtractorTest extends UiServiceTestCase { |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 42 | |
| 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 | } |