1 package com.android.systemui.statusbar.notification.logging
2 
3 import android.app.Notification
4 import android.graphics.Bitmap
5 import android.graphics.drawable.Icon
6 import android.testing.TestableLooper
7 import android.widget.RemoteViews
8 import androidx.test.ext.junit.runners.AndroidJUnit4
9 import androidx.test.filters.SmallTest
10 import com.android.systemui.SysuiTestCase
11 import com.android.systemui.statusbar.notification.row.NotificationRowContentBinder
12 import com.android.systemui.statusbar.notification.row.NotificationTestHelper
13 import com.android.systemui.tests.R
14 import com.google.common.truth.Truth.assertThat
15 import org.junit.Before
16 import org.junit.Test
17 import org.junit.runner.RunWith
18 
19 @SmallTest
20 @RunWith(AndroidJUnit4::class)
21 @TestableLooper.RunWithLooper
22 class NotificationMemoryViewWalkerTest : SysuiTestCase() {
23 
24     private lateinit var testHelper: NotificationTestHelper
25 
26     @Before
setUpnull27     fun setUp() {
28         allowTestableLooperAsMainThread()
29         testHelper = NotificationTestHelper(mContext, mDependency, TestableLooper.get(this))
30     }
31 
32     @Test
testViewWalker_nullRow_returnsEmptyViewnull33     fun testViewWalker_nullRow_returnsEmptyView() {
34         val result = NotificationMemoryViewWalker.getViewUsage(null)
35         assertThat(result).isNotNull()
36         assertThat(result).isEmpty()
37     }
38 
39     @Test
testViewWalker_plainNotificationnull40     fun testViewWalker_plainNotification() {
41         val row = testHelper.createRow()
42         val result = NotificationMemoryViewWalker.getViewUsage(row)
43         assertThat(result).hasSize(3)
44         assertThat(result)
45             .contains(NotificationViewUsage(ViewType.PRIVATE_EXPANDED_VIEW, 0, 0, 0, 0, 0, 0))
46         assertThat(result)
47             .contains(NotificationViewUsage(ViewType.PRIVATE_CONTRACTED_VIEW, 0, 0, 0, 0, 0, 0))
48         assertThat(result).contains(NotificationViewUsage(ViewType.TOTAL, 0, 0, 0, 0, 0, 0))
49     }
50 
51     @Test
testViewWalker_plainNotification_withPublicViewnull52     fun testViewWalker_plainNotification_withPublicView() {
53         val icon = Icon.createWithBitmap(Bitmap.createBitmap(10, 10, Bitmap.Config.ARGB_8888))
54         val publicIcon = Icon.createWithBitmap(Bitmap.createBitmap(20, 20, Bitmap.Config.ARGB_8888))
55         testHelper.setDefaultInflationFlags(NotificationRowContentBinder.FLAG_CONTENT_VIEW_ALL)
56         val row =
57             testHelper.createRow(
58                 Notification.Builder(mContext)
59                     .setContentText("Test")
60                     .setContentTitle("title")
61                     .setSmallIcon(icon)
62                     .setPublicVersion(
63                         Notification.Builder(mContext)
64                             .setContentText("Public Test")
65                             .setContentTitle("title")
66                             .setSmallIcon(publicIcon)
67                             .build()
68                     )
69                     .build()
70             )
71         val result = NotificationMemoryViewWalker.getViewUsage(row)
72         assertThat(result).hasSize(4)
73         assertThat(result)
74             .contains(
75                 NotificationViewUsage(
76                     ViewType.PRIVATE_EXPANDED_VIEW,
77                     icon.bitmap.allocationByteCount,
78                     0,
79                     0,
80                     0,
81                     0,
82                     icon.bitmap.allocationByteCount
83                 )
84             )
85         assertThat(result)
86             .contains(
87                 NotificationViewUsage(
88                     ViewType.PRIVATE_CONTRACTED_VIEW,
89                     icon.bitmap.allocationByteCount,
90                     0,
91                     0,
92                     0,
93                     0,
94                     icon.bitmap.allocationByteCount
95                 )
96             )
97         assertThat(result)
98             .contains(
99                 NotificationViewUsage(
100                     ViewType.PUBLIC_VIEW,
101                     publicIcon.bitmap.allocationByteCount,
102                     0,
103                     0,
104                     0,
105                     0,
106                     publicIcon.bitmap.allocationByteCount
107                 )
108             )
109         assertThat(result)
110             .contains(
111                 NotificationViewUsage(
112                     ViewType.TOTAL,
113                     icon.bitmap.allocationByteCount + publicIcon.bitmap.allocationByteCount,
114                     0,
115                     0,
116                     0,
117                     0,
118                     icon.bitmap.allocationByteCount + publicIcon.bitmap.allocationByteCount
119                 )
120             )
121     }
122 
123     @Test
testViewWalker_bigPictureNotificationnull124     fun testViewWalker_bigPictureNotification() {
125         val bigPicture = Bitmap.createBitmap(200, 200, Bitmap.Config.ARGB_8888)
126         val icon = Icon.createWithBitmap(Bitmap.createBitmap(10, 10, Bitmap.Config.ARGB_8888))
127         val largeIcon = Icon.createWithBitmap(Bitmap.createBitmap(30, 30, Bitmap.Config.ARGB_8888))
128         val row =
129             testHelper.createRow(
130                 Notification.Builder(mContext)
131                     .setContentText("Test")
132                     .setContentTitle("title")
133                     .setSmallIcon(icon)
134                     .setLargeIcon(largeIcon)
135                     .setStyle(Notification.BigPictureStyle().bigPicture(bigPicture))
136                     .build()
137             )
138         val result = NotificationMemoryViewWalker.getViewUsage(row)
139         assertThat(result).hasSize(3)
140         assertThat(result)
141             .contains(
142                 NotificationViewUsage(
143                     ViewType.PRIVATE_EXPANDED_VIEW,
144                     icon.bitmap.allocationByteCount,
145                     largeIcon.bitmap.allocationByteCount,
146                     0,
147                     bigPicture.allocationByteCount,
148                     0,
149                     bigPicture.allocationByteCount +
150                         icon.bitmap.allocationByteCount +
151                         largeIcon.bitmap.allocationByteCount
152                 )
153             )
154 
155         assertThat(result)
156             .contains(
157                 NotificationViewUsage(
158                     ViewType.PRIVATE_CONTRACTED_VIEW,
159                     icon.bitmap.allocationByteCount,
160                     largeIcon.bitmap.allocationByteCount,
161                     0,
162                     0,
163                     0,
164                     icon.bitmap.allocationByteCount + largeIcon.bitmap.allocationByteCount
165                 )
166             )
167         assertThat(result)
168             .contains(
169                 NotificationViewUsage(
170                     ViewType.TOTAL,
171                     icon.bitmap.allocationByteCount,
172                     largeIcon.bitmap.allocationByteCount,
173                     0,
174                     bigPicture.allocationByteCount,
175                     0,
176                     bigPicture.allocationByteCount +
177                         icon.bitmap.allocationByteCount +
178                         largeIcon.bitmap.allocationByteCount
179                 )
180             )
181     }
182 
183     @Test
testViewWalker_customViewnull184     fun testViewWalker_customView() {
185         val icon = Icon.createWithBitmap(Bitmap.createBitmap(10, 10, Bitmap.Config.ARGB_8888))
186         val bitmap = Bitmap.createBitmap(200, 200, Bitmap.Config.ARGB_8888)
187 
188         val views = RemoteViews(mContext.packageName, R.layout.custom_view_dark)
189         views.setImageViewBitmap(R.id.custom_view_dark_image, bitmap)
190         val row =
191             testHelper.createRow(
192                 Notification.Builder(mContext)
193                     .setContentText("Test")
194                     .setContentTitle("title")
195                     .setSmallIcon(icon)
196                     .setCustomContentView(views)
197                     .setCustomBigContentView(views)
198                     .build()
199             )
200         val result = NotificationMemoryViewWalker.getViewUsage(row)
201         assertThat(result).hasSize(3)
202         assertThat(result)
203             .contains(
204                 NotificationViewUsage(
205                     ViewType.PRIVATE_CONTRACTED_VIEW,
206                     icon.bitmap.allocationByteCount,
207                     0,
208                     0,
209                     0,
210                     bitmap.allocationByteCount,
211                     bitmap.allocationByteCount + icon.bitmap.allocationByteCount
212                 )
213             )
214         assertThat(result)
215             .contains(
216                 NotificationViewUsage(
217                     ViewType.PRIVATE_EXPANDED_VIEW,
218                     icon.bitmap.allocationByteCount,
219                     0,
220                     0,
221                     0,
222                     bitmap.allocationByteCount,
223                     bitmap.allocationByteCount + icon.bitmap.allocationByteCount
224                 )
225             )
226         assertThat(result)
227             .contains(
228                 NotificationViewUsage(
229                     ViewType.TOTAL,
230                     icon.bitmap.allocationByteCount,
231                     0,
232                     0,
233                     0,
234                     bitmap.allocationByteCount,
235                     bitmap.allocationByteCount + icon.bitmap.allocationByteCount
236                 )
237             )
238     }
239 }
240