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 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/trace_writer.h"
     27 #include "perfetto/tracing/core/tracing_service.h"
     28 
     29 namespace perfetto {
     30 
     31 namespace base {
     32 class TestTaskRunner;
     33 }
     34 
     35 class MockProducer : public Producer {
     36  public:
     37   struct EnabledDataSource {
     38     DataSourceInstanceID id;
     39     BufferID target_buffer;
     40     TracingSessionID session_id;
     41   };
     42 
     43   explicit MockProducer(base::TestTaskRunner*);
     44   ~MockProducer() override;
     45 
     46   void Connect(TracingService* svc,
     47                const std::string& producer_name,
     48                uid_t uid = 42,
     49                size_t shared_memory_size_hint_bytes = 0);
     50   void RegisterDataSource(const std::string& name,
     51                           bool ack_stop = false,
     52                           bool ack_start = false,
     53                           bool handle_incremental_state_clear = false);
     54   void UnregisterDataSource(const std::string& name);
     55   void RegisterTraceWriter(uint32_t writer_id, uint32_t target_buffer);
     56   void UnregisterTraceWriter(uint32_t writer_id);
     57   void WaitForTracingSetup();
     58   void WaitForDataSourceSetup(const std::string& name);
     59   void WaitForDataSourceStart(const std::string& name);
     60   void WaitForDataSourceStop(const std::string& name);
     61   DataSourceInstanceID GetDataSourceInstanceId(const std::string& name);
     62   const EnabledDataSource* GetDataSourceInstance(const std::string& name);
     63   std::unique_ptr<TraceWriter> CreateTraceWriter(
     64       const std::string& data_source_name);
     65 
     66   // Expect a flush. Flushes |writer_to_flush| if non-null. If |reply| is true,
     67   // replies to the flush request, otherwise ignores it and doesn't reply.
     68   void WaitForFlush(TraceWriter* writer_to_flush, bool reply = true);
     69   // Same as above, but with a vector of writers.
     70   void WaitForFlush(std::vector<TraceWriter*> writers_to_flush,
     71                     bool reply = true);
     72 
     73   TracingService::ProducerEndpoint* endpoint() {
     74     return service_endpoint_.get();
     75   }
     76 
     77   // Producer implementation.
     78   MOCK_METHOD0(OnConnect, void());
     79   MOCK_METHOD0(OnDisconnect, void());
     80   MOCK_METHOD2(SetupDataSource,
     81                void(DataSourceInstanceID, const DataSourceConfig&));
     82   MOCK_METHOD2(StartDataSource,
     83                void(DataSourceInstanceID, const DataSourceConfig&));
     84   MOCK_METHOD1(StopDataSource, void(DataSourceInstanceID));
     85   MOCK_METHOD0(OnTracingSetup, void());
     86   MOCK_METHOD3(Flush,
     87                void(FlushRequestID, const DataSourceInstanceID*, size_t));
     88   MOCK_METHOD2(ClearIncrementalState,
     89                void(const DataSourceInstanceID*, size_t));
     90 
     91  private:
     92   base::TestTaskRunner* const task_runner_;
     93   std::string producer_name_;
     94   std::unique_ptr<TracingService::ProducerEndpoint> service_endpoint_;
     95   std::map<std::string, EnabledDataSource> data_source_instances_;
     96 };
     97 
     98 }  // namespace perfetto
     99 
    100 #endif  // SRC_TRACING_TEST_MOCK_PRODUCER_H_
    101