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#include "src/tracing/test/mock_consumer.h"
18
19#include "perfetto/tracing/core/trace_config.h"
20#include "src/base/test/test_task_runner.h"
21
22using ::testing::_;
23using ::testing::Invoke;
24
25namespace perfetto {
26
27MockConsumer::MockConsumer(base::TestTaskRunner* task_runner)
28    : task_runner_(task_runner) {}
29
30MockConsumer::~MockConsumer() {
31  if (!service_endpoint_)
32    return;
33  static int i = 0;
34  auto checkpoint_name = "on_consumer_disconnect_" + std::to_string(i++);
35  auto on_disconnect = task_runner_->CreateCheckpoint(checkpoint_name);
36  EXPECT_CALL(*this, OnDisconnect()).WillOnce(Invoke(on_disconnect));
37  service_endpoint_.reset();
38  task_runner_->RunUntilCheckpoint(checkpoint_name);
39}
40
41void MockConsumer::Connect(Service* svc) {
42  service_endpoint_ = svc->ConnectConsumer(this);
43  static int i = 0;
44  auto checkpoint_name = "on_consumer_connect_" + std::to_string(i++);
45  auto on_connect = task_runner_->CreateCheckpoint(checkpoint_name);
46  EXPECT_CALL(*this, OnConnect()).WillOnce(Invoke(on_connect));
47  task_runner_->RunUntilCheckpoint(checkpoint_name);
48}
49
50void MockConsumer::EnableTracing(const TraceConfig& trace_config,
51                                 base::ScopedFile write_into_file) {
52  service_endpoint_->EnableTracing(trace_config, std::move(write_into_file));
53}
54
55void MockConsumer::DisableTracing() {
56  service_endpoint_->DisableTracing();
57}
58
59void MockConsumer::FreeBuffers() {
60  service_endpoint_->FreeBuffers();
61}
62
63void MockConsumer::WaitForTracingDisabled() {
64  static int i = 0;
65  auto checkpoint_name = "on_tracing_disabled_consumer_" + std::to_string(i++);
66  auto on_tracing_disabled = task_runner_->CreateCheckpoint(checkpoint_name);
67  EXPECT_CALL(*this, OnTracingDisabled()).WillOnce(Invoke(on_tracing_disabled));
68  task_runner_->RunUntilCheckpoint(checkpoint_name);
69}
70
71MockConsumer::FlushRequest MockConsumer::Flush(uint32_t timeout_ms) {
72  static int i = 0;
73  auto checkpoint_name = "on_consumer_flush_" + std::to_string(i++);
74  auto on_flush = task_runner_->CreateCheckpoint(checkpoint_name);
75  std::shared_ptr<bool> result(new bool());
76  service_endpoint_->Flush(timeout_ms, [result, on_flush](bool success) {
77    *result = success;
78    on_flush();
79  });
80
81  base::TestTaskRunner* task_runner = task_runner_;
82  auto wait_for_flush_completion = [result, task_runner,
83                                    checkpoint_name]() -> bool {
84    task_runner->RunUntilCheckpoint(checkpoint_name);
85    return *result;
86  };
87
88  return FlushRequest(wait_for_flush_completion);
89}
90
91std::vector<protos::TracePacket> MockConsumer::ReadBuffers() {
92  std::vector<protos::TracePacket> decoded_packets;
93  static int i = 0;
94  std::string checkpoint_name = "on_read_buffers_" + std::to_string(i++);
95  auto on_read_buffers = task_runner_->CreateCheckpoint(checkpoint_name);
96  EXPECT_CALL(*this, OnTraceData(_, _))
97      .WillRepeatedly(
98          Invoke([&decoded_packets, on_read_buffers](
99                     std::vector<TracePacket>* packets, bool has_more) {
100            for (TracePacket& packet : *packets) {
101              decoded_packets.emplace_back();
102              protos::TracePacket* decoded_packet = &decoded_packets.back();
103              packet.Decode(decoded_packet);
104            }
105            if (!has_more)
106              on_read_buffers();
107          }));
108  service_endpoint_->ReadBuffers();
109  task_runner_->RunUntilCheckpoint(checkpoint_name);
110  return decoded_packets;
111}
112
113}  // namespace perfetto
114