InstrumentationUtils.java revision 5821806d5e7f356e8fa4b058a389a808ea183019
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
5package org.chromium.base.test.util;
6
7import android.app.Instrumentation;
8
9import java.util.concurrent.Callable;
10import java.util.concurrent.ExecutionException;
11import java.util.concurrent.FutureTask;
12
13/**
14 * Utility methods built around the android.app.Instrumentation class.
15 */
16public 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}
33