| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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.cronet_test_apk; | 5 package org.chromium.net; |
| 6 | 6 |
| 7 import static org.chromium.base.test.util.ScalableTimeout.scaleTimeout; | 7 import static org.chromium.base.test.util.ScalableTimeout.scaleTimeout; |
| 8 | 8 |
| 9 import android.os.SystemClock; | 9 import android.os.SystemClock; |
| 10 | 10 |
| 11 /** | 11 /** |
| 12 * Helper methods for creating and managing criteria. | 12 * Helper methods for creating and managing criteria. |
| 13 * <p> | 13 * <p> |
| 14 * If possible, use callbacks or testing delegates instead of criteria as they | 14 * If possible, use callbacks or testing delegates instead of criteria as they |
| 15 * do not introduce any polling delays. Should only use Criteria if no suitable | 15 * do not introduce any polling delays. Should only use Criteria if no suitable |
| (...skipping 19 matching lines...) Expand all Loading... |
| 35 * @param maxTimeoutMs The maximum number of ms that this check will be | 35 * @param maxTimeoutMs The maximum number of ms that this check will be |
| 36 * performed for before timeout. | 36 * performed for before timeout. |
| 37 * @param checkIntervalMs The number of ms between checks. | 37 * @param checkIntervalMs The number of ms between checks. |
| 38 * @return true iff checking has ended with the criteria being satisfied. | 38 * @return true iff checking has ended with the criteria being satisfied. |
| 39 * @throws InterruptedException | 39 * @throws InterruptedException |
| 40 */ | 40 */ |
| 41 public static boolean pollForCriteria(Criteria criteria, long maxTimeoutMs, | 41 public static boolean pollForCriteria(Criteria criteria, long maxTimeoutMs, |
| 42 long checkIntervalMs) throws InterruptedException { | 42 long checkIntervalMs) throws InterruptedException { |
| 43 boolean isSatisfied = criteria.isSatisfied(); | 43 boolean isSatisfied = criteria.isSatisfied(); |
| 44 long startTime = SystemClock.uptimeMillis(); | 44 long startTime = SystemClock.uptimeMillis(); |
| 45 while (!isSatisfied && | 45 while (!isSatisfied |
| 46 SystemClock.uptimeMillis() - startTime < maxTimeoutMs) { | 46 && SystemClock.uptimeMillis() - startTime < maxTimeoutMs) { |
| 47 Thread.sleep(checkIntervalMs); | 47 Thread.sleep(checkIntervalMs); |
| 48 isSatisfied = criteria.isSatisfied(); | 48 isSatisfied = criteria.isSatisfied(); |
| 49 } | 49 } |
| 50 return isSatisfied; | 50 return isSatisfied; |
| 51 } | 51 } |
| 52 | 52 |
| 53 /** | 53 /** |
| 54 * Checks whether the given Criteria is satisfied polling at a default | 54 * Checks whether the given Criteria is satisfied polling at a default |
| 55 * interval. | 55 * interval. |
| 56 * | 56 * |
| (...skipping 20 matching lines...) Expand all Loading... |
| 77 int count = 0; | 77 int count = 0; |
| 78 boolean success = false; | 78 boolean success = false; |
| 79 while (count < maxAttempts && !success) { | 79 while (count < maxAttempts && !success) { |
| 80 count++; | 80 count++; |
| 81 runnable.run(); | 81 runnable.run(); |
| 82 success = pollForCriteria(criteria, maxTimeoutMs, checkIntervalMs); | 82 success = pollForCriteria(criteria, maxTimeoutMs, checkIntervalMs); |
| 83 } | 83 } |
| 84 return success; | 84 return success; |
| 85 } | 85 } |
| 86 } | 86 } |
| OLD | NEW |