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_PRODUCER_H_
18#define SRC_TRACING_TEST_MOCK_PRODUCER_H_
19
20#include <map>
21#include <memory>
22#include <string>
23
24#include "gmock/gmock.h"
25#include "perfetto/tracing/core/producer.h"
26#include "perfetto/tracing/core/service.h"
27#include "perfetto/tracing/core/trace_writer.h"
28
29namespace perfetto {
30
31namespace base {
32class TestTaskRunner;
33}
34
35class MockProducer : public Producer {
36 public:
37  struct EnabledDataSource {
38    DataSourceInstanceID id;
39    BufferID target_buffer;
40  };
41
42  explicit MockProducer(base::TestTaskRunner*);
43  ~MockProducer() override;
44
45  void Connect(Service* svc,
46               const std::string& producer_name,
47               uid_t uid = 42,
48               size_t shared_memory_size_hint_bytes = 0);
49  void RegisterDataSource(const std::string& name);
50  void UnregisterDataSource(const std::string& name);
51  void WaitForTracingSetup();
52  void WaitForDataSourceStart(const std::string& name);
53  void WaitForDataSourceStop(const std::string& name);
54  std::unique_ptr<TraceWriter> CreateTraceWriter(
55      const std::string& data_source_name);
56
57  // If |writer_to_flush| != nullptr does NOT reply to the flush request.
58  // If |writer_to_flush| == nullptr does NOT reply to the flush request.
59  void WaitForFlush(TraceWriter* writer_to_flush);
60
61  Service::ProducerEndpoint* endpoint() { return service_endpoint_.get(); }
62
63  // Producer implementation.
64  MOCK_METHOD0(OnConnect, void());
65  MOCK_METHOD0(OnDisconnect, void());
66  MOCK_METHOD2(CreateDataSourceInstance,
67               void(DataSourceInstanceID, const DataSourceConfig&));
68  MOCK_METHOD1(TearDownDataSourceInstance, void(DataSourceInstanceID));
69  MOCK_METHOD0(OnTracingSetup, void());
70  MOCK_METHOD3(Flush,
71               void(FlushRequestID, const DataSourceInstanceID*, size_t));
72
73 private:
74  base::TestTaskRunner* const task_runner_;
75  std::string producer_name_;
76  std::unique_ptr<Service::ProducerEndpoint> service_endpoint_;
77  std::map<std::string, EnabledDataSource> data_source_instances_;
78};
79
80}  // namespace perfetto
81
82#endif  // SRC_TRACING_TEST_MOCK_PRODUCER_H_
83