Temporarily enable traced for testing

The CDD doesn't enforce traced to be enabled (`persist.traced.enable`)
by default in all device types. In fact, only handheld is required to
have do to so.

Originally, the test assumes that traced is enabled by default, which
may not be true for all device types.

Improve that by temporarily enabling traced if it's not enabled, run
the tests and disable traced again if enabled by the test case.

MUST_SLEEP to allow statsd <-> traced <-> traced_probes communication to happen

Bug: 270535557
Test: atest CtsStatsdHostTestCases
Change-Id: Ie7de101975a0265de2c4a1dadacdfb3618c2c828
diff --git a/tests/src/android/cts/statsd/alert/AnomalyDetectionTests.java b/tests/src/android/cts/statsd/alert/AnomalyDetectionTests.java
index f08a553..ea47cc3 100644
--- a/tests/src/android/cts/statsd/alert/AnomalyDetectionTests.java
+++ b/tests/src/android/cts/statsd/alert/AnomalyDetectionTests.java
@@ -64,19 +64,34 @@
     private static final int ANOMALY_EVENT_ID = 101;
     private static final int INCIDENTD_SECTION = -1;
 
+    private boolean defaultSystemTracingConfigurationHasChanged = false;
+
     @Override
     protected void setUp() throws Exception {
         super.setUp();
         if (!INCIDENTD_TESTS_ENABLED) {
             CLog.w(TAG, TAG + " anomaly tests are disabled by a flag. Change flag to true to run");
         }
+        if (PERFETTO_TESTS_ENABLED) {
+            // Default Android configuration can only change for device type that doesn't require SystemTracingEnabled
+            // by default in CDD.
+            String chars = getDevice().getProperty("ro.build.characteristics");
+            if (!isSystemTracingEnabled() && chars.contains("automotive")) {
+                enableSystemTracing();
+                defaultSystemTracingConfigurationHasChanged = true;
+            }
+        }
     }
 
     @Override
     protected void tearDown() throws Exception {
         super.tearDown();
         if (PERFETTO_TESTS_ENABLED) {
-            //Deadline to finish trace collection
+            // Disable SystemTracing if previously enabled at test setUp()
+            if (defaultSystemTracingConfigurationHasChanged) {
+                disableSystemTracing();
+            }
+            // Deadline to finish trace collection
             final long deadLine = System.currentTimeMillis() + 10000;
             while (isSystemTracingEnabled()) {
                 if (System.currentTimeMillis() > deadLine) {
diff --git a/tests/src/android/cts/statsd/atom/AtomTestCase.java b/tests/src/android/cts/statsd/atom/AtomTestCase.java
index 0380294..51ba977 100644
--- a/tests/src/android/cts/statsd/atom/AtomTestCase.java
+++ b/tests/src/android/cts/statsd/atom/AtomTestCase.java
@@ -241,6 +241,14 @@
                 + " cat " + path + " ; else echo -1 ; fi");
     }
 
+    protected void enableSystemTracing() throws Exception {
+        getDevice().executeShellCommand("setprop persist.traced.enable 1");
+    }
+
+    protected void disableSystemTracing() throws Exception {
+        getDevice().executeShellCommand("setprop persist.traced.enable 0");
+    }
+
     /**
      * Determines whether perfetto enabled the kernel ftrace tracer.
      */