From fe61c9af9003d79a89a28f693ccee500ea270056 Mon Sep 17 00:00:00 2001 From: Adrian Roos Date: Thu, 31 Mar 2022 15:40:23 +0200 Subject: [PATCH] VirtualDisplayTest: Rethrow runOnUiThread exceptions on test thread Catches and rethrows exceptions on the test thread to avoid taking down the entire instrumentation test when tests fail. Bug: 223843046 Test: atest VirtualDisplayTest Change-Id: I78b6777ff3cabc532cd73390cc5b4c23f64ae127 --- .../hardware/display/VirtualDisplayTest.java | 28 ++++++++++--------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/core/tests/coretests/src/android/hardware/display/VirtualDisplayTest.java b/core/tests/coretests/src/android/hardware/display/VirtualDisplayTest.java index 114317abc07c1..eb8e13d913908 100644 --- a/core/tests/coretests/src/android/hardware/display/VirtualDisplayTest.java +++ b/core/tests/coretests/src/android/hardware/display/VirtualDisplayTest.java @@ -332,21 +332,23 @@ public class VirtualDisplayTest extends AndroidTestCase { } private void runOnUiThread(Runnable runnable) { - Runnable waiter = new Runnable() { - @Override - public void run() { - synchronized (this) { - notifyAll(); - } - } - }; - synchronized (waiter) { - mHandler.post(runnable); - mHandler.post(waiter); + final Throwable[] thrown = new Throwable[1]; + assertTrue("Timed out", mHandler.runWithScissors(() -> { try { - waiter.wait(TIMEOUT); - } catch (InterruptedException ex) { + runnable.run(); + } catch (Throwable t) { + t.printStackTrace(); + thrown[0] = t; } + }, TIMEOUT)); + if (thrown[0] != null) { + if (thrown[0] instanceof RuntimeException) { + throw (RuntimeException) thrown[0]; + } + if (thrown[0] instanceof Error) { + throw (Error) thrown[0]; + } + throw new RuntimeException(thrown[0]); } }