diff options
author | 2024-01-11 11:12:19 +0000 | |
---|---|---|
committer | 2024-01-11 11:12:19 +0000 | |
commit | d9d3df6cca1034033c6136e2f7087abed729cb69 (patch) | |
tree | 6ff53ac0d387f9600caf7fdfd2f96a51df6d451d /tests/hostside | |
parent | 15929475a5e93c5fdb6a377352d859ddd10e711e (diff) | |
parent | 00c5174302d350721fd06622b151f2d439de3386 (diff) |
Merge "Log issue metadata when navigating from notification." into udc-mainline-prod
Diffstat (limited to 'tests/hostside')
4 files changed, 155 insertions, 9 deletions
diff --git a/tests/hostside/safetycenter/helper-app/Android.bp b/tests/hostside/safetycenter/helper-app/Android.bp index cf4372d99..a05f8d2f3 100644 --- a/tests/hostside/safetycenter/helper-app/Android.bp +++ b/tests/hostside/safetycenter/helper-app/Android.bp @@ -30,6 +30,7 @@ android_test_helper_app { static_libs: [ "androidx.test.rules", "androidx.test.ext.junit", + "safety-center-pending-intents", "safety-center-test-util-lib", ], }
\ No newline at end of file diff --git a/tests/hostside/safetycenter/helper-app/src/android/safetycenter/hostside/device/SafetyCenterInteractionLoggingHelperTests.kt b/tests/hostside/safetycenter/helper-app/src/android/safetycenter/hostside/device/SafetyCenterInteractionLoggingHelperTests.kt index 784701b8a..0f1356ca4 100644 --- a/tests/hostside/safetycenter/helper-app/src/android/safetycenter/hostside/device/SafetyCenterInteractionLoggingHelperTests.kt +++ b/tests/hostside/safetycenter/helper-app/src/android/safetycenter/hostside/device/SafetyCenterInteractionLoggingHelperTests.kt @@ -19,14 +19,21 @@ package android.safetycenter.hostside.device import android.content.Context import android.os.Bundle import android.safetycenter.SafetyCenterManager.EXTRA_SAFETY_SOURCES_GROUP_ID +import android.safetycenter.SafetyCenterManager.EXTRA_SAFETY_SOURCE_ID +import android.safetycenter.SafetyCenterManager.EXTRA_SAFETY_SOURCE_ISSUE_ID import androidx.test.core.app.ApplicationProvider import androidx.test.ext.junit.runners.AndroidJUnit4 import com.android.safetycenter.testing.SafetyCenterActivityLauncher.launchSafetyCenterActivity +import com.android.safetycenter.testing.SafetyCenterActivityLauncher.launchSafetyCenterQsActivity import com.android.safetycenter.testing.SafetyCenterActivityLauncher.openPageAndExit import com.android.safetycenter.testing.SafetyCenterFlags import com.android.safetycenter.testing.SafetyCenterTestConfigs +import com.android.safetycenter.testing.SafetyCenterTestConfigs.Companion.SINGLE_SOURCE_ID import com.android.safetycenter.testing.SafetyCenterTestHelper import com.android.safetycenter.testing.SafetyCenterTestRule +import com.android.safetycenter.testing.SafetySourceTestData +import com.android.safetycenter.testing.SafetySourceTestData.Companion.INFORMATION_ISSUE_ID +import com.android.safetycenter.testing.UiTestHelper.waitAllTextDisplayed import org.junit.Before import org.junit.Rule import org.junit.Test @@ -47,6 +54,7 @@ class SafetyCenterInteractionLoggingHelperTests { private val context: Context = ApplicationProvider.getApplicationContext() private val safetyCenterTestHelper = SafetyCenterTestHelper(context) private val safetyCenterTestConfigs = SafetyCenterTestConfigs(context) + private val safetySourceTestData = SafetySourceTestData(context) @get:Rule val safetyCenterTestRule = SafetyCenterTestRule(safetyCenterTestHelper) @@ -61,6 +69,34 @@ class SafetyCenterInteractionLoggingHelperTests { } @Test + fun openSafetyCenterFullFromQs() { + safetyCenterTestHelper.setConfig(safetyCenterTestConfigs.singleSourceConfig) + safetyCenterTestHelper.setData(SINGLE_SOURCE_ID, safetySourceTestData.informationWithIssue) + + context.launchSafetyCenterQsActivity { + openPageAndExit("Settings") { waitAllTextDisplayed("OK") } + } + } + + @Test + fun openSafetyCenterWithIssueIntent() { + safetyCenterTestHelper.setConfig(safetyCenterTestConfigs.singleSourceConfig) + + safetyCenterTestHelper.setData(SINGLE_SOURCE_ID, safetySourceTestData.informationWithIssue) + + val extras = Bundle() + extras.putString(EXTRA_SAFETY_SOURCE_ID, SINGLE_SOURCE_ID) + extras.putString(EXTRA_SAFETY_SOURCE_ISSUE_ID, INFORMATION_ISSUE_ID) + + context.launchSafetyCenterActivity(extras) {} + } + + @Test + fun openSafetyCenterQs() { + context.launchSafetyCenterQsActivity {} + } + + @Test fun openSubpageFromIntentExtra() { val config = safetyCenterTestConfigs.singleSourceConfig safetyCenterTestHelper.setConfig(config) diff --git a/tests/hostside/safetycenter/helper-app/src/android/safetycenter/hostside/device/SafetyCenterNotificationLoggingHelperTests.kt b/tests/hostside/safetycenter/helper-app/src/android/safetycenter/hostside/device/SafetyCenterNotificationLoggingHelperTests.kt index 458516379..dc3cb3fc2 100644 --- a/tests/hostside/safetycenter/helper-app/src/android/safetycenter/hostside/device/SafetyCenterNotificationLoggingHelperTests.kt +++ b/tests/hostside/safetycenter/helper-app/src/android/safetycenter/hostside/device/SafetyCenterNotificationLoggingHelperTests.kt @@ -21,12 +21,16 @@ import android.safetycenter.SafetySourceData import android.safetycenter.SafetySourceIssue import androidx.test.core.app.ApplicationProvider import androidx.test.ext.junit.runners.AndroidJUnit4 +import com.android.safetycenter.pendingintents.PendingIntentSender +import com.android.safetycenter.testing.SafetyCenterActivityLauncher import com.android.safetycenter.testing.SafetyCenterFlags import com.android.safetycenter.testing.SafetyCenterTestConfigs import com.android.safetycenter.testing.SafetyCenterTestConfigs.Companion.SINGLE_SOURCE_ID import com.android.safetycenter.testing.SafetyCenterTestHelper import com.android.safetycenter.testing.SafetyCenterTestRule import com.android.safetycenter.testing.SafetySourceTestData +import com.android.safetycenter.testing.StatusBarNotificationWithChannel +import com.android.safetycenter.testing.TestNotificationListener import org.junit.Before import org.junit.Rule import org.junit.Test @@ -49,7 +53,9 @@ class SafetyCenterNotificationLoggingHelperTests { private val safetySourceTestData = SafetySourceTestData(context) private val safetyCenterTestConfigs = SafetyCenterTestConfigs(context) - @get:Rule val safetyCenterTestRule = SafetyCenterTestRule(safetyCenterTestHelper) + @get:Rule + val safetyCenterTestRule = + SafetyCenterTestRule(safetyCenterTestHelper, withNotifications = true) @Before fun setUp() { @@ -64,6 +70,13 @@ class SafetyCenterNotificationLoggingHelperTests { safetyCenterTestHelper.setData(SINGLE_SOURCE_ID, newTestDataWithNotifiableIssue()) } + @Test + fun openSafetyCenterFromNotification() { + safetyCenterTestHelper.setData(SINGLE_SOURCE_ID, newTestDataWithNotifiableIssue()) + + sendContentPendingIntent(TestNotificationListener.waitForSingleNotification()) + } + private fun newTestDataWithNotifiableIssue(): SafetySourceData = safetySourceTestData .defaultCriticalDataBuilder() @@ -74,4 +87,17 @@ class SafetyCenterNotificationLoggingHelperTests { .build() ) .build() + + companion object { + private fun sendContentPendingIntent( + statusBarNotificationWithChannel: StatusBarNotificationWithChannel + ) { + val contentIntent = + statusBarNotificationWithChannel.statusBarNotification.notification.contentIntent + SafetyCenterActivityLauncher.executeBlockAndExit( + launchActivity = { PendingIntentSender.send(contentIntent) }, + block = {} // No action required + ) + } + } } diff --git a/tests/hostside/safetycenter/src/android/safetycenter/hostside/SafetyCenterInteractionLoggingHostTest.kt b/tests/hostside/safetycenter/src/android/safetycenter/hostside/SafetyCenterInteractionLoggingHostTest.kt index 5fe9e0a2a..91222d045 100644 --- a/tests/hostside/safetycenter/src/android/safetycenter/hostside/SafetyCenterInteractionLoggingHostTest.kt +++ b/tests/hostside/safetycenter/src/android/safetycenter/hostside/SafetyCenterInteractionLoggingHostTest.kt @@ -24,10 +24,13 @@ import com.android.compatibility.common.util.ApiLevelUtil import com.android.os.AtomsProto.Atom import com.android.os.AtomsProto.SafetyCenterInteractionReported import com.android.os.AtomsProto.SafetyCenterInteractionReported.Action +import com.android.os.AtomsProto.SafetyCenterInteractionReported.NavigationSource import com.android.os.AtomsProto.SafetyCenterInteractionReported.ViewType import com.android.tradefed.testtype.DeviceJUnit4ClassRunner import com.android.tradefed.testtype.junit4.BaseHostJUnit4Test import com.google.common.truth.Truth.assertThat +import java.math.BigInteger +import java.security.MessageDigest import org.junit.After import org.junit.Assume.assumeTrue import org.junit.Before @@ -67,15 +70,76 @@ class SafetyCenterInteractionLoggingHostTest : BaseHostJUnit4Test() { val safetyCenterViewedAtoms = getInteractionReportedAtoms(Action.SAFETY_CENTER_VIEWED) - assertThat(safetyCenterViewedAtoms).isNotEmpty() + assertThat(safetyCenterViewedAtoms).hasSize(1) + with(safetyCenterViewedAtoms.first()) { + assertThat(navigationSource).isEqualTo(NavigationSource.SOURCE_UNKNOWN) + assertThat(viewType).isEqualTo(ViewType.FULL) + } } @Test - fun sendNotification_recordsNotificationPostedEvent() { + fun openSafetyCenterQs_recordsSafetyCenterViewedEvent() { + helperAppRule.runTest(TEST_CLASS_NAME, "openSafetyCenterQs") + + val safetyCenterViewedAtoms = getInteractionReportedAtoms(Action.SAFETY_CENTER_VIEWED) + + assertThat(safetyCenterViewedAtoms).hasSize(1) + with(safetyCenterViewedAtoms.first()) { + assertThat(navigationSource).isEqualTo(NavigationSource.QUICK_SETTINGS_TILE) + assertThat(viewType).isEqualTo(ViewType.QUICK_SETTINGS) + } + } + + @Test + fun openSafetyCenterFullFromQs_recordsViewEventWithCorrectSource() { + helperAppRule.runTest(TEST_CLASS_NAME, "openSafetyCenterFullFromQs") + + val safetyCenterViewedAtoms = getInteractionReportedAtoms(Action.SAFETY_CENTER_VIEWED) + + val viewTypesToNavSources = + safetyCenterViewedAtoms.associate { Pair(it.viewType, it.navigationSource) } + assertThat(viewTypesToNavSources) + .containsEntry(ViewType.FULL, NavigationSource.QUICK_SETTINGS_TILE) + } + + @Test + fun openSafetyCenterWithIssueIntent_recordsViewEventWithAssociatedIssueMetadata() { + helperAppRule.runTest(TEST_CLASS_NAME, testMethodName = "openSafetyCenterWithIssueIntent") + + val safetyCenterViewedAtoms = getInteractionReportedAtoms(Action.SAFETY_CENTER_VIEWED) + + assertThat(safetyCenterViewedAtoms).hasSize(1) + with(safetyCenterViewedAtoms.first()) { + assertThat(navigationSource).isEqualTo(NavigationSource.NOTIFICATION) + assertThat(encodedSafetySourceId).isEqualTo(ENCODED_SINGLE_SOURCE_ID) + assertThat(encodedIssueTypeId).isEqualTo(ENCODED_ISSUE_TYPE_ID) + } + } + + @Test + fun openSafetyCenterWithNotification_recordsViewEventWithAssociatedIssueMetadata() { assumeAtLeastUpsideDownCake("Safety Center notification APIs require Android U+") helperAppRule.runTest( testClassName = ".SafetyCenterNotificationLoggingHelperTests", + testMethodName = "openSafetyCenterFromNotification" + ) + + val safetyCenterViewedAtoms = getInteractionReportedAtoms(Action.SAFETY_CENTER_VIEWED) + + assertThat(safetyCenterViewedAtoms).hasSize(1) + with(safetyCenterViewedAtoms.first()) { + assertThat(navigationSource).isEqualTo(NavigationSource.NOTIFICATION) + assertThat(encodedSafetySourceId).isEqualTo(ENCODED_SINGLE_SOURCE_ID) + assertThat(encodedIssueTypeId).isEqualTo(ENCODED_ISSUE_TYPE_ID) + } + } + + @Test + fun sendNotification_recordsNotificationPostedEvent() { + assumeAtLeastUpsideDownCake("Safety Center notification APIs require Android U+") + helperAppRule.runTest( + testClassName = ".SafetyCenterNotificationLoggingHelperTests", testMethodName = "sendNotification" ) @@ -97,8 +161,7 @@ class SafetyCenterInteractionLoggingHostTest : BaseHostJUnit4Test() { assertThat(safetyCenterViewedAtoms).hasSize(1) with(safetyCenterViewedAtoms.first()) { assertThat(viewType).isEqualTo(ViewType.SUBPAGE) - assertThat(navigationSource) - .isEqualTo(SafetyCenterInteractionReported.NavigationSource.SOURCE_UNKNOWN) + assertThat(navigationSource).isEqualTo(NavigationSource.SOURCE_UNKNOWN) assertThat(sessionId).isNotNull() } } @@ -113,8 +176,7 @@ class SafetyCenterInteractionLoggingHostTest : BaseHostJUnit4Test() { val subpageViewedEvent = safetyCenterViewedAtoms.find { it.viewType == ViewType.SUBPAGE } assertThat(subpageViewedEvent).isNotNull() - assertThat(subpageViewedEvent!!.navigationSource) - .isEqualTo(SafetyCenterInteractionReported.NavigationSource.SAFETY_CENTER) + assertThat(subpageViewedEvent!!.navigationSource).isEqualTo(NavigationSource.SAFETY_CENTER) assertThat(safetyCenterViewedAtoms.map { it.sessionId }.distinct()).hasSize(1) } @@ -129,8 +191,7 @@ class SafetyCenterInteractionLoggingHostTest : BaseHostJUnit4Test() { assertThat(safetyCenterViewedAtoms).hasSize(1) with(safetyCenterViewedAtoms.first()) { assertThat(viewType).isEqualTo(ViewType.SUBPAGE) - assertThat(navigationSource) - .isEqualTo(SafetyCenterInteractionReported.NavigationSource.SETTINGS) + assertThat(navigationSource).isEqualTo(NavigationSource.SETTINGS) assertThat(sessionId).isNotNull() } } @@ -148,5 +209,27 @@ class SafetyCenterInteractionLoggingHostTest : BaseHostJUnit4Test() { private companion object { const val TEST_CLASS_NAME = ".SafetyCenterInteractionLoggingHelperTests" + + // LINT.IfChange(single_source_id) + val ENCODED_SINGLE_SOURCE_ID = encodeId("test_single_source_id") + // LINT.ThenChange(/tests/utils/safetycenter/java/com/android/safetycenter/testing/SafetyCenterTestConfigs.kt:issue_type_id) + + // LINT.IfChange(issue_type_id) + val ENCODED_ISSUE_TYPE_ID = encodeId("issue_type_id") + // LINT.ThenChange(/tests/utils/safetycenter/java/com/android/safetycenter/testing/SafetySourceTestData.kt:issue_type_id) + + /** + * Encodes a string into an long ID. The ID is a SHA-256 of the string, truncated to 64 + * bits. + */ + fun encodeId(id: String?): Long { + if (id == null) return 0 + + val digest = MessageDigest.getInstance("MD5") + digest.update(id.toByteArray()) + + // Truncate to the size of a long + return BigInteger(digest.digest()).toLong() + } } } |