blob: 89a2fde4d29cf781a3d1b8ba3976ac3776685457 [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:
35 explicit TestHelper(base::TestTaskRunner* task_runner);
36
37 // Consumer implementation.
38 void OnConnect() override;
39 void OnDisconnect() override;
Primiano Tuccidca727d2018-04-04 11:31:55 +020040 void OnTracingDisabled() override;
Lalit Magantic4c3ceb2018-03-29 20:38:13 +010041 void OnTraceData(std::vector<TracePacket> packets, bool has_more) override;
Primiano Tucci9ba1d842018-12-20 17:31:04 +010042 void OnDetach(bool) override;
43 void OnAttach(bool, const TraceConfig&) override;
Lalit Magantic4c3ceb2018-03-29 20:38:13 +010044
45 void StartServiceIfRequired();
46 FakeProducer* ConnectFakeProducer();
47 void ConnectConsumer();
Primiano Tucci9ba1d842018-12-20 17:31:04 +010048 void StartTracing(const TraceConfig& config,
49 base::ScopedFile = base::ScopedFile());
Primiano Tuccic1855302018-12-06 10:36:55 +000050 void DisableTracing();
51 void FlushAndWait(uint32_t timeout_ms);
Lalit Maganti3f0b7c62018-04-18 19:10:09 +010052 void ReadData(uint32_t read_count = 0);
Primiano Tucci9ba1d842018-12-20 17:31:04 +010053 void DetachConsumer(const std::string& key);
54 bool AttachConsumer(const std::string& key);
Lalit Maganti36557d82018-04-11 14:36:17 +010055
56 void WaitForConsumerConnect();
57 void WaitForProducerEnabled();
Primiano Tuccic1855302018-12-06 10:36:55 +000058 void WaitForTracingDisabled(uint32_t timeout_ms = 5000);
Lalit Maganti3f0b7c62018-04-18 19:10:09 +010059 void WaitForReadData(uint32_t read_count = 0);
Lalit Magantic4c3ceb2018-03-29 20:38:13 +010060
61 std::function<void()> WrapTask(const std::function<void()>& function);
62
63 TaskRunnerThread* service_thread() { return &service_thread_; }
64 TaskRunnerThread* producer_thread() { return &producer_thread_; }
Lalit Maganti36557d82018-04-11 14:36:17 +010065 const std::vector<protos::TracePacket>& trace() { return trace_; }
Lalit Magantic4c3ceb2018-03-29 20:38:13 +010066
67 private:
68 base::TestTaskRunner* task_runner_ = nullptr;
Primiano Tucci9ba1d842018-12-20 17:31:04 +010069 int cur_consumer_num_ = 0;
Lalit Magantic4c3ceb2018-03-29 20:38:13 +010070
Lalit Maganti36557d82018-04-11 14:36:17 +010071 std::function<void()> on_connect_callback_;
72 std::function<void()> on_packets_finished_callback_;
73 std::function<void()> on_stop_tracing_callback_;
Primiano Tucci9ba1d842018-12-20 17:31:04 +010074 std::function<void()> on_detach_callback_;
75 std::function<void(bool)> on_attach_callback_;
Lalit Maganti36557d82018-04-11 14:36:17 +010076
77 std::vector<protos::TracePacket> trace_;
Lalit Magantic4c3ceb2018-03-29 20:38:13 +010078
79 TaskRunnerThread service_thread_;
80 TaskRunnerThread producer_thread_;
Florian Mayer6a1a4d52018-06-08 16:47:07 +010081 std::unique_ptr<TracingService::ConsumerEndpoint> endpoint_; // Keep last.
Lalit Magantic4c3ceb2018-03-29 20:38:13 +010082};
83
84} // namespace perfetto
85
86#endif // TEST_TEST_HELPER_H_