1 /*
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 
17 package com.android.documentsui.testing;
18 
19 import android.os.Handler;
20 import android.os.Looper;
21 import android.os.Message;
22 import android.os.SystemClock;
23 
24 import java.util.TimerTask;
25 
26 /**
27  * A test double of {@link Handler}, backed by {@link TestTimer}.
28  */
29 public class TestHandler extends Handler {
30     private TestTimer mTimer = new TestTimer();
31 
32     // 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 
TestHandler()40     public TestHandler() {
41         // Use main looper to trick underlying handler, we're not using it at all.
42         super(Looper.getMainLooper());
43     }
44 
hasScheduledMessage()45     public boolean hasScheduledMessage() {
46         return mTimer.hasScheduledTask();
47     }
48 
dispatchNextMessage()49     public void dispatchNextMessage() {
50         mTimer.fastForwardToNextTask();
51 
52         mTimeDelta = mTimer.getNow() - SystemClock.uptimeMillis();
53     }
54 
dispatchAllScheduledMessages()55     public void dispatchAllScheduledMessages() {
56         while (hasScheduledMessage()) {
57             dispatchNextMessage();
58         }
59     }
60 
dispatchAllMessages()61     public void dispatchAllMessages() {
62         while (hasScheduledMessage()) {
63             dispatchAllScheduledMessages();
64         }
65     }
66 
67     @Override
sendMessageAtTime(Message msg, long uptimeMillis)68     public boolean sendMessageAtTime(Message msg, long uptimeMillis) {
69         msg.setTarget(this);
70         TimerTask task = new MessageTimerTask(msg);
71         mTimer.scheduleAtTime(new TestTimer.Task(task), uptimeMillis + mTimeDelta);
72         return true;
73     }
74 
75     private static class MessageTimerTask extends TimerTask {
76         private Message mMessage;
77 
MessageTimerTask(Message message)78         private MessageTimerTask(Message message) {
79             mMessage = message;
80         }
81 
82         @Override
run()83         public void run() {
84             mMessage.getTarget().dispatchMessage(mMessage);
85         }
86     }
87 }
88