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 "test/test_helper.h" 18 19 #include "gtest/gtest.h" 20 #include "perfetto/traced/traced.h" 21 #include "perfetto/tracing/core/trace_packet.h" 22 #include "test/task_runner_thread_delegates.h" 23 24 #include "src/tracing/ipc/default_socket.h" 25 26 #include "perfetto/trace/trace_packet.pb.h" 27 #include "perfetto/trace/trace_packet.pbzero.h" 28 29 namespace perfetto { 30 31 // If we're building on Android and starting the daemons ourselves, 32 // create the sockets in a world-writable location. 33 #if PERFETTO_BUILDFLAG(PERFETTO_OS_ANDROID) && \ 34 PERFETTO_BUILDFLAG(PERFETTO_START_DAEMONS) 35 #define TEST_PRODUCER_SOCK_NAME "/data/local/tmp/traced_producer" 36 #define TEST_CONSUMER_SOCK_NAME "/data/local/tmp/traced_consumer" 37 #else 38 #define TEST_PRODUCER_SOCK_NAME ::perfetto::GetProducerSocket() 39 #define TEST_CONSUMER_SOCK_NAME ::perfetto::GetConsumerSocket() 40 #endif 41 42 TestHelper::TestHelper(base::TestTaskRunner* task_runner) 43 : task_runner_(task_runner), 44 service_thread_("perfetto.svc"), 45 producer_thread_("perfetto.prd") {} 46 47 void TestHelper::OnConnect() { 48 std::move(on_connect_callback_)(); 49 } 50 51 void TestHelper::OnDisconnect() { 52 FAIL() << "Consumer unexpectedly disconnected from the service"; 53 } 54 55 void TestHelper::OnTracingDisabled() { 56 std::move(on_stop_tracing_callback_)(); 57 } 58 59 void TestHelper::OnTraceData(std::vector<TracePacket> packets, bool has_more) { 60 for (auto& encoded_packet : packets) { 61 protos::TracePacket packet; 62 ASSERT_TRUE(encoded_packet.Decode(&packet)); 63 if (packet.has_clock_snapshot() || packet.has_trace_config() || 64 packet.has_trace_stats()) { 65 continue; 66 } 67 ASSERT_EQ(protos::TracePacket::kTrustedUid, 68 packet.optional_trusted_uid_case()); 69 trace_.push_back(std::move(packet)); 70 } 71 72 if (!has_more) { 73 std::move(on_packets_finished_callback_)(); 74 } 75 } 76 77 void TestHelper::StartServiceIfRequired() { 78 #if PERFETTO_BUILDFLAG(PERFETTO_START_DAEMONS) 79 service_thread_.Start(std::unique_ptr<ServiceDelegate>( 80 new ServiceDelegate(TEST_PRODUCER_SOCK_NAME, TEST_CONSUMER_SOCK_NAME))); 81 #endif 82 } 83 84 FakeProducer* TestHelper::ConnectFakeProducer() { 85 std::unique_ptr<FakeProducerDelegate> producer_delegate( 86 new FakeProducerDelegate( 87 TEST_PRODUCER_SOCK_NAME, 88 WrapTask(task_runner_->CreateCheckpoint("producer.enabled")))); 89 FakeProducerDelegate* producer_delegate_cached = producer_delegate.get(); 90 producer_thread_.Start(std::move(producer_delegate)); 91 return producer_delegate_cached->producer(); 92 } 93 94 void TestHelper::ConnectConsumer() { 95 on_connect_callback_ = task_runner_->CreateCheckpoint("consumer.connected"); 96 endpoint_ = 97 ConsumerIPCClient::Connect(TEST_CONSUMER_SOCK_NAME, this, task_runner_); 98 } 99 100 void TestHelper::StartTracing(const TraceConfig& config) { 101 on_stop_tracing_callback_ = task_runner_->CreateCheckpoint("stop.tracing"); 102 endpoint_->EnableTracing(config); 103 } 104 105 void TestHelper::ReadData() { 106 on_packets_finished_callback_ = 107 task_runner_->CreateCheckpoint("readback.complete"); 108 endpoint_->ReadBuffers(); 109 } 110 111 void TestHelper::WaitForConsumerConnect() { 112 task_runner_->RunUntilCheckpoint("consumer.connected"); 113 } 114 115 void TestHelper::WaitForProducerEnabled() { 116 task_runner_->RunUntilCheckpoint("producer.enabled"); 117 } 118 119 void TestHelper::WaitForTracingDisabled() { 120 task_runner_->RunUntilCheckpoint("stop.tracing"); 121 } 122 123 void TestHelper::WaitForReadData() { 124 task_runner_->RunUntilCheckpoint("readback.complete"); 125 } 126 127 std::function<void()> TestHelper::WrapTask( 128 const std::function<void()>& function) { 129 return [this, function] { task_runner_->PostTask(function); }; 130 } 131 132 } // namespace perfetto 133