1 /*
2  * Copyright (C) 2023 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.systemui.statusbar.notification.row
17 
18 import android.app.Notification
19 import android.app.Person
20 import android.platform.test.annotations.EnableFlags
21 import android.testing.TestableLooper
22 import androidx.test.ext.junit.runners.AndroidJUnit4
23 import androidx.test.filters.SmallTest
24 import com.android.internal.R
25 import com.android.systemui.SysuiTestCase
26 import com.android.systemui.statusbar.notification.row.NotificationRowContentBinder.FLAG_CONTENT_VIEW_SINGLE_LINE
27 import com.android.systemui.statusbar.notification.row.SingleLineViewInflater.inflateSingleLineViewHolder
28 import com.android.systemui.statusbar.notification.row.shared.AsyncHybridViewInflation
29 import com.android.systemui.statusbar.notification.row.ui.viewbinder.SingleLineConversationViewBinder
30 import com.android.systemui.util.mockito.mock
31 import kotlin.test.assertEquals
32 import org.junit.Before
33 import org.junit.Test
34 import org.junit.runner.RunWith
35 
36 @SmallTest
37 @RunWith(AndroidJUnit4::class)
38 @TestableLooper.RunWithLooper
39 class SingleLineConversationViewBinderTest : SysuiTestCase() {
40     private lateinit var notificationBuilder: Notification.Builder
41     private lateinit var helper: NotificationTestHelper
42 
43     @Before
setUpnull44     fun setUp() {
45         allowTestableLooperAsMainThread()
46         helper = NotificationTestHelper(context, mDependency, TestableLooper.get(this))
47         notificationBuilder = Notification.Builder(context, CHANNEL_ID)
48         notificationBuilder
49             .setSmallIcon(R.drawable.ic_corp_icon)
50             .setContentTitle(CONTENT_TITLE)
51             .setContentText(CONTENT_TEXT)
52     }
53 
54     @Test
55     @EnableFlags(AsyncHybridViewInflation.FLAG_NAME)
bindGroupConversationSingleLineViewnull56     fun bindGroupConversationSingleLineView() {
57         // GIVEN a row with a group conversation notification
58         val user =
59             Person.Builder()
60                 //                .setIcon(Icon.createWithResource(mContext,
61                 // R.drawable.ic_account_circle))
62                 .setName(USER_NAME)
63                 .build()
64         val style =
65             Notification.MessagingStyle(user)
66                 .addMessage(MESSAGE_TEXT, System.currentTimeMillis(), user)
67                 .addMessage(
68                     "How about lunch?",
69                     System.currentTimeMillis(),
70                     Person.Builder().setName("user2").build()
71                 )
72                 .setGroupConversation(true)
73         notificationBuilder.setStyle(style).setShortcutId(SHORTCUT_ID)
74         val notification = notificationBuilder.build()
75         val row = helper.createRow(notification)
76 
77         val viewHolder =
78             inflateSingleLineViewHolder(
79                 isConversation = true,
80                 reinflateFlags = FLAG_CONTENT_VIEW_SINGLE_LINE,
81                 entry = row.entry,
82                 context = context,
83                 logger = mock()
84             )
85                 as HybridConversationNotificationView
86         val viewModel =
87             SingleLineViewInflater.inflateSingleLineViewModel(
88                 notification = notification,
89                 messagingStyle = style,
90                 builder = notificationBuilder,
91                 systemUiContext = context,
92             )
93         // WHEN: binds the viewHolder
94         SingleLineConversationViewBinder.bind(
95             viewModel,
96             viewHolder,
97         )
98 
99         // THEN: the single-line conversation view should be bind with view model's corresponding
100         // fields
101         assertEquals(viewModel.titleText, viewHolder.titleView.text)
102         assertEquals(viewModel.contentText, viewHolder.textView.text)
103         assertEquals(
104             viewModel.conversationData?.conversationSenderName,
105             viewHolder.conversationSenderNameView.text
106         )
107     }
108 
109     private companion object {
110         const val CHANNEL_ID = "CHANNEL_ID"
111         const val CONTENT_TITLE = "CONTENT_TITLE"
112         const val CONTENT_TEXT = "CONTENT_TEXT"
113         const val USER_NAME = "USER_NAME"
114         const val MESSAGE_TEXT = "MESSAGE_TEXT"
115         const val SHORTCUT_ID = "Shortcut"
116     }
117 }
118