blob: 87a9be57ab992abcff1dc9d9e34ce5ff10e1e2bc [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
28namespace perfetto {
29
30class TestHelper : public Consumer {
31 public:
32 explicit TestHelper(base::TestTaskRunner* task_runner);
33
34 // Consumer implementation.
35 void OnConnect() override;
36 void OnDisconnect() override;
37 void OnTracingStop() override;
38 void OnTraceData(std::vector<TracePacket> packets, bool has_more) override;
39
40 void StartServiceIfRequired();
41 FakeProducer* ConnectFakeProducer();
42 void ConnectConsumer();
43 void StartTracing(const TraceConfig& config);
44 void ReadData(std::function<void(const TracePacket::DecodedTracePacket&)>
45 packet_callback,
46 std::function<void()> on_finish_callback);
47
48 std::function<void()> WrapTask(const std::function<void()>& function);
49
50 TaskRunnerThread* service_thread() { return &service_thread_; }
51 TaskRunnerThread* producer_thread() { return &producer_thread_; }
52
53 private:
54 base::TestTaskRunner* task_runner_ = nullptr;
55
56 std::function<void(const TracePacket::DecodedTracePacket&)> packet_callback_;
57 std::function<void()> continuation_callack_;
58
59 TaskRunnerThread service_thread_;
60 TaskRunnerThread producer_thread_;
61 std::unique_ptr<Service::ConsumerEndpoint> endpoint_; // Keep last.
62};
63
64} // namespace perfetto
65
66#endif // TEST_TEST_HELPER_H_