1464c846534b827ce5af15f3bc17d074491a86f64Gustav Sennton/*
2464c846534b827ce5af15f3bc17d074491a86f64Gustav Sennton * Copyright (C) 2018 The Android Open Source Project
3464c846534b827ce5af15f3bc17d074491a86f64Gustav Sennton *
4464c846534b827ce5af15f3bc17d074491a86f64Gustav Sennton * Licensed under the Apache License, Version 2.0 (the "License");
5464c846534b827ce5af15f3bc17d074491a86f64Gustav Sennton * you may not use this file except in compliance with the License.
6464c846534b827ce5af15f3bc17d074491a86f64Gustav Sennton * You may obtain a copy of the License at
7464c846534b827ce5af15f3bc17d074491a86f64Gustav Sennton *
8464c846534b827ce5af15f3bc17d074491a86f64Gustav Sennton *      http://www.apache.org/licenses/LICENSE-2.0
9464c846534b827ce5af15f3bc17d074491a86f64Gustav Sennton *
10464c846534b827ce5af15f3bc17d074491a86f64Gustav Sennton * Unless required by applicable law or agreed to in writing, software
11464c846534b827ce5af15f3bc17d074491a86f64Gustav Sennton * distributed under the License is distributed on an "AS IS" BASIS,
12464c846534b827ce5af15f3bc17d074491a86f64Gustav Sennton * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13464c846534b827ce5af15f3bc17d074491a86f64Gustav Sennton * See the License for the specific language governing permissions and
14464c846534b827ce5af15f3bc17d074491a86f64Gustav Sennton * limitations under the License.
15464c846534b827ce5af15f3bc17d074491a86f64Gustav Sennton */
16464c846534b827ce5af15f3bc17d074491a86f64Gustav Sennton
17464c846534b827ce5af15f3bc17d074491a86f64Gustav Senntonpackage androidx.webkit;
18464c846534b827ce5af15f3bc17d074491a86f64Gustav Sennton
19464c846534b827ce5af15f3bc17d074491a86f64Gustav Senntonimport junit.framework.Assert;
20464c846534b827ce5af15f3bc17d074491a86f64Gustav Sennton
21464c846534b827ce5af15f3bc17d074491a86f64Gustav Senntonimport java.util.concurrent.Callable;
22464c846534b827ce5af15f3bc17d074491a86f64Gustav Sennton
23464c846534b827ce5af15f3bc17d074491a86f64Gustav Sennton/**
24464c846534b827ce5af15f3bc17d074491a86f64Gustav Sennton * A class for checking a specific statement {@link #check()} through polling, either until the
25464c846534b827ce5af15f3bc17d074491a86f64Gustav Sennton * statement is true, or until timing out.
26464c846534b827ce5af15f3bc17d074491a86f64Gustav Sennton *
27464c846534b827ce5af15f3bc17d074491a86f64Gustav Sennton * Copy-pasted from CTS: com.android.compatibility.common.util.PollingCheck.
28464c846534b827ce5af15f3bc17d074491a86f64Gustav Sennton */
29464c846534b827ce5af15f3bc17d074491a86f64Gustav Senntonpublic abstract class PollingCheck {
30464c846534b827ce5af15f3bc17d074491a86f64Gustav Sennton    private static final long TIME_SLICE = 50;
31464c846534b827ce5af15f3bc17d074491a86f64Gustav Sennton    private long mTimeout = 3000;
32464c846534b827ce5af15f3bc17d074491a86f64Gustav Sennton
33464c846534b827ce5af15f3bc17d074491a86f64Gustav Sennton    public PollingCheck(long timeout) {
34464c846534b827ce5af15f3bc17d074491a86f64Gustav Sennton        mTimeout = timeout;
35464c846534b827ce5af15f3bc17d074491a86f64Gustav Sennton    }
36464c846534b827ce5af15f3bc17d074491a86f64Gustav Sennton
37464c846534b827ce5af15f3bc17d074491a86f64Gustav Sennton    protected abstract boolean check();
38464c846534b827ce5af15f3bc17d074491a86f64Gustav Sennton
39464c846534b827ce5af15f3bc17d074491a86f64Gustav Sennton    public void run() {
40464c846534b827ce5af15f3bc17d074491a86f64Gustav Sennton        if (check()) {
41464c846534b827ce5af15f3bc17d074491a86f64Gustav Sennton            return;
42464c846534b827ce5af15f3bc17d074491a86f64Gustav Sennton        }
43464c846534b827ce5af15f3bc17d074491a86f64Gustav Sennton
44464c846534b827ce5af15f3bc17d074491a86f64Gustav Sennton        long timeout = mTimeout;
45464c846534b827ce5af15f3bc17d074491a86f64Gustav Sennton        while (timeout > 0) {
46464c846534b827ce5af15f3bc17d074491a86f64Gustav Sennton            try {
47464c846534b827ce5af15f3bc17d074491a86f64Gustav Sennton                Thread.sleep(TIME_SLICE);
48464c846534b827ce5af15f3bc17d074491a86f64Gustav Sennton            } catch (InterruptedException e) {
49464c846534b827ce5af15f3bc17d074491a86f64Gustav Sennton                Assert.fail("unexpected InterruptedException");
50464c846534b827ce5af15f3bc17d074491a86f64Gustav Sennton            }
51464c846534b827ce5af15f3bc17d074491a86f64Gustav Sennton
52464c846534b827ce5af15f3bc17d074491a86f64Gustav Sennton            if (check()) {
53464c846534b827ce5af15f3bc17d074491a86f64Gustav Sennton                return;
54464c846534b827ce5af15f3bc17d074491a86f64Gustav Sennton            }
55464c846534b827ce5af15f3bc17d074491a86f64Gustav Sennton
56464c846534b827ce5af15f3bc17d074491a86f64Gustav Sennton            timeout -= TIME_SLICE;
57464c846534b827ce5af15f3bc17d074491a86f64Gustav Sennton        }
58464c846534b827ce5af15f3bc17d074491a86f64Gustav Sennton
59464c846534b827ce5af15f3bc17d074491a86f64Gustav Sennton        Assert.fail("unexpected timeout");
60464c846534b827ce5af15f3bc17d074491a86f64Gustav Sennton    }
61464c846534b827ce5af15f3bc17d074491a86f64Gustav Sennton
62464c846534b827ce5af15f3bc17d074491a86f64Gustav Sennton    public static void check(CharSequence message, long timeout, Callable<Boolean> condition)
63464c846534b827ce5af15f3bc17d074491a86f64Gustav Sennton            throws Exception {
64464c846534b827ce5af15f3bc17d074491a86f64Gustav Sennton        while (timeout > 0) {
65464c846534b827ce5af15f3bc17d074491a86f64Gustav Sennton            if (condition.call()) {
66464c846534b827ce5af15f3bc17d074491a86f64Gustav Sennton                return;
67464c846534b827ce5af15f3bc17d074491a86f64Gustav Sennton            }
68464c846534b827ce5af15f3bc17d074491a86f64Gustav Sennton
69464c846534b827ce5af15f3bc17d074491a86f64Gustav Sennton            Thread.sleep(TIME_SLICE);
70464c846534b827ce5af15f3bc17d074491a86f64Gustav Sennton            timeout -= TIME_SLICE;
71464c846534b827ce5af15f3bc17d074491a86f64Gustav Sennton        }
72464c846534b827ce5af15f3bc17d074491a86f64Gustav Sennton
73464c846534b827ce5af15f3bc17d074491a86f64Gustav Sennton        Assert.fail(message.toString());
74464c846534b827ce5af15f3bc17d074491a86f64Gustav Sennton    }
75464c846534b827ce5af15f3bc17d074491a86f64Gustav Sennton}
76