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 android.app.notification.legacy34.cts
17 
18 import android.Manifest.permission.POST_NOTIFICATIONS
19 import android.app.Notification
20 import android.app.Notification.FLAG_NO_CLEAR
21 import android.app.NotificationChannel
22 import android.app.NotificationManager
23 import android.app.stubs.shared.NotificationHelper
24 import android.content.Context
25 import android.service.notification.StatusBarNotification
26 import androidx.test.InstrumentationRegistry
27 import androidx.test.runner.AndroidJUnit4
28 import org.junit.After
29 import org.junit.Assert.assertEquals
30 import org.junit.Assert.assertNotEquals
31 import org.junit.Assert.assertNotNull
32 import org.junit.Before
33 import org.junit.Test
34 import org.junit.runner.RunWith
35 
36 /**
37  * Home for tests that need to verify behavior for apps that target sdk version 34.
38  */
39 @RunWith(AndroidJUnit4::class)
40 class NotificationManagerApi34Test {
41     private lateinit var notificationManager: NotificationManager
42     private lateinit var context: Context
43     private lateinit var helper: NotificationHelper
44 
45     @Before
setUpnull46     fun setUp() {
47         context = InstrumentationRegistry.getContext()
48         InstrumentationRegistry.getInstrumentation().getUiAutomation()
49                 .adoptShellPermissionIdentity(POST_NOTIFICATIONS)
50         helper = NotificationHelper(context)
51         notificationManager = context.getSystemService(
52                 Context.NOTIFICATION_SERVICE) as NotificationManager
53         notificationManager.createNotificationChannel(
54             NotificationChannel(
55                 NOTIFICATION_CHANNEL_ID,
56                 "name",
57                 NotificationManager.IMPORTANCE_DEFAULT
58             )
59         )
60         assertEquals(context.applicationInfo.targetSdkVersion, 34)
61     }
62 
63     @After
tearDownnull64     fun tearDown() {
65         notificationManager.cancelAll()
66     }
67 
68     @Test
testMediaStyle_noClearFlagNotSetnull69     fun testMediaStyle_noClearFlagNotSet() {
70         val id = 99
71         val notification: Notification = Notification.Builder(context, NOTIFICATION_CHANNEL_ID)
72             .setSmallIcon(R.drawable.icon_black)
73             .setStyle(Notification.MediaStyle())
74             .build()
75         notificationManager.notify(id, notification)
76         val sbn: StatusBarNotification = helper.findPostedNotification(
77             null,
78             id,
79             NotificationHelper.SEARCH_TYPE.APP
80         )
81         assertNotNull(sbn)
82         assertNotEquals(FLAG_NO_CLEAR, sbn.getNotification().flags and FLAG_NO_CLEAR)
83     }
84 
85     @Test
testCustomMediaStyle_noClearFlagNotSetnull86     fun testCustomMediaStyle_noClearFlagNotSet() {
87         val id = 99
88         val notification: Notification = Notification.Builder(context, NOTIFICATION_CHANNEL_ID)
89             .setSmallIcon(R.drawable.icon_black)
90             .setStyle(Notification.DecoratedMediaCustomViewStyle())
91             .build()
92         notificationManager.notify(id, notification)
93         val sbn: StatusBarNotification = helper.findPostedNotification(
94             null,
95             id,
96             NotificationHelper.SEARCH_TYPE.APP
97         )
98         assertNotNull(sbn)
99         assertNotEquals(FLAG_NO_CLEAR, sbn.getNotification().flags and FLAG_NO_CLEAR)
100     }
101 
102     companion object {
103         val TAG = NotificationManagerApi34Test::class.java.simpleName
104         val PKG = "android.app.notification.legacy34.cts"
105         const val NOTIFICATION_CHANNEL_ID = "NotificationManagerApi34Test"
106     }
107 }
108