Garfield, Tan | 48ef36f | 2016-06-09 12:04:22 -0700 | [diff] [blame] | 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; |
Garfield Tan | d79ef7f | 2017-01-25 18:42:15 -0800 | [diff] [blame^] | 22 | import android.os.SystemClock; |
Garfield, Tan | 48ef36f | 2016-06-09 12:04:22 -0700 | [diff] [blame] | 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 | |
Garfield Tan | d79ef7f | 2017-01-25 18:42:15 -0800 | [diff] [blame^] | 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 | |
Garfield, Tan | 48ef36f | 2016-06-09 12:04:22 -0700 | [diff] [blame] | 40 | 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 Tan | d79ef7f | 2017-01-25 18:42:15 -0800 | [diff] [blame^] | 51 | |
| 52 | mTimeDelta = mTimer.getNow() - SystemClock.uptimeMillis(); |
| 53 | } |
| 54 | |
| 55 | public void dispatchAllScheduledMessages() { |
| 56 | while (hasScheduledMessage()) { |
| 57 | dispatchNextMessage(); |
| 58 | } |
Garfield, Tan | 48ef36f | 2016-06-09 12:04:22 -0700 | [diff] [blame] | 59 | } |
| 60 | |
Garfield, Tan | 4833477 | 2016-06-28 17:17:38 -0700 | [diff] [blame] | 61 | public void dispatchAllMessages() { |
| 62 | while (hasScheduledMessage()) { |
Garfield Tan | d79ef7f | 2017-01-25 18:42:15 -0800 | [diff] [blame^] | 63 | dispatchAllScheduledMessages(); |
Garfield, Tan | 4833477 | 2016-06-28 17:17:38 -0700 | [diff] [blame] | 64 | } |
| 65 | } |
| 66 | |
Garfield, Tan | 48ef36f | 2016-06-09 12:04:22 -0700 | [diff] [blame] | 67 | @Override |
| 68 | public boolean sendMessageAtTime(Message msg, long uptimeMillis) { |
| 69 | msg.setTarget(this); |
| 70 | TimerTask task = new MessageTimerTask(msg); |
Garfield Tan | d79ef7f | 2017-01-25 18:42:15 -0800 | [diff] [blame^] | 71 | mTimer.scheduleAtTime(new TestTimer.Task(task), uptimeMillis + mTimeDelta); |
Garfield, Tan | 48ef36f | 2016-06-09 12:04:22 -0700 | [diff] [blame] | 72 | 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 | } |