| /* | |
| * 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 <android/gui/WindowInfosUpdate.h> | |
| #include <gmock/gmock.h> | |
| #include <gtest/gtest.h> | |
| #include <gui/WindowInfo.h> | |
| #include <utils/StrongPointer.h> | |
| #include <flat_set> | |
| #include <memory> | |
| #include <vector> | |
| #include "FakeInputTracingBackend.h" | |
| #include "FakeWindows.h" | |
| #include "InputReaderTracer.h" | |
| #include "InputTracingBackendInterface.h" | |
| using ::testing::Field; | |
| namespace android { | |
| class InputReaderTracerTest : public testing::Test { | |
| protected: | |
| std::shared_ptr<input_trace::VerifyingTrace> mTrace; | |
| std::shared_ptr<input_trace::FakeInputTracingBackend> mBackend; | |
| // A non-secure window, to be added at the start of all tests. This checks that the code under | |
| // test isn't just looking for the presence of any windows at all, without checking for them | |
| // being secure. | |
| gui::WindowInfo mInitialWindow; | |
| std::unique_ptr<InputReaderTracer> mTracer; | |
| void SetUp() override { | |
| mTrace = std::make_shared<input_trace::VerifyingTrace>(); | |
| mBackend = std::make_shared<input_trace::FakeInputTracingBackend>(mTrace); | |
| mTracer = std::make_unique<InputReaderTracer>(mBackend); | |
| mInitialWindow.name = "Initial window"; | |
| } | |
| }; | |
| TEST_F(InputReaderTracerTest, InitialSecureWindowVisible) { | |
| gui::WindowInfo secureWindow; | |
| secureWindow.inputConfig |= gui::WindowInfo::InputConfig::SENSITIVE_FOR_PRIVACY; | |
| mTracer->onWindowInfosChanged( | |
| createWindowInfosUpdate({mInitialWindow, secureWindow}, {}, 0, 0)); | |
| mTracer->traceRawEvent({.when = 123}); | |
| mTrace->expectAllRawEventsAtTimestampToMatchMetadata(123, | |
| Field(&input_trace::TracedEventMetadata:: | |
| isSecure, | |
| true)); | |
| mTrace->verifyExpectedEventsTraced(); | |
| } | |
| TEST_F(InputReaderTracerTest, SecureWindowAppearsAfterInitialization) { | |
| mTracer->onWindowInfosChanged(createWindowInfosUpdate({mInitialWindow}, {}, 0, 0)); | |
| mTracer->traceRawEvent({.when = 123}); | |
| mTrace->expectAllRawEventsAtTimestampToMatchMetadata(123, | |
| Field(&input_trace::TracedEventMetadata:: | |
| isSecure, | |
| false)); | |
| gui::WindowInfo secureWindow; | |
| secureWindow.inputConfig |= gui::WindowInfo::InputConfig::SENSITIVE_FOR_PRIVACY; | |
| mTracer->onWindowInfosChanged( | |
| createWindowInfosUpdate({mInitialWindow, secureWindow}, {}, 0, 0)); | |
| mTracer->traceRawEvent({.when = 456}); | |
| mTrace->expectAllRawEventsAtTimestampToMatchMetadata(456, | |
| Field(&input_trace::TracedEventMetadata:: | |
| isSecure, | |
| true)); | |
| mTrace->verifyExpectedEventsTraced(); | |
| } | |
| TEST_F(InputReaderTracerTest, IgnoresInvisibleOrNoInputChannelSecureWindows) { | |
| mTracer->onWindowInfosChanged(createWindowInfosUpdate({mInitialWindow}, {}, 0, 0)); | |
| gui::WindowInfo secureInvisibleWindow; | |
| secureInvisibleWindow.inputConfig |= gui::WindowInfo::InputConfig::SENSITIVE_FOR_PRIVACY; | |
| secureInvisibleWindow.inputConfig |= gui::WindowInfo::InputConfig::NOT_VISIBLE; | |
| gui::WindowInfo secureNoInputChannelWindow; | |
| secureNoInputChannelWindow.inputConfig |= gui::WindowInfo::InputConfig::SENSITIVE_FOR_PRIVACY; | |
| secureNoInputChannelWindow.inputConfig |= gui::WindowInfo::InputConfig::NO_INPUT_CHANNEL; | |
| mTracer->onWindowInfosChanged(createWindowInfosUpdate({mInitialWindow, secureInvisibleWindow, | |
| secureNoInputChannelWindow}, | |
| {}, 0, 0)); | |
| mTracer->traceRawEvent({.when = 123}); | |
| mTrace->expectAllRawEventsAtTimestampToMatchMetadata(123, | |
| Field(&input_trace::TracedEventMetadata:: | |
| isSecure, | |
| false)); | |
| mTrace->verifyExpectedEventsTraced(); | |
| } | |
| TEST_F(InputReaderTracerTest, InitialImeConnectionActive) { | |
| mTracer->setImeConnectionActive(true); | |
| mTracer->traceRawEvent({.when = 123}); | |
| mTrace->expectAllRawEventsAtTimestampToMatchMetadata(123, | |
| Field(&input_trace::TracedEventMetadata:: | |
| isImeConnectionActive, | |
| true)); | |
| mTrace->verifyExpectedEventsTraced(); | |
| } | |
| TEST_F(InputReaderTracerTest, ImeConnectionActiveChangesAfterInitialization) { | |
| mTracer->traceRawEvent({.when = 123}); | |
| mTrace->expectAllRawEventsAtTimestampToMatchMetadata(123, | |
| Field(&input_trace::TracedEventMetadata:: | |
| isImeConnectionActive, | |
| false)); | |
| mTracer->setImeConnectionActive(true); | |
| mTracer->traceRawEvent({.when = 456}); | |
| mTrace->expectAllRawEventsAtTimestampToMatchMetadata(456, | |
| Field(&input_trace::TracedEventMetadata:: | |
| isImeConnectionActive, | |
| true)); | |
| mTracer->setImeConnectionActive(false); | |
| mTracer->traceRawEvent({.when = 789}); | |
| mTrace->expectAllRawEventsAtTimestampToMatchMetadata(789, | |
| Field(&input_trace::TracedEventMetadata:: | |
| isImeConnectionActive, | |
| false)); | |
| mTrace->verifyExpectedEventsTraced(); | |
| } | |
| TEST_F(InputReaderTracerTest, PopulatesTargetsFromVisibleWindows) { | |
| mInitialWindow.ownerUid = gui::Uid{1000}; | |
| gui::WindowInfo window1; | |
| window1.ownerUid = gui::Uid{1001}; | |
| gui::WindowInfo window2; | |
| window2.ownerUid = gui::Uid{1002}; | |
| // A non-visible window should be ignored | |
| gui::WindowInfo invisibleWindow; | |
| invisibleWindow.ownerUid = gui::Uid{1003}; | |
| invisibleWindow.inputConfig |= gui::WindowInfo::InputConfig::NOT_VISIBLE; | |
| // A window with no input channel should be ignored | |
| gui::WindowInfo noInputChannelWindow; | |
| noInputChannelWindow.ownerUid = gui::Uid{1004}; | |
| noInputChannelWindow.inputConfig |= gui::WindowInfo::InputConfig::NO_INPUT_CHANNEL; | |
| mTracer->onWindowInfosChanged(createWindowInfosUpdate({mInitialWindow, window1, window2, | |
| invisibleWindow, noInputChannelWindow}, | |
| /*displayInfos=*/{}, | |
| /*vsyncId=*/0, | |
| /*timestamp=*/0)); | |
| mTracer->traceRawEvent({.when = 123}); | |
| std::flat_set<gui::Uid> expectedTargets = {gui::Uid{1000}, gui::Uid{1001}, gui::Uid{1002}}; | |
| mTrace->expectAllRawEventsAtTimestampToMatchMetadata(123, | |
| Field(&input_trace::TracedEventMetadata:: | |
| targets, | |
| expectedTargets)); | |
| mTrace->verifyExpectedEventsTraced(); | |
| } | |
| } // namespace android |