blob: 563345fe80014468317c7e25a9cb7500f2d0e873 [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 Tucci07e104d2018-04-03 20:45:35 +020046 void ReadData(std::function<void(const protos::TracePacket&)> packet_callback,
Lalit Magantic4c3ceb2018-03-29 20:38:13 +010047 std::function<void()> on_finish_callback);
48
49 std::function<void()> WrapTask(const std::function<void()>& function);
50
51 TaskRunnerThread* service_thread() { return &service_thread_; }
52 TaskRunnerThread* producer_thread() { return &producer_thread_; }
53
54 private:
55 base::TestTaskRunner* task_runner_ = nullptr;
56
Primiano Tucci07e104d2018-04-03 20:45:35 +020057 std::function<void(const protos::TracePacket&)> packet_callback_;
Lalit Magantic4c3ceb2018-03-29 20:38:13 +010058 std::function<void()> continuation_callack_;
59
60 TaskRunnerThread service_thread_;
61 TaskRunnerThread producer_thread_;
62 std::unique_ptr<Service::ConsumerEndpoint> endpoint_; // Keep last.
63};
64
65} // namespace perfetto
66
67#endif // TEST_TEST_HELPER_H_