blob: 599ce5d2cfa1a7f7e5369d0ef77b1a46d3c0759d [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
20#include "perfetto/tracing/core/consumer.h"
21#include "perfetto/tracing/core/trace_config.h"
22#include "perfetto/tracing/core/trace_packet.h"
23#include "perfetto/tracing/ipc/consumer_ipc_client.h"
24#include "src/base/test/test_task_runner.h"
25#include "test/fake_producer.h"
26#include "test/task_runner_thread.h"
27
Primiano Tucci07e104d2018-04-03 20:45:35 +020028#include "perfetto/trace/trace_packet.pb.h"
29
Lalit Magantic4c3ceb2018-03-29 20:38:13 +010030namespace perfetto {
31
32class TestHelper : public Consumer {
33 public:
34 explicit TestHelper(base::TestTaskRunner* task_runner);
35
36 // Consumer implementation.
37 void OnConnect() override;
38 void OnDisconnect() override;
Primiano Tuccidca727d2018-04-04 11:31:55 +020039 void OnTracingDisabled() override;
Lalit Magantic4c3ceb2018-03-29 20:38:13 +010040 void OnTraceData(std::vector<TracePacket> packets, bool has_more) override;
41
42 void StartServiceIfRequired();
43 FakeProducer* ConnectFakeProducer();
44 void ConnectConsumer();
45 void StartTracing(const TraceConfig& config);
Primiano Tuccic1855302018-12-06 10:36:55 +000046 void DisableTracing();
47 void FlushAndWait(uint32_t timeout_ms);
Lalit Maganti3f0b7c62018-04-18 19:10:09 +010048 void ReadData(uint32_t read_count = 0);
Lalit Maganti36557d82018-04-11 14:36:17 +010049
50 void WaitForConsumerConnect();
51 void WaitForProducerEnabled();
Primiano Tuccic1855302018-12-06 10:36:55 +000052 void WaitForTracingDisabled(uint32_t timeout_ms = 5000);
Lalit Maganti3f0b7c62018-04-18 19:10:09 +010053 void WaitForReadData(uint32_t read_count = 0);
Lalit Magantic4c3ceb2018-03-29 20:38:13 +010054
55 std::function<void()> WrapTask(const std::function<void()>& function);
56
57 TaskRunnerThread* service_thread() { return &service_thread_; }
58 TaskRunnerThread* producer_thread() { return &producer_thread_; }
Lalit Maganti36557d82018-04-11 14:36:17 +010059 const std::vector<protos::TracePacket>& trace() { return trace_; }
Lalit Magantic4c3ceb2018-03-29 20:38:13 +010060
61 private:
62 base::TestTaskRunner* task_runner_ = nullptr;
63
Lalit Maganti36557d82018-04-11 14:36:17 +010064 std::function<void()> on_connect_callback_;
65 std::function<void()> on_packets_finished_callback_;
66 std::function<void()> on_stop_tracing_callback_;
67
68 std::vector<protos::TracePacket> trace_;
Lalit Magantic4c3ceb2018-03-29 20:38:13 +010069
70 TaskRunnerThread service_thread_;
71 TaskRunnerThread producer_thread_;
Florian Mayer6a1a4d52018-06-08 16:47:07 +010072 std::unique_ptr<TracingService::ConsumerEndpoint> endpoint_; // Keep last.
Lalit Magantic4c3ceb2018-03-29 20:38:13 +010073};
74
75} // namespace perfetto
76
77#endif // TEST_TEST_HELPER_H_