summaryrefslogtreecommitdiff
path: root/tests/hostside
diff options
context:
space:
mode:
author Max Spencer <maxspencer@google.com> 2023-04-19 13:13:42 +0000
committer Max Spencer <maxspencer@google.com> 2023-04-20 15:56:20 +0000
commit1d14059c47a82b38a6cd774c753e2fc8c813b947 (patch)
tree3a9e3b7c79b8574abb286d3dae970abba6b9af45 /tests/hostside
parent6002bf2a221dc790cda84b519ca745e4cc8d453f (diff)
Add tests for SafetyCenterSystemEvent logging
This CL just introduces the necessary test class and some basic test cases which future CLs will build upon. Bug: 268328334 Test: atest SafetyCenterSystemEventLoggingHostTest Change-Id: Id94557283633fa84c1c81545e3b4965c3895dbef
Diffstat (limited to 'tests/hostside')
-rw-r--r--tests/hostside/safetycenter/helper-app/src/android/safetycenter/hostside/device/SafetySourceStateCollectedLoggingHelperTests.kt42
-rw-r--r--tests/hostside/safetycenter/src/android/safetycenter/hostside/SafetyCenterSystemEventReportedLoggingHostTest.kt109
2 files changed, 147 insertions, 4 deletions
diff --git a/tests/hostside/safetycenter/helper-app/src/android/safetycenter/hostside/device/SafetySourceStateCollectedLoggingHelperTests.kt b/tests/hostside/safetycenter/helper-app/src/android/safetycenter/hostside/device/SafetySourceStateCollectedLoggingHelperTests.kt
index 7c85ea02a..a5b87ac22 100644
--- a/tests/hostside/safetycenter/helper-app/src/android/safetycenter/hostside/device/SafetySourceStateCollectedLoggingHelperTests.kt
+++ b/tests/hostside/safetycenter/helper-app/src/android/safetycenter/hostside/device/SafetySourceStateCollectedLoggingHelperTests.kt
@@ -17,14 +17,17 @@
package android.safetycenter.hostside.device
import android.content.Context
+import android.safetycenter.SafetyCenterManager
import androidx.test.core.app.ApplicationProvider
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.android.compatibility.common.util.SystemUtil
-import com.android.safetycenter.testing.SafetyCenterFlags
-import com.android.safetycenter.testing.SafetyCenterTestConfigs
+import com.android.safetycenter.testing.*
import com.android.safetycenter.testing.SafetyCenterTestConfigs.Companion.SOURCE_ID_1
-import com.android.safetycenter.testing.SafetyCenterTestHelper
-import com.android.safetycenter.testing.SafetySourceTestData
+import com.android.safetycenter.testing.SafetyCenterTestConfigs.Companion.SOURCE_ID_2
+import com.android.safetycenter.testing.SafetyCenterTestConfigs.Companion.SOURCE_ID_3
+import com.android.safetycenter.testing.SafetySourceIntentHandler.Request
+import com.android.safetycenter.testing.SafetySourceIntentHandler.Response
+import com.android.safetycenter.testing.SafetySourceReceiver.Companion.refreshSafetySourcesWithReceiverPermissionAndWait
import org.junit.After
import org.junit.Before
import org.junit.Test
@@ -36,6 +39,7 @@ class SafetySourceStateCollectedLoggingHelperTests {
private val safetyCenterTestHelper = SafetyCenterTestHelper(context)
private val safetySourceTestData = SafetySourceTestData(context)
private val safetyCenterTestConfigs = SafetyCenterTestConfigs(context)
+ private val safetyCenterManager = context.getSystemService(SafetyCenterManager::class.java)!!
@Before
fun setUp() {
@@ -61,4 +65,34 @@ class SafetySourceStateCollectedLoggingHelperTests {
fun setSafetySourceData_source1() {
safetyCenterTestHelper.setData(SOURCE_ID_1, safetySourceTestData.information)
}
+
+ @Test
+ fun refreshAllSources_reasonPageOpen_allSuccessful() {
+ simulateRefresh(
+ Response.SetData(safetySourceTestData.information),
+ Response.SetData(safetySourceTestData.recommendationWithAccountIssue),
+ Response.SetData(safetySourceTestData.criticalWithResolvingDeviceIssue)
+ )
+ }
+
+ @Test
+ fun refreshAllSources_reasonPageOpen_oneError() {
+ simulateRefresh(
+ Response.SetData(safetySourceTestData.information),
+ Response.SetData(safetySourceTestData.information),
+ Response.Error
+ )
+ }
+
+ private fun simulateRefresh(
+ source1Response: Response,
+ source2Response: Response,
+ source3Response: Response,
+ refreshReason: Int = SafetyCenterManager.REFRESH_REASON_PAGE_OPEN
+ ) {
+ SafetySourceReceiver.setResponse(Request.Refresh(SOURCE_ID_1), source1Response)
+ SafetySourceReceiver.setResponse(Request.Refresh(SOURCE_ID_2), source2Response)
+ SafetySourceReceiver.setResponse(Request.Refresh(SOURCE_ID_3), source3Response)
+ safetyCenterManager.refreshSafetySourcesWithReceiverPermissionAndWait(refreshReason)
+ }
}
diff --git a/tests/hostside/safetycenter/src/android/safetycenter/hostside/SafetyCenterSystemEventReportedLoggingHostTest.kt b/tests/hostside/safetycenter/src/android/safetycenter/hostside/SafetyCenterSystemEventReportedLoggingHostTest.kt
new file mode 100644
index 000000000..7e1844f0f
--- /dev/null
+++ b/tests/hostside/safetycenter/src/android/safetycenter/hostside/SafetyCenterSystemEventReportedLoggingHostTest.kt
@@ -0,0 +1,109 @@
+/*
+ * Copyright (C) 2023 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
+
+import android.cts.statsdatom.lib.ConfigUtils
+import android.cts.statsdatom.lib.ReportUtils
+import android.safetycenter.hostside.rules.HelperAppRule
+import android.safetycenter.hostside.rules.RequireSafetyCenterRule
+import com.android.os.AtomsProto.Atom
+import com.android.os.AtomsProto.SafetyCenterSystemEventReported.EventType
+import com.android.os.AtomsProto.SafetyCenterSystemEventReported.Result
+import com.android.tradefed.testtype.DeviceJUnit4ClassRunner
+import com.android.tradefed.testtype.junit4.BaseHostJUnit4Test
+import com.google.common.truth.Truth.assertThat
+import org.junit.After
+import org.junit.Before
+import org.junit.Rule
+import org.junit.Test
+import org.junit.rules.RuleChain
+import org.junit.runner.RunWith
+
+@RunWith(DeviceJUnit4ClassRunner::class)
+class SafetyCenterSystemEventReportedLoggingHostTest : BaseHostJUnit4Test() {
+
+ private val safetyCenterRule = RequireSafetyCenterRule(this)
+ private val helperAppRule = HelperAppRule(this, HelperApp.APK_NAME, HelperApp.PACKAGE_NAME)
+
+ @Rule
+ @JvmField
+ val rules: RuleChain = RuleChain.outerRule(safetyCenterRule).around(helperAppRule)
+
+ @Before
+ fun setUp() {
+ ConfigUtils.removeConfig(device)
+ ReportUtils.clearReports(device)
+ ConfigUtils.uploadConfigForPushedAtom(
+ device,
+ safetyCenterRule.getSafetyCenterPackageName(),
+ Atom.SAFETY_CENTER_SYSTEM_EVENT_REPORTED_FIELD_NUMBER
+ )
+ // TODO(b/239682646): Consider adding a target preparer that unlocks the device (like CTS)
+ }
+
+ @After
+ fun tearDown() {
+ ReportUtils.clearReports(device)
+ ConfigUtils.removeConfig(device)
+ }
+
+ @Test
+ fun refreshAllSources_allSourcesSuccessful_successAtomsForEachSourceAndOverallCompletion() {
+ helperAppRule.runTest(
+ ".SafetySourceStateCollectedLoggingHelperTests",
+ "refreshAllSources_reasonPageOpen_allSuccessful"
+ )
+
+ val systemEventAtoms =
+ ReportUtils.getEventMetricDataList(device).mapNotNull {
+ it.atom.safetyCenterSystemEventReported
+ }
+
+ assertThat(systemEventAtoms.groupingBy { it.eventType }.eachCount())
+ .containsExactly(
+ EventType.COMPLETE_GET_NEW_DATA,
+ 1, // Overall refresh complete
+ EventType.SINGLE_SOURCE_GET_NEW_DATA,
+ 3 // For the three sources in the multiple sources test config
+ )
+ assertThat(systemEventAtoms.groupingBy { it.result }.eachCount())
+ .containsExactly(Result.SUCCESS, 4)
+ }
+
+ @Test
+ fun refreshAllSources_oneSourceError_errorAtomForThatSourceAndOverall() {
+ helperAppRule.runTest(
+ ".SafetySourceStateCollectedLoggingHelperTests",
+ "refreshAllSources_reasonPageOpen_oneError"
+ )
+
+ val systemEventAtoms =
+ ReportUtils.getEventMetricDataList(device).mapNotNull {
+ it.atom.safetyCenterSystemEventReported
+ }
+
+ assertThat(systemEventAtoms.groupingBy { Pair(it.eventType, it.result) }.eachCount())
+ .containsExactly(
+ Pair(EventType.COMPLETE_GET_NEW_DATA, Result.ERROR),
+ 1, // Overall refresh is an error
+ Pair(EventType.SINGLE_SOURCE_GET_NEW_DATA, Result.ERROR),
+ 1, // For the source which had an error
+ Pair(EventType.SINGLE_SOURCE_GET_NEW_DATA, Result.SUCCESS),
+ 2 // The remaining two sources
+ )
+ }
+}