OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 package org.chromium.chrome.browser.webapps; | |
6 | |
7 import android.app.Activity; | |
8 | |
9 import org.junit.rules.TestRule; | |
10 import org.junit.runner.Description; | |
11 import org.junit.runners.model.Statement; | |
12 | |
13 import org.chromium.base.ActivityState; | |
14 import org.chromium.base.ApplicationStatus; | |
15 import org.chromium.base.ApplicationStatus.ActivityStateListener; | |
16 import org.chromium.content.browser.test.util.Criteria; | |
17 import org.chromium.content.browser.test.util.CriteriaHelper; | |
18 | |
19 import javax.annotation.Nullable; | |
20 | |
21 /** | |
22 * Test rule tracking which Chrome activity is currently at the top of the task. | |
23 */ | |
24 public class TopActivityListener implements TestRule { | |
25 private final ActivityStateListener mListener = new ActivityStateListener()
{ | |
26 @Override | |
27 public void onActivityStateChange(Activity activity, int newState) { | |
28 if (newState == ActivityState.RESUMED) { | |
29 mMostRecentActivity = activity; | |
30 } | |
31 } | |
32 }; | |
33 | |
34 private Activity mMostRecentActivity; | |
35 | |
36 @Override | |
37 public Statement apply(final Statement base, Description description) { | |
38 return new Statement() { | |
39 @Override | |
40 public void evaluate() throws Throwable { | |
41 ApplicationStatus.registerStateListenerForAllActivities(mListene
r); | |
42 base.evaluate(); | |
43 ApplicationStatus.unregisterActivityStateListener(mListener); | |
44 } | |
45 }; | |
46 } | |
47 | |
48 @Nullable | |
49 public Activity getMostRecentActivity() { | |
50 return mMostRecentActivity; | |
51 } | |
52 | |
53 @SuppressWarnings("unchecked") | |
54 public <T extends Activity> T waitFor(final Class<T> expectedActivityClass)
{ | |
55 CriteriaHelper.pollUiThread(new Criteria() { | |
56 @Override | |
57 public boolean isSatisfied() { | |
58 return mMostRecentActivity != null | |
59 && expectedActivityClass.isAssignableFrom(mMostRecentAct
ivity.getClass()); | |
60 } | |
61 }); | |
62 return (T) mMostRecentActivity; | |
63 } | |
64 } | |
OLD | NEW |