Fix NoFocusedWindow_DoesNotSendDuplicateAnr test

This test requires that the application timeout be larger than the "no
focused window" timeout.

But after a recent refactor, this became false - both values were at
100ms.

The test should've failed close to 50% of the time, but due to bad luck,
failure rate was less, so it wasn't caught earlier. Furthermore, for
some reason this failure was not reproducing on host.

In this CL, refactor the test to always use half of the app timeout.
This will avoid such mistakes in the future.

At the same time, we bump up the timeout to have a 100 ms buffer, which
was empirically found to be sufficient for most host environments.

I also found that we were waiting for the incorrect amount of time.
Since the "no focused window" timer starts immediately, this should have
been accounted for when asserting the ANR timeout receipt.

One negative consequence of this CL is that the test now takes 800 ms to
run, but at least it should no longer be flaky.

Test: TEST=inputflinger_tests; m $TEST && adb sync data && adb shell -t data/nativetest64/$TEST/$TEST --gtest_filter="*NoFocusedWindow_DoesNotSendDuplicateAnr*" --gtest_repeat=200 --gtest_break_on_failure
Bug: 293179353
Change-Id: I00526a0172e14746e77c871182d08c4c3de2e518
diff --git a/services/inputflinger/tests/InputDispatcher_test.cpp b/services/inputflinger/tests/InputDispatcher_test.cpp
index 38970f2..79f4d1e 100644
--- a/services/inputflinger/tests/InputDispatcher_test.cpp
+++ b/services/inputflinger/tests/InputDispatcher_test.cpp
@@ -6848,6 +6848,10 @@
 // We have a focused application, but no focused window
 // Make sure that we don't notify policy twice about the same ANR.
 TEST_F(InputDispatcherSingleWindowAnr, NoFocusedWindow_DoesNotSendDuplicateAnr) {
+    const std::chrono::duration appTimeout = 400ms;
+    mApplication->setDispatchingTimeout(appTimeout);
+    mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, mApplication);
+
     mWindow->setFocusable(false);
     mDispatcher->onWindowInfosChanged({{*mWindow->getInfo()}, {}, 0, 0});
     mWindow->consumeFocusEvent(false);
@@ -6855,13 +6859,18 @@
     // Once a focused event arrives, we get an ANR for this application
     // We specify the injection timeout to be smaller than the application timeout, to ensure that
     // injection times out (instead of failing).
+    const std::chrono::duration eventInjectionTimeout = 100ms;
+    ASSERT_LT(eventInjectionTimeout, appTimeout);
     const InputEventInjectionResult result =
             injectKey(*mDispatcher, AKEY_EVENT_ACTION_DOWN, /*repeatCount=*/0, ADISPLAY_ID_DEFAULT,
-                      InputEventInjectionSync::WAIT_FOR_RESULT, 100ms, /*allowKeyRepeat=*/false);
-    ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result);
-    const std::chrono::duration appTimeout =
-            mApplication->getDispatchingTimeout(DISPATCHING_TIMEOUT);
-    mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(appTimeout, mApplication);
+                      InputEventInjectionSync::WAIT_FOR_RESULT, eventInjectionTimeout,
+                      /*allowKeyRepeat=*/false);
+    ASSERT_EQ(InputEventInjectionResult::TIMED_OUT, result)
+            << "result=" << ftl::enum_string(result);
+    // We already waited for 'eventInjectionTimeout`, because the countdown started when the event
+    // was first injected. So now we have (appTimeout - eventInjectionTimeout) left to wait.
+    std::chrono::duration remainingWaitTime = appTimeout - eventInjectionTimeout;
+    mFakePolicy->assertNotifyNoFocusedWindowAnrWasCalled(remainingWaitTime, mApplication);
 
     std::this_thread::sleep_for(appTimeout);
     // ANR should not be raised again. It is up to policy to do that if it desires.