@@ -58,6 +58,7 @@ namespace android {
5858using namespace ftl::flag_operators;
5959using testing::AllOf;
6060using std::chrono_literals::operator""ms;
61+ using std::chrono_literals::operator""s;
6162
6263// Arbitrary display properties.
6364static constexpr int32_t DISPLAY_ID = 0;
@@ -149,7 +150,7 @@ static void assertAxisNotPresent(MultiTouchInputMapper& mapper, int axis) {
149150 std::istringstream iss(dump);
150151 for (std::string line; std::getline(iss, line);) {
151152 ALOGE("%s", line.c_str());
152- std::this_thread::sleep_for(std::chrono::milliseconds(1) );
153+ std::this_thread::sleep_for(1ms );
153154 }
154155}
155156
@@ -1374,13 +1375,23 @@ class InputReaderIntegrationTest : public testing::Test {
13741375 mFakePolicy.clear();
13751376 }
13761377
1377- std::optional<InputDeviceInfo> findDeviceByName(const std::string& name) {
1378- const std::vector<InputDeviceInfo> inputDevices = mFakePolicy->getInputDevices();
1379- const auto& it = std::find_if(inputDevices.begin(), inputDevices.end(),
1380- [&name](const InputDeviceInfo& info) {
1381- return info.getIdentifier().name == name;
1382- });
1383- return it != inputDevices.end() ? std::make_optional(*it) : std::nullopt;
1378+ std::optional<InputDeviceInfo> waitForDevice(const std::string& deviceName) {
1379+ std::chrono::time_point start = std::chrono::steady_clock::now();
1380+ while (true) {
1381+ const std::vector<InputDeviceInfo> inputDevices = mFakePolicy->getInputDevices();
1382+ const auto& it = std::find_if(inputDevices.begin(), inputDevices.end(),
1383+ [&deviceName](const InputDeviceInfo& info) {
1384+ return info.getIdentifier().name == deviceName;
1385+ });
1386+ if (it != inputDevices.end()) {
1387+ return std::make_optional(*it);
1388+ }
1389+ std::this_thread::sleep_for(1ms);
1390+ std::chrono::duration elapsed = std::chrono::steady_clock::now() - start;
1391+ if (elapsed > 5s) {
1392+ return {};
1393+ }
1394+ }
13841395 }
13851396
13861397 void setupInputReader() {
@@ -1433,7 +1444,7 @@ TEST_F(InputReaderIntegrationTest, AddNewDevice) {
14331444 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
14341445 ASSERT_EQ(initialNumDevices + 1, mFakePolicy->getInputDevices().size());
14351446
1436- const auto device = findDeviceByName (keyboard->getName());
1447+ const auto device = waitForDevice (keyboard->getName());
14371448 ASSERT_TRUE(device.has_value());
14381449 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, device->getKeyboardType());
14391450 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, device->getSources());
@@ -1476,7 +1487,7 @@ TEST_F(InputReaderIntegrationTest, ExternalStylusesButtons) {
14761487 std::unique_ptr<UinputExternalStylus> stylus = createUinputDevice<UinputExternalStylus>();
14771488 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
14781489
1479- const auto device = findDeviceByName (stylus->getName());
1490+ const auto device = waitForDevice (stylus->getName());
14801491 ASSERT_TRUE(device.has_value());
14811492
14821493 // An external stylus with buttons should also be recognized as a keyboard.
@@ -1516,7 +1527,7 @@ TEST_F(InputReaderIntegrationTest, KeyboardWithStylusButtons) {
15161527 BTN_STYLUS3});
15171528 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
15181529
1519- const auto device = findDeviceByName (keyboard->getName());
1530+ const auto device = waitForDevice (keyboard->getName());
15201531 ASSERT_TRUE(device.has_value());
15211532
15221533 // An alphabetical keyboard that reports stylus buttons should not be recognized as a stylus.
@@ -1533,7 +1544,7 @@ TEST_F(InputReaderIntegrationTest, HidUsageKeyboardIsNotAStylus) {
15331544 std::initializer_list<int>{KEY_VOLUMEUP, KEY_VOLUMEDOWN});
15341545 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
15351546
1536- const auto device = findDeviceByName (keyboard->getName());
1547+ const auto device = waitForDevice (keyboard->getName());
15371548 ASSERT_TRUE(device.has_value());
15381549
15391550 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, device->getSources())
@@ -1587,7 +1598,7 @@ class BaseTouchIntegrationTest : public InputReaderIntegrationTest {
15871598 mDevice = createUinputDevice<UinputTouchScreen>(Rect(0, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT));
15881599 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
15891600 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1590- const auto info = findDeviceByName (mDevice->getName());
1601+ const auto info = waitForDevice (mDevice->getName());
15911602 ASSERT_TRUE(info);
15921603 mDeviceInfo = *info;
15931604 }
@@ -1658,7 +1669,7 @@ class TouchIntegrationTest : public BaseTouchIntegrationTest,
16581669 ViewportType::INTERNAL);
16591670 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
16601671 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1661- const auto info = findDeviceByName (mDevice->getName());
1672+ const auto info = waitForDevice (mDevice->getName());
16621673 ASSERT_TRUE(info);
16631674 mDeviceInfo = *info;
16641675 }
@@ -1991,7 +2002,7 @@ TEST_P(TouchIntegrationTest, ExternalStylusConnectedDuringTouchGesture) {
19912002 auto externalStylus = createUinputDevice<UinputExternalStylus>();
19922003 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
19932004 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1994- const auto stylusInfo = findDeviceByName (externalStylus->getName());
2005+ const auto stylusInfo = waitForDevice (externalStylus->getName());
19952006 ASSERT_TRUE(stylusInfo);
19962007
19972008 // Move
@@ -2062,7 +2073,7 @@ class StylusButtonIntegrationTest : public BaseTouchIntegrationTest {
20622073 mStylus = mStylusDeviceLifecycleTracker.get();
20632074 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
20642075 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2065- const auto info = findDeviceByName (mStylus->getName());
2076+ const auto info = waitForDevice (mStylus->getName());
20662077 ASSERT_TRUE(info);
20672078 mStylusInfo = *info;
20682079 }
@@ -2332,11 +2343,11 @@ TEST_F(ExternalStylusIntegrationTest, ExternalStylusConnectionChangesTouchscreen
23322343 createUinputDevice<UinputExternalStylusWithPressure>();
23332344 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
23342345 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2335- const auto stylusInfo = findDeviceByName (stylus->getName());
2346+ const auto stylusInfo = waitForDevice (stylus->getName());
23362347 ASSERT_TRUE(stylusInfo);
23372348
23382349 // Connecting an external stylus changes the source of the touchscreen.
2339- const auto deviceInfo = findDeviceByName (mDevice->getName());
2350+ const auto deviceInfo = waitForDevice (mDevice->getName());
23402351 ASSERT_TRUE(deviceInfo);
23412352 ASSERT_TRUE(isFromSource(deviceInfo->getSources(), STYLUS_FUSION_SOURCE));
23422353}
@@ -2350,7 +2361,7 @@ TEST_F(ExternalStylusIntegrationTest, FusedExternalStylusPressureReported) {
23502361 createUinputDevice<UinputExternalStylusWithPressure>();
23512362 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
23522363 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2353- const auto stylusInfo = findDeviceByName (stylus->getName());
2364+ const auto stylusInfo = waitForDevice (stylus->getName());
23542365 ASSERT_TRUE(stylusInfo);
23552366
23562367 ASSERT_EQ(AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_KEYBOARD, stylusInfo->getSources());
@@ -2396,7 +2407,7 @@ TEST_F(ExternalStylusIntegrationTest, FusedExternalStylusPressureNotReported) {
23962407 createUinputDevice<UinputExternalStylusWithPressure>();
23972408 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
23982409 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2399- const auto stylusInfo = findDeviceByName (stylus->getName());
2410+ const auto stylusInfo = waitForDevice (stylus->getName());
24002411 ASSERT_TRUE(stylusInfo);
24012412
24022413 ASSERT_EQ(AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_KEYBOARD, stylusInfo->getSources());
@@ -2476,7 +2487,7 @@ TEST_F(ExternalStylusIntegrationTest, UnfusedExternalStylus) {
24762487 std::unique_ptr<UinputExternalStylus> stylus = createUinputDevice<UinputExternalStylus>();
24772488 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
24782489 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2479- const auto stylusInfo = findDeviceByName (stylus->getName());
2490+ const auto stylusInfo = waitForDevice (stylus->getName());
24802491 ASSERT_TRUE(stylusInfo);
24812492
24822493 ASSERT_EQ(AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_KEYBOARD, stylusInfo->getSources());
0 commit comments