[TeX] Added host side CTS tests

- for expresslog APIs end to end validation

Bug: 266629537
Test: atest CtsStatsdAtomHostTestCases
Change-Id: Iccf13c632c89fabfcde18a177f08f816658a1e89
Merged-In: Iccf13c632c89fabfcde18a177f08f816658a1e89
diff --git a/hostsidetests/statsdatom/Android.bp b/hostsidetests/statsdatom/Android.bp
index 0bf408a..159b144 100644
--- a/hostsidetests/statsdatom/Android.bp
+++ b/hostsidetests/statsdatom/Android.bp
@@ -51,6 +51,7 @@
         "src/**/tls/*.java",
         "src/**/wifi/*.java",
         "src/**/incremental/*.java",
+        "src/**/express/*.java",
     ],
 
     test_suites: [
@@ -80,6 +81,7 @@
         ":CtsSimpleApp",
         ":CtsStatsdAlarmHelper",
         ":CtsStatsdAlarmHelper2",
+        ":CtsStatsdExpressLogHelper",
     ],
     per_testcase_directory: true,
 }
diff --git a/hostsidetests/statsdatom/apps/expressloghelperapp/Android.bp b/hostsidetests/statsdatom/apps/expressloghelperapp/Android.bp
new file mode 100644
index 0000000..152300b
--- /dev/null
+++ b/hostsidetests/statsdatom/apps/expressloghelperapp/Android.bp
@@ -0,0 +1,71 @@
+// 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 {
+    default_applicable_licenses: ["Android-Apache-2.0"],
+}
+
+android_test_helper_app {
+    name: "CtsStatsdExpressLogHelper",
+    defaults: ["cts_defaults"],
+    platform_apis: true,
+    min_sdk_version: "30",
+    srcs: [
+        "src/**/*.java",
+    ],
+    libs: [
+        "androidx.annotation_annotation",
+        "android.test.runner",
+        "junit",
+    ],
+    static_libs: [
+        "ctstestrunner-axt",
+        "compatibility-device-util-axt",
+        "androidx.legacy_legacy-support-v4",
+        "androidx.test.rules",
+        "cts-net-utils",
+        "modules-utils-expresslog",
+    ],
+    jni_libs: [
+        "libexpresslog_helperapp_jni",
+    ],
+    compile_multilib: "both",
+    v4_signature: true,
+}
+
+cc_library_shared {
+    name: "libexpresslog_helperapp_jni",
+    sdk_version: "current",
+    min_sdk_version: "30",
+    cflags: [
+        "-Wall",
+        "-Werror",
+        "-Wextra",
+        "-DNAMESPACE_FOR_HASH_FUNCTIONS=farmhash",
+    ],
+    srcs: [
+        "jni/onload.cpp",
+    ],
+    header_libs: [
+        "liblog_headers",
+        "libnativehelper_header_only",
+    ],
+    shared_libs: [
+        "liblog",
+    ],
+    static_libs: [
+        "libexpresslog_jni",
+        "libtextclassifier_hash_static",
+    ],
+}
diff --git a/hostsidetests/statsdatom/apps/expressloghelperapp/AndroidManifest.xml b/hostsidetests/statsdatom/apps/expressloghelperapp/AndroidManifest.xml
new file mode 100644
index 0000000..2770669
--- /dev/null
+++ b/hostsidetests/statsdatom/apps/expressloghelperapp/AndroidManifest.xml
@@ -0,0 +1,25 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- 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.
+-->
+
+<manifest xmlns:android="http://schemas.android.com/apk/res/android"
+     package="com.android.server.cts.device.expresslog"
+     android:versionCode="10">
+
+    <instrumentation android:name="androidx.test.runner.AndroidJUnitRunner"
+            android:targetPackage="com.android.server.cts.device.expresslog"
+            android:label="Telemetry Express Logging Helper Tests" />
+
+</manifest>
diff --git a/hostsidetests/statsdatom/apps/expressloghelperapp/jni/.clang-format b/hostsidetests/statsdatom/apps/expressloghelperapp/jni/.clang-format
new file mode 100644
index 0000000..cead3a0
--- /dev/null
+++ b/hostsidetests/statsdatom/apps/expressloghelperapp/jni/.clang-format
@@ -0,0 +1,17 @@
+BasedOnStyle: Google
+AllowShortIfStatementsOnASingleLine: true
+AllowShortFunctionsOnASingleLine: false
+AllowShortLoopsOnASingleLine: true
+BinPackArguments: true
+BinPackParameters: true
+ColumnLimit: 100
+CommentPragmas: NOLINT:.*
+ContinuationIndentWidth: 8
+DerivePointerAlignment: false
+IndentWidth: 4
+PointerAlignment: Left
+TabWidth: 4
+AccessModifierOffset: -4
+IncludeCategories:
+  - Regex:    '^"Log\.h"'
+    Priority:    -1
diff --git a/hostsidetests/statsdatom/apps/expressloghelperapp/jni/onload.cpp b/hostsidetests/statsdatom/apps/expressloghelperapp/jni/onload.cpp
new file mode 100644
index 0000000..a112467
--- /dev/null
+++ b/hostsidetests/statsdatom/apps/expressloghelperapp/jni/onload.cpp
@@ -0,0 +1,40 @@
+/*
+ * 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.
+ */
+
+#define LOG_TAG "TeX"
+
+#include <jni.h>
+#include <log/log.h>
+
+namespace android {
+extern int register_com_android_modules_expresslog_Utils(JNIEnv* env);
+}  // namespace android
+
+using namespace android;
+
+extern "C" jint JNI_OnLoad(JavaVM* vm, void* /* reserved */) {
+    JNIEnv* env = NULL;
+    jint result = -1;
+
+    if (vm->GetEnv((void**)&env, JNI_VERSION_1_4) != JNI_OK) {
+        ALOGE("GetEnv failed!");
+        return result;
+    }
+    ALOG_ASSERT(env, "Could not retrieve the env!");
+
+    register_com_android_modules_expresslog_Utils(env);
+    return JNI_VERSION_1_4;
+}
diff --git a/hostsidetests/statsdatom/apps/expressloghelperapp/src/com/android/server/cts/device/expresslog/AtomTests.java b/hostsidetests/statsdatom/apps/expressloghelperapp/src/com/android/server/cts/device/expresslog/AtomTests.java
new file mode 100644
index 0000000..235bafd
--- /dev/null
+++ b/hostsidetests/statsdatom/apps/expressloghelperapp/src/com/android/server/cts/device/expresslog/AtomTests.java
@@ -0,0 +1,44 @@
+/*
+ * 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 com.android.server.cts.device.expresslog;
+
+import com.android.modules.expresslog.Counter;
+
+import org.junit.Test;
+
+public class AtomTests {
+
+    private static final int TEST_UID = 123;
+
+    static {
+        System.loadLibrary("expresslog_helperapp_jni");
+    }
+
+    @Test
+    public void testCounterMetric() throws Exception {
+        Counter.logIncrement("tex_test.value_telemetry_express_test_counter");
+        Counter.logIncrement("tex_test.value_telemetry_express_test_counter", 10);
+    }
+
+    @Test
+    public void testCounterWithUidMetric() throws Exception {
+        Counter.logIncrementWithUid("tex_test.value_telemetry_express_test_counter_with_uid",
+                TEST_UID);
+        Counter.logIncrementWithUid("tex_test.value_telemetry_express_test_counter_with_uid",
+                TEST_UID, 10);
+    }
+
+}
diff --git a/hostsidetests/statsdatom/src/android/cts/statsdatom/express/ExpresslogAtomsTests.java b/hostsidetests/statsdatom/src/android/cts/statsdatom/express/ExpresslogAtomsTests.java
new file mode 100644
index 0000000..45137f4
--- /dev/null
+++ b/hostsidetests/statsdatom/src/android/cts/statsdatom/express/ExpresslogAtomsTests.java
@@ -0,0 +1,158 @@
+/*
+ * 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.cts.statsdatom.express;
+
+import static com.google.common.truth.Truth.assertThat;
+
+import android.cts.statsdatom.lib.AtomTestUtils;
+import android.cts.statsdatom.lib.ConfigUtils;
+import android.cts.statsdatom.lib.DeviceUtils;
+import android.cts.statsdatom.lib.ReportUtils;
+
+import com.android.internal.os.StatsdConfigProto.FieldValueMatcher;
+import com.android.internal.os.StatsdConfigProto.StatsdConfig;
+import com.android.os.StatsLog.EventMetricData;
+import com.android.os.expresslog.ExpressEventReported;
+import com.android.os.expresslog.ExpressUidEventReported;
+import com.android.os.expresslog.ExpresslogExtensionAtoms;
+import com.android.tradefed.build.IBuildInfo;
+import com.android.tradefed.device.DeviceNotAvailableException;
+import com.android.tradefed.device.ITestDevice;
+import com.android.tradefed.testtype.DeviceTestCase;
+import com.android.tradefed.testtype.IBuildReceiver;
+import com.android.tradefed.util.RunUtil;
+
+import com.google.protobuf.ExtensionRegistry;
+
+import java.io.FileNotFoundException;
+import java.util.List;
+
+import javax.annotation.Nullable;
+
+public class ExpresslogAtomsTests extends DeviceTestCase implements IBuildReceiver {
+
+    private static final String EXPRESSLOG_TEST_APK = "CtsStatsdExpressLogHelper.apk";
+    private static final String EXPRESSLOG_TEST_PKG = "com.android.server.cts.device.expresslog";
+
+    private IBuildInfo mCtsBuild;
+
+    /**
+     * Install the expresslog test app to the device.
+     */
+    private static void installExpressLogTestApp(ITestDevice device, IBuildInfo ctsBuildInfo)
+            throws FileNotFoundException, DeviceNotAvailableException {
+        DeviceUtils.installTestApp(device, EXPRESSLOG_TEST_APK, EXPRESSLOG_TEST_PKG, ctsBuildInfo);
+    }
+
+    /**
+     * Uninstall the expresslog test app to the device.
+     */
+    private static void uninstallExpressLogTestApp(ITestDevice device) throws Exception {
+        DeviceUtils.uninstallTestApp(device, EXPRESSLOG_TEST_PKG);
+    }
+
+    private static void runDeviceTestsOnExpresslogApp(ITestDevice device,
+            @Nullable String testMethodName)
+            throws DeviceNotAvailableException {
+        DeviceUtils.runDeviceTests(device, EXPRESSLOG_TEST_PKG, ".AtomTests",
+                testMethodName);
+    }
+
+    @Override
+    protected void setUp() throws Exception {
+        super.setUp();
+        ConfigUtils.removeConfig(getDevice());
+        ReportUtils.clearReports(getDevice());
+        installExpressLogTestApp(getDevice(), mCtsBuild);
+        RunUtil.getDefault().sleep(AtomTestUtils.WAIT_TIME_LONG);
+    }
+
+    @Override
+    protected void tearDown() throws Exception {
+        ConfigUtils.removeConfig(getDevice());
+        ReportUtils.clearReports(getDevice());
+        uninstallExpressLogTestApp(getDevice());
+        super.tearDown();
+    }
+
+    @Override
+    public void setBuild(IBuildInfo buildInfo) {
+        mCtsBuild = buildInfo;
+    }
+
+    public void testExpressLogCounterAtom() throws Exception {
+        final int atomTag = ExpresslogExtensionAtoms.EXPRESS_EVENT_REPORTED_FIELD_NUMBER;
+        final int metricIdFieldNumber = 1;
+        final long testMetricId = -2003432401640472195L;
+
+        StatsdConfig.Builder config = ConfigUtils.createConfigBuilder(EXPRESSLOG_TEST_PKG);
+        FieldValueMatcher.Builder expressMetricIdMatcher = ConfigUtils.createFvm(
+                        metricIdFieldNumber)
+                .setEqInt(testMetricId);
+        ConfigUtils.addEventMetric(config, atomTag, List.of(expressMetricIdMatcher));
+        ConfigUtils.uploadConfig(getDevice(), config);
+
+        runDeviceTestsOnExpresslogApp(getDevice(), "testCounterMetric");
+        RunUtil.getDefault().sleep(AtomTestUtils.WAIT_TIME_SHORT);
+
+        ExtensionRegistry registry = ExtensionRegistry.newInstance();
+        ExpresslogExtensionAtoms.registerAllExtensions(registry);
+
+        // Verify correctness of data - we expect two atoms emitted by the test
+        List<EventMetricData> data = ReportUtils.getEventMetricDataList(getDevice(), registry);
+        assertThat(data).hasSize(2);
+
+        ExpressEventReported atom1 = data.get(0).getAtom().getExtension(
+                ExpresslogExtensionAtoms.expressEventReported);
+        assertThat(atom1.getValue()).isEqualTo(1);
+        ExpressEventReported atom2 = data.get(1).getAtom().getExtension(
+                ExpresslogExtensionAtoms.expressEventReported);
+        assertThat(atom2.getValue()).isEqualTo(10);
+    }
+
+    public void testExpressLogCounterWithUidAtom() throws Exception {
+        final int atomTag = ExpresslogExtensionAtoms.EXPRESS_UID_EVENT_REPORTED_FIELD_NUMBER;
+        final int metricIdFieldNumber = 1;
+        final long testMetricId = 4877884967786456322L;
+
+        StatsdConfig.Builder config = ConfigUtils.createConfigBuilder(EXPRESSLOG_TEST_PKG);
+        FieldValueMatcher.Builder expressMetricIdMatcher = ConfigUtils.createFvm(
+                        metricIdFieldNumber)
+                .setEqInt(testMetricId);
+        ConfigUtils.addEventMetric(config, atomTag, List.of(expressMetricIdMatcher));
+        ConfigUtils.uploadConfig(getDevice(), config);
+
+        runDeviceTestsOnExpresslogApp(getDevice(), "testCounterWithUidMetric");
+        RunUtil.getDefault().sleep(AtomTestUtils.WAIT_TIME_SHORT);
+
+        ExtensionRegistry registry = ExtensionRegistry.newInstance();
+        ExpresslogExtensionAtoms.registerAllExtensions(registry);
+
+        // Verify correctness of data - we expect two atoms emitted by the test
+        List<EventMetricData> data = ReportUtils.getEventMetricDataList(getDevice(), registry);
+        assertThat(data).hasSize(2);
+
+        ExpressUidEventReported atom1 = data.get(0).getAtom().getExtension(
+                ExpresslogExtensionAtoms.expressUidEventReported);
+        assertThat(atom1.getValue()).isEqualTo(1);
+        assertThat(atom1.getUid()).isAtLeast(0);
+        ExpressUidEventReported atom2 = data.get(1).getAtom().getExtension(
+                ExpresslogExtensionAtoms.expressUidEventReported);
+        assertThat(atom2.getValue()).isEqualTo(10);
+        assertThat(atom2.getUid()).isAtLeast(0);
+    }
+}
diff --git a/hostsidetests/statsdatom/src/android/cts/statsdatom/express/OWNERS b/hostsidetests/statsdatom/src/android/cts/statsdatom/express/OWNERS
new file mode 100644
index 0000000..6b79f25
--- /dev/null
+++ b/hostsidetests/statsdatom/src/android/cts/statsdatom/express/OWNERS
@@ -0,0 +1,6 @@
+# These atom tests are owned by the statsd team.
+jeffreyhuang@google.com
+muhammadq@google.com
+singhtejinder@google.com
+tsaichristine@google.com
+sharaienko@google.com