1 /*
2  * Copyright (C) 2019 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 com.android.compatibility.common.util;
18 
19 import static org.junit.Assert.assertEquals;
20 import static org.junit.Assert.assertFalse;
21 import static org.junit.Assert.assertTrue;
22 
23 import org.junit.Before;
24 import org.junit.Test;
25 import org.junit.runner.RunWith;
26 import org.junit.runners.JUnit4;
27 
28 /** Tests for {@link PollingCheck}. */
29 @RunWith(JUnit4.class)
30 public class PollingCheckTest {
31     private PollingCheck.PollingCheckClock testClock;
32 
33     @Before
setUp()34     public void setUp() {
35         testClock = new TestClock();
36     }
37 
38     @Test
testCheckSuccess()39     public void testCheckSuccess() throws Exception {
40         assertTrue("check failed", PollingCheck.check(testClock, 10, 100, () -> true));
41     }
42 
43     @Test
testCheckEventualSuccess()44     public void testCheckEventualSuccess() throws Exception {
45         final int[] i = {0};
46         assertTrue("check failed", PollingCheck.check(testClock, 10, 1000, () -> ++i[0] == 3));
47         assertEquals("Condition expected to be checked three times", 3, i[0]);
48         assertEquals("Time advanced unexpectedly", 20, testClock.currentTimeMillis());
49     }
50 
51     @Test
testCheckTimeout()52     public void testCheckTimeout() throws Exception {
53         final int[] i = {0};
54         assertFalse(
55                 "Expected failure due to timeout",
56                 PollingCheck.check(testClock, 10, 50, () -> i[0]++ == 9));
57     }
58 
59     @Test
testCheckFailure()60     public void testCheckFailure() throws Exception {
61         assertFalse(
62                 "Expected failure due to condition not being true",
63                 PollingCheck.check(testClock, 10, 100, () -> false));
64     }
65 
66     @Test
testCheckChecksConditionAtLeastOnce()67     public void testCheckChecksConditionAtLeastOnce() throws Exception {
68         final boolean[] conditionChecked = {false};
69         PollingCheck.check(
70                 testClock,
71                 10,
72                 0,
73                 () -> {
74                     conditionChecked[0] = true;
75                     return true;
76                 });
77 
78         assertTrue("Expected condition to be checked", conditionChecked[0]);
79     }
80 
81     @Test
testLongConditionCheck()82     public void testLongConditionCheck() throws Exception {
83         PollingCheck.check(
84                 testClock,
85                 10,
86                 500,
87                 () -> {
88                     testClock.sleep(5); // condition takes some time to evaluate
89                     return testClock.currentTimeMillis() >= 50;
90                 });
91 
92         assertEquals(50, testClock.currentTimeMillis());
93     }
94 
95     @Test
testCheckMessage()96     public void testCheckMessage() throws Exception {
97         try {
98             PollingCheck.check(testClock, "Expected message", 10, 0, () -> false);
99         } catch (AssertionError e) {
100             assertEquals("Expected message", e.getMessage());
101         }
102     }
103 
104     @Test
testWaitForTimeout()105     public void testWaitForTimeout() throws Exception {
106         try {
107             PollingCheck.waitFor(testClock, 10, 500, () -> false);
108         } catch (AssertionError e) {
109             assertEquals("Unexpected timeout waiting for condition", e.getMessage());
110         }
111 
112         assertEquals(500, testClock.currentTimeMillis());
113     }
114 
115     @Test
testWaitForSuccess()116     public void testWaitForSuccess() throws Exception {
117         PollingCheck.waitFor(testClock, 10, 500, () -> testClock.currentTimeMillis() >= 200);
118         assertEquals(200, testClock.currentTimeMillis());
119     }
120 
121     private final class TestClock implements PollingCheck.PollingCheckClock {
122         private long currentTime = 0;
123 
124         @Override
currentTimeMillis()125         public long currentTimeMillis() {
126             return currentTime;
127         }
128 
129         @Override
sleep(long millis)130         public void sleep(long millis) {
131             currentTime += millis;
132         }
133     }
134 }
135