Ritwika Mitra | 8deec2a | 2019-07-22 13:36:04 -0700 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package com.android.car.assist.client; |
| 17 | |
| 18 | import static com.google.common.truth.Truth.assertThat; |
| 19 | |
| 20 | import android.app.Notification; |
| 21 | import android.app.PendingIntent; |
| 22 | import android.content.Context; |
| 23 | import android.content.Intent; |
| 24 | import android.os.UserHandle; |
| 25 | import android.service.notification.StatusBarNotification; |
| 26 | |
| 27 | import androidx.core.app.NotificationCompat; |
| 28 | import androidx.core.app.NotificationCompat.Action; |
| 29 | import androidx.core.app.NotificationCompat.MessagingStyle; |
| 30 | import androidx.core.app.Person; |
| 31 | import androidx.core.app.RemoteInput; |
| 32 | |
| 33 | import org.junit.Before; |
| 34 | import org.junit.Test; |
| 35 | import org.junit.runner.RunWith; |
| 36 | import org.robolectric.RobolectricTestRunner; |
| 37 | import org.robolectric.RuntimeEnvironment; |
| 38 | import org.robolectric.annotation.Config; |
| 39 | |
| 40 | @RunWith(RobolectricTestRunner.class) |
| 41 | @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION) |
| 42 | public 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 | } |