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