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 #ifndef TEST_TEST_HELPER_H_
     18 #define TEST_TEST_HELPER_H_
     19 
     20 #include "perfetto/tracing/core/consumer.h"
     21 #include "perfetto/tracing/core/trace_config.h"
     22 #include "perfetto/tracing/core/trace_packet.h"
     23 #include "perfetto/tracing/ipc/consumer_ipc_client.h"
     24 #include "src/base/test/test_task_runner.h"
     25 #include "test/fake_producer.h"
     26 #include "test/task_runner_thread.h"
     27 
     28 #include "perfetto/trace/trace_packet.pb.h"
     29 
     30 namespace perfetto {
     31 
     32 class TestHelper : public Consumer {
     33  public:
     34   explicit TestHelper(base::TestTaskRunner* task_runner);
     35 
     36   // Consumer implementation.
     37   void OnConnect() override;
     38   void OnDisconnect() override;
     39   void OnTracingDisabled() override;
     40   void OnTraceData(std::vector<TracePacket> packets, bool has_more) override;
     41 
     42   void StartServiceIfRequired();
     43   FakeProducer* ConnectFakeProducer();
     44   void ConnectConsumer();
     45   void StartTracing(const TraceConfig& config);
     46   void ReadData();
     47 
     48   void WaitForConsumerConnect();
     49   void WaitForProducerEnabled();
     50   void WaitForTracingDisabled();
     51   void WaitForReadData();
     52 
     53   std::function<void()> WrapTask(const std::function<void()>& function);
     54 
     55   TaskRunnerThread* service_thread() { return &service_thread_; }
     56   TaskRunnerThread* producer_thread() { return &producer_thread_; }
     57   const std::vector<protos::TracePacket>& trace() { return trace_; }
     58 
     59  private:
     60   base::TestTaskRunner* task_runner_ = nullptr;
     61 
     62   std::function<void()> on_connect_callback_;
     63   std::function<void()> on_packets_finished_callback_;
     64   std::function<void()> on_stop_tracing_callback_;
     65 
     66   std::vector<protos::TracePacket> trace_;
     67 
     68   TaskRunnerThread service_thread_;
     69   TaskRunnerThread producer_thread_;
     70   std::unique_ptr<Service::ConsumerEndpoint> endpoint_;  // Keep last.
     71 };
     72 
     73 }  // namespace perfetto
     74 
     75 #endif  // TEST_TEST_HELPER_H_
     76