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(TracingService* svc, uid_t = 0); 51 void EnableTracing(const TraceConfig&, base::ScopedFile = base::ScopedFile()); 52 void StartTracing(); 53 void ChangeTraceConfig(const TraceConfig&); 54 void DisableTracing(); 55 void FreeBuffers(); 56 void WaitForTracingDisabled(uint32_t timeout_ms = 3000); 57 FlushRequest Flush(uint32_t timeout_ms = 10000); 58 std::vector<protos::gen::TracePacket> ReadBuffers(); 59 void GetTraceStats(); 60 void WaitForTraceStats(bool success); 61 TracingServiceState QueryServiceState(); 62 void ObserveEvents(uint32_t enabled_event_types); 63 ObservableEvents WaitForObservableEvents(); 64 endpoint()65 TracingService::ConsumerEndpoint* endpoint() { 66 return service_endpoint_.get(); 67 } 68 69 // Consumer implementation. 70 MOCK_METHOD0(OnConnect, void()); 71 MOCK_METHOD0(OnDisconnect, void()); 72 MOCK_METHOD1(OnTracingDisabled, void(const std::string& /*error*/)); 73 MOCK_METHOD2(OnTraceData, 74 void(std::vector<TracePacket>* /*packets*/, bool /*has_more*/)); 75 MOCK_METHOD1(OnDetach, void(bool)); 76 MOCK_METHOD2(OnAttach, void(bool, const TraceConfig&)); 77 MOCK_METHOD2(OnTraceStats, void(bool, const TraceStats&)); 78 MOCK_METHOD1(OnObservableEvents, void(const ObservableEvents&)); 79 80 // gtest doesn't support move-only types. This wrapper is here jut to pass 81 // a pointer to the vector (rather than the vector itself) to the mock method. OnTraceData(std::vector<TracePacket> packets,bool has_more)82 void OnTraceData(std::vector<TracePacket> packets, bool has_more) override { 83 OnTraceData(&packets, has_more); 84 } 85 86 private: 87 base::TestTaskRunner* const task_runner_; 88 std::unique_ptr<TracingService::ConsumerEndpoint> service_endpoint_; 89 }; 90 91 } // namespace perfetto 92 93 #endif // SRC_TRACING_TEST_MOCK_CONSUMER_H_ 94