Lalit Maganti | c4c3ceb | 2018-03-29 20:38:13 +0100 | [diff] [blame] | 1 | /* |
| 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 Tucci | 07e104d | 2018-04-03 20:45:35 +0200 | [diff] [blame] | 28 | #include "perfetto/trace/trace_packet.pb.h" |
| 29 | |
Lalit Maganti | c4c3ceb | 2018-03-29 20:38:13 +0100 | [diff] [blame] | 30 | namespace perfetto { |
| 31 | |
| 32 | class 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 Tucci | dca727d | 2018-04-04 11:31:55 +0200 | [diff] [blame] | 39 | void OnTracingDisabled() override; |
Lalit Maganti | c4c3ceb | 2018-03-29 20:38:13 +0100 | [diff] [blame] | 40 | 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 Maganti | 36557d8 | 2018-04-11 14:36:17 +0100 | [diff] [blame^] | 46 | void ReadData(); |
| 47 | |
| 48 | void WaitForConsumerConnect(); |
| 49 | void WaitForProducerEnabled(); |
| 50 | void WaitForTracingDisabled(); |
| 51 | void WaitForReadData(); |
Lalit Maganti | c4c3ceb | 2018-03-29 20:38:13 +0100 | [diff] [blame] | 52 | |
| 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 Maganti | 36557d8 | 2018-04-11 14:36:17 +0100 | [diff] [blame^] | 57 | const std::vector<protos::TracePacket>& trace() { return trace_; } |
Lalit Maganti | c4c3ceb | 2018-03-29 20:38:13 +0100 | [diff] [blame] | 58 | |
| 59 | private: |
| 60 | base::TestTaskRunner* task_runner_ = nullptr; |
| 61 | |
Lalit Maganti | 36557d8 | 2018-04-11 14:36:17 +0100 | [diff] [blame^] | 62 | 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 Maganti | c4c3ceb | 2018-03-29 20:38:13 +0100 | [diff] [blame] | 67 | |
| 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_ |