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/base/scoped_file.h" 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 29 #include "perfetto/trace/trace_packet.pb.h" 30 31 namespace perfetto { 32 33 class TestHelper : public Consumer { 34 public: 35 static const char* GetConsumerSocketName(); 36 static const char* GetProducerSocketName(); 37 38 explicit TestHelper(base::TestTaskRunner* task_runner); 39 40 // Consumer implementation. 41 void OnConnect() override; 42 void OnDisconnect() override; 43 void OnTracingDisabled() override; 44 void OnTraceData(std::vector<TracePacket> packets, bool has_more) override; 45 void OnDetach(bool) override; 46 void OnAttach(bool, const TraceConfig&) override; 47 void OnTraceStats(bool, const TraceStats&) override; 48 void OnObservableEvents(const ObservableEvents&) override; 49 50 void StartServiceIfRequired(); 51 FakeProducer* ConnectFakeProducer(); 52 void ConnectConsumer(); 53 void StartTracing(const TraceConfig& config, 54 base::ScopedFile = base::ScopedFile()); 55 void DisableTracing(); 56 void FlushAndWait(uint32_t timeout_ms); 57 void ReadData(uint32_t read_count = 0); 58 void DetachConsumer(const std::string& key); 59 bool AttachConsumer(const std::string& key); 60 61 void WaitForConsumerConnect(); 62 void WaitForProducerSetup(); 63 void WaitForProducerEnabled(); 64 void WaitForTracingDisabled(uint32_t timeout_ms = 5000); 65 void WaitForReadData(uint32_t read_count = 0); 66 AddID(const std::string & checkpoint)67 std::string AddID(const std::string& checkpoint) { 68 return checkpoint + "." + std::to_string(instance_num_); 69 } 70 CreateCheckpoint(const std::string & checkpoint)71 std::function<void()> CreateCheckpoint(const std::string& checkpoint) { 72 return task_runner_->CreateCheckpoint(AddID(checkpoint)); 73 } 74 75 void RunUntilCheckpoint(const std::string& checkpoint, 76 uint32_t timeout_ms = 5000) { 77 return task_runner_->RunUntilCheckpoint(AddID(checkpoint), timeout_ms); 78 } 79 80 std::function<void()> WrapTask(const std::function<void()>& function); 81 service_thread()82 TaskRunnerThread* service_thread() { return &service_thread_; } producer_thread()83 TaskRunnerThread* producer_thread() { return &producer_thread_; } trace()84 const std::vector<protos::TracePacket>& trace() { return trace_; } 85 86 private: 87 static uint64_t next_instance_num_; 88 uint64_t instance_num_; 89 base::TestTaskRunner* task_runner_ = nullptr; 90 int cur_consumer_num_ = 0; 91 92 std::function<void()> on_connect_callback_; 93 std::function<void()> on_packets_finished_callback_; 94 std::function<void()> on_stop_tracing_callback_; 95 std::function<void()> on_detach_callback_; 96 std::function<void(bool)> on_attach_callback_; 97 98 std::vector<protos::TracePacket> trace_; 99 100 TaskRunnerThread service_thread_; 101 TaskRunnerThread producer_thread_; 102 std::unique_ptr<TracingService::ConsumerEndpoint> endpoint_; // Keep last. 103 }; 104 105 } // namespace perfetto 106 107 #endif // TEST_TEST_HELPER_H_ 108