Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(748)

Side by Side Diff: chrome/test/android/javatests_staging/src/org/chromium/chrome/test/util/ActivityUtils.java

Issue 1141283003: Upstream oodles of Chrome for Android code into Chromium. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: final patch? Created 5 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2015 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.chrome.test.util;
6
7 import android.app.Activity;
8 import android.app.DialogFragment;
9 import android.app.Fragment;
10 import android.app.Instrumentation;
11 import android.app.Instrumentation.ActivityMonitor;
12
13 import junit.framework.Assert;
14
15 import org.chromium.chrome.browser.preferences.Preferences;
16 import org.chromium.content.browser.test.util.Criteria;
17 import org.chromium.content.browser.test.util.CriteriaHelper;
18
19 /**
20 * Collection of activity utilities.
21 */
22 public class ActivityUtils {
23 private static final long ACTIVITY_START_TIMEOUT_MS = 3000;
24 private static final long CONDITION_POLL_INTERVAL_MS = 100;
25
26 /**
27 * Waits for a particular fragment to be present on a given activity.
28 */
29 private static class FragmentPresentCriteria implements Criteria {
30
31 private final Activity mActivity;
32 private final String mFragmentTag;
33
34 public FragmentPresentCriteria(Activity activity, String fragmentTag) {
35 mActivity = activity;
36 mFragmentTag = fragmentTag;
37 }
38
39 @Override
40 public boolean isSatisfied() {
41 Fragment fragment = mActivity.getFragmentManager().findFragmentByTag (mFragmentTag);
42 if (fragment == null) return false;
43 if (fragment instanceof DialogFragment) {
44 DialogFragment dialogFragment = (DialogFragment) fragment;
45 return dialogFragment.getDialog() != null
46 && dialogFragment.getDialog().isShowing();
47 }
48 return fragment.getView() != null;
49 }
50 }
51
52 /**
53 * Captures an activity of a particular type that is triggered from some act ion.
54 *
55 * @param <T> The type of activity to wait for.
56 * @param activityType The class type of the activity.
57 * @param activityTrigger The action that will trigger the new activity (run in this thread).
58 * @return The spawned activity.
59 */
60 public static <T> T waitForActivity(Instrumentation instrumentation, Class<T > activityType,
61 Runnable activityTrigger) {
62 return waitForActivityWithTimeout(instrumentation, activityType, activit yTrigger,
63 ACTIVITY_START_TIMEOUT_MS);
64 }
65
66 /**
67 * Captures an activity of a particular type that is triggered from some act ion.
68 *
69 * @param activityType The class type of the activity.
70 * @param activityTrigger The action that will trigger the new activity (run in this thread).
71 * @param timeOut The maximum time to wait for activity creation
72 * @return The spawned activity.
73 */
74 public static <T> T waitForActivityWithTimeout(Instrumentation instrumentati on,
75 Class<T> activityType, Runnable activityTrigger, long timeOut) {
76 ActivityMonitor monitor =
77 instrumentation.addMonitor(activityType.getCanonicalName(), null , false);
78
79 activityTrigger.run();
80 instrumentation.waitForIdleSync();
81 Activity activity = monitor.getLastActivity();
82 if (activity == null) {
83 activity = monitor.waitForActivityWithTimeout(timeOut);
84 }
85 Assert.assertNotNull(activityType.getName() + " did not start in: " + ti meOut, activity);
86
87 return activityType.cast(activity);
88 }
89
90 /**
91 * Waits for a fragment to be registered by the specified activity.
92 *
93 * @param activity The activity that owns the fragment.
94 * @param fragmentTag The tag of the fragment to be loaded.
95 */
96 @SuppressWarnings("unchecked")
97 public static <T> T waitForFragment(Activity activity, String fragmentTag)
98 throws InterruptedException {
99 Assert.assertTrue(String.format("Could not locate the fragment with tag '%s'", fragmentTag),
100 CriteriaHelper.pollForCriteria(new FragmentPresentCriteria(activ ity, fragmentTag),
101 ACTIVITY_START_TIMEOUT_MS, CONDITION_POLL_INTERVAL_MS));
102 return (T) activity.getFragmentManager().findFragmentByTag(fragmentTag);
103 }
104
105 /**
106 * Waits until the specified fragment has been attached to the specified act ivity. Note that
107 * we don't guarantee that the fragment is visible. Some UI operations can h appen too
108 * quickly and we can miss the time that a fragment is visible. This method allows you to get a
109 * reference to any fragment that was attached to the activity at any point.
110 *
111 * @param <T> A subclass of android.app.Fragment
112 * @param activity An instance or subclass of Preferences
113 * @param fragmentClass The class object for T
114 * @return A reference to the requested fragment or null.
115 */
116 @SuppressWarnings("unchecked")
117 public static <T extends Fragment> T waitForFragmentToAttach(
118 final Preferences activity, final Class<T> fragmentClass)
119 throws InterruptedException {
120 boolean isFragmentAttached = CriteriaHelper.pollForCriteria(
121 new Criteria() {
122 @Override
123 public boolean isSatisfied() {
124 return fragmentClass.isInstance(activity.getFragmentForT est());
125 }
126 },
127 ACTIVITY_START_TIMEOUT_MS, CONDITION_POLL_INTERVAL_MS);
128 Assert.assertTrue("Could not find fragment " + fragmentClass, isFragment Attached);
129 return (T) activity.getFragmentForTest();
130 }
131 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698