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.
5 package org.chromium.content.browser.test.util;
7 import android.app.Activity;
8 import android.app.Instrumentation;
10 import junit.framework.Assert;
12 import java.util.concurrent.Semaphore;
13 import java.util.concurrent.TimeUnit;
16 * Collection of UI utilities.
18 public class UiUtils {
19 private static final int WAIT_FOR_RESPONSE_MS = 10000; // timeout to wait for runOnUiThread()
22 * Runs the runnable on the UI thread.
24 * @param activity The activity on which the runnable must run.
25 * @param runnable The runnable to run.
27 public static void runOnUiThread(Activity activity, final Runnable runnable) {
28 final Semaphore finishedSemaphore = new Semaphore(0);
29 activity.runOnUiThread(new Runnable() {
33 finishedSemaphore.release();
36 Assert.assertTrue(finishedSemaphore.tryAcquire(1, WAIT_FOR_RESPONSE_MS,
37 TimeUnit.MILLISECONDS));
38 } catch (InterruptedException ignored) {
39 Assert.assertTrue("Interrupted while waiting for main thread Runnable", false);
44 * Waits for the UI thread to settle down.
46 * Waits for an extra period of time after the UI loop is idle.
48 * @param instrumentation Instrumentation object used by the test.
50 public static void settleDownUI(Instrumentation instrumentation) throws InterruptedException {
51 instrumentation.waitForIdleSync();