1/*
2 * Copyright 2018 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
17package androidx.core.os;
18
19import static org.junit.Assert.assertEquals;
20import static org.junit.Assert.assertTrue;
21
22import static java.util.Arrays.asList;
23import static java.util.concurrent.TimeUnit.SECONDS;
24
25import android.os.Handler;
26import android.os.HandlerThread;
27import android.support.test.filters.SmallTest;
28
29import org.junit.After;
30import org.junit.Before;
31import org.junit.Test;
32
33import java.util.ArrayList;
34import java.util.List;
35import java.util.concurrent.CountDownLatch;
36
37@SmallTest
38public final class HandlerCompatTest {
39    private final HandlerThread mThread = new HandlerThread("handler-compat-test");
40    private Handler mHandler;
41
42    @Before public void before() {
43        mThread.start();
44        mHandler = new Handler(mThread.getLooper());
45    }
46
47    @After public void after() {
48        assertTrue(mThread.quit());
49    }
50
51    @Test public void postDelayedWithToken() throws InterruptedException {
52        // Schedule a latch at 300ms to block the test thread.
53        final CountDownLatch latch = new CountDownLatch(1);
54        mHandler.postDelayed(new Runnable() {
55            @Override
56            public void run() {
57                latch.countDown();
58            }
59        }, 300);
60
61        final List<String> events = new ArrayList<>();
62        final Object token = new Object();
63
64        // Schedule an event at 200ms with the token.
65        HandlerCompat.postDelayed(mHandler, new Runnable() {
66            @Override
67            public void run() {
68                events.add("200");
69            }
70        }, token, 200);
71
72        // Schedule an event at 100ms which removes future messages with the token.
73        HandlerCompat.postDelayed(mHandler, new Runnable() {
74            @Override
75            public void run() {
76                events.add("100");
77                mHandler.removeCallbacksAndMessages(token);
78            }
79        }, token, 100);
80
81        // Schedule an event immediately to ensure the delays are being honored.
82        mHandler.post(new Runnable() {
83            @Override
84            public void run() {
85                events.add("0");
86            }
87        });
88
89        assertTrue(latch.await(1, SECONDS));
90        assertEquals(asList("0", "100"), events);
91    }
92}
93