blob: 8e9770fac01db5be6d735448cab66bb30cf9eaf8 [file] [log] [blame]
Ritwika Mitra8deec2a2019-07-22 13:36:04 -07001/*
2 * Copyright (C) 2019 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 */
16package com.android.car.assist.client;
17
18import static com.google.common.truth.Truth.assertThat;
19
20import android.app.Notification;
21import android.app.PendingIntent;
22import android.content.Context;
23import android.content.Intent;
24import android.os.UserHandle;
25import android.service.notification.StatusBarNotification;
26
27import androidx.core.app.NotificationCompat;
28import androidx.core.app.NotificationCompat.Action;
29import androidx.core.app.NotificationCompat.MessagingStyle;
30import androidx.core.app.Person;
31import androidx.core.app.RemoteInput;
32
33import org.junit.Before;
34import org.junit.Test;
35import org.junit.runner.RunWith;
36import org.robolectric.RobolectricTestRunner;
37import org.robolectric.RuntimeEnvironment;
38import org.robolectric.annotation.Config;
39
40@RunWith(RobolectricTestRunner.class)
41@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
42public class CarAssistUtilsTest {
43
44 private Context mContext;
45 private static final String PKG_1 = "package_1";
46 private static final String OP_PKG = "OpPackage";
47 private static final int ID = 1;
48 private static final String TAG = "Tag";
49 private static final int UID = 2;
50 private static final int INITIAL_PID = 3;
51 private static final String CHANNEL_ID = "CHANNEL_ID";
52 private static final String CONTENT_TITLE = "CONTENT_TITLE";
53 private static final String STATIC_USER_NAME = "STATIC_USER_NAME";
54 private static final String SENDER_NAME = "Larry";
55 private static final String SENDER_CONTACT_URI = "TEST_SENDER_URI";
56 private static final String REMOTE_INPUT_KEY = "REMOTE_INPUT_KEY";
57 private static final String REPLY_ACTION = "test.package.REPLY";
58 private static final String READ_ACTION = "test.package.READ";
59 private static final long POST_TIME = 12345L;
60 private static final int ICON = android.R.drawable.ic_media_play;
61 private static final String OVERRIDE_GROUP_KEY = "OVERRIDE_GROUP_KEY";
62 private static final UserHandle USER_HANDLE = new UserHandle(12);
63
64 @Before
65 public void setup() {
66 mContext = RuntimeEnvironment.application;
67 }
68
69 @Test
70 public void testCarCompatMessagingNotification_qualifyingNotification() {
71 assertThat(CarAssistUtils.isCarCompatibleMessagingNotification(
72 buildStatusBarNotification(/* hasReplyAction */ true, /* hasMessagingStyle */
73 true))).isTrue();
74 }
75
76 @Test
77 public void testCarCompatMessagingNotification_noReplyNotification() {
78 assertThat(CarAssistUtils.isCarCompatibleMessagingNotification(
79 buildStatusBarNotification(/* hasReplyAction */ false, /* hasMessagingStyle */
80 true))).isTrue();
81 }
82
83 @Test
84 public void testCarCompatMessagingNotifcation_noMessagingStyleNotification() {
85 assertThat(CarAssistUtils.isCarCompatibleMessagingNotification(
86 buildStatusBarNotification(/* hasReplyAction */ true, /* hasMessagingStyle */
87 false))).isFalse();
88 }
89
90 private StatusBarNotification buildStatusBarNotification(boolean hasReplyAction,
91 boolean hasMessagingStyle) {
92
93 NotificationCompat.Builder builder = new NotificationCompat.Builder(mContext, CHANNEL_ID)
94 .setContentTitle(CONTENT_TITLE)
95 .setCategory(Notification.CATEGORY_MESSAGE)
96 .addAction(buildMarkAsReadAction())
97 .setShowWhen(true);
98
99 if (hasReplyAction) {
100 builder.addAction(buildReplyAction());
101 }
102
103 if (hasMessagingStyle) {
104 builder.setStyle(buildMessagingStyle());
105 }
106
107 return new StatusBarNotification(PKG_1, OP_PKG,
108 ID, TAG, UID, INITIAL_PID, builder.build(), USER_HANDLE,
109 OVERRIDE_GROUP_KEY, POST_TIME);
110 }
111
112 private Action buildMarkAsReadAction() {
113 Intent intent = new Intent(mContext, this.getClass()).setAction(READ_ACTION);
114 PendingIntent pendingIntent = PendingIntent.getService(mContext, 0, intent,
115 PendingIntent.FLAG_UPDATE_CURRENT);
116 return new Action.Builder(ICON, "read", pendingIntent)
117 .setSemanticAction(Action.SEMANTIC_ACTION_MARK_AS_READ)
118 .setShowsUserInterface(false)
119 .build();
120 }
121
122 private Action buildReplyAction() {
123 Intent intent = new Intent(mContext, this.getClass())
124 .setAction(REPLY_ACTION);
125 PendingIntent replyIntent = PendingIntent.getService(mContext, 0, intent,
126 PendingIntent.FLAG_UPDATE_CURRENT);
127
128 return new Action.Builder(ICON,
129 "reply", replyIntent)
130 .setSemanticAction(Action.SEMANTIC_ACTION_REPLY)
131 .setShowsUserInterface(false)
132 .addRemoteInput(
133 new RemoteInput.Builder(REMOTE_INPUT_KEY)
134 .build()
135 )
136 .build();
137 }
138
139 private MessagingStyle buildMessagingStyle() {
140 Person user = new Person.Builder()
141 .setName(STATIC_USER_NAME)
142 .build();
143 NotificationCompat.MessagingStyle messagingStyle =
144 new NotificationCompat.MessagingStyle(user);
145 Person sender = new Person.Builder()
146 .setName(SENDER_NAME)
147 .setUri(SENDER_CONTACT_URI)
148 .build();
149 messagingStyle.addMessage("Hello World", POST_TIME, sender);
150 return messagingStyle;
151 }
152}