| /* | |
| * Copyright 2026 The Android Open Source Project | |
| * | |
| * Licensed under the Apache License, Version 2.0 (the "License"); | |
| * you may not use this file except in compliance with the License. | |
| * You may obtain a copy of the License at | |
| * | |
| * http://www.apache.org/licenses/LICENSE-2.0 | |
| * | |
| * Unless required by applicable law or agreed to in writing, software | |
| * distributed under the License is distributed on an "AS IS" BASIS, | |
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| * See the License for the specific language governing permissions and | |
| * limitations under the License. | |
| */ | |
| #include "InputTracingPerfettoBackend.h" | |
| #include <gtest/gtest.h> | |
| #include <perfetto/tracing.h> | |
| #include <chrono> | |
| #include <condition_variable> | |
| #include <memory> | |
| #include <mutex> | |
| namespace android::input_trace::impl { | |
| using namespace std::chrono_literals; | |
| class InputTracingPerfettoBackendTest : public testing::Test { | |
| protected: | |
| void SetUp() override { | |
| PerfettoBackend::sUseInProcessBackendForTest = true; | |
| mBackend = std::make_unique<PerfettoBackend>(); | |
| } | |
| std::unique_ptr<perfetto::TracingSession> startTraceSession(uint32_t clearPeriodMs) { | |
| perfetto::TraceConfig config; | |
| config.add_buffers()->set_size_kb(10); | |
| config.mutable_incremental_state_config()->set_clear_period_ms(clearPeriodMs); | |
| perfetto::DataSourceConfig* dataSourceConfig = config.add_data_sources()->mutable_config(); | |
| dataSourceConfig->set_name("android.input.inputevent"); | |
| std::unique_ptr<perfetto::TracingSession> tracingSession(perfetto::Tracing::NewTrace()); | |
| tracingSession->Setup(config); | |
| tracingSession->StartBlocking(); | |
| return tracingSession; | |
| } | |
| std::unique_ptr<PerfettoBackend> mBackend; | |
| }; | |
| TEST_F(InputTracingPerfettoBackendTest, CallsRegisteredCallbackOnIncrementalStateCleared) { | |
| std::mutex lock; | |
| std::condition_variable cv; | |
| bool callbackCalled = false; | |
| mBackend->registerIncrementalStateClearedCallback(this, [&]() { | |
| std::scoped_lock l(lock); | |
| callbackCalled = true; | |
| cv.notify_all(); | |
| }); | |
| std::unique_ptr<perfetto::TracingSession> tracingSession = | |
| startTraceSession(/*clearPeriodMs=*/10); | |
| { | |
| std::unique_lock l(lock); | |
| ASSERT_TRUE(cv.wait_for(l, 1s, [&]() { return callbackCalled; })); | |
| } | |
| tracingSession->StopBlocking(); | |
| mBackend->deregisterIncrementalStateClearedCallback(this); | |
| } | |
| TEST_F(InputTracingPerfettoBackendTest, CallsMultipleRegisteredCallbacks) { | |
| std::mutex lock; | |
| std::condition_variable cv; | |
| bool callback1Called = false; | |
| bool callback2Called = false; | |
| mBackend->registerIncrementalStateClearedCallback(&callback1Called, [&]() { | |
| std::scoped_lock l(lock); | |
| callback1Called = true; | |
| cv.notify_all(); | |
| }); | |
| mBackend->registerIncrementalStateClearedCallback(&callback2Called, [&]() { | |
| std::scoped_lock l(lock); | |
| callback2Called = true; | |
| cv.notify_all(); | |
| }); | |
| std::unique_ptr<perfetto::TracingSession> tracingSession = | |
| startTraceSession(/*clearPeriodMs=*/10); | |
| { | |
| std::unique_lock l(lock); | |
| ASSERT_TRUE(cv.wait_for(l, 1s, [&]() { return callback1Called && callback2Called; })); | |
| } | |
| tracingSession->StopBlocking(); | |
| mBackend->deregisterIncrementalStateClearedCallback(&callback1Called); | |
| mBackend->deregisterIncrementalStateClearedCallback(&callback2Called); | |
| } | |
| TEST_F(InputTracingPerfettoBackendTest, DeregisteredCallbackNotCalled) { | |
| std::mutex lock; | |
| std::condition_variable cv; | |
| bool callback1Called = false; | |
| bool callback2Called = false; | |
| mBackend->registerIncrementalStateClearedCallback(&callback1Called, [&]() { | |
| std::scoped_lock l(lock); | |
| callback1Called = true; | |
| cv.notify_all(); | |
| }); | |
| mBackend->registerIncrementalStateClearedCallback(&callback2Called, [&]() { | |
| std::scoped_lock l(lock); | |
| callback2Called = true; | |
| cv.notify_all(); | |
| }); | |
| mBackend->deregisterIncrementalStateClearedCallback(&callback1Called); | |
| std::unique_ptr<perfetto::TracingSession> tracingSession = | |
| startTraceSession(/*clearPeriodMs=*/10); | |
| { | |
| std::unique_lock l(lock); | |
| ASSERT_TRUE(cv.wait_for(l, 1s, [&]() { return callback2Called; })); | |
| } | |
| tracingSession->StopBlocking(); | |
| mBackend->deregisterIncrementalStateClearedCallback(&callback2Called); | |
| std::scoped_lock l(lock); | |
| EXPECT_FALSE(callback1Called); | |
| } | |
| } // namespace android::input_trace::impl |