OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package org.chromium.content.browser.test.util; | 5 package org.chromium.content.browser.test.util; |
6 | 6 |
7 import android.app.Activity; | 7 import android.app.Activity; |
8 import android.app.Instrumentation; | 8 import android.app.Instrumentation; |
9 import android.view.View; | |
10 | 9 |
11 import java.lang.reflect.Field; | 10 import junit.framework.Assert; |
| 11 |
12 import java.util.concurrent.Semaphore; | 12 import java.util.concurrent.Semaphore; |
13 import java.util.concurrent.TimeUnit; | 13 import java.util.concurrent.TimeUnit; |
14 | 14 |
15 import junit.framework.Assert; | |
16 | |
17 /** | 15 /** |
18 * Collection of UI utilities. | 16 * Collection of UI utilities. |
19 */ | 17 */ |
20 public class UiUtils { | 18 public class UiUtils { |
21 private static final int WAIT_FOR_RESPONSE_MS = 10000; // timeout to wait f
or runOnUiThread() | 19 private static final int WAIT_FOR_RESPONSE_MS = 10000; // timeout to wait f
or runOnUiThread() |
22 | 20 |
23 /** | 21 /** |
24 * Runs the runnable on the UI thread. | 22 * Runs the runnable on the UI thread. |
25 * | 23 * |
26 * @param activity The activity on which the runnable must run. | 24 * @param activity The activity on which the runnable must run. |
(...skipping 19 matching lines...) Expand all Loading... |
46 * Waits for the UI thread to settle down. | 44 * Waits for the UI thread to settle down. |
47 * <p> | 45 * <p> |
48 * Waits for an extra period of time after the UI loop is idle. | 46 * Waits for an extra period of time after the UI loop is idle. |
49 * | 47 * |
50 * @param instrumentation Instrumentation object used by the test. | 48 * @param instrumentation Instrumentation object used by the test. |
51 */ | 49 */ |
52 public static void settleDownUI(Instrumentation instrumentation) throws Inte
rruptedException { | 50 public static void settleDownUI(Instrumentation instrumentation) throws Inte
rruptedException { |
53 instrumentation.waitForIdleSync(); | 51 instrumentation.waitForIdleSync(); |
54 Thread.sleep(1000); | 52 Thread.sleep(1000); |
55 } | 53 } |
56 | |
57 /** | |
58 * Find the parent View for the Id across all activities, as dialogs | |
59 * will be displayed in a separate activity. Note that in the case | |
60 * of several views sharing the same ID across activities, this will | |
61 * return the first view found. | |
62 * | |
63 * @param viewId The Id of the view. | |
64 * @return the view which contains the given id or null if no view with the
given id exists. | |
65 */ | |
66 public static View findParentViewForIdAcrossActivities(int viewId) { | |
67 View[] availableViews = null; | |
68 try { | |
69 Class<?> windowManager = Class.forName("android.view.WindowManagerIm
pl"); | |
70 | |
71 Field viewsField = windowManager.getDeclaredField("mViews"); | |
72 Field instanceField = windowManager.getDeclaredField("sWindowManager
"); | |
73 viewsField.setAccessible(true); | |
74 instanceField.setAccessible(true); | |
75 Object instance = instanceField.get(null); | |
76 availableViews = (View[]) viewsField.get(instance); | |
77 } catch(Exception e) { | |
78 Assert.fail("Could not fetch the available views."); | |
79 } | |
80 | |
81 if (availableViews == null || availableViews.length == 0) { | |
82 return null; | |
83 } | |
84 | |
85 for (View view : availableViews) { | |
86 if (view.findViewById(viewId) != null) { | |
87 return view; | |
88 } | |
89 } | |
90 | |
91 return null; | |
92 } | |
93 } | 54 } |
OLD | NEW |