blob: b26a88d3d415eb38faefbb2e2b26485c9d4e82ea [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);
Lalit Maganti36557d82018-04-11 14:36:17 +010046 void ReadData();
47
48 void WaitForConsumerConnect();
49 void WaitForProducerEnabled();
50 void WaitForTracingDisabled();
51 void WaitForReadData();
Lalit Magantic4c3ceb2018-03-29 20:38:13 +010052
53 std::function<void()> WrapTask(const std::function<void()>& function);
54
55 TaskRunnerThread* service_thread() { return &service_thread_; }
56 TaskRunnerThread* producer_thread() { return &producer_thread_; }
Lalit Maganti36557d82018-04-11 14:36:17 +010057 const std::vector<protos::TracePacket>& trace() { return trace_; }
Lalit Magantic4c3ceb2018-03-29 20:38:13 +010058
59 private:
60 base::TestTaskRunner* task_runner_ = nullptr;
61
Lalit Maganti36557d82018-04-11 14:36:17 +010062 std::function<void()> on_connect_callback_;
63 std::function<void()> on_packets_finished_callback_;
64 std::function<void()> on_stop_tracing_callback_;
65
66 std::vector<protos::TracePacket> trace_;
Lalit Magantic4c3ceb2018-03-29 20:38:13 +010067
68 TaskRunnerThread service_thread_;
69 TaskRunnerThread producer_thread_;
70 std::unique_ptr<Service::ConsumerEndpoint> endpoint_; // Keep last.
71};
72
73} // namespace perfetto
74
75#endif // TEST_TEST_HELPER_H_