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 | |
Primiano Tucci | 9ba1d84 | 2018-12-20 17:31:04 +0100 | [diff] [blame] | 20 | #include "perfetto/base/scoped_file.h" |
Lalit Maganti | c4c3ceb | 2018-03-29 20:38:13 +0100 | [diff] [blame] | 21 | #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 Tucci | 07e104d | 2018-04-03 20:45:35 +0200 | [diff] [blame] | 29 | #include "perfetto/trace/trace_packet.pb.h" |
| 30 | |
Lalit Maganti | c4c3ceb | 2018-03-29 20:38:13 +0100 | [diff] [blame] | 31 | namespace perfetto { |
| 32 | |
| 33 | class TestHelper : public Consumer { |
| 34 | public: |
Primiano Tucci | 106605c | 2019-01-08 21:12:58 +0000 | [diff] [blame^] | 35 | static const char* GetConsumerSocketName(); |
| 36 | |
Lalit Maganti | c4c3ceb | 2018-03-29 20:38:13 +0100 | [diff] [blame] | 37 | explicit TestHelper(base::TestTaskRunner* task_runner); |
| 38 | |
| 39 | // Consumer implementation. |
| 40 | void OnConnect() override; |
| 41 | void OnDisconnect() override; |
Primiano Tucci | dca727d | 2018-04-04 11:31:55 +0200 | [diff] [blame] | 42 | void OnTracingDisabled() override; |
Lalit Maganti | c4c3ceb | 2018-03-29 20:38:13 +0100 | [diff] [blame] | 43 | void OnTraceData(std::vector<TracePacket> packets, bool has_more) override; |
Primiano Tucci | 9ba1d84 | 2018-12-20 17:31:04 +0100 | [diff] [blame] | 44 | void OnDetach(bool) override; |
| 45 | void OnAttach(bool, const TraceConfig&) override; |
Lalit Maganti | c4c3ceb | 2018-03-29 20:38:13 +0100 | [diff] [blame] | 46 | |
| 47 | void StartServiceIfRequired(); |
| 48 | FakeProducer* ConnectFakeProducer(); |
| 49 | void ConnectConsumer(); |
Primiano Tucci | 9ba1d84 | 2018-12-20 17:31:04 +0100 | [diff] [blame] | 50 | void StartTracing(const TraceConfig& config, |
| 51 | base::ScopedFile = base::ScopedFile()); |
Primiano Tucci | c185530 | 2018-12-06 10:36:55 +0000 | [diff] [blame] | 52 | void DisableTracing(); |
| 53 | void FlushAndWait(uint32_t timeout_ms); |
Lalit Maganti | 3f0b7c6 | 2018-04-18 19:10:09 +0100 | [diff] [blame] | 54 | void ReadData(uint32_t read_count = 0); |
Primiano Tucci | 9ba1d84 | 2018-12-20 17:31:04 +0100 | [diff] [blame] | 55 | void DetachConsumer(const std::string& key); |
| 56 | bool AttachConsumer(const std::string& key); |
Lalit Maganti | 36557d8 | 2018-04-11 14:36:17 +0100 | [diff] [blame] | 57 | |
| 58 | void WaitForConsumerConnect(); |
| 59 | void WaitForProducerEnabled(); |
Primiano Tucci | c185530 | 2018-12-06 10:36:55 +0000 | [diff] [blame] | 60 | void WaitForTracingDisabled(uint32_t timeout_ms = 5000); |
Lalit Maganti | 3f0b7c6 | 2018-04-18 19:10:09 +0100 | [diff] [blame] | 61 | void WaitForReadData(uint32_t read_count = 0); |
Lalit Maganti | c4c3ceb | 2018-03-29 20:38:13 +0100 | [diff] [blame] | 62 | |
| 63 | std::function<void()> WrapTask(const std::function<void()>& function); |
| 64 | |
| 65 | TaskRunnerThread* service_thread() { return &service_thread_; } |
| 66 | TaskRunnerThread* producer_thread() { return &producer_thread_; } |
Lalit Maganti | 36557d8 | 2018-04-11 14:36:17 +0100 | [diff] [blame] | 67 | const std::vector<protos::TracePacket>& trace() { return trace_; } |
Lalit Maganti | c4c3ceb | 2018-03-29 20:38:13 +0100 | [diff] [blame] | 68 | |
| 69 | private: |
| 70 | base::TestTaskRunner* task_runner_ = nullptr; |
Primiano Tucci | 9ba1d84 | 2018-12-20 17:31:04 +0100 | [diff] [blame] | 71 | int cur_consumer_num_ = 0; |
Lalit Maganti | c4c3ceb | 2018-03-29 20:38:13 +0100 | [diff] [blame] | 72 | |
Lalit Maganti | 36557d8 | 2018-04-11 14:36:17 +0100 | [diff] [blame] | 73 | std::function<void()> on_connect_callback_; |
| 74 | std::function<void()> on_packets_finished_callback_; |
| 75 | std::function<void()> on_stop_tracing_callback_; |
Primiano Tucci | 9ba1d84 | 2018-12-20 17:31:04 +0100 | [diff] [blame] | 76 | std::function<void()> on_detach_callback_; |
| 77 | std::function<void(bool)> on_attach_callback_; |
Lalit Maganti | 36557d8 | 2018-04-11 14:36:17 +0100 | [diff] [blame] | 78 | |
| 79 | std::vector<protos::TracePacket> trace_; |
Lalit Maganti | c4c3ceb | 2018-03-29 20:38:13 +0100 | [diff] [blame] | 80 | |
| 81 | TaskRunnerThread service_thread_; |
| 82 | TaskRunnerThread producer_thread_; |
Florian Mayer | 6a1a4d5 | 2018-06-08 16:47:07 +0100 | [diff] [blame] | 83 | std::unique_ptr<TracingService::ConsumerEndpoint> endpoint_; // Keep last. |
Lalit Maganti | c4c3ceb | 2018-03-29 20:38:13 +0100 | [diff] [blame] | 84 | }; |
| 85 | |
| 86 | } // namespace perfetto |
| 87 | |
| 88 | #endif // TEST_TEST_HELPER_H_ |