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