1 /*
2  * Copyright (C) 2021 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 
17 package com.android.wm.shell.flicker.bubble
18 
19 import android.app.INotificationManager
20 import android.app.NotificationManager
21 import android.content.Context
22 import android.content.pm.PackageManager
23 import android.os.ServiceManager
24 import android.tools.common.Rotation
25 import android.tools.device.flicker.legacy.FlickerBuilder
26 import android.tools.device.flicker.legacy.FlickerTestData
27 import android.tools.device.flicker.legacy.LegacyFlickerTest
28 import android.tools.device.flicker.legacy.LegacyFlickerTestFactory
29 import android.tools.device.helpers.SYSTEMUI_PACKAGE
30 import androidx.test.uiautomator.By
31 import androidx.test.uiautomator.UiObject2
32 import androidx.test.uiautomator.Until
33 import com.android.server.wm.flicker.helpers.LaunchBubbleHelper
34 import com.android.wm.shell.flicker.BaseTest
35 import org.junit.runners.Parameterized
36 
37 /** Base configurations for Bubble flicker tests */
38 abstract class BaseBubbleScreen(flicker: LegacyFlickerTest) : BaseTest(flicker) {
39 
40     protected val context: Context = instrumentation.context
41     protected val testApp = LaunchBubbleHelper(instrumentation)
42 
43     private val notifyManager =
44         INotificationManager.Stub.asInterface(
45             ServiceManager.getService(Context.NOTIFICATION_SERVICE)
46         )
47 
48     private val uid =
49         context.packageManager
50             .getApplicationInfo(testApp.`package`, PackageManager.ApplicationInfoFlags.of(0))
51             .uid
52 
53     @JvmOverloads
54     protected open fun buildTransition(
55         extraSpec: FlickerBuilder.() -> Unit = {}
56     ): FlickerBuilder.() -> Unit {
57         return {
58             setup {
59                 notifyManager.setBubblesAllowed(
60                     testApp.`package`,
61                     uid,
62                     NotificationManager.BUBBLE_PREFERENCE_ALL
63                 )
64                 testApp.launchViaIntent(wmHelper)
65                 waitAndGetAddBubbleBtn()
66                 waitAndGetCancelAllBtn()
67             }
68 
69             teardown {
70                 notifyManager.setBubblesAllowed(
71                     testApp.`package`,
72                     uid,
73                     NotificationManager.BUBBLE_PREFERENCE_NONE
74                 )
75                 device.wait(
76                     Until.gone(By.res(SYSTEM_UI_PACKAGE, BUBBLE_RES_NAME)),
77                     FIND_OBJECT_TIMEOUT
78                 )
79                 testApp.exit(wmHelper)
80             }
81 
82             extraSpec(this)
83         }
84     }
85 
86     protected fun FlickerTestData.waitAndGetAddBubbleBtn(): UiObject2? =
87         device.wait(Until.findObject(By.text("Add Bubble")), FIND_OBJECT_TIMEOUT)
88     protected fun FlickerTestData.waitAndGetCancelAllBtn(): UiObject2? =
89         device.wait(Until.findObject(By.text("Cancel All Bubble")), FIND_OBJECT_TIMEOUT)
90 
91     companion object {
92         @Parameterized.Parameters(name = "{0}")
93         @JvmStatic
94         fun getParams() =
95             LegacyFlickerTestFactory.nonRotationTests(
96                 supportedRotations = listOf(Rotation.ROTATION_0)
97             )
98 
99         const val FIND_OBJECT_TIMEOUT = 4000L
100         const val SYSTEM_UI_PACKAGE = SYSTEMUI_PACKAGE
101         const val BUBBLE_RES_NAME = "bubble_view"
102     }
103 }
104