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 ChangeTraceConfig(const TraceConfig&); 57 void DisableTracing(); 58 void FreeBuffers(); 59 void WaitForTracingDisabled(uint32_t timeout_ms = 3000) { 60 return WaitForTracingDisabledWithError(testing::_, timeout_ms); 61 } 62 void WaitForTracingDisabledWithError( 63 const testing::Matcher<const std::string&>& error_matcher, 64 uint32_t timeout_ms = 3000); 65 FlushRequest Flush( 66 uint32_t timeout_ms = 10000, 67 FlushFlags = FlushFlags(FlushFlags::Initiator::kConsumerSdk, 68 FlushFlags::Reason::kExplicit)); 69 std::vector<protos::gen::TracePacket> ReadBuffers(); 70 void GetTraceStats(); 71 TraceStats WaitForTraceStats(bool success); 72 TracingServiceState QueryServiceState(); 73 void ObserveEvents(uint32_t enabled_event_types); 74 ObservableEvents WaitForObservableEvents(); 75 void CloneSession(TracingSessionID); 76 endpoint()77 TracingService::ConsumerEndpoint* endpoint() { 78 return service_endpoint_.get(); 79 } 80 81 // Consumer implementation. 82 MOCK_METHOD(void, OnConnect, (), (override)); 83 MOCK_METHOD(void, OnDisconnect, (), (override)); 84 MOCK_METHOD(void, 85 OnTracingDisabled, 86 (const std::string& /*error*/), 87 (override)); 88 MOCK_METHOD(void, 89 OnTraceData, 90 (std::vector<TracePacket>* /*packets*/, bool /*has_more*/)); 91 MOCK_METHOD(void, OnDetach, (bool), (override)); 92 MOCK_METHOD(void, OnAttach, (bool, const TraceConfig&), (override)); 93 MOCK_METHOD(void, OnTraceStats, (bool, const TraceStats&), (override)); 94 MOCK_METHOD(void, OnObservableEvents, (const ObservableEvents&), (override)); 95 MOCK_METHOD(void, OnSessionCloned, (const OnSessionClonedArgs&), (override)); 96 97 // gtest doesn't support move-only types. This wrapper is here jut to pass 98 // a pointer to the vector (rather than the vector itself) to the mock method. OnTraceData(std::vector<TracePacket> packets,bool has_more)99 void OnTraceData(std::vector<TracePacket> packets, bool has_more) override { 100 OnTraceData(&packets, has_more); 101 } 102 103 private: 104 base::TestTaskRunner* const task_runner_; 105 std::unique_ptr<TracingService::ConsumerEndpoint> service_endpoint_; 106 }; 107 108 } // namespace perfetto 109 110 #endif // SRC_TRACING_TEST_MOCK_CONSUMER_H_ 111