Vladislav Kaznacheev | d959c9d | 2018-01-23 14:03:36 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package android.util; |
| 18 | |
| 19 | import org.junit.Assert; |
| 20 | |
| 21 | /** |
| 22 | * Utility used for testing that allows to poll for a certain condition to happen within a timeout. |
| 23 | * |
| 24 | * Code copied from com.android.compatibility.common.util.PollingCheck |
| 25 | */ |
| 26 | public abstract class PollingCheck { |
| 27 | |
| 28 | private static final long DEFAULT_TIMEOUT = 3000; |
| 29 | private static final long TIME_SLICE = 50; |
| 30 | private final long mTimeout; |
| 31 | |
| 32 | /** |
| 33 | * The condition that the PollingCheck should use to proceed successfully. |
| 34 | */ |
| 35 | public interface PollingCheckCondition { |
| 36 | |
| 37 | /** |
| 38 | * @return Whether the polling condition has been met. |
| 39 | */ |
| 40 | boolean canProceed(); |
| 41 | } |
| 42 | |
| 43 | public PollingCheck(long timeout) { |
| 44 | mTimeout = timeout; |
| 45 | } |
| 46 | |
| 47 | protected abstract boolean check(); |
| 48 | |
| 49 | /** |
| 50 | * Start running the polling check. |
| 51 | */ |
| 52 | public void run() { |
| 53 | if (check()) { |
| 54 | return; |
| 55 | } |
| 56 | |
| 57 | long timeout = mTimeout; |
| 58 | while (timeout > 0) { |
| 59 | try { |
| 60 | Thread.sleep(TIME_SLICE); |
| 61 | } catch (InterruptedException e) { |
| 62 | Assert.fail("unexpected InterruptedException"); |
| 63 | } |
| 64 | |
| 65 | if (check()) { |
| 66 | return; |
| 67 | } |
| 68 | |
| 69 | timeout -= TIME_SLICE; |
| 70 | } |
| 71 | |
| 72 | Assert.fail("unexpected timeout"); |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Instantiate and start polling for a given condition with a default 3000ms timeout. |
| 77 | * |
| 78 | * @param condition The condition to check for success. |
| 79 | */ |
| 80 | public static void waitFor(final PollingCheckCondition condition) { |
| 81 | new PollingCheck(DEFAULT_TIMEOUT) { |
| 82 | @Override |
| 83 | protected boolean check() { |
| 84 | return condition.canProceed(); |
| 85 | } |
| 86 | }.run(); |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Instantiate and start polling for a given condition. |
| 91 | * |
| 92 | * @param timeout Time out in ms |
| 93 | * @param condition The condition to check for success. |
| 94 | */ |
| 95 | public static void waitFor(long timeout, final PollingCheckCondition condition) { |
| 96 | new PollingCheck(timeout) { |
| 97 | @Override |
| 98 | protected boolean check() { |
| 99 | return condition.canProceed(); |
| 100 | } |
| 101 | }.run(); |
| 102 | } |
| 103 | } |
| 104 | |