xref: /aosp_15_r20/external/perfetto/src/tracing/test/mock_consumer.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_CONSUMER_H_
18 #define SRC_TRACING_TEST_MOCK_CONSUMER_H_
19 
20 #include <memory>
21 #include <string_view>
22 
23 #include "perfetto/ext/tracing/core/consumer.h"
24 #include "perfetto/ext/tracing/core/trace_packet.h"
25 #include "perfetto/ext/tracing/core/tracing_service.h"
26 #include "perfetto/tracing/core/tracing_service_state.h"
27 #include "test/gtest_and_gmock.h"
28 
29 #include "protos/perfetto/trace/trace_packet.gen.h"
30 
31 namespace perfetto {
32 
33 namespace base {
34 class TestTaskRunner;
35 }
36 
37 class MockConsumer : public Consumer {
38  public:
39   class FlushRequest {
40    public:
FlushRequest(std::function<bool (void)> wait_func)41     FlushRequest(std::function<bool(void)> wait_func) : wait_func_(wait_func) {}
WaitForReply()42     bool WaitForReply() { return wait_func_(); }
43 
44    private:
45     std::function<bool(void)> wait_func_;
46   };
47 
48   explicit MockConsumer(base::TestTaskRunner*);
49   ~MockConsumer() override;
50 
51   void Connect(std::unique_ptr<TracingService::ConsumerEndpoint>);
52   void Connect(TracingService* svc, uid_t = 0);
53   void ForceDisconnect();
54   void EnableTracing(const TraceConfig&, base::ScopedFile = base::ScopedFile());
55   void StartTracing();
56   void Detach(std::string key);
57   void Attach(std::string key);
58   void ChangeTraceConfig(const TraceConfig&);
59   void DisableTracing();
60   void FreeBuffers();
61   void WaitForTracingDisabled(uint32_t timeout_ms = 3000) {
62     return WaitForTracingDisabledWithError(testing::_, timeout_ms);
63   }
64   void WaitForTracingDisabledWithError(
65       const testing::Matcher<const std::string&>& error_matcher,
66       uint32_t timeout_ms = 3000);
67   FlushRequest Flush(
68       uint32_t timeout_ms = 10000,
69       FlushFlags = FlushFlags(FlushFlags::Initiator::kConsumerSdk,
70                               FlushFlags::Reason::kExplicit));
71   std::vector<protos::gen::TracePacket> ReadBuffers();
72   void GetTraceStats();
73   TraceStats WaitForTraceStats(bool success);
74   TracingServiceState QueryServiceState();
75   void ObserveEvents(uint32_t enabled_event_types);
76   ObservableEvents WaitForObservableEvents();
77   void CloneSession(TracingSessionID);
78 
endpoint()79   TracingService::ConsumerEndpoint* endpoint() {
80     return service_endpoint_.get();
81   }
82 
83   // Consumer implementation.
84   MOCK_METHOD(void, OnConnect, (), (override));
85   MOCK_METHOD(void, OnDisconnect, (), (override));
86   MOCK_METHOD(void,
87               OnTracingDisabled,
88               (const std::string& /*error*/),
89               (override));
90   MOCK_METHOD(void,
91               OnTraceData,
92               (std::vector<TracePacket>* /*packets*/, bool /*has_more*/));
93   MOCK_METHOD(void, OnDetach, (bool), (override));
94   MOCK_METHOD(void, OnAttach, (bool, const TraceConfig&), (override));
95   MOCK_METHOD(void, OnTraceStats, (bool, const TraceStats&), (override));
96   MOCK_METHOD(void, OnObservableEvents, (const ObservableEvents&), (override));
97   MOCK_METHOD(void, OnSessionCloned, (const OnSessionClonedArgs&), (override));
98 
99   // gtest doesn't support move-only types. This wrapper is here jut to pass
100   // a pointer to the vector (rather than the vector itself) to the mock method.
OnTraceData(std::vector<TracePacket> packets,bool has_more)101   void OnTraceData(std::vector<TracePacket> packets, bool has_more) override {
102     OnTraceData(&packets, has_more);
103   }
104 
105  private:
106   base::TestTaskRunner* const task_runner_;
107   std::unique_ptr<TracingService::ConsumerEndpoint> service_endpoint_;
108 };
109 
110 }  // namespace perfetto
111 
112 #endif  // SRC_TRACING_TEST_MOCK_CONSUMER_H_
113