blob: 4904db511e819089d70b331eb73c8421d9bcd0bf [file] [log] [blame]
Lalit Magantic4c3ceb2018-03-29 20:38:13 +01001/*
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#ifndef TEST_TEST_HELPER_H_
18#define TEST_TEST_HELPER_H_
19
Primiano Tucci2c5488f2019-06-01 03:27:28 +010020#include "perfetto/ext/base/scoped_file.h"
21#include "perfetto/ext/tracing/core/consumer.h"
22#include "perfetto/ext/tracing/core/trace_config.h"
23#include "perfetto/ext/tracing/core/trace_packet.h"
24#include "perfetto/ext/tracing/ipc/consumer_ipc_client.h"
Lalit Magantic4c3ceb2018-03-29 20:38:13 +010025#include "src/base/test/test_task_runner.h"
26#include "test/fake_producer.h"
27#include "test/task_runner_thread.h"
28
Primiano Tucci07e104d2018-04-03 20:45:35 +020029#include "perfetto/trace/trace_packet.pb.h"
30
Lalit Magantic4c3ceb2018-03-29 20:38:13 +010031namespace perfetto {
32
33class TestHelper : public Consumer {
34 public:
Primiano Tucci106605c2019-01-08 21:12:58 +000035 static const char* GetConsumerSocketName();
Stephen Nuskoe8238112019-04-09 18:37:00 +010036 static const char* GetProducerSocketName();
Primiano Tucci106605c2019-01-08 21:12:58 +000037
Lalit Magantic4c3ceb2018-03-29 20:38:13 +010038 explicit TestHelper(base::TestTaskRunner* task_runner);
39
40 // Consumer implementation.
41 void OnConnect() override;
42 void OnDisconnect() override;
Primiano Tuccidca727d2018-04-04 11:31:55 +020043 void OnTracingDisabled() override;
Lalit Magantic4c3ceb2018-03-29 20:38:13 +010044 void OnTraceData(std::vector<TracePacket> packets, bool has_more) override;
Primiano Tucci9ba1d842018-12-20 17:31:04 +010045 void OnDetach(bool) override;
46 void OnAttach(bool, const TraceConfig&) override;
Eric Secklereaf29ed2019-01-23 09:53:55 +000047 void OnTraceStats(bool, const TraceStats&) override;
Eric Seckler7b0c9452019-03-18 13:14:36 +000048 void OnObservableEvents(const ObservableEvents&) override;
Lalit Magantic4c3ceb2018-03-29 20:38:13 +010049
50 void StartServiceIfRequired();
51 FakeProducer* ConnectFakeProducer();
52 void ConnectConsumer();
Primiano Tucci9ba1d842018-12-20 17:31:04 +010053 void StartTracing(const TraceConfig& config,
54 base::ScopedFile = base::ScopedFile());
Primiano Tuccic1855302018-12-06 10:36:55 +000055 void DisableTracing();
56 void FlushAndWait(uint32_t timeout_ms);
Lalit Maganti3f0b7c62018-04-18 19:10:09 +010057 void ReadData(uint32_t read_count = 0);
Primiano Tucci9ba1d842018-12-20 17:31:04 +010058 void DetachConsumer(const std::string& key);
59 bool AttachConsumer(const std::string& key);
Lalit Maganti36557d82018-04-11 14:36:17 +010060
61 void WaitForConsumerConnect();
Stephen Nuskoe8238112019-04-09 18:37:00 +010062 void WaitForProducerSetup();
Lalit Maganti36557d82018-04-11 14:36:17 +010063 void WaitForProducerEnabled();
Primiano Tuccic1855302018-12-06 10:36:55 +000064 void WaitForTracingDisabled(uint32_t timeout_ms = 5000);
Lalit Maganti3f0b7c62018-04-18 19:10:09 +010065 void WaitForReadData(uint32_t read_count = 0);
Lalit Magantic4c3ceb2018-03-29 20:38:13 +010066
Florian Mayer05a87c92019-01-30 13:17:51 +000067 std::string AddID(const std::string& checkpoint) {
68 return checkpoint + "." + std::to_string(instance_num_);
69 }
70
71 std::function<void()> CreateCheckpoint(const std::string& checkpoint) {
72 return task_runner_->CreateCheckpoint(AddID(checkpoint));
73 }
74
75 void RunUntilCheckpoint(const std::string& checkpoint,
76 uint32_t timeout_ms = 5000) {
77 return task_runner_->RunUntilCheckpoint(AddID(checkpoint), timeout_ms);
78 }
79
Lalit Magantic4c3ceb2018-03-29 20:38:13 +010080 std::function<void()> WrapTask(const std::function<void()>& function);
81
82 TaskRunnerThread* service_thread() { return &service_thread_; }
83 TaskRunnerThread* producer_thread() { return &producer_thread_; }
Lalit Maganti36557d82018-04-11 14:36:17 +010084 const std::vector<protos::TracePacket>& trace() { return trace_; }
Lalit Magantic4c3ceb2018-03-29 20:38:13 +010085
86 private:
Florian Mayer05a87c92019-01-30 13:17:51 +000087 static uint64_t next_instance_num_;
88 uint64_t instance_num_;
Lalit Magantic4c3ceb2018-03-29 20:38:13 +010089 base::TestTaskRunner* task_runner_ = nullptr;
Primiano Tucci9ba1d842018-12-20 17:31:04 +010090 int cur_consumer_num_ = 0;
Lalit Magantic4c3ceb2018-03-29 20:38:13 +010091
Lalit Maganti36557d82018-04-11 14:36:17 +010092 std::function<void()> on_connect_callback_;
93 std::function<void()> on_packets_finished_callback_;
94 std::function<void()> on_stop_tracing_callback_;
Primiano Tucci9ba1d842018-12-20 17:31:04 +010095 std::function<void()> on_detach_callback_;
96 std::function<void(bool)> on_attach_callback_;
Lalit Maganti36557d82018-04-11 14:36:17 +010097
98 std::vector<protos::TracePacket> trace_;
Lalit Magantic4c3ceb2018-03-29 20:38:13 +010099
100 TaskRunnerThread service_thread_;
101 TaskRunnerThread producer_thread_;
Florian Mayer6a1a4d52018-06-08 16:47:07 +0100102 std::unique_ptr<TracingService::ConsumerEndpoint> endpoint_; // Keep last.
Lalit Magantic4c3ceb2018-03-29 20:38:13 +0100103};
104
105} // namespace perfetto
106
107#endif // TEST_TEST_HELPER_H_