blob: a5a13018f83a02947240d429d13acad21bdedfe8 [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 Tucci9ba1d842018-12-20 17:31:04 +010020#include "perfetto/base/scoped_file.h"
Lalit Magantic4c3ceb2018-03-29 20:38:13 +010021#include "perfetto/tracing/core/consumer.h"
22#include "perfetto/tracing/core/trace_config.h"
23#include "perfetto/tracing/core/trace_packet.h"
24#include "perfetto/tracing/ipc/consumer_ipc_client.h"
25#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();
36
Lalit Magantic4c3ceb2018-03-29 20:38:13 +010037 explicit TestHelper(base::TestTaskRunner* task_runner);
38
39 // Consumer implementation.
40 void OnConnect() override;
41 void OnDisconnect() override;
Primiano Tuccidca727d2018-04-04 11:31:55 +020042 void OnTracingDisabled() override;
Lalit Magantic4c3ceb2018-03-29 20:38:13 +010043 void OnTraceData(std::vector<TracePacket> packets, bool has_more) override;
Primiano Tucci9ba1d842018-12-20 17:31:04 +010044 void OnDetach(bool) override;
45 void OnAttach(bool, const TraceConfig&) override;
Lalit Magantic4c3ceb2018-03-29 20:38:13 +010046
47 void StartServiceIfRequired();
48 FakeProducer* ConnectFakeProducer();
49 void ConnectConsumer();
Primiano Tucci9ba1d842018-12-20 17:31:04 +010050 void StartTracing(const TraceConfig& config,
51 base::ScopedFile = base::ScopedFile());
Primiano Tuccic1855302018-12-06 10:36:55 +000052 void DisableTracing();
53 void FlushAndWait(uint32_t timeout_ms);
Lalit Maganti3f0b7c62018-04-18 19:10:09 +010054 void ReadData(uint32_t read_count = 0);
Primiano Tucci9ba1d842018-12-20 17:31:04 +010055 void DetachConsumer(const std::string& key);
56 bool AttachConsumer(const std::string& key);
Lalit Maganti36557d82018-04-11 14:36:17 +010057
58 void WaitForConsumerConnect();
59 void WaitForProducerEnabled();
Primiano Tuccic1855302018-12-06 10:36:55 +000060 void WaitForTracingDisabled(uint32_t timeout_ms = 5000);
Lalit Maganti3f0b7c62018-04-18 19:10:09 +010061 void WaitForReadData(uint32_t read_count = 0);
Lalit Magantic4c3ceb2018-03-29 20:38:13 +010062
63 std::function<void()> WrapTask(const std::function<void()>& function);
64
65 TaskRunnerThread* service_thread() { return &service_thread_; }
66 TaskRunnerThread* producer_thread() { return &producer_thread_; }
Lalit Maganti36557d82018-04-11 14:36:17 +010067 const std::vector<protos::TracePacket>& trace() { return trace_; }
Lalit Magantic4c3ceb2018-03-29 20:38:13 +010068
69 private:
70 base::TestTaskRunner* task_runner_ = nullptr;
Primiano Tucci9ba1d842018-12-20 17:31:04 +010071 int cur_consumer_num_ = 0;
Lalit Magantic4c3ceb2018-03-29 20:38:13 +010072
Lalit Maganti36557d82018-04-11 14:36:17 +010073 std::function<void()> on_connect_callback_;
74 std::function<void()> on_packets_finished_callback_;
75 std::function<void()> on_stop_tracing_callback_;
Primiano Tucci9ba1d842018-12-20 17:31:04 +010076 std::function<void()> on_detach_callback_;
77 std::function<void(bool)> on_attach_callback_;
Lalit Maganti36557d82018-04-11 14:36:17 +010078
79 std::vector<protos::TracePacket> trace_;
Lalit Magantic4c3ceb2018-03-29 20:38:13 +010080
81 TaskRunnerThread service_thread_;
82 TaskRunnerThread producer_thread_;
Florian Mayer6a1a4d52018-06-08 16:47:07 +010083 std::unique_ptr<TracingService::ConsumerEndpoint> endpoint_; // Keep last.
Lalit Magantic4c3ceb2018-03-29 20:38:13 +010084};
85
86} // namespace perfetto
87
88#endif // TEST_TEST_HELPER_H_