2 * Copyright (C) 2007 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package android.bluetooth;
19 import android.bluetooth.AtCommandHandler;
20 import android.bluetooth.AtCommandResult;
21 import android.bluetooth.AtParser;
24 import junit.framework.*;
26 public class AtParserTest extends TestCase {
28 /* An AtCommandHandler instrumented for testing purposes
30 private class HandlerTest extends AtCommandHandler {
31 boolean mBasicCalled, mActionCalled, mReadCalled, mTestCalled,
33 int mBasicReturn, mActionReturn, mReadReturn, mTestReturn, mSetReturn;
38 this(AtCommandResult.ERROR, AtCommandResult.ERROR,
39 AtCommandResult.ERROR, AtCommandResult.ERROR,
40 AtCommandResult.ERROR);
43 HandlerTest(int a, int b, int c, int d, int e) {
53 mActionCalled = false;
60 public boolean wasCalled() { // helper
61 return mBasicCalled || mActionCalled || mReadCalled ||
62 mTestCalled || mSetCalled;
65 public AtCommandResult handleBasicCommand(String args) {
68 return new AtCommandResult(mBasicReturn);
71 public AtCommandResult handleActionCommand() {
73 return new AtCommandResult(mActionReturn);
76 public AtCommandResult handleReadCommand() {
78 return new AtCommandResult(mReadReturn);
81 public AtCommandResult handleSetCommand(Object[] args) {
84 return new AtCommandResult(mSetReturn);
87 public AtCommandResult handleTestCommand() {
89 return new AtCommandResult(mTestReturn);
93 private AtParser mParser;
96 protected void setUp() throws Exception {
98 mParser = new AtParser();
102 protected void tearDown() throws Exception {
107 /* Test that the right method is being called
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);
115 assertTrue(Arrays.equals(
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);
127 /* Test some crazy strings
129 public void testBasic2() throws Exception {
130 HandlerTest A = new HandlerTest(0, 1, 1, 1, 1);
131 mParser.register('A', A);
133 assertTrue(Arrays.equals(
135 mParser.process(" ").toStrings()));
137 assertTrue(Arrays.equals(
139 mParser.process(" a T a t \"\" 1 2 3 a 4 ")
141 assertEquals("T\"\"123A4", A.mBasicArgs);
143 assertTrue(Arrays.equals(
145 mParser.process(" a T a t \"foo BaR12Z\" 1 2 3 a 4 ")
147 assertEquals("T\"foo BaR12Z\"123A4", A.mBasicArgs);
149 assertTrue(Arrays.equals(
151 mParser.process("ATA\"").toStrings()));
152 assertEquals("\"\"", A.mBasicArgs);
154 assertTrue(Arrays.equals(
156 mParser.process("ATA\"a").toStrings()));
157 assertEquals("\"a\"", A.mBasicArgs);
159 assertTrue(Arrays.equals(
161 mParser.process("ATa\" ").toStrings()));
162 assertEquals("\" \"", A.mBasicArgs);
164 assertTrue(Arrays.equals(
166 mParser.process("ATA \"one \" two \"t hr ee ")
168 assertEquals("\"one \"TWO\"t hr ee \"", A.mBasicArgs);
171 /* Simple extended commands
173 public void testExt1() throws Exception {
174 HandlerTest A = new HandlerTest(1, 0, 0, 0, 0);
175 mParser.register("+A", A);
177 assertTrue(Arrays.equals(
178 new String[]{"ERROR"},
179 mParser.process("AT+B").toStrings()));
180 assertFalse(A.wasCalled());
182 assertTrue(Arrays.equals(
184 mParser.process("AT+A").toStrings()));
185 assertTrue(A.mActionCalled);
186 A.mActionCalled = false;
187 assertFalse(A.wasCalled());
190 assertTrue(Arrays.equals(
192 mParser.process("AT+A=").toStrings()));
193 assertTrue(A.mSetCalled);
194 A.mSetCalled = false;
195 assertFalse(A.wasCalled());
196 assertEquals(1, A.mSetArgs.length);
199 assertTrue(Arrays.equals(
201 mParser.process("AT+A=?").toStrings()));
202 assertTrue(A.mTestCalled);
203 A.mTestCalled = false;
204 assertFalse(A.wasCalled());
207 assertTrue(Arrays.equals(
209 mParser.process("AT+A?").toStrings()));
210 assertTrue(A.mReadCalled);
211 A.mReadCalled = false;
212 assertFalse(A.wasCalled());
218 /* Test chained commands
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);
228 assertTrue(Arrays.equals(
229 new String[]{"ERROR"},
230 mParser.process("AT+B;+C").toStrings()));
231 assertTrue(B.mActionCalled);
232 assertTrue(C.mActionCalled);
236 assertTrue(Arrays.equals(
237 new String[]{"ERROR"},
238 mParser.process("AT+C;+B").toStrings()));
239 assertFalse(B.wasCalled());
240 assertTrue(C.mActionCalled);
247 public void testSet1() throws Exception {
248 HandlerTest A = new HandlerTest(1, 1, 1, 0, 1);
249 mParser.register("+AAAA", A);
250 Object[] expectedResult;
252 assertTrue(Arrays.equals(
254 mParser.process("AT+AAAA=1").toStrings()));
255 expectedResult = new Object[]{(Integer)1};
256 assertTrue(Arrays.equals(expectedResult, A.mSetArgs));
259 assertTrue(Arrays.equals(
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));
266 assertTrue(Arrays.equals(
268 mParser.process("AT+AAAA=3,0,0,1").toStrings()));
269 expectedResult = new Object[]{(Integer)3, (Integer)0, (Integer)0,
271 assertTrue(Arrays.equals(expectedResult, A.mSetArgs));
274 assertTrue(Arrays.equals(
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));
281 assertTrue(Arrays.equals(
283 mParser.process("AT+AAAA=").toStrings()));
284 expectedResult = new Object[]{""};
285 assertTrue(Arrays.equals(expectedResult, A.mSetArgs));
288 assertTrue(Arrays.equals(
290 mParser.process("AT+AAAA=,").toStrings()));
291 expectedResult = new Object[]{"", ""};
292 assertTrue(Arrays.equals(expectedResult, A.mSetArgs));
295 assertTrue(Arrays.equals(
297 mParser.process("AT+AAAA=,,,").toStrings()));
298 expectedResult = new Object[]{"", "", "", ""};
299 assertTrue(Arrays.equals(expectedResult, A.mSetArgs));
302 assertTrue(Arrays.equals(
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));
311 /* Test repeat command "A/"
313 public void testRepeat() throws Exception {
314 HandlerTest A = new HandlerTest(0, 0, 0, 0, 0);
315 mParser.register('A', A);
317 // Try repeated command on fresh parser
318 assertTrue(Arrays.equals(
320 mParser.process("A/").toStrings()));
321 assertFalse(A.wasCalled());
324 assertTrue(Arrays.equals(
326 mParser.process("ATA").toStrings()));
327 assertTrue(A.mBasicCalled);
328 assertEquals("", A.mBasicArgs);
331 // Now repeat the command
332 assertTrue(Arrays.equals(
334 mParser.process("A/").toStrings()));
335 assertTrue(A.mBasicCalled);
336 assertEquals("", A.mBasicArgs);
340 assertTrue(Arrays.equals(
342 mParser.process("A/").toStrings()));
343 assertTrue(A.mBasicCalled);
344 assertEquals("", A.mBasicArgs);