| 1 |
/* |
| 2 |
* Copyright (C) 2022 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.server.art; |
| 18 |
|
| 19 |
import static com.google.common.truth.Truth.assertThat; |
| 20 |
|
| 21 |
import static org.mockito.Mockito.lenient; |
| 22 |
|
| 23 |
import androidx.test.filters.SmallTest; |
| 24 |
|
| 25 |
import com.android.server.art.testing.MockClock; |
| 26 |
|
| 27 |
import org.junit.Before; |
| 28 |
import org.junit.Test; |
| 29 |
import org.junit.runner.RunWith; |
| 30 |
import org.mockito.Mock; |
| 31 |
import org.mockito.junit.MockitoJUnitRunner; |
| 32 |
|
| 33 |
import java.util.ArrayList; |
| 34 |
import java.util.List; |
| 35 |
|
| 36 |
@SmallTest |
| 37 |
@RunWith(MockitoJUnitRunner.StrictStubs.class) |
| 38 |
public class DebouncerTest { |
| 39 |
private MockClock mMockClock; |
| 40 |
private Debouncer mDebouncer; |
| 41 |
|
| 42 |
@Before |
| 43 |
public void setUp() throws Exception { |
| 44 |
mMockClock = new MockClock(); |
| 45 |
mDebouncer = |
| 46 |
new Debouncer(100 /* intervalMs */, () -> mMockClock.createScheduledExecutor()); |
| 47 |
} |
| 48 |
|
| 49 |
@Test |
| 50 |
public void test() throws Exception { |
| 51 |
List<Integer> list = new ArrayList<>(); |
| 52 |
|
| 53 |
mDebouncer.maybeRunAsync(() -> list.add(1)); |
| 54 |
mDebouncer.maybeRunAsync(() -> list.add(2)); |
| 55 |
mMockClock.advanceTime(100); |
| 56 |
mDebouncer.maybeRunAsync(() -> list.add(3)); |
| 57 |
mMockClock.advanceTime(99); |
| 58 |
mDebouncer.maybeRunAsync(() -> list.add(4)); |
| 59 |
mMockClock.advanceTime(99); |
| 60 |
mDebouncer.maybeRunAsync(() -> list.add(5)); |
| 61 |
mMockClock.advanceTime(1000); |
| 62 |
|
| 63 |
assertThat(list).containsExactly(2, 5).inOrder(); |
| 64 |
} |
| 65 |
} |