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.core; 18 19 import android.bluetooth.AtCommandHandler; 20 import android.bluetooth.AtCommandResult; 21 import android.bluetooth.AtParser; 22 23 import java.util.*; 24 import junit.framework.*; 25 26 public class AtParserTest extends TestCase { 27 28 /* An AtCommandHandler instrumented for testing purposes 29 */ 30 private class HandlerTest extends AtCommandHandler { 31 boolean mBasicCalled, mActionCalled, mReadCalled, mTestCalled, 32 mSetCalled; 33 int mBasicReturn, mActionReturn, mReadReturn, mTestReturn, mSetReturn; 34 Object[] mSetArgs; 35 String mBasicArgs; 36 HandlerTest()37 HandlerTest() { 38 this(AtCommandResult.ERROR, AtCommandResult.ERROR, 39 AtCommandResult.ERROR, AtCommandResult.ERROR, 40 AtCommandResult.ERROR); 41 } 42 HandlerTest(int a, int b, int c, int d, int e)43 HandlerTest(int a, int b, int c, int d, int e) { 44 mBasicReturn = a; 45 mActionReturn = b; 46 mReadReturn = c; 47 mSetReturn = d; 48 mTestReturn = e; 49 reset(); 50 } reset()51 public void reset() { 52 mBasicCalled = false; 53 mActionCalled = false; 54 mReadCalled = false; 55 mSetCalled = false; 56 mTestCalled = false; 57 mSetArgs = null; 58 mBasicArgs = null; 59 } wasCalled()60 public boolean wasCalled() { // helper 61 return mBasicCalled || mActionCalled || mReadCalled || 62 mTestCalled || mSetCalled; 63 } 64 @Override handleBasicCommand(String args)65 public AtCommandResult handleBasicCommand(String args) { 66 mBasicCalled = true; 67 mBasicArgs = args; 68 return new AtCommandResult(mBasicReturn); 69 } 70 @Override handleActionCommand()71 public AtCommandResult handleActionCommand() { 72 mActionCalled = true; 73 return new AtCommandResult(mActionReturn); 74 } 75 @Override handleReadCommand()76 public AtCommandResult handleReadCommand() { 77 mReadCalled = true; 78 return new AtCommandResult(mReadReturn); 79 } 80 @Override handleSetCommand(Object[] args)81 public AtCommandResult handleSetCommand(Object[] args) { 82 mSetCalled = true; 83 mSetArgs = args; 84 return new AtCommandResult(mSetReturn); 85 } 86 @Override handleTestCommand()87 public AtCommandResult handleTestCommand() { 88 mTestCalled = true; 89 return new AtCommandResult(mTestReturn); 90 } 91 } 92 93 private AtParser mParser; 94 95 @Override setUp()96 protected void setUp() throws Exception { 97 super.setUp(); 98 mParser = new AtParser(); 99 } 100 101 @Override tearDown()102 protected void tearDown() throws Exception { 103 super.tearDown(); 104 } 105 106 107 /* Test that the right method is being called 108 */ 109 /* public void testBasic1() throws Exception { 110 HandlerTest D = new HandlerTest(0, 1, 1, 1, 1); 111 HandlerTest A = new HandlerTest(0, 1, 1, 1, 1); 112 mParser.register('D', D); 113 mParser.register('A', A); 114 115 assertTrue(Arrays.equals( 116 new String[]{"OK"}, 117 mParser.process(" A T D = ? T 1 2 3 4 ").toStrings())); 118 assertTrue(D.mBasicCalled); 119 assertFalse(D.mActionCalled); 120 assertFalse(D.mTestCalled); 121 assertFalse(D.mSetCalled); 122 assertFalse(D.mReadCalled); 123 assertFalse(A.wasCalled()); 124 assertEquals("=?T1234", D.mBasicArgs); 125 } 126 */ 127 /* Test some crazy strings 128 *//* 129 public void testBasic2() throws Exception { 130 HandlerTest A = new HandlerTest(0, 1, 1, 1, 1); 131 mParser.register('A', A); 132 133 assertTrue(Arrays.equals( 134 new String[]{}, 135 mParser.process(" ").toStrings())); 136 137 assertTrue(Arrays.equals( 138 new String[]{"OK"}, 139 mParser.process(" a T a t \"\" 1 2 3 a 4 ") 140 .toStrings())); 141 assertEquals("T\"\"123A4", A.mBasicArgs); 142 143 assertTrue(Arrays.equals( 144 new String[]{"OK"}, 145 mParser.process(" a T a t \"foo BaR12Z\" 1 2 3 a 4 ") 146 .toStrings())); 147 assertEquals("T\"foo BaR12Z\"123A4", A.mBasicArgs); 148 149 assertTrue(Arrays.equals( 150 new String[]{"OK"}, 151 mParser.process("ATA\"").toStrings())); 152 assertEquals("\"\"", A.mBasicArgs); 153 154 assertTrue(Arrays.equals( 155 new String[]{"OK"}, 156 mParser.process("ATA\"a").toStrings())); 157 assertEquals("\"a\"", A.mBasicArgs); 158 159 assertTrue(Arrays.equals( 160 new String[]{"OK"}, 161 mParser.process("ATa\" ").toStrings())); 162 assertEquals("\" \"", A.mBasicArgs); 163 164 assertTrue(Arrays.equals( 165 new String[]{"OK"}, 166 mParser.process("ATA \"one \" two \"t hr ee ") 167 .toStrings())); 168 assertEquals("\"one \"TWO\"t hr ee \"", A.mBasicArgs); 169 }*/ 170 171 /* Simple extended commands 172 *//* 173 public void testExt1() throws Exception { 174 HandlerTest A = new HandlerTest(1, 0, 0, 0, 0); 175 mParser.register("+A", A); 176 177 assertTrue(Arrays.equals( 178 new String[]{"ERROR"}, 179 mParser.process("AT+B").toStrings())); 180 assertFalse(A.wasCalled()); 181 182 assertTrue(Arrays.equals( 183 new String[]{"OK"}, 184 mParser.process("AT+A").toStrings())); 185 assertTrue(A.mActionCalled); 186 A.mActionCalled = false; 187 assertFalse(A.wasCalled()); 188 A.reset(); 189 190 assertTrue(Arrays.equals( 191 new String[]{"OK"}, 192 mParser.process("AT+A=").toStrings())); 193 assertTrue(A.mSetCalled); 194 A.mSetCalled = false; 195 assertFalse(A.wasCalled()); 196 assertEquals(1, A.mSetArgs.length); 197 A.reset(); 198 199 assertTrue(Arrays.equals( 200 new String[]{"OK"}, 201 mParser.process("AT+A=?").toStrings())); 202 assertTrue(A.mTestCalled); 203 A.mTestCalled = false; 204 assertFalse(A.wasCalled()); 205 A.reset(); 206 207 assertTrue(Arrays.equals( 208 new String[]{"OK"}, 209 mParser.process("AT+A?").toStrings())); 210 assertTrue(A.mReadCalled); 211 A.mReadCalled = false; 212 assertFalse(A.wasCalled()); 213 A.reset(); 214 } 215 */ 216 217 218 /* Test chained commands 219 *//* 220 public void testChain1() throws Exception { 221 HandlerTest A = new HandlerTest(0, 1, 1, 1, 1); 222 HandlerTest B = new HandlerTest(1, 0, 0, 0, 0); 223 HandlerTest C = new HandlerTest(1, 1, 1, 1, 1); 224 mParser.register('A', A); 225 mParser.register("+B", B); 226 mParser.register("+C", C); 227 228 assertTrue(Arrays.equals( 229 new String[]{"ERROR"}, 230 mParser.process("AT+B;+C").toStrings())); 231 assertTrue(B.mActionCalled); 232 assertTrue(C.mActionCalled); 233 B.reset(); 234 C.reset(); 235 236 assertTrue(Arrays.equals( 237 new String[]{"ERROR"}, 238 mParser.process("AT+C;+B").toStrings())); 239 assertFalse(B.wasCalled()); 240 assertTrue(C.mActionCalled); 241 B.reset(); 242 C.reset(); 243 }*/ 244 245 /* Test Set command 246 *//* 247 public void testSet1() throws Exception { 248 HandlerTest A = new HandlerTest(1, 1, 1, 0, 1); 249 mParser.register("+AAAA", A); 250 Object[] expectedResult; 251 252 assertTrue(Arrays.equals( 253 new String[]{"OK"}, 254 mParser.process("AT+AAAA=1").toStrings())); 255 expectedResult = new Object[]{(Integer)1}; 256 assertTrue(Arrays.equals(expectedResult, A.mSetArgs)); 257 A.reset(); 258 259 assertTrue(Arrays.equals( 260 new String[]{"OK"}, 261 mParser.process("AT+AAAA=1,2,3").toStrings())); 262 expectedResult = new Object[]{(Integer)1, (Integer)2, (Integer)3}; 263 assertTrue(Arrays.equals(expectedResult, A.mSetArgs)); 264 A.reset(); 265 266 assertTrue(Arrays.equals( 267 new String[]{"OK"}, 268 mParser.process("AT+AAAA=3,0,0,1").toStrings())); 269 expectedResult = new Object[]{(Integer)3, (Integer)0, (Integer)0, 270 (Integer)1}; 271 assertTrue(Arrays.equals(expectedResult, A.mSetArgs)); 272 A.reset(); 273 274 assertTrue(Arrays.equals( 275 new String[]{"OK"}, 276 mParser.process("AT+AAAA=\"foo\",1,\"b,ar").toStrings())); 277 expectedResult = new Object[]{"\"foo\"", 1, "\"b,ar\""}; 278 assertTrue(Arrays.equals(expectedResult, A.mSetArgs)); 279 A.reset(); 280 281 assertTrue(Arrays.equals( 282 new String[]{"OK"}, 283 mParser.process("AT+AAAA=").toStrings())); 284 expectedResult = new Object[]{""}; 285 assertTrue(Arrays.equals(expectedResult, A.mSetArgs)); 286 A.reset(); 287 288 assertTrue(Arrays.equals( 289 new String[]{"OK"}, 290 mParser.process("AT+AAAA=,").toStrings())); 291 expectedResult = new Object[]{"", ""}; 292 assertTrue(Arrays.equals(expectedResult, A.mSetArgs)); 293 A.reset(); 294 295 assertTrue(Arrays.equals( 296 new String[]{"OK"}, 297 mParser.process("AT+AAAA=,,,").toStrings())); 298 expectedResult = new Object[]{"", "", "", ""}; 299 assertTrue(Arrays.equals(expectedResult, A.mSetArgs)); 300 A.reset(); 301 302 assertTrue(Arrays.equals( 303 new String[]{"OK"}, 304 mParser.process("AT+AAAA=,1,,\"foo\",").toStrings())); 305 expectedResult = new Object[]{"", 1, "", "\"foo\"", ""}; 306 assertEquals(5, A.mSetArgs.length); 307 assertTrue(Arrays.equals(expectedResult, A.mSetArgs)); 308 A.reset(); 309 }*/ 310 311 /* Test repeat command "A/" 312 *//* 313 public void testRepeat() throws Exception { 314 HandlerTest A = new HandlerTest(0, 0, 0, 0, 0); 315 mParser.register('A', A); 316 317 // Try repeated command on fresh parser 318 assertTrue(Arrays.equals( 319 new String[]{}, 320 mParser.process("A/").toStrings())); 321 assertFalse(A.wasCalled()); 322 A.reset(); 323 324 assertTrue(Arrays.equals( 325 new String[]{"OK"}, 326 mParser.process("ATA").toStrings())); 327 assertTrue(A.mBasicCalled); 328 assertEquals("", A.mBasicArgs); 329 A.reset(); 330 331 // Now repeat the command 332 assertTrue(Arrays.equals( 333 new String[]{"OK"}, 334 mParser.process("A/").toStrings())); 335 assertTrue(A.mBasicCalled); 336 assertEquals("", A.mBasicArgs); 337 A.reset(); 338 339 // Multiple repeats 340 assertTrue(Arrays.equals( 341 new String[]{"OK"}, 342 mParser.process("A/").toStrings())); 343 assertTrue(A.mBasicCalled); 344 assertEquals("", A.mBasicArgs); 345 A.reset(); 346 347 }*/ 348 } 349