blob: f08c98a160c81907c4cab32e5e3b15533b7ceb17 [file] [log] [blame]
Primiano Tuccidca727d2018-04-04 11:31:55 +02001/*
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
29namespace perfetto {
30
31namespace base {
32class TestTaskRunner;
33}
34
35class MockConsumer : public Consumer {
36 public:
Primiano Tuccid52e6272018-04-06 19:06:53 +020037 class FlushRequest {
38 public:
39 FlushRequest(std::function<bool(void)> wait_func) : wait_func_(wait_func) {}
40 bool WaitForReply() { return wait_func_(); }
41
42 private:
43 std::function<bool(void)> wait_func_;
44 };
45
Primiano Tuccidca727d2018-04-04 11:31:55 +020046 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();
Primiano Tucci3cbb10a2018-04-10 17:52:40 +010054 FlushRequest Flush(uint32_t timeout_ms = 10000);
Primiano Tuccidca727d2018-04-04 11:31:55 +020055 std::vector<protos::TracePacket> ReadBuffers();
56
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.
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_