| 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 public void waitFor(final Class<? extends Activity> expectedActivityClass) { | |
| 54 CriteriaHelper.pollUiThread(new Criteria() { | |
| 55 @Override | |
| 56 public boolean isSatisfied() { | |
| 57 return mMostRecentActivity != null | |
| 58 && expectedActivityClass.isAssignableFrom(mMostRecentAct
ivity.getClass()); | |
| 59 } | |
| 60 }); | |
| 61 } | |
| 62 } | |
| OLD | NEW |