- add sources.
[platform/framework/web/crosswalk.git] / src / content / public / test / android / javatests / src / org / chromium / content / browser / test / util / UiUtils.java
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.content.browser.test.util;
6
7 import android.app.Activity;
8 import android.app.Instrumentation;
9
10 import junit.framework.Assert;
11
12 import java.util.concurrent.Semaphore;
13 import java.util.concurrent.TimeUnit;
14
15 /**
16  * Collection of UI utilities.
17  */
18 public class UiUtils {
19     private static final int WAIT_FOR_RESPONSE_MS = 10000;  // timeout to wait for runOnUiThread()
20
21     /**
22      * Runs the runnable on the UI thread.
23      *
24      * @param activity The activity on which the runnable must run.
25      * @param runnable The runnable to run.
26      */
27     public static void runOnUiThread(Activity activity, final Runnable runnable) {
28         final Semaphore finishedSemaphore = new Semaphore(0);
29         activity.runOnUiThread(new Runnable() {
30             @Override
31             public void run() {
32                 runnable.run();
33                 finishedSemaphore.release();
34             }});
35         try {
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);
40         }
41     }
42
43     /**
44      * Waits for the UI thread to settle down.
45      * <p>
46      * Waits for an extra period of time after the UI loop is idle.
47      *
48      * @param instrumentation Instrumentation object used by the test.
49      */
50     public static void settleDownUI(Instrumentation instrumentation) throws InterruptedException {
51         instrumentation.waitForIdleSync();
52         Thread.sleep(1000);
53     }
54 }