Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 281f5eea authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Android (Google) Code Review
Browse files

Merge changes I6a3e2fb9,Icc102a51

* changes:
  Introduce .wm.flicker.pip.tv.TvPipNotificationTests
  Prepare .wm.shell.flicker for test migration
parents cbd281c3 757e60e0
Loading
Loading
Loading
Loading
+11 −0
Original line number Original line Diff line number Diff line
@@ -32,8 +32,19 @@
    <!-- Workaround grant runtime permission exception from b/152733071 -->
    <!-- Workaround grant runtime permission exception from b/152733071 -->
    <uses-permission android:name="android.permission.PACKAGE_USAGE_STATS"/>
    <uses-permission android:name="android.permission.PACKAGE_USAGE_STATS"/>
    <uses-permission android:name="android.permission.READ_LOGS"/>
    <uses-permission android:name="android.permission.READ_LOGS"/>
    <!-- Force-stop test apps -->
    <uses-permission android:name="android.permission.FORCE_STOP_PACKAGES"/>
    <application>
    <application>
        <uses-library android:name="android.test.runner"/>
        <uses-library android:name="android.test.runner"/>

        <service android:name=".NotificationListener"
                 android:exported="true"
                 android:label="WMShellTestsNotificationListenerService"
                 android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE">
            <intent-filter>
                <action android:name="android.service.notification.NotificationListenerService" />
            </intent-filter>
        </service>
    </application>
    </application>


    <instrumentation android:name="androidx.test.runner.AndroidJUnitRunner"
    <instrumentation android:name="androidx.test.runner.AndroidJUnitRunner"
+36 −0
Original line number Original line Diff line number Diff line
@@ -14,18 +14,23 @@
 * limitations under the License.
 * limitations under the License.
 */
 */


package com.android.wm.shell.flicker.helpers
package com.android.wm.shell.flicker


import android.app.Instrumentation
import android.content.ComponentName
import android.support.test.launcherhelper.ILauncherStrategy
import com.android.server.wm.flicker.helpers.StandardAppHelper


abstract class FlickerAppHelper(
const val IME_WINDOW_NAME = "InputMethod"
    instr: Instrumentation,
const val PIP_WINDOW_NAME = "PipMenuActivity"
    launcherName: String,

    launcherStrategy: ILauncherStrategy
// Test App
) : StandardAppHelper(instr, sFlickerPackage, launcherName, launcherStrategy) {
const val TEST_APP_PACKAGE_NAME = "com.android.wm.shell.flicker.testapp"
    companion object {
// Test App > Pip Activity
        var sFlickerPackage = "com.android.wm.shell.flicker.testapp"
val TEST_APP_PIP_ACTIVITY_COMPONENT_NAME: ComponentName = ComponentName.createRelative(
    }
        TEST_APP_PACKAGE_NAME, ".PipActivity")
}
const val TEST_APP_PIP_ACTIVITY_LABEL = "PipApp"
const val TEST_APP_PIP_ACTIVITY_WINDOW_NAME = "PipActivity"
// Test App > Ime Activity
val TEST_APP_IME_ACTIVITY_COMPONENT_NAME: ComponentName = ComponentName.createRelative(
        TEST_APP_PACKAGE_NAME, ".ImeActivity")
const val TEST_APP_IME_ACTIVITY_LABEL = "ImeApp"

const val SYSTEM_UI_PACKAGE_NAME = "com.android.systemui"
 No newline at end of file
+4 −0
Original line number Original line Diff line number Diff line
@@ -16,6 +16,7 @@


package com.android.wm.shell.flicker
package com.android.wm.shell.flicker


import android.content.pm.PackageManager
import android.os.RemoteException
import android.os.RemoteException
import android.os.SystemClock
import android.os.SystemClock
import android.platform.helpers.IAppHelper
import android.platform.helpers.IAppHelper
@@ -41,6 +42,9 @@ abstract class FlickerTestBase {
    val uiDevice by lazy {
    val uiDevice by lazy {
        UiDevice.getInstance(instrumentation)
        UiDevice.getInstance(instrumentation)
    }
    }
    val packageManager: PackageManager by lazy {
        instrumentation.context.getPackageManager()
    }


    /**
    /**
     * Build a test tag for the test
     * Build a test tag for the test
+89 −0
Original line number Original line Diff line number Diff line
/*
 * Copyright (C) 2020 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.wm.shell.flicker

import android.service.notification.NotificationListenerService
import android.service.notification.StatusBarNotification
import android.util.Log
import com.android.compatibility.common.util.SystemUtil.runShellCommand

class NotificationListener : NotificationListenerService() {
    private val notifications: MutableMap<Any, StatusBarNotification> = mutableMapOf()

    override fun onNotificationPosted(sbn: StatusBarNotification) {
        if (DEBUG) Log.d(TAG, "onNotificationPosted: $sbn")
        notifications[sbn.key] = sbn
    }

    override fun onNotificationRemoved(sbn: StatusBarNotification) {
        if (DEBUG) Log.d(TAG, "onNotificationRemoved: $sbn")
        notifications.remove(sbn.key)
    }

    override fun onListenerConnected() {
        if (DEBUG) Log.d(TAG, "onListenerConnected")
        instance = this
    }

    override fun onListenerDisconnected() {
        if (DEBUG) Log.d(TAG, "onListenerDisconnected")
        instance = null
        notifications.clear()
    }

    companion object {
        private const val DEBUG = false
        private const val TAG = "WMShellFlickerTests_NotificationListener"

        private const val CMD_NOTIFICATION_ALLOW_LISTENER = "cmd notification allow_listener %s"
        private const val CMD_NOTIFICATION_DISALLOW_LISTENER =
                "cmd notification disallow_listener %s"
        private const val COMPONENT_NAME = "com.android.wm.shell.flicker/.NotificationListener"

        private var instance: NotificationListener? = null

        fun startNotificationListener(): Boolean {
            if (instance != null) {
                return true
            }

            runShellCommand(CMD_NOTIFICATION_ALLOW_LISTENER.format(COMPONENT_NAME))
            return wait { instance != null }
        }

        fun stopNotificationListener(): Boolean {
            if (instance == null) {
                return true
            }

            runShellCommand(CMD_NOTIFICATION_DISALLOW_LISTENER.format(COMPONENT_NAME))
            return wait { instance == null }
        }

        fun waitForNotificationToAppear(predicate: (StatusBarNotification) -> Boolean): Boolean {
            return instance?.let {
                wait { it.notifications.values.any(predicate) }
            } ?: throw IllegalStateException("NotificationListenerService is not connected")
        }

        fun waitForNotificationToDisappear(predicate: (StatusBarNotification) -> Boolean): Boolean {
            return instance?.let {
                wait { it.notifications.values.none(predicate) }
            } ?: throw IllegalStateException("NotificationListenerService is not connected")
        }
    }
}
 No newline at end of file
+45 −0
Original line number Original line Diff line number Diff line
/*
 * Copyright (C) 2020 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.wm.shell.flicker

import android.os.SystemClock

private const val DEFAULT_TIMEOUT = 10000L
private const val DEFAULT_POLL_INTERVAL = 1000L

fun wait(condition: () -> Boolean): Boolean {
    val (success, _) = waitForResult(extractor = condition, validator = { it })
    return success
}

fun <R> waitForResult(
    timeout: Long = DEFAULT_TIMEOUT,
    interval: Long = DEFAULT_POLL_INTERVAL,
    extractor: () -> R,
    validator: (R) -> Boolean = { it != null }
): Pair<Boolean, R?> {
    val startTime = SystemClock.uptimeMillis()
    do {
        val result = extractor()
        if (validator(result)) {
            return (true to result)
        }
        SystemClock.sleep(interval)
    } while (SystemClock.uptimeMillis() - startTime < timeout)

    return (false to null)
}
 No newline at end of file
Loading