perfetto: add CTS tests and make integration tests run on CTS

This CL creates tests for CTS and also sets up infrastructure to run
the ftrace integration test on CTS as well.

Note that for CTS, we never setup the service or the ftrace producer
because we expect both to be included and running as part of the system
image.

Test: CL is tests
Change-Id: Ie128cdb1cab3d220ef52d158c370f69626e0a339
Bug:
diff --git a/Android.bp b/Android.bp
index 64d8065..6b350eb 100644
--- a/Android.bp
+++ b/Android.bp
@@ -1793,3 +1793,79 @@
     "perfetto_defaults",
   ],
 }
+
+// These targets are appended to the autogenerated Android.bp by tools/gen_android_bp.
+// TODO(lalitm): find a better way to do this sharing.
+
+// TODO(lalitm): uncomment these when the CTS tests are to be enabled.
+/*
+cc_test {
+  name: "CtsPerfettoTestCases",
+  srcs: [
+    "src/base/android_task_runner.cc",
+    "src/base/test/test_task_runner.cc",
+    "src/ftrace_reader/cpu_reader.cc",
+    "src/ftrace_reader/event_info.cc",
+    "src/ftrace_reader/format_parser.cc",
+    "src/ftrace_reader/ftrace_controller.cc",
+    "src/ftrace_reader/ftrace_procfs.cc",
+    "src/ftrace_reader/proto_translation_table.cc",
+    "src/traced/probes/ftrace_producer.cc",
+    "test/fake_consumer.cc",
+    "test/end_to_end_integrationtest.cc",
+    "test/end_to_end_integrationtest_cts.cc",
+  ],
+  static_libs: [
+    "libgmock",
+    "libgtest",
+    "libgtest_main",
+    "perfetto_src_tracing_ipc",
+  ],
+  shared_libs: [
+    "libandroid",
+    "liblog",
+    "libprotobuf-cpp-lite",
+  ],
+  test_suites: [
+    "cts",
+    "vts",
+    "general-tests",
+  ],
+  compile_multilib: "both",
+  multilib: {
+    lib32: {
+        suffix: "32",
+    },
+    lib64: {
+        suffix: "64",
+    },
+  },
+  cflags: [
+    "-DPERFETTO_BUILD_WITH_ANDROID",
+  ],
+}
+
+cc_library_shared {
+  name: "libperfettocts_jni",
+  srcs: [
+    "src/base/android_task_runner.cc",
+    "src/base/test/test_task_runner.cc",
+    "test/fake_producer.cc",
+    "test/cts/producer/jni/fake_producer_jni.cc",
+  ],
+  static_libs: [
+    "libprotobuf-cpp-lite",
+    "perfetto_src_tracing_ipc",
+  ],
+  shared_libs: [
+    "libandroid",
+    "liblog",
+    "libnativehelper_compat_libc++",
+  ],
+  compile_multilib: "both",
+  stl: "libc++_static",
+  cflags: [
+    "-DPERFETTO_BUILD_WITH_ANDROID",
+  ],
+}
+*/
\ No newline at end of file
diff --git a/Android.bp.extras b/Android.bp.extras
index e69de29..decc0fe 100644
--- a/Android.bp.extras
+++ b/Android.bp.extras
@@ -0,0 +1,75 @@
+// These targets are appended to the autogenerated Android.bp by tools/gen_android_bp.
+// TODO(lalitm): find a better way to do this sharing.
+
+// TODO(lalitm): uncomment these when the CTS tests are to be enabled.
+/*
+cc_test {
+  name: "CtsPerfettoTestCases",
+  srcs: [
+    "src/base/android_task_runner.cc",
+    "src/base/test/test_task_runner.cc",
+    "src/ftrace_reader/cpu_reader.cc",
+    "src/ftrace_reader/event_info.cc",
+    "src/ftrace_reader/format_parser.cc",
+    "src/ftrace_reader/ftrace_controller.cc",
+    "src/ftrace_reader/ftrace_procfs.cc",
+    "src/ftrace_reader/proto_translation_table.cc",
+    "src/traced/probes/ftrace_producer.cc",
+    "test/fake_consumer.cc",
+    "test/end_to_end_integrationtest.cc",
+    "test/end_to_end_integrationtest_cts.cc",
+  ],
+  static_libs: [
+    "libgmock",
+    "libgtest",
+    "libgtest_main",
+    "perfetto_src_tracing_ipc",
+  ],
+  shared_libs: [
+    "libandroid",
+    "liblog",
+    "libprotobuf-cpp-lite",
+  ],
+  test_suites: [
+    "cts",
+    "vts",
+    "general-tests",
+  ],
+  compile_multilib: "both",
+  multilib: {
+    lib32: {
+        suffix: "32",
+    },
+    lib64: {
+        suffix: "64",
+    },
+  },
+  cflags: [
+    "-DPERFETTO_BUILD_WITH_ANDROID",
+  ],
+}
+
+cc_library_shared {
+  name: "libperfettocts_jni",
+  srcs: [
+    "src/base/android_task_runner.cc",
+    "src/base/test/test_task_runner.cc",
+    "test/fake_producer.cc",
+    "test/cts/producer/jni/fake_producer_jni.cc",
+  ],
+  static_libs: [
+    "libprotobuf-cpp-lite",
+    "perfetto_src_tracing_ipc",
+  ],
+  shared_libs: [
+    "libandroid",
+    "liblog",
+    "libnativehelper_compat_libc++",
+  ],
+  compile_multilib: "both",
+  stl: "libc++_static",
+  cflags: [
+    "-DPERFETTO_BUILD_WITH_ANDROID",
+  ],
+}
+*/
diff --git a/cts/Android.mk b/cts/Android.mk
deleted file mode 100644
index 228da8a..0000000
--- a/cts/Android.mk
+++ /dev/null
@@ -1,41 +0,0 @@
-# Copyright (C) 2018 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.
-
-LOCAL_PATH:= $(call my-dir)
-
-include $(CLEAR_VARS)
-
-LOCAL_MODULE:= CtsPerfettoTestCases
-LOCAL_MODULE_PATH := $(TARGET_OUT_DATA)/nativetest
-LOCAL_MULTILIB := both
-LOCAL_MODULE_STEM_32 := $(LOCAL_MODULE)32
-LOCAL_MODULE_STEM_64 := $(LOCAL_MODULE)64
-LOCAL_CPP_EXTENSION := .cc
-
-LOCAL_SRC_FILES := \
-    perfetto_cts.cc
-
-LOCAL_CFLAGS := -Wall -Werror
-
-LOCAL_STATIC_LIBRARIES := \
-    libgtest \
-    libgtest_main \
-    perfetto_src_tracing_ipc
-
-# Tag this module as a cts test artifact
-LOCAL_COMPATIBILITY_SUITE := cts vts general-tests
-
-include $(BUILD_CTS_EXECUTABLE)
-
-include $(call all-makefiles-under,$(LOCAL_PATH))
\ No newline at end of file
diff --git a/cts/perfetto_cts.cc b/cts/perfetto_cts.cc
deleted file mode 100644
index ae4e91e..0000000
--- a/cts/perfetto_cts.cc
+++ /dev/null
@@ -1,23 +0,0 @@
-/*
- * Copyright (C) 2018 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_NDEBUG 0
-#define LOG_TAG "PerfettoTest"
-
-#include <gtest/gtest.h>
-
-TEST(PerfettoTest, TestRealProducer) {
-}
\ No newline at end of file
diff --git a/cts/producer/jni/Android.mk b/cts/producer/jni/Android.mk
deleted file mode 100644
index 366536e..0000000
--- a/cts/producer/jni/Android.mk
+++ /dev/null
@@ -1,42 +0,0 @@
-# Copyright (C) 2018 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.
-#
-LOCAL_PATH := $(call my-dir)
-
-include $(CLEAR_VARS)
-
-LOCAL_MODULE    := libperfettoctsproducer_jni
-
-LOCAL_MULTILIB := both
-
-LOCAL_MODULE_TAGS := optional
-
-LOCAL_CPP_EXTENSION := .cc
-LOCAL_SRC_FILES := \
-	producer.cc
-
-LOCAL_C_INCLUDES := \
-	$(JNI_H_INCLUDE) \
-	system/core/include
-
-LOCAL_CFLAGS += -Wall -Werror
-
-# TODO(lalitm): enable this once we have the below as NDK compatible.
-# LOCAL_STATIC_LIBRARIES := perfetto_src_tracing_ipc
-LOCAL_SHARED_LIBRARIES := \
-    liblog
-
-LOCAL_SDK_VERSION := current
-
-include $(BUILD_SHARED_LIBRARY)
diff --git a/cts/producer/jni/producer.cc b/cts/producer/jni/producer.cc
deleted file mode 100644
index dd1f019..0000000
--- a/cts/producer/jni/producer.cc
+++ /dev/null
@@ -1,19 +0,0 @@
-/*
- * Copyright (C) 2018 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_NDEBUG 0
-#define LOG_TAG "PerfettoCtsMockProducer"
-#include <log/log.h>
\ No newline at end of file
diff --git a/cts/producer/src/android/perfetto/producer/PerfettoProducerActivity.java b/cts/producer/src/android/perfetto/producer/PerfettoProducerActivity.java
deleted file mode 100644
index b2c995a..0000000
--- a/cts/producer/src/android/perfetto/producer/PerfettoProducerActivity.java
+++ /dev/null
@@ -1,31 +0,0 @@
-/*
- * Copyright (C) 2018 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.perfetto.producer;
-
-import android.app.Activity;
-import android.os.Bundle;
-import android.util.Log;
-
-import java.lang.Override;
-
-public class PerfettoProducerActivity extends Activity {
-
-    @Override
-    public void onCreate(Bundle icicle) {
-        super.onCreate(icicle);
-    }
-}
diff --git a/src/traced/probes/ftrace_producer.cc b/src/traced/probes/ftrace_producer.cc
index 9467c92..5d7b7f7 100644
--- a/src/traced/probes/ftrace_producer.cc
+++ b/src/traced/probes/ftrace_producer.cc
@@ -171,13 +171,12 @@
 FtraceProducer::SinkDelegate::~SinkDelegate() = default;
 
 FtraceProducer::BundleHandle FtraceProducer::SinkDelegate::GetBundleForCpu(
-    size_t cpu) {
+    size_t) {
   trace_packet_ = writer_->NewTracePacket();
   return BundleHandle(trace_packet_->set_ftrace_events());
 }
 
-void FtraceProducer::SinkDelegate::OnBundleComplete(size_t cpu,
-                                                    BundleHandle bundle) {
+void FtraceProducer::SinkDelegate::OnBundleComplete(size_t, BundleHandle) {
   trace_packet_->Finalize();
 }
 
diff --git a/cts/README.md b/test/cts/README.md
similarity index 100%
rename from cts/README.md
rename to test/cts/README.md
diff --git a/cts/producer/Android.mk b/test/cts/producer/Android.mk
similarity index 76%
rename from cts/producer/Android.mk
rename to test/cts/producer/Android.mk
index 8076909..aa621e2 100644
--- a/cts/producer/Android.mk
+++ b/test/cts/producer/Android.mk
@@ -16,30 +16,21 @@
 
 include $(CLEAR_VARS)
 
+# tag this module as a cts test artifact
+LOCAL_COMPATIBILITY_SUITE := cts vts general-tests
+
 # Don't include this package in any target
 LOCAL_MODULE_TAGS := tests
 
-# When built, explicitly put it in the data partition.
-LOCAL_MODULE_PATH := $(TARGET_OUT_DATA_APPS)
-
-LOCAL_DEX_PREOPT := false
-
 LOCAL_MULTILIB := both
 
-LOCAL_PROGUARD_ENABLED := disabled
-
 LOCAL_SRC_FILES := $(call all-java-files-under, src)
 
-# tag this module as a cts test artifact
-LOCAL_COMPATIBILITY_SUITE := cts vts general-tests
-
 LOCAL_PACKAGE_NAME := CtsPerfettoProducerApp
 
 LOCAL_JNI_SHARED_LIBRARIES := \
-	libperfettoctsproducer_jni
+    libperfettocts_jni \
+    libnativehelper_compat_libc++
 
-LOCAL_SDK_VERSION := current
 
-include $(BUILD_PACKAGE)
-
-include $(call all-makefiles-under,$(LOCAL_PATH))
\ No newline at end of file
+# include $(BUILD_PACKAGE)
diff --git a/cts/producer/AndroidManifest.xml b/test/cts/producer/AndroidManifest.xml
similarity index 81%
rename from cts/producer/AndroidManifest.xml
rename to test/cts/producer/AndroidManifest.xml
index 2a86452..6924a9a 100755
--- a/cts/producer/AndroidManifest.xml
+++ b/test/cts/producer/AndroidManifest.xml
@@ -17,14 +17,20 @@
 
 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
     package="android.perfetto.producer">
-
     <application>
-        <activity android:name=".PerfettoProducerActivity" >
+        <activity android:name=".ProducerActivity" >
             <intent-filter>
                 <action android:name="android.intent.action.MAIN" />
                 <category android:name="android.intent.category.LAUNCHER" />
             </intent-filter>
         </activity>
+
+        <service
+            android:name=".ProducerService" />
+
+        <service
+            android:name=".ProducerIsolatedService"
+            android:isolatedProcess="true" />
     </application>
 </manifest>
 
diff --git a/test/cts/producer/jni/fake_producer_jni.cc b/test/cts/producer/jni/fake_producer_jni.cc
new file mode 100644
index 0000000..75b5b32
--- /dev/null
+++ b/test/cts/producer/jni/fake_producer_jni.cc
@@ -0,0 +1,50 @@
+/*
+ * Copyright (C) 2018 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.
+ */
+
+#include <jni.h>
+
+#include "perfetto/traced/traced.h"
+
+#include "src/base/test/test_task_runner.h"
+
+#include "test/fake_producer.h"
+
+namespace perfetto {
+namespace {
+void ListenAndRespond(const std::string& name) {
+  base::TestTaskRunner task_runner;
+  FakeProducer producer(name);
+  producer.Connect(PERFETTO_PRODUCER_SOCK_NAME, &task_runner);
+  task_runner.Run();
+}
+}  // namespace
+}  // namespace perfetto
+
+extern "C" JNIEXPORT void JNICALL
+Java_android_perfetto_producer_ProducerActivity_setupProducer(JNIEnv*, jclass) {
+  perfetto::ListenAndRespond("android.perfetto.cts.ProducerActivity");
+}
+
+extern "C" JNIEXPORT void JNICALL
+Java_android_perfetto_producer_ProducerIsolatedService_setupProducer(JNIEnv*,
+                                                                     jclass) {
+  perfetto::ListenAndRespond("android.perfetto.cts.ProducerIsolatedService");
+}
+
+extern "C" JNIEXPORT void JNICALL
+Java_android_perfetto_producer_ProducerService_setupProducer(JNIEnv*, jclass) {
+  perfetto::ListenAndRespond("android.perfetto.cts.ProducerService");
+}
diff --git a/test/cts/producer/src/android/perfetto/producer/ProducerActivity.java b/test/cts/producer/src/android/perfetto/producer/ProducerActivity.java
new file mode 100644
index 0000000..321ae3b
--- /dev/null
+++ b/test/cts/producer/src/android/perfetto/producer/ProducerActivity.java
@@ -0,0 +1,45 @@
+/*
+ * Copyright (C) 2018 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.perfetto.producer;
+
+import android.app.Activity;
+import android.content.Intent;
+import android.os.Bundle;
+
+public class ProducerActivity extends Activity {
+    @Override
+    public void onCreate(Bundle savedInstanceState) {
+        super.onCreate(savedInstanceState);
+
+        startService(new Intent(this, ProducerService.class));
+        startService(new Intent(this, ProducerIsolatedService.class));
+
+        System.loadLibrary("perfettocts_jni");
+        new Thread(new Runnable() {
+            public void run() {
+                try {
+                    setupProducer();
+                } catch (Exception ex) {
+                    ex.printStackTrace();
+                }
+            }
+        })
+                .start();
+    }
+
+    private static native void setupProducer();
+}
diff --git a/test/cts/producer/src/android/perfetto/producer/ProducerIsolatedService.java b/test/cts/producer/src/android/perfetto/producer/ProducerIsolatedService.java
new file mode 100644
index 0000000..cde5c32
--- /dev/null
+++ b/test/cts/producer/src/android/perfetto/producer/ProducerIsolatedService.java
@@ -0,0 +1,54 @@
+/*
+ * Copyright (C) 2018 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.perfetto.producer;
+
+import android.app.Service;
+import android.os.IBinder;
+import android.content.Intent;
+
+public class ProducerIsolatedService extends Service {
+    @Override
+    public void onCreate() {
+        System.loadLibrary("perfettocts_jni");
+        new Thread(new Runnable() {
+            @Override
+            public void run() {
+                try {
+                    setupProducer();
+                } catch (Exception ex) {
+                    ex.printStackTrace();
+                }
+            }
+        })
+                .start();
+    }
+
+    @Override
+    public int onStartCommand(Intent intent, int flags, int startId) {
+        return START_STICKY;
+    }
+
+    @Override
+    public IBinder onBind(Intent intent) {
+        return null;
+    }
+
+    @Override
+    public void onDestroy() {}
+
+    private static native void setupProducer();
+}
diff --git a/test/cts/producer/src/android/perfetto/producer/ProducerService.java b/test/cts/producer/src/android/perfetto/producer/ProducerService.java
new file mode 100644
index 0000000..f649ddc
--- /dev/null
+++ b/test/cts/producer/src/android/perfetto/producer/ProducerService.java
@@ -0,0 +1,54 @@
+/*
+ * Copyright (C) 2018 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.perfetto.producer;
+
+import android.app.Service;
+import android.os.IBinder;
+import android.content.Intent;
+
+public class ProducerService extends Service {
+    @Override
+    public void onCreate() {
+        System.loadLibrary("perfettocts_jni");
+        new Thread(new Runnable() {
+            @Override
+            public void run() {
+                try {
+                    setupProducer();
+                } catch (Exception ex) {
+                    ex.printStackTrace();
+                }
+            }
+        })
+                .start();
+    }
+
+    @Override
+    public int onStartCommand(Intent intent, int flags, int startId) {
+        return START_STICKY;
+    }
+
+    @Override
+    public IBinder onBind(Intent intent) {
+        return null;
+    }
+
+    @Override
+    public void onDestroy() {}
+
+    private static native void setupProducer();
+}
diff --git a/test/end_to_end_integrationtest_cts.cc b/test/end_to_end_integrationtest_cts.cc
new file mode 100644
index 0000000..1e45d25
--- /dev/null
+++ b/test/end_to_end_integrationtest_cts.cc
@@ -0,0 +1,86 @@
+/*
+ * Copyright (C) 2018 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.
+ */
+
+#include <gtest/gtest.h>
+
+#include "perfetto/trace/test_event.pbzero.h"
+#include "perfetto/trace/trace_packet.pb.h"
+#include "perfetto/trace/trace_packet.pbzero.h"
+#include "perfetto/traced/traced.h"
+#include "perfetto/tracing/core/trace_packet.h"
+
+#include "test/fake_consumer.h"
+
+namespace perfetto {
+
+class PerfettoCtsTest : public ::testing::Test {
+ protected:
+  void TestMockProducer(const std::string& producer_name) {
+    base::TestTaskRunner task_runner;
+    auto finish = task_runner.CreateCheckpoint("no.more.packets");
+
+    // Setup the trace config.
+    TraceConfig trace_config;
+    trace_config.add_buffers()->set_size_kb(4096 * 10);
+    trace_config.set_duration_ms(200);
+
+    auto* ds_config = trace_config.add_data_sources()->mutable_config();
+    ds_config->set_name(producer_name);
+    ds_config->set_target_buffer(0);
+
+    // Setip the function.
+    uint64_t total = 0;
+    auto function = [&total, &finish](std::vector<TracePacket> packets,
+                                      bool has_more) {
+      if (has_more) {
+        for (auto& packet : packets) {
+          packet.Decode();
+          ASSERT_TRUE(packet->has_test());
+          ASSERT_EQ(packet->test(), "test");
+        }
+        total += packets.size();
+
+        // TODO(lalitm): renable this when stiching inside the service is
+        // present.
+        // ASSERT_FALSE(packets->empty());
+      } else {
+        ASSERT_EQ(total, 10u);
+        ASSERT_TRUE(packets.empty());
+        finish();
+      }
+    };
+
+    // Finally, make the consumer connect to the service.
+    FakeConsumer consumer(trace_config, std::move(function), &task_runner);
+    consumer.Connect(PERFETTO_CONSUMER_SOCK_NAME);
+
+    task_runner.RunUntilCheckpoint("no.more.packets");
+  }
+};
+
+TEST_F(PerfettoCtsTest, TestProducerActivity) {
+  TestMockProducer("android.perfetto.cts.ProducerActivity");
+}
+
+TEST_F(PerfettoCtsTest, TestProducerService) {
+  TestMockProducer("android.perfetto.cts.ProducerService");
+}
+
+TEST_F(PerfettoCtsTest, TestProducerIsolatedService) {
+  TestMockProducer("android.perfetto.cts.ProducerIsolatedService");
+}
+
+}  // namespace perfetto