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