blob: c458c9ba30818bd881e2c1d5ce12888276b45298 [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;
Garfield Tand79ef7f2017-01-25 18:42:15 -080022import android.os.SystemClock;
Garfield, Tan48ef36f2016-06-09 12:04:22 -070023
24import java.util.TimerTask;
25
26/**
27 * A test double of {@link Handler}, backed by {@link TestTimer}.
28 */
29public class TestHandler extends Handler {
30 private TestTimer mTimer = new TestTimer();
31
Garfield Tand79ef7f2017-01-25 18:42:15 -080032 // Handler uses SystemClock.uptimeMillis() when scheduling task to get current time, but
33 // TestTimer has its own warped time for us to "fast forward" into the future. Therefore after
34 // we "fast forwarded" TestTimer once Handler may schedule tasks running in the "past" relative
35 // to the fast-forwarded TestTimer and cause problems. This value is used to track how much we
36 // fast-forward into the future to make sure we schedule tasks in the future of TestTimer as
37 // well.
38 private long mTimeDelta = 0;
39
Garfield, Tan48ef36f2016-06-09 12:04:22 -070040 public TestHandler() {
41 // Use main looper to trick underlying handler, we're not using it at all.
42 super(Looper.getMainLooper());
43 }
44
45 public boolean hasScheduledMessage() {
46 return mTimer.hasScheduledTask();
47 }
48
49 public void dispatchNextMessage() {
50 mTimer.fastForwardToNextTask();
Garfield Tand79ef7f2017-01-25 18:42:15 -080051
52 mTimeDelta = mTimer.getNow() - SystemClock.uptimeMillis();
53 }
54
55 public void dispatchAllScheduledMessages() {
56 while (hasScheduledMessage()) {
57 dispatchNextMessage();
58 }
Garfield, Tan48ef36f2016-06-09 12:04:22 -070059 }
60
Garfield, Tan48334772016-06-28 17:17:38 -070061 public void dispatchAllMessages() {
62 while (hasScheduledMessage()) {
Garfield Tand79ef7f2017-01-25 18:42:15 -080063 dispatchAllScheduledMessages();
Garfield, Tan48334772016-06-28 17:17:38 -070064 }
65 }
66
Garfield, Tan48ef36f2016-06-09 12:04:22 -070067 @Override
68 public boolean sendMessageAtTime(Message msg, long uptimeMillis) {
69 msg.setTarget(this);
70 TimerTask task = new MessageTimerTask(msg);
Garfield Tand79ef7f2017-01-25 18:42:15 -080071 mTimer.scheduleAtTime(new TestTimer.Task(task), uptimeMillis + mTimeDelta);
Garfield, Tan48ef36f2016-06-09 12:04:22 -070072 return true;
73 }
74
75 private static class MessageTimerTask extends TimerTask {
76 private Message mMessage;
77
78 private MessageTimerTask(Message message) {
79 mMessage = message;
80 }
81
82 @Override
83 public void run() {
84 mMessage.getTarget().dispatchMessage(mMessage);
85 }
86 }
87}