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 17package android.os; 18 19import android.os.Broadcaster; 20import android.os.Handler; 21import android.os.Message; 22import android.test.suitebuilder.annotation.MediumTest; 23import android.test.suitebuilder.annotation.SmallTest; 24import junit.framework.TestCase; 25 26public class BroadcasterTest extends TestCase { 27 private static final int MESSAGE_A = 23234; 28 private static final int MESSAGE_B = 3; 29 private static final int MESSAGE_C = 14; 30 private static final int MESSAGE_D = 95; 31 32 @MediumTest 33 public void test1() throws Exception { 34 /* 35 * One handler requestes one message, with a translation 36 */ 37 HandlerTester tester = new HandlerTester() { 38 Handler h; 39 40 public void go() { 41 Broadcaster b = new Broadcaster(); 42 h = new H(); 43 44 b.request(MESSAGE_A, h, MESSAGE_B); 45 46 Message msg = new Message(); 47 msg.what = MESSAGE_A; 48 49 b.broadcast(msg); 50 } 51 52 public void handleMessage(Message msg) { 53 if (msg.what == MESSAGE_B) { 54 success(); 55 } else { 56 failure(); 57 } 58 } 59 }; 60 tester.doTest(1000); 61 } 62 63 private static class Tests2and3 extends HandlerTester { 64 Tests2and3(int n) { 65 N = n; 66 } 67 68 int N; 69 Handler mHandlers[]; 70 boolean mSuccess[]; 71 72 public void go() { 73 Broadcaster b = new Broadcaster(); 74 mHandlers = new Handler[N]; 75 mSuccess = new boolean[N]; 76 for (int i = 0; i < N; i++) { 77 mHandlers[i] = new H(); 78 mSuccess[i] = false; 79 b.request(MESSAGE_A, mHandlers[i], MESSAGE_B + i); 80 } 81 82 Message msg = new Message(); 83 msg.what = MESSAGE_A; 84 85 b.broadcast(msg); 86 } 87 88 public void handleMessage(Message msg) { 89 int index = msg.what - MESSAGE_B; 90 if (index < 0 || index >= N) { 91 failure(); 92 } else { 93 if (msg.getTarget() == mHandlers[index]) { 94 mSuccess[index] = true; 95 } 96 } 97 boolean winner = true; 98 for (int i = 0; i < N; i++) { 99 if (!mSuccess[i]) { 100 winner = false; 101 } 102 } 103 if (winner) { 104 success(); 105 } 106 } 107 } 108 109 @MediumTest 110 public void test2() throws Exception { 111 /* 112 * 2 handlers request the same message, with different translations 113 */ 114 HandlerTester tester = new Tests2and3(2); 115 tester.doTest(1000); 116 } 117 118 @MediumTest 119 public void test3() throws Exception { 120 /* 121 * 1000 handlers request the same message, with different translations 122 */ 123 HandlerTester tester = new Tests2and3(10); 124 tester.doTest(1000); 125 } 126 127 @MediumTest 128 public void test4() throws Exception { 129 /* 130 * Two handlers request different messages, with translations, sending 131 * only one. The other one should never get sent. 132 */ 133 HandlerTester tester = new HandlerTester() { 134 Handler h1; 135 Handler h2; 136 137 public void go() { 138 Broadcaster b = new Broadcaster(); 139 h1 = new H(); 140 h2 = new H(); 141 142 b.request(MESSAGE_A, h1, MESSAGE_C); 143 b.request(MESSAGE_B, h2, MESSAGE_D); 144 145 Message msg = new Message(); 146 msg.what = MESSAGE_A; 147 148 b.broadcast(msg); 149 } 150 151 public void handleMessage(Message msg) { 152 if (msg.what == MESSAGE_C && msg.getTarget() == h1) { 153 success(); 154 } else { 155 failure(); 156 } 157 } 158 }; 159 tester.doTest(1000); 160 } 161 162 @MediumTest 163 public void test5() throws Exception { 164 /* 165 * Two handlers request different messages, with translations, sending 166 * only one. The other one should never get sent. 167 */ 168 HandlerTester tester = new HandlerTester() { 169 Handler h1; 170 Handler h2; 171 172 public void go() { 173 Broadcaster b = new Broadcaster(); 174 h1 = new H(); 175 h2 = new H(); 176 177 b.request(MESSAGE_A, h1, MESSAGE_C); 178 b.request(MESSAGE_B, h2, MESSAGE_D); 179 180 Message msg = new Message(); 181 msg.what = MESSAGE_B; 182 183 b.broadcast(msg); 184 } 185 186 public void handleMessage(Message msg) { 187 if (msg.what == MESSAGE_D && msg.getTarget() == h2) { 188 success(); 189 } else { 190 failure(); 191 } 192 } 193 }; 194 tester.doTest(1000); 195 } 196 197 @MediumTest 198 public void test6() throws Exception { 199 /* 200 * Two handlers request same message. Cancel the request for the 201 * 2nd handler, make sure the first still works. 202 */ 203 HandlerTester tester = new HandlerTester() { 204 Handler h1; 205 Handler h2; 206 207 public void go() { 208 Broadcaster b = new Broadcaster(); 209 h1 = new H(); 210 h2 = new H(); 211 212 b.request(MESSAGE_A, h1, MESSAGE_C); 213 b.request(MESSAGE_A, h2, MESSAGE_D); 214 b.cancelRequest(MESSAGE_A, h2, MESSAGE_D); 215 216 Message msg = new Message(); 217 msg.what = MESSAGE_A; 218 219 b.broadcast(msg); 220 } 221 222 public void handleMessage(Message msg) { 223 if (msg.what == MESSAGE_C && msg.getTarget() == h1) { 224 success(); 225 } else { 226 failure(); 227 } 228 } 229 }; 230 tester.doTest(1000); 231 } 232} 233