OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 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.chrome.test.util; | 5 package org.chromium.chrome.test.util; |
6 | 6 |
7 import android.app.Activity; | 7 import android.app.Activity; |
8 import android.app.DialogFragment; | 8 import android.app.DialogFragment; |
9 import android.app.Fragment; | 9 import android.app.Fragment; |
10 import android.app.Instrumentation; | 10 import android.app.Instrumentation; |
11 import android.app.Instrumentation.ActivityMonitor; | 11 import android.app.Instrumentation.ActivityMonitor; |
| 12 import android.content.Context; |
| 13 import android.content.Intent; |
| 14 import android.os.Bundle; |
| 15 import android.support.v4.app.ActivityOptionsCompat; |
12 | 16 |
13 import junit.framework.Assert; | 17 import junit.framework.Assert; |
14 | 18 |
| 19 import org.chromium.base.test.util.ScalableTimeout; |
| 20 import org.chromium.chrome.R; |
15 import org.chromium.chrome.browser.preferences.Preferences; | 21 import org.chromium.chrome.browser.preferences.Preferences; |
| 22 import org.chromium.chrome.browser.util.IntentUtils; |
16 import org.chromium.content.browser.test.util.Criteria; | 23 import org.chromium.content.browser.test.util.Criteria; |
17 import org.chromium.content.browser.test.util.CriteriaHelper; | 24 import org.chromium.content.browser.test.util.CriteriaHelper; |
18 | 25 |
19 /** | 26 /** |
20 * Collection of activity utilities. | 27 * Collection of activity utilities. |
21 */ | 28 */ |
22 public class ActivityUtils { | 29 public class ActivityUtils { |
23 private static final long ACTIVITY_START_TIMEOUT_MS = 3000; | 30 private static final long ACTIVITY_START_TIMEOUT_MS = ScalableTimeout.scaleT
imeout(3000); |
24 private static final long CONDITION_POLL_INTERVAL_MS = 100; | 31 private static final long CONDITION_POLL_INTERVAL_MS = 100; |
25 | 32 |
26 /** | 33 /** |
27 * Waits for a particular fragment to be present on a given activity. | 34 * Waits for a particular fragment to be present on a given activity. |
28 */ | 35 */ |
29 private static class FragmentPresentCriteria extends Criteria { | 36 private static class FragmentPresentCriteria extends Criteria { |
30 | 37 |
31 private final Activity mActivity; | 38 private final Activity mActivity; |
32 private final String mFragmentTag; | 39 private final String mFragmentTag; |
33 | 40 |
(...skipping 10 matching lines...) Expand all Loading... |
44 if (fragment instanceof DialogFragment) { | 51 if (fragment instanceof DialogFragment) { |
45 DialogFragment dialogFragment = (DialogFragment) fragment; | 52 DialogFragment dialogFragment = (DialogFragment) fragment; |
46 return dialogFragment.getDialog() != null | 53 return dialogFragment.getDialog() != null |
47 && dialogFragment.getDialog().isShowing(); | 54 && dialogFragment.getDialog().isShowing(); |
48 } | 55 } |
49 return fragment.getView() != null; | 56 return fragment.getView() != null; |
50 } | 57 } |
51 } | 58 } |
52 | 59 |
53 /** | 60 /** |
| 61 * Captures an activity of a particular type by launching an intent explicit
ly targeting the |
| 62 * activity. |
| 63 * |
| 64 * @param <T> The type of activity to wait for. |
| 65 * @param activityType The class type of the activity. |
| 66 * @return The spawned activity. |
| 67 */ |
| 68 public static <T> T waitForActivity( |
| 69 final Instrumentation instrumentation, final Class<T> activityType)
{ |
| 70 Runnable intentTrigger = new Runnable() { |
| 71 @Override |
| 72 public void run() { |
| 73 Context context = instrumentation.getTargetContext().getApplicat
ionContext(); |
| 74 Intent activityIntent = new Intent(); |
| 75 activityIntent.setClass(context, activityType); |
| 76 activityIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
| 77 activityIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_DOCUMENT); |
| 78 |
| 79 Bundle optionsBundle = |
| 80 ActivityOptionsCompat |
| 81 .makeCustomAnimation(context, R.anim.activity_op
en_enter, 0) |
| 82 .toBundle(); |
| 83 IntentUtils.safeStartActivity(context, activityIntent, optionsBu
ndle); |
| 84 } |
| 85 }; |
| 86 return waitForActivity(instrumentation, activityType, intentTrigger); |
| 87 } |
| 88 |
| 89 /** |
54 * Captures an activity of a particular type that is triggered from some act
ion. | 90 * Captures an activity of a particular type that is triggered from some act
ion. |
55 * | 91 * |
56 * @param <T> The type of activity to wait for. | 92 * @param <T> The type of activity to wait for. |
57 * @param activityType The class type of the activity. | 93 * @param activityType The class type of the activity. |
58 * @param activityTrigger The action that will trigger the new activity (run
in this thread). | 94 * @param activityTrigger The action that will trigger the new activity (run
in this thread). |
59 * @return The spawned activity. | 95 * @return The spawned activity. |
60 */ | 96 */ |
61 public static <T> T waitForActivity(Instrumentation instrumentation, Class<T
> activityType, | 97 public static <T> T waitForActivity(Instrumentation instrumentation, Class<T
> activityType, |
62 Runnable activityTrigger) { | 98 Runnable activityTrigger) { |
63 return waitForActivityWithTimeout(instrumentation, activityType, activit
yTrigger, | 99 return waitForActivityWithTimeout(instrumentation, activityType, activit
yTrigger, |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
118 CriteriaHelper.pollInstrumentationThread( | 154 CriteriaHelper.pollInstrumentationThread( |
119 new Criteria("Could not find fragment " + fragmentClass) { | 155 new Criteria("Could not find fragment " + fragmentClass) { |
120 @Override | 156 @Override |
121 public boolean isSatisfied() { | 157 public boolean isSatisfied() { |
122 return fragmentClass.isInstance(activity.getFragmentForT
est()); | 158 return fragmentClass.isInstance(activity.getFragmentForT
est()); |
123 } | 159 } |
124 }, ACTIVITY_START_TIMEOUT_MS, CONDITION_POLL_INTERVAL_MS); | 160 }, ACTIVITY_START_TIMEOUT_MS, CONDITION_POLL_INTERVAL_MS); |
125 return (T) activity.getFragmentForTest(); | 161 return (T) activity.getFragmentForTest(); |
126 } | 162 } |
127 } | 163 } |
OLD | NEW |