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 "gmock/gmock.h" 23 #include "perfetto/tracing/core/consumer.h" 24 #include "perfetto/tracing/core/service.h" 25 #include "perfetto/tracing/core/trace_packet.h" 26 27 #include "perfetto/trace/trace_packet.pb.h" 28 29 namespace perfetto { 30 31 namespace base { 32 class TestTaskRunner; 33 } 34 35 class MockConsumer : public Consumer { 36 public: 37 class FlushRequest { 38 public: FlushRequest(std::function<bool (void)> wait_func)39 FlushRequest(std::function<bool(void)> wait_func) : wait_func_(wait_func) {} WaitForReply()40 bool WaitForReply() { return wait_func_(); } 41 42 private: 43 std::function<bool(void)> wait_func_; 44 }; 45 46 explicit MockConsumer(base::TestTaskRunner*); 47 ~MockConsumer() override; 48 49 void Connect(Service* svc); 50 void EnableTracing(const TraceConfig&, base::ScopedFile = base::ScopedFile()); 51 void DisableTracing(); 52 void FreeBuffers(); 53 void WaitForTracingDisabled(); 54 FlushRequest Flush(uint32_t timeout_ms = 10000); 55 std::vector<protos::TracePacket> ReadBuffers(); 56 endpoint()57 Service::ConsumerEndpoint* endpoint() { return service_endpoint_.get(); } 58 59 // Consumer implementation. 60 MOCK_METHOD0(OnConnect, void()); 61 MOCK_METHOD0(OnDisconnect, void()); 62 MOCK_METHOD0(OnTracingDisabled, void()); 63 MOCK_METHOD2(OnTraceData, 64 void(std::vector<TracePacket>* /*packets*/, bool /*has_more*/)); 65 66 // gtest doesn't support move-only types. This wrapper is here jut to pass 67 // a pointer to the vector (rather than the vector itself) to the mock method. OnTraceData(std::vector<TracePacket> packets,bool has_more)68 void OnTraceData(std::vector<TracePacket> packets, bool has_more) override { 69 OnTraceData(&packets, has_more); 70 } 71 72 private: 73 base::TestTaskRunner* const task_runner_; 74 std::unique_ptr<Service::ConsumerEndpoint> service_endpoint_; 75 }; 76 77 } // namespace perfetto 78 79 #endif // SRC_TRACING_TEST_MOCK_CONSUMER_H_ 80