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 SRC_TRACING_TEST_MOCK_PRODUCER_H_ 18 #define SRC_TRACING_TEST_MOCK_PRODUCER_H_ 19 20 #include <map> 21 #include <memory> 22 #include <string> 23 24 #include "perfetto/ext/tracing/core/producer.h" 25 #include "perfetto/ext/tracing/core/shared_memory.h" 26 #include "perfetto/ext/tracing/core/trace_writer.h" 27 #include "perfetto/ext/tracing/core/tracing_service.h" 28 #include "test/gtest_and_gmock.h" 29 30 namespace perfetto { 31 32 namespace base { 33 class TestTaskRunner; 34 } 35 36 class MockProducer : public Producer { 37 public: 38 struct EnabledDataSource { 39 DataSourceInstanceID id; 40 BufferID target_buffer; 41 TracingSessionID session_id; 42 }; 43 44 explicit MockProducer(base::TestTaskRunner*); 45 ~MockProducer() override; 46 47 void Connect(TracingService* svc, 48 const std::string& producer_name, 49 uid_t uid = 42, 50 size_t shared_memory_size_hint_bytes = 0, 51 size_t shared_memory_page_size_hint_bytes = 0, 52 std::unique_ptr<SharedMemory> shm = nullptr); 53 void RegisterDataSource(const std::string& name, 54 bool ack_stop = false, 55 bool ack_start = false, 56 bool handle_incremental_state_clear = false); 57 void UnregisterDataSource(const std::string& name); 58 void RegisterTraceWriter(uint32_t writer_id, uint32_t target_buffer); 59 void UnregisterTraceWriter(uint32_t writer_id); 60 void WaitForTracingSetup(); 61 void WaitForDataSourceSetup(const std::string& name); 62 void WaitForDataSourceStart(const std::string& name); 63 void WaitForDataSourceStop(const std::string& name); 64 DataSourceInstanceID GetDataSourceInstanceId(const std::string& name); 65 const EnabledDataSource* GetDataSourceInstance(const std::string& name); 66 std::unique_ptr<TraceWriter> CreateTraceWriter( 67 const std::string& data_source_name); 68 69 // Expect a flush. Flushes |writer_to_flush| if non-null. If |reply| is true, 70 // replies to the flush request, otherwise ignores it and doesn't reply. 71 void WaitForFlush(TraceWriter* writer_to_flush, bool reply = true); 72 // Same as above, but with a vector of writers. 73 void WaitForFlush(std::vector<TraceWriter*> writers_to_flush, 74 bool reply = true); 75 endpoint()76 TracingService::ProducerEndpoint* endpoint() { 77 return service_endpoint_.get(); 78 } 79 80 // Producer implementation. 81 MOCK_METHOD0(OnConnect, void()); 82 MOCK_METHOD0(OnDisconnect, void()); 83 MOCK_METHOD2(SetupDataSource, 84 void(DataSourceInstanceID, const DataSourceConfig&)); 85 MOCK_METHOD2(StartDataSource, 86 void(DataSourceInstanceID, const DataSourceConfig&)); 87 MOCK_METHOD1(StopDataSource, void(DataSourceInstanceID)); 88 MOCK_METHOD0(OnTracingSetup, void()); 89 MOCK_METHOD3(Flush, 90 void(FlushRequestID, const DataSourceInstanceID*, size_t)); 91 MOCK_METHOD2(ClearIncrementalState, 92 void(const DataSourceInstanceID*, size_t)); 93 94 private: 95 base::TestTaskRunner* const task_runner_; 96 std::string producer_name_; 97 std::unique_ptr<TracingService::ProducerEndpoint> service_endpoint_; 98 std::map<std::string, EnabledDataSource> data_source_instances_; 99 }; 100 101 } // namespace perfetto 102 103 #endif // SRC_TRACING_TEST_MOCK_PRODUCER_H_ 104