diff options
author | 2023-03-01 16:04:33 +0000 | |
---|---|---|
committer | 2023-03-09 13:40:35 +0000 | |
commit | f524040d27ba4058ce3760afc157985d47213cba (patch) | |
tree | dac65655035c7ae2f80d6e2fd6f06b1927904e79 /tests/hostside | |
parent | 7169f1374451a325f9cd096fb6153e709c4712c2 (diff) |
Notification logging tests
Bug: 268309616
Test: atest SafetyCenterLoggingHostTest#sendNotification_recordsNotificationPostedEvent
Change-Id: I954f482e60cadb9dc4343aca980f7a2ba2a13905
Diffstat (limited to 'tests/hostside')
2 files changed, 103 insertions, 1 deletions
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 new file mode 100644 index 000000000..f4677cfed --- /dev/null +++ b/tests/hostside/safetycenter/helper-app/src/android/safetycenter/hostside/device/SafetyCenterNotificationLoggingHelperTests.kt @@ -0,0 +1,80 @@ +/* + * Copyright (C) 2022 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 android.safetycenter.hostside.device + +import android.content.Context +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.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.SafetySourceTestData +import org.junit.After +import org.junit.Before +import org.junit.Test +import org.junit.runner.RunWith + +/** + * Contains "helper tests" that perform on-device setup and interaction code for Safety Center's + * interaction logging host-side tests. These "helper tests" just perform arrange and act steps and + * should not contain assertions. Assertions are performed in the host-side tests that run these + * helper tests. + * + * Some context: host-side tests are unable to interact with the device UI in a detailed manner, and + * must run "helper tests" on the device to perform in-depth interactions or setup that must be + * performed by code running on the device. + */ +@RunWith(AndroidJUnit4::class) +class SafetyCenterNotificationLoggingHelperTests { + private val context: Context = ApplicationProvider.getApplicationContext() + private val safetyCenterTestHelper = SafetyCenterTestHelper(context) + private val safetySourceTestData = SafetySourceTestData(context) + private val safetyCenterTestConfigs = SafetyCenterTestConfigs(context) + + @Before + fun setUp() { + safetyCenterTestHelper.setup() + SafetyCenterFlags.notificationsEnabled = true + SafetyCenterFlags.notificationsAllowedSources = setOf(SINGLE_SOURCE_ID) + SafetyCenterFlags.allowStatsdLogging = true + safetyCenterTestHelper.setConfig(safetyCenterTestConfigs.singleSourceConfig) + } + + @After + fun tearDown() { + safetyCenterTestHelper.reset() + } + + @Test + fun sendNotification() { + safetyCenterTestHelper.setData(SINGLE_SOURCE_ID, newTestDataWithNotifiableIssue()) + } + + private fun newTestDataWithNotifiableIssue(): SafetySourceData = + safetySourceTestData + .defaultCriticalDataBuilder() + .addIssue( + safetySourceTestData + .defaultRecommendationIssueBuilder("Notify immediately", "This is urgent!") + .setNotificationBehavior(SafetySourceIssue.NOTIFICATION_BEHAVIOR_IMMEDIATELY) + .build() + ) + .build() +} diff --git a/tests/hostside/safetycenter/src/android/safetycenter/hostside/SafetyCenterInteractionLoggingHostTest.kt b/tests/hostside/safetycenter/src/android/safetycenter/hostside/SafetyCenterInteractionLoggingHostTest.kt index 29d8e09ac..ddcbd815b 100644 --- a/tests/hostside/safetycenter/src/android/safetycenter/hostside/SafetyCenterInteractionLoggingHostTest.kt +++ b/tests/hostside/safetycenter/src/android/safetycenter/hostside/SafetyCenterInteractionLoggingHostTest.kt @@ -33,7 +33,7 @@ import org.junit.Before import org.junit.Test import org.junit.runner.RunWith -/** Host-side test for statsd interaction logging in the Safety Center UI. */ +/** Host-side tests for Safety Center statsd logging. */ @RunWith(DeviceJUnit4ClassRunner::class) class SafetyCenterInteractionLoggingHostTest : BaseHostJUnit4Test() { @@ -94,6 +94,28 @@ class SafetyCenterInteractionLoggingHostTest : BaseHostJUnit4Test() { assertThat(safetyCenterViewedEvents).isNotEmpty() } + @Test + fun sendNotification_recordsNotificationPostedEvent() { + DeviceUtils.runDeviceTests( + device, + HELPER_PACKAGE, + ".NotificationLoggingHelperTests", + "sendNotification" + ) + Thread.sleep(AtomTestUtils.WAIT_TIME_LONG.toLong()) // Wait for report to be updated + + val notificationPostedEvents = + ReportUtils.getEventMetricDataList(device).filter { + it.atom.safetyCenterInteractionReported.action == + SafetyCenterInteractionReported.Action.NOTIFICATION_POSTED + } + + assertThat(notificationPostedEvents).hasSize(1) + val atom = notificationPostedEvents.first().atom.safetyCenterInteractionReported + assertThat(atom.viewType) + .isEqualTo(SafetyCenterInteractionReported.ViewType.VIEW_TYPE_NOTIFICATION) + } + // TODO(b/239682646): Add more tests private fun ITestDevice.supportsSafetyCenter(): Boolean { |