blob: c18ef1f9027c310d4b978385e440313fd55c0f01 [file] [log] [blame]
Garfield, Tan48ef36f2016-06-09 12:04:22 -07001/*
2 * Copyright (C) 2016 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 com.android.documentsui.testing;
18
19import android.os.Handler;
20import android.os.Looper;
21import android.os.Message;
22
23import java.util.TimerTask;
24
25/**
26 * A test double of {@link Handler}, backed by {@link TestTimer}.
27 */
28public class TestHandler extends Handler {
29 private TestTimer mTimer = new TestTimer();
30
31 public TestHandler() {
32 // Use main looper to trick underlying handler, we're not using it at all.
33 super(Looper.getMainLooper());
34 }
35
36 public boolean hasScheduledMessage() {
37 return mTimer.hasScheduledTask();
38 }
39
40 public void dispatchNextMessage() {
41 mTimer.fastForwardToNextTask();
42 }
43
44 @Override
45 public boolean sendMessageAtTime(Message msg, long uptimeMillis) {
46 msg.setTarget(this);
47 TimerTask task = new MessageTimerTask(msg);
48 mTimer.scheduleAtTime(new TestTimer.Task(task), uptimeMillis);
49 return true;
50 }
51
52 private static class MessageTimerTask extends TimerTask {
53 private Message mMessage;
54
55 private MessageTimerTask(Message message) {
56 mMessage = message;
57 }
58
59 @Override
60 public void run() {
61 mMessage.getTarget().dispatchMessage(mMessage);
62 }
63 }
64}