1 /* 2 * Copyright (C) 2007 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 android.os; 18 19 import android.os.Handler; 20 import android.os.Looper; 21 import android.os.Message; 22 import android.os.MessageQueue.IdleHandler; 23 import android.test.suitebuilder.annotation.MediumTest; 24 import junit.framework.TestCase; 25 26 public class IdleHandlerTest extends TestCase { 27 28 private static class BaseTestHandler extends TestHandlerThread { 29 Handler mHandler; 30 31 public BaseTestHandler() { 32 } 33 34 public void go() { 35 mHandler = new Handler() { 36 public void handleMessage(Message msg) { 37 BaseTestHandler.this.handleMessage(msg); 38 } 39 }; 40 } 41 42 public void addIdleHandler() { 43 Looper.myQueue().addIdleHandler(new IdleHandler() { 44 public boolean queueIdle() { 45 return BaseTestHandler.this.queueIdle(); 46 } 47 }); 48 } 49 50 public void handleMessage(Message msg) { 51 } 52 53 public boolean queueIdle() { 54 return false; 55 } 56 } 57 58 @MediumTest 59 public void testOneShotFirst() throws Exception { 60 TestHandlerThread tester = new BaseTestHandler() { 61 int mCount; 62 63 public void go() { 64 super.go(); 65 mCount = 0; 66 mHandler.sendMessageDelayed(mHandler.obtainMessage(0), 100); 67 addIdleHandler(); 68 } 69 70 public void handleMessage(Message msg) { 71 if (msg.what == 0) { 72 mHandler.sendMessageDelayed(mHandler.obtainMessage(1), 100); 73 } else if (msg.what == 1) { 74 if (mCount == 1) { 75 success(); 76 } else { 77 failure(new RuntimeException( 78 "Idle handler called " + mCount + " times")); 79 } 80 } 81 } 82 83 public boolean queueIdle() { 84 mCount++; 85 return false; 86 } 87 }; 88 89 tester.doTest(1000); 90 } 91 92 @MediumTest 93 public void testOneShotLater() throws Exception { 94 TestHandlerThread tester = new BaseTestHandler() { 95 int mCount; 96 97 public void go() { 98 super.go(); 99 mCount = 0; 100 mHandler.sendMessage(mHandler.obtainMessage(0)); 101 } 102 103 public void handleMessage(Message msg) { 104 if (msg.what == 0) { 105 addIdleHandler(); 106 mHandler.sendMessageDelayed(mHandler.obtainMessage(1), 100); 107 } else if (msg.what == 1) { 108 mHandler.sendMessageDelayed(mHandler.obtainMessage(2), 100); 109 } else if (msg.what == 2) { 110 if (mCount == 1) { 111 success(); 112 } else { 113 failure(new RuntimeException( 114 "Idle handler called " + mCount + " times")); 115 } 116 } 117 } 118 119 public boolean queueIdle() { 120 mCount++; 121 return false; 122 } 123 }; 124 125 tester.doTest(1000); 126 } 127 128 129 @MediumTest 130 public void testRepeatedFirst() throws Exception { 131 TestHandlerThread tester = new BaseTestHandler() { 132 int mCount; 133 134 public void go() { 135 super.go(); 136 mCount = 0; 137 mHandler.sendMessageDelayed(mHandler.obtainMessage(0), 100); 138 addIdleHandler(); 139 } 140 141 public void handleMessage(Message msg) { 142 if (msg.what == 0) { 143 mHandler.sendMessageDelayed(mHandler.obtainMessage(1), 100); 144 } else if (msg.what == 1) { 145 if (mCount == 2) { 146 success(); 147 } else { 148 failure(new RuntimeException( 149 "Idle handler called " + mCount + " times")); 150 } 151 } 152 } 153 154 public boolean queueIdle() { 155 mCount++; 156 return true; 157 } 158 }; 159 160 tester.doTest(1000); 161 } 162 163 @MediumTest 164 public void testRepeatedLater() throws Exception { 165 TestHandlerThread tester = new BaseTestHandler() { 166 int mCount; 167 168 public void go() { 169 super.go(); 170 mCount = 0; 171 mHandler.sendMessage(mHandler.obtainMessage(0)); 172 } 173 174 public void handleMessage(Message msg) { 175 if (msg.what == 0) { 176 addIdleHandler(); 177 mHandler.sendMessageDelayed(mHandler.obtainMessage(1), 100); 178 } else if (msg.what == 1) { 179 mHandler.sendMessageDelayed(mHandler.obtainMessage(2), 100); 180 } else if (msg.what == 2) { 181 if (mCount == 2) { 182 success(); 183 } else { 184 failure(new RuntimeException( 185 "Idle handler called " + mCount + " times")); 186 } 187 } 188 } 189 190 public boolean queueIdle() { 191 mCount++; 192 return true; 193 } 194 }; 195 196 tester.doTest(1000); 197 } 198 } 199 200