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