1/*
2 * Copyright (C) 2018 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <random>
18#include <sys/system_properties.h>
19
20#include "gtest/gtest.h"
21#include "perfetto/trace/test_event.pbzero.h"
22#include "perfetto/trace/trace_packet.pb.h"
23#include "perfetto/trace/trace_packet.pbzero.h"
24#include "perfetto/traced/traced.h"
25#include "perfetto/tracing/core/trace_packet.h"
26#include "src/base/test/test_task_runner.h"
27#include "test/test_helper.h"
28
29#include "perfetto/trace/trace_packet.pb.h"
30
31namespace perfetto {
32
33class PerfettoCtsTest : public ::testing::Test {
34 protected:
35  void TestMockProducer(const std::string& producer_name) {
36    // Filter out watches; they do not have the required infrastructure to run
37    // these tests.
38    char chars[PROP_VALUE_MAX + 1];
39    int ret = __system_property_get("ro.build.characteristics", chars);
40    ASSERT_GE(ret, 0);
41    std::string characteristics(chars);
42    if (characteristics.find("watch") != std::string::npos) {
43      return;
44    }
45
46    base::TestTaskRunner task_runner;
47
48    TestHelper helper(&task_runner);
49    helper.ConnectConsumer();
50    helper.WaitForConsumerConnect();
51
52    TraceConfig trace_config;
53    trace_config.add_buffers()->set_size_kb(1024);
54    trace_config.set_duration_ms(200);
55
56    auto* ds_config = trace_config.add_data_sources()->mutable_config();
57    ds_config->set_name(producer_name);
58    ds_config->set_target_buffer(0);
59
60    static constexpr uint32_t kRandomSeed = 42;
61    static constexpr uint32_t kEventCount = 10;
62    static constexpr uint32_t kMessageSizeBytes = 1024;
63    ds_config->mutable_for_testing()->set_seed(kRandomSeed);
64    ds_config->mutable_for_testing()->set_message_count(kEventCount);
65    ds_config->mutable_for_testing()->set_message_size(kMessageSizeBytes);
66    ds_config->mutable_for_testing()->set_send_batch_on_register(true);
67
68    helper.StartTracing(trace_config);
69    helper.WaitForTracingDisabled();
70
71    helper.ReadData();
72    helper.WaitForReadData();
73
74    const auto& packets = helper.trace();
75    ASSERT_EQ(packets.size(), kEventCount);
76
77    std::minstd_rand0 rnd_engine(kRandomSeed);
78    for (const auto& packet : packets) {
79      ASSERT_TRUE(packet.has_for_testing());
80      ASSERT_EQ(packet.for_testing().seq_value(), rnd_engine());
81    }
82  }
83};
84
85TEST_F(PerfettoCtsTest, TestProducerActivity) {
86  TestMockProducer("android.perfetto.cts.ProducerActivity");
87}
88
89TEST_F(PerfettoCtsTest, TestProducerService) {
90  TestMockProducer("android.perfetto.cts.ProducerService");
91}
92
93TEST_F(PerfettoCtsTest, TestProducerIsolatedService) {
94  TestMockProducer("android.perfetto.cts.ProducerIsolatedService");
95}
96
97}  // namespace perfetto
98