1 /*
2  * Copyright (C) 2018 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.systemui.statusbar.phone;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 
21 import static org.mockito.Mockito.mock;
22 import static org.mockito.Mockito.when;
23 
24 import android.app.ActivityManager;
25 import android.app.Notification;
26 import android.content.Context;
27 import android.os.UserHandle;
28 import android.service.notification.StatusBarNotification;
29 
30 import com.android.systemui.R;
31 import com.android.systemui.statusbar.SbnBuilder;
32 import com.android.systemui.statusbar.notification.collection.NotificationEntry;
33 import com.android.systemui.statusbar.notification.collection.NotificationEntryBuilder;
34 import com.android.systemui.statusbar.notification.row.ExpandableNotificationRow;
35 
36 /**
37  * Helper class for creating groups/summaries without having to inflate them.
38  */
39 public final class NotificationGroupTestHelper {
40     private static final String TEST_CHANNEL_ID = "test_channel";
41     private static final String TEST_GROUP_ID = "test_group";
42     private static final String TEST_PACKAGE_NAME = "test_pkg";
43     private int mId = 0;
44     private final Context mContext;
45 
NotificationGroupTestHelper(Context context)46     public NotificationGroupTestHelper(Context context) {
47         mContext = context;
48     }
49 
createSummaryNotification()50     public NotificationEntry createSummaryNotification() {
51         return createSummaryNotification(Notification.GROUP_ALERT_ALL, mId++, null);
52     }
53 
createSummaryNotification(int groupAlertBehavior)54     public NotificationEntry createSummaryNotification(int groupAlertBehavior) {
55         return createSummaryNotification(groupAlertBehavior, mId++, null);
56     }
57 
createSummaryNotification(int groupAlertBehavior, int id, String tag)58     public NotificationEntry createSummaryNotification(int groupAlertBehavior, int id, String tag) {
59         return createEntry(id, tag, true, groupAlertBehavior);
60     }
61 
createSummaryNotification( int groupAlertBehavior, int id, String tag, long when)62     public NotificationEntry createSummaryNotification(
63             int groupAlertBehavior, int id, String tag, long when) {
64         NotificationEntry entry = createSummaryNotification(groupAlertBehavior, id, tag);
65         entry.getSbn().getNotification().when = when;
66         return entry;
67     }
68 
createChildNotification()69     public NotificationEntry createChildNotification() {
70         return createChildNotification(Notification.GROUP_ALERT_ALL);
71     }
72 
createChildNotification(int groupAlertBehavior)73     public NotificationEntry createChildNotification(int groupAlertBehavior) {
74         return createEntry(mId++, null, false, groupAlertBehavior);
75     }
76 
createChildNotification(int groupAlertBehavior, int id, String tag)77     public NotificationEntry createChildNotification(int groupAlertBehavior, int id, String tag) {
78         return createEntry(id, tag, false, groupAlertBehavior);
79     }
80 
createChildNotification( int groupAlertBehavior, int id, String tag, long when)81     public NotificationEntry createChildNotification(
82             int groupAlertBehavior, int id, String tag, long when) {
83         NotificationEntry entry = createChildNotification(groupAlertBehavior, id, tag);
84         entry.getSbn().getNotification().when = when;
85         return entry;
86     }
87 
createEntry(int id, String tag, boolean isSummary, int groupAlertBehavior)88     public NotificationEntry createEntry(int id, String tag, boolean isSummary,
89             int groupAlertBehavior) {
90         Notification notif = new Notification.Builder(mContext, TEST_CHANNEL_ID)
91                 .setContentTitle("Title")
92                 .setSmallIcon(R.drawable.ic_person)
93                 .setGroupAlertBehavior(groupAlertBehavior)
94                 .setGroupSummary(isSummary)
95                 .setGroup(TEST_GROUP_ID)
96                 .build();
97         NotificationEntry entry = new NotificationEntryBuilder()
98                 .setPkg(TEST_PACKAGE_NAME)
99                 .setOpPkg(TEST_PACKAGE_NAME)
100                 .setId(id)
101                 .setNotification(notif)
102                 .setTag(tag)
103                 .setUser(new UserHandle(ActivityManager.getCurrentUser()))
104                 .build();
105 
106         ExpandableNotificationRow row = mock(ExpandableNotificationRow.class);
107         entry.setRow(row);
108         when(row.getEntry()).thenReturn(entry);
109         return entry;
110     }
111 
incrementPost(NotificationEntry entry, int increment)112     public StatusBarNotification incrementPost(NotificationEntry entry, int increment) {
113         StatusBarNotification oldSbn = entry.getSbn();
114         final long oldPostTime = oldSbn.getPostTime();
115         final long newPostTime = oldPostTime + increment;
116         entry.setSbn(new SbnBuilder(oldSbn)
117                 .setPostTime(newPostTime)
118                 .build());
119         assertThat(oldSbn.getPostTime()).isEqualTo(oldPostTime);
120         assertThat(entry.getSbn().getPostTime()).isEqualTo(newPostTime);
121         return oldSbn;
122     }
123 }
124