OLD | NEW |
---|---|
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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.browser.webapps; | 5 package org.chromium.chrome.browser.webapps; |
6 | 6 |
7 import android.app.Activity; | 7 import android.app.Activity; |
8 | 8 |
9 import org.junit.rules.TestRule; | 9 import org.junit.rules.TestRule; |
10 import org.junit.runner.Description; | 10 import org.junit.runner.Description; |
11 import org.junit.runners.model.Statement; | 11 import org.junit.runners.model.Statement; |
12 | 12 |
13 import org.chromium.base.ActivityState; | 13 import org.chromium.base.ActivityState; |
14 import org.chromium.base.ApplicationStatus; | 14 import org.chromium.base.ApplicationStatus; |
15 import org.chromium.base.ApplicationStatus.ActivityStateListener; | 15 import org.chromium.base.ApplicationStatus.ActivityStateListener; |
16 import org.chromium.content.browser.test.util.Criteria; | 16 import org.chromium.content.browser.test.util.Criteria; |
17 import org.chromium.content.browser.test.util.CriteriaHelper; | 17 import org.chromium.content.browser.test.util.CriteriaHelper; |
18 | 18 |
19 import java.util.concurrent.TimeUnit; | |
20 | |
19 import javax.annotation.Nullable; | 21 import javax.annotation.Nullable; |
20 | 22 |
21 /** | 23 /** |
22 * Test rule tracking which Chrome activity is currently at the top of the task. | 24 * Test rule tracking which Chrome activity is currently at the top of the task. |
23 */ | 25 */ |
24 public class TopActivityListener implements TestRule { | 26 public class TopActivityListener implements TestRule { |
27 private static final long ONE_SECOND_IN_MILLIS = TimeUnit.SECONDS.toMillis(1 ); | |
28 | |
25 private final ActivityStateListener mListener = new ActivityStateListener() { | 29 private final ActivityStateListener mListener = new ActivityStateListener() { |
26 @Override | 30 @Override |
27 public void onActivityStateChange(Activity activity, int newState) { | 31 public void onActivityStateChange(Activity activity, int newState) { |
28 if (newState == ActivityState.RESUMED) { | 32 if (newState == ActivityState.RESUMED) { |
29 mMostRecentActivity = activity; | 33 mMostRecentActivity = activity; |
34 } else if (activity == mMostRecentActivity && newState == ActivitySt ate.PAUSED) { | |
35 mMostRecentActivity = null; | |
36 mTimeWhenSetToNullMillis = System.currentTimeMillis(); | |
30 } | 37 } |
31 } | 38 } |
32 }; | 39 }; |
33 | 40 |
41 private long mTimeWhenSetToNullMillis; | |
34 private Activity mMostRecentActivity; | 42 private Activity mMostRecentActivity; |
35 | 43 |
36 @Override | 44 @Override |
37 public Statement apply(final Statement base, Description description) { | 45 public Statement apply(final Statement base, Description description) { |
38 return new Statement() { | 46 return new Statement() { |
39 @Override | 47 @Override |
40 public void evaluate() throws Throwable { | 48 public void evaluate() throws Throwable { |
41 ApplicationStatus.registerStateListenerForAllActivities(mListene r); | 49 ApplicationStatus.registerStateListenerForAllActivities(mListene r); |
42 base.evaluate(); | 50 base.evaluate(); |
43 ApplicationStatus.unregisterActivityStateListener(mListener); | 51 ApplicationStatus.unregisterActivityStateListener(mListener); |
44 } | 52 } |
45 }; | 53 }; |
46 } | 54 } |
47 | 55 |
48 @Nullable | 56 @Nullable |
49 public Activity getMostRecentActivity() { | 57 public Activity getMostRecentActivity() { |
50 return mMostRecentActivity; | 58 return mMostRecentActivity; |
51 } | 59 } |
52 | 60 |
53 @SuppressWarnings("unchecked") | 61 @SuppressWarnings("unchecked") |
54 public <T extends Activity> T waitFor(final Class<T> expectedActivityClass) { | 62 public <T extends Activity> T waitFor(final Class<T> expectedActivityClass) { |
55 CriteriaHelper.pollUiThread(new Criteria() { | 63 CriteriaHelper.pollUiThread(new Criteria() { |
56 @Override | 64 @Override |
57 public boolean isSatisfied() { | 65 public boolean isSatisfied() { |
58 return mMostRecentActivity != null | 66 return mMostRecentActivity != null |
Ted C
2017/07/10 18:10:15
why not just use ApplicationStatus.getLastTrackedF
piotrs
2017/07/10 23:40:54
An Oversight. I deleted this whole class.
| |
59 && expectedActivityClass.isAssignableFrom(mMostRecentAct ivity.getClass()); | 67 && expectedActivityClass.isAssignableFrom(mMostRecentAct ivity.getClass()); |
60 } | 68 } |
61 }); | 69 }); |
62 return (T) mMostRecentActivity; | 70 return (T) mMostRecentActivity; |
63 } | 71 } |
72 | |
73 public void waitForExternalApp() { | |
74 CriteriaHelper.pollUiThread(new Criteria() { | |
75 @Override | |
76 public boolean isSatisfied() { | |
77 return mMostRecentActivity == null | |
Ted C
2017/07/10 18:10:15
Just use ApplicationStatus.getStateForApplication
piotrs
2017/07/10 23:40:54
An Oversight. I deleted this whole class.
| |
78 && (System.currentTimeMillis() - mTimeWhenSetToNullMilli s) | |
Ted C
2017/07/10 18:10:15
this delay is very much specific to your test. In
piotrs
2017/07/10 23:40:54
I don't quite follow. My intention was to ensure t
Ted C
2017/07/11 00:05:38
In general, any explicit time is likely to give yo
| |
79 > ONE_SECOND_IN_MILLIS; | |
80 } | |
81 }); | |
82 } | |
64 } | 83 } |
OLD | NEW |