| 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | package org.chromium.base.test.util; |
| 6 | |
| 7 | import android.app.Instrumentation; |
| 8 | |
| 9 | import java.util.concurrent.Callable; |
| 10 | import java.util.concurrent.ExecutionException; |
| 11 | import java.util.concurrent.FutureTask; |
| 12 | |
| 13 | /** |
| 14 | * Utility methods built around the android.app.Instrumentation class. |
| 15 | */ |
| 16 | public final class InstrumentationUtils { |
| 17 | |
| 18 | private InstrumentationUtils() { |
| 19 | } |
| 20 | |
| 21 | public static <R> R runOnMainSyncAndGetResult(Instrumentation instrumentation, |
| 22 | Callable<R> callable) throws Throwable { |
| 23 | FutureTask<R> task = new FutureTask<R>(callable); |
| 24 | instrumentation.runOnMainSync(task); |
| 25 | try { |
| 26 | return task.get(); |
| 27 | } catch (ExecutionException e) { |
| 28 | // Unwrap the cause of the exception and re-throw it. |
| 29 | throw e.getCause(); |
| 30 | } |
| 31 | } |
| 32 | } |