Home | History | Annotate | Download | only in test
      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 "perfetto/tracing/core/trace_stats.h"
     21 #include "src/base/test/test_task_runner.h"
     22 
     23 using ::testing::_;
     24 using ::testing::Invoke;
     25 
     26 namespace perfetto {
     27 
     28 MockConsumer::MockConsumer(base::TestTaskRunner* task_runner)
     29     : task_runner_(task_runner) {}
     30 
     31 MockConsumer::~MockConsumer() {
     32   if (!service_endpoint_)
     33     return;
     34   static int i = 0;
     35   auto checkpoint_name = "on_consumer_disconnect_" + std::to_string(i++);
     36   auto on_disconnect = task_runner_->CreateCheckpoint(checkpoint_name);
     37   EXPECT_CALL(*this, OnDisconnect()).WillOnce(Invoke(on_disconnect));
     38   service_endpoint_.reset();
     39   task_runner_->RunUntilCheckpoint(checkpoint_name);
     40 }
     41 
     42 void MockConsumer::Connect(TracingService* svc, uid_t uid) {
     43   service_endpoint_ = svc->ConnectConsumer(this, uid);
     44   static int i = 0;
     45   auto checkpoint_name = "on_consumer_connect_" + std::to_string(i++);
     46   auto on_connect = task_runner_->CreateCheckpoint(checkpoint_name);
     47   EXPECT_CALL(*this, OnConnect()).WillOnce(Invoke(on_connect));
     48   task_runner_->RunUntilCheckpoint(checkpoint_name);
     49 }
     50 
     51 void MockConsumer::EnableTracing(const TraceConfig& trace_config,
     52                                  base::ScopedFile write_into_file) {
     53   service_endpoint_->EnableTracing(trace_config, std::move(write_into_file));
     54 }
     55 
     56 void MockConsumer::StartTracing() {
     57   service_endpoint_->StartTracing();
     58 }
     59 
     60 void MockConsumer::ChangeTraceConfig(const TraceConfig& trace_config) {
     61   service_endpoint_->ChangeTraceConfig(trace_config);
     62 }
     63 
     64 void MockConsumer::DisableTracing() {
     65   service_endpoint_->DisableTracing();
     66 }
     67 
     68 void MockConsumer::FreeBuffers() {
     69   service_endpoint_->FreeBuffers();
     70 }
     71 
     72 void MockConsumer::WaitForTracingDisabled(uint32_t timeout_ms) {
     73   static int i = 0;
     74   auto checkpoint_name = "on_tracing_disabled_consumer_" + std::to_string(i++);
     75   auto on_tracing_disabled = task_runner_->CreateCheckpoint(checkpoint_name);
     76   EXPECT_CALL(*this, OnTracingDisabled()).WillOnce(Invoke(on_tracing_disabled));
     77   task_runner_->RunUntilCheckpoint(checkpoint_name, timeout_ms);
     78 }
     79 
     80 MockConsumer::FlushRequest MockConsumer::Flush(uint32_t timeout_ms) {
     81   static int i = 0;
     82   auto checkpoint_name = "on_consumer_flush_" + std::to_string(i++);
     83   auto on_flush = task_runner_->CreateCheckpoint(checkpoint_name);
     84   std::shared_ptr<bool> result(new bool());
     85   service_endpoint_->Flush(timeout_ms, [result, on_flush](bool success) {
     86     *result = success;
     87     on_flush();
     88   });
     89 
     90   base::TestTaskRunner* task_runner = task_runner_;
     91   auto wait_for_flush_completion = [result, task_runner,
     92                                     checkpoint_name]() -> bool {
     93     task_runner->RunUntilCheckpoint(checkpoint_name);
     94     return *result;
     95   };
     96 
     97   return FlushRequest(wait_for_flush_completion);
     98 }
     99 
    100 std::vector<protos::TracePacket> MockConsumer::ReadBuffers() {
    101   std::vector<protos::TracePacket> decoded_packets;
    102   static int i = 0;
    103   std::string checkpoint_name = "on_read_buffers_" + std::to_string(i++);
    104   auto on_read_buffers = task_runner_->CreateCheckpoint(checkpoint_name);
    105   EXPECT_CALL(*this, OnTraceData(_, _))
    106       .WillRepeatedly(
    107           Invoke([&decoded_packets, on_read_buffers](
    108                      std::vector<TracePacket>* packets, bool has_more) {
    109             for (TracePacket& packet : *packets) {
    110               decoded_packets.emplace_back();
    111               protos::TracePacket* decoded_packet = &decoded_packets.back();
    112               packet.Decode(decoded_packet);
    113             }
    114             if (!has_more)
    115               on_read_buffers();
    116           }));
    117   service_endpoint_->ReadBuffers();
    118   task_runner_->RunUntilCheckpoint(checkpoint_name);
    119   return decoded_packets;
    120 }
    121 
    122 void MockConsumer::GetTraceStats() {
    123   service_endpoint_->GetTraceStats();
    124 }
    125 
    126 void MockConsumer::WaitForTraceStats(bool success) {
    127   static int i = 0;
    128   auto checkpoint_name = "on_trace_stats_" + std::to_string(i++);
    129   auto on_trace_stats = task_runner_->CreateCheckpoint(checkpoint_name);
    130   auto result_callback = [on_trace_stats](bool, const TraceStats&) {
    131     on_trace_stats();
    132   };
    133   if (success) {
    134     EXPECT_CALL(*this,
    135                 OnTraceStats(true, testing::Property(&TraceStats::total_buffers,
    136                                                      testing::Gt(0u))))
    137         .WillOnce(Invoke(result_callback));
    138   } else {
    139     EXPECT_CALL(*this, OnTraceStats(false, _))
    140         .WillOnce(Invoke(result_callback));
    141   }
    142   task_runner_->RunUntilCheckpoint(checkpoint_name);
    143 }
    144 
    145 void MockConsumer::ObserveEvents(uint32_t enabled_event_types) {
    146   service_endpoint_->ObserveEvents(enabled_event_types);
    147 }
    148 
    149 ObservableEvents MockConsumer::WaitForObservableEvents() {
    150   ObservableEvents events;
    151   static int i = 0;
    152   std::string checkpoint_name = "on_observable_events_" + std::to_string(i++);
    153   auto on_observable_events = task_runner_->CreateCheckpoint(checkpoint_name);
    154   EXPECT_CALL(*this, OnObservableEvents(_))
    155       .WillOnce(Invoke([&events, on_observable_events](
    156                            const ObservableEvents& observable_events) {
    157         events = observable_events;
    158         on_observable_events();
    159       }));
    160   task_runner_->RunUntilCheckpoint(checkpoint_name);
    161   return events;
    162 }
    163 
    164 }  // namespace perfetto
    165