Chromium Code Reviews| 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 { |
| 25 private final ActivityStateListener mListener = new ActivityStateListener() { | 27 private final ActivityStateListener mListener = new ActivityStateListener() { |
| 26 @Override | 28 @Override |
| 27 public void onActivityStateChange(Activity activity, int newState) { | 29 public void onActivityStateChange(Activity activity, int newState) { |
| 28 if (newState == ActivityState.RESUMED) { | 30 if (newState == ActivityState.RESUMED) { |
| 29 mMostRecentActivity = activity; | 31 mMostRecentActivity = activity; |
| 32 } else if (activity == mMostRecentActivity && newState == ActivitySt ate.PAUSED) { | |
| 33 mMostRecentActivity = null; | |
| 34 mTimeWhenSetToNullMillis = System.currentTimeMillis(); | |
| 30 } | 35 } |
| 31 } | 36 } |
| 32 }; | 37 }; |
| 33 | 38 |
| 39 private long mTimeWhenSetToNullMillis; | |
| 34 private Activity mMostRecentActivity; | 40 private Activity mMostRecentActivity; |
| 35 | 41 |
| 36 @Override | 42 @Override |
| 37 public Statement apply(final Statement base, Description description) { | 43 public Statement apply(final Statement base, Description description) { |
| 38 return new Statement() { | 44 return new Statement() { |
| 39 @Override | 45 @Override |
| 40 public void evaluate() throws Throwable { | 46 public void evaluate() throws Throwable { |
| 41 ApplicationStatus.registerStateListenerForAllActivities(mListene r); | 47 ApplicationStatus.registerStateListenerForAllActivities(mListene r); |
| 42 base.evaluate(); | 48 base.evaluate(); |
| 43 ApplicationStatus.unregisterActivityStateListener(mListener); | 49 ApplicationStatus.unregisterActivityStateListener(mListener); |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 54 public <T extends Activity> T waitFor(final Class<T> expectedActivityClass) { | 60 public <T extends Activity> T waitFor(final Class<T> expectedActivityClass) { |
| 55 CriteriaHelper.pollUiThread(new Criteria() { | 61 CriteriaHelper.pollUiThread(new Criteria() { |
| 56 @Override | 62 @Override |
| 57 public boolean isSatisfied() { | 63 public boolean isSatisfied() { |
| 58 return mMostRecentActivity != null | 64 return mMostRecentActivity != null |
| 59 && expectedActivityClass.isAssignableFrom(mMostRecentAct ivity.getClass()); | 65 && expectedActivityClass.isAssignableFrom(mMostRecentAct ivity.getClass()); |
| 60 } | 66 } |
| 61 }); | 67 }); |
| 62 return (T) mMostRecentActivity; | 68 return (T) mMostRecentActivity; |
| 63 } | 69 } |
| 70 | |
| 71 public void waitForExternalApp() { | |
| 72 CriteriaHelper.pollUiThread(new Criteria() { | |
| 73 @Override | |
| 74 public boolean isSatisfied() { | |
| 75 return mMostRecentActivity == null | |
| 76 && (System.currentTimeMillis() - mTimeWhenSetToNullMilli s) | |
| 77 > TimeUnit.SECONDS.toMillis(1); | |
|
dominickn
2017/07/07 05:56:04
Nit: possibly put the 1 as a static class level co
piotrs
2017/07/09 23:43:38
Done.
| |
| 78 } | |
| 79 }); | |
| 80 } | |
| 64 } | 81 } |
| OLD | NEW |