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 bool no_flush = false); 60 void UnregisterDataSource(const std::string& name); 61 void RegisterTrackEventDataSource( 62 const std::initializer_list<std::string>& categories, 63 uint32_t id); 64 void UpdateTrackEventDataSource( 65 const std::initializer_list<std::string>& categories, 66 uint32_t id); 67 void RegisterTraceWriter(uint32_t writer_id, uint32_t target_buffer); 68 void UnregisterTraceWriter(uint32_t writer_id); 69 void WaitForTracingSetup(); 70 void WaitForDataSourceSetup(const std::string& name); 71 void WaitForDataSourceStart(const std::string& name); 72 void WaitForDataSourceStop(const std::string& name); 73 DataSourceInstanceID GetDataSourceInstanceId(const std::string& name); 74 const EnabledDataSource* GetDataSourceInstance(const std::string& name); 75 std::unique_ptr<TraceWriter> CreateTraceWriter( 76 const std::string& data_source_name); 77 78 // Expect a flush. Flushes |writer_to_flush| if non-null. If |reply| is true, 79 // replies to the flush request, otherwise ignores it and doesn't reply. 80 void ExpectFlush(TraceWriter* writer_to_flush, 81 bool reply = true, 82 FlushFlags expected_flags = FlushFlags()); 83 // Same as above, but with a vector of writers. 84 void ExpectFlush(std::vector<TraceWriter*> writers_to_flush, 85 bool reply = true, 86 FlushFlags expected_flags = FlushFlags()); 87 endpoint()88 TracingService::ProducerEndpoint* endpoint() { 89 return service_endpoint_.get(); 90 } 91 92 // Producer implementation. 93 MOCK_METHOD(void, OnConnect, (), (override)); 94 MOCK_METHOD(void, OnDisconnect, (), (override)); 95 MOCK_METHOD(void, 96 SetupDataSource, 97 (DataSourceInstanceID, const DataSourceConfig&), 98 (override)); 99 MOCK_METHOD(void, 100 StartDataSource, 101 (DataSourceInstanceID, const DataSourceConfig&), 102 (override)); 103 MOCK_METHOD(void, StopDataSource, (DataSourceInstanceID), (override)); 104 MOCK_METHOD(void, OnTracingSetup, (), (override)); 105 MOCK_METHOD(void, 106 Flush, 107 (FlushRequestID, const DataSourceInstanceID*, size_t, FlushFlags), 108 (override)); 109 MOCK_METHOD(void, 110 ClearIncrementalState, 111 (const DataSourceInstanceID*, size_t), 112 (override)); 113 114 private: 115 base::TestTaskRunner* const task_runner_; 116 std::string producer_name_; 117 std::unique_ptr<TracingService::ProducerEndpoint> service_endpoint_; 118 std::map<std::string, EnabledDataSource> data_source_instances_; 119 }; 120 121 } // namespace perfetto 122 123 #endif // SRC_TRACING_TEST_MOCK_PRODUCER_H_ 124