Skip to content

Commit 163fba0

Browse files
Eric RoweAndroid (Google) Code Review
authored andcommitted
Merge "Improve logging and flexibility of BT stress tests." into froyo
2 parents 797e688 + e1d666b commit 163fba0

File tree

3 files changed

+158
-37
lines changed

3 files changed

+158
-37
lines changed

core/tests/coretests/AndroidManifest.xml

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1205,8 +1205,12 @@
12051205

12061206
</application>
12071207

1208-
<instrumentation
1209-
android:name="android.test.InstrumentationTestRunner"
1210-
android:targetPackage="com.android.frameworks.coretests"
1211-
android:label="Frameworks Core Tests" />
1208+
<instrumentation android:name="android.test.InstrumentationTestRunner"
1209+
android:targetPackage="com.android.frameworks.coretests"
1210+
android:label="Frameworks Core Tests" />
1211+
1212+
<instrumentation android:name="android.bluetooth.BluetoothTestRunner"
1213+
android:targetPackage="com.android.frameworks.coretests"
1214+
android:label="Bluetooth Tests" />
1215+
12121216
</manifest>

core/tests/coretests/src/android/bluetooth/BluetoothStressTest.java

Lines changed: 77 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,23 @@
1616

1717
package android.bluetooth;
1818

19+
import java.io.BufferedWriter;
20+
import java.io.File;
21+
import java.io.FileWriter;
22+
import java.io.IOException;
23+
1924
import android.app.Instrumentation;
2025
import android.content.BroadcastReceiver;
2126
import android.content.Context;
2227
import android.content.Intent;
2328
import android.content.IntentFilter;
29+
import android.os.Environment;
2430
import android.test.InstrumentationTestCase;
2531
import android.util.Log;
2632

2733
public class BluetoothStressTest extends InstrumentationTestCase {
28-
private static final String TAG = "BluetoothEnablerStressTest";
34+
private static final String TAG = "BluetoothStressTest";
35+
private static final String OUTPUT_FILE = "BluetoothStressTestOutput.txt";
2936

3037
/**
3138
* Timeout for {@link BluetoothAdapter#disable()} in ms.
@@ -67,14 +74,12 @@ public class BluetoothStressTest extends InstrumentationTestCase {
6774
*/
6875
private static final int POLL_TIME = 100;
6976

70-
private static final int ENABLE_ITERATIONS = 100;
71-
private static final int DISCOVERABLE_ITERATIONS = 1000;
72-
private static final int SCAN_ITERATIONS = 1000;
73-
7477
private Context mContext;
7578

7679
private Instrumentation mInstrumentation;
7780

81+
private BufferedWriter mOutputWriter;
82+
7883
private class BluetoothReceiver extends BroadcastReceiver {
7984
private int mFiredFlags = 0;
8085

@@ -144,6 +149,14 @@ protected void setUp() throws Exception {
144149
mInstrumentation = getInstrumentation();
145150
mContext = mInstrumentation.getTargetContext();
146151

152+
try {
153+
mOutputWriter = new BufferedWriter(new FileWriter(new File(
154+
Environment.getExternalStorageDirectory(), OUTPUT_FILE), true));
155+
} catch (IOException e) {
156+
Log.w(TAG, "Test output file could not be opened", e);
157+
mOutputWriter = null;
158+
}
159+
147160
IntentFilter filter = new IntentFilter();
148161
filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
149162
filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
@@ -157,24 +170,34 @@ protected void tearDown() throws Exception {
157170
super.tearDown();
158171

159172
mContext.unregisterReceiver(mReceiver);
173+
174+
if (mOutputWriter != null) {
175+
try {
176+
mOutputWriter.close();
177+
} catch (IOException e) {
178+
Log.w(TAG, "Test output file could not be closed", e);
179+
}
180+
}
160181
}
161182

162-
public void testEnableDisable() {
183+
public void testEnable() {
184+
int iterations = BluetoothTestRunner.sEnableIterations;
163185
BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
164186

165-
for (int i = 0; i < ENABLE_ITERATIONS; i++) {
166-
Log.i(TAG, "Enable iteration " + (i + 1) + " of " + ENABLE_ITERATIONS);
187+
for (int i = 0; i < iterations; i++) {
188+
writeOutput("enable iteration " + (i + 1) + " of " + iterations);
167189
enable(adapter);
168190
disable(adapter);
169191
}
170192
}
171193

172194
public void testDiscoverable() {
195+
int iterations = BluetoothTestRunner.sDiscoverableIterations;
173196
BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
174197
enable(adapter);
175198

176-
for (int i = 0; i < DISCOVERABLE_ITERATIONS; i++) {
177-
Log.i(TAG, "Discoverable iteration " + (i + 1) + " of " + DISCOVERABLE_ITERATIONS);
199+
for (int i = 0; i < iterations; i++) {
200+
writeOutput("discoverable iteration " + (i + 1) + " of " + iterations);
178201
discoverable(adapter);
179202
undiscoverable(adapter);
180203
}
@@ -183,11 +206,12 @@ public void testDiscoverable() {
183206
}
184207

185208
public void testScan() {
209+
int iterations = BluetoothTestRunner.sScanIterations;
186210
BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
187211
enable(adapter);
188212

189-
for (int i = 0; i < SCAN_ITERATIONS; i++) {
190-
Log.i(TAG, "Scan iteration " + (i + 1) + " of " + SCAN_ITERATIONS);
213+
for (int i = 0; i < iterations; i++) {
214+
writeOutput("scan iteration " + (i + 1) + " of " + iterations);
191215
startScan(adapter);
192216
stopScan(adapter);
193217
}
@@ -217,7 +241,7 @@ private void disable(BluetoothAdapter adapter) {
217241
mask = 0; // Don't check for received intents since we might have missed them.
218242
break;
219243
default:
220-
fail("disable() invalid state: " + state);
244+
fail("disable() invalid state: state=" + state);
221245
}
222246

223247
long s = System.currentTimeMillis();
@@ -227,6 +251,8 @@ private void disable(BluetoothAdapter adapter) {
227251
assertFalse(adapter.isEnabled());
228252
if ((mReceiver.getFiredFlags() & mask) == mask) {
229253
mReceiver.resetFiredFlags();
254+
writeOutput(String.format("disable() completed in %d ms",
255+
(System.currentTimeMillis() - s)));
230256
return;
231257
}
232258
} else {
@@ -238,9 +264,8 @@ private void disable(BluetoothAdapter adapter) {
238264

239265
int firedFlags = mReceiver.getFiredFlags();
240266
mReceiver.resetFiredFlags();
241-
fail("disable() timeout: " +
242-
"state=" + state + " (expected " + BluetoothAdapter.STATE_OFF + ") " +
243-
"flags=" + firedFlags + " (expected " + mask + ")");
267+
fail(String.format("disable() timeout: state=%d (expected %d), flags=0x%x (expected 0x%x)",
268+
state, BluetoothAdapter.STATE_OFF, firedFlags, mask));
244269
}
245270

246271
private void enable(BluetoothAdapter adapter) {
@@ -272,6 +297,8 @@ private void enable(BluetoothAdapter adapter) {
272297
assertTrue(adapter.isEnabled());
273298
if ((mReceiver.getFiredFlags() & mask) == mask) {
274299
mReceiver.resetFiredFlags();
300+
writeOutput(String.format("enable() completed in %d ms",
301+
(System.currentTimeMillis() - s)));
275302
return;
276303
}
277304
} else {
@@ -283,9 +310,8 @@ private void enable(BluetoothAdapter adapter) {
283310

284311
int firedFlags = mReceiver.getFiredFlags();
285312
mReceiver.resetFiredFlags();
286-
fail("enable() timeout: " +
287-
"state=" + state + " (expected " + BluetoothAdapter.STATE_OFF + ") " +
288-
"flags=" + firedFlags + " (expected " + mask + ")");
313+
fail(String.format("enable() timeout: state=%d (expected %d), flags=0x%x (expected 0x%x)",
314+
state, BluetoothAdapter.STATE_ON, firedFlags, mask));
289315
}
290316

291317
private void discoverable(BluetoothAdapter adapter) {
@@ -310,6 +336,8 @@ private void discoverable(BluetoothAdapter adapter) {
310336
if (scanMode == BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE) {
311337
if ((mReceiver.getFiredFlags() & mask) == mask) {
312338
mReceiver.resetFiredFlags();
339+
writeOutput(String.format("discoverable() completed in %d ms",
340+
(System.currentTimeMillis() - s)));
313341
return;
314342
}
315343
} else {
@@ -320,10 +348,9 @@ private void discoverable(BluetoothAdapter adapter) {
320348

321349
int firedFlags = mReceiver.getFiredFlags();
322350
mReceiver.resetFiredFlags();
323-
fail("discoverable() timeout: " +
324-
"scanMode=" + scanMode + " (expected " +
325-
BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE + ") " +
326-
"flags=" + firedFlags + " (expected " + mask + ")");
351+
fail(String.format("discoverable() timeout: scanMode=%d (expected %d), flags=0x%x "
352+
+ "(expected 0x%x)", scanMode, BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE,
353+
firedFlags, mask));
327354
}
328355

329356
private void undiscoverable(BluetoothAdapter adapter) {
@@ -348,6 +375,8 @@ private void undiscoverable(BluetoothAdapter adapter) {
348375
if (scanMode == BluetoothAdapter.SCAN_MODE_CONNECTABLE) {
349376
if ((mReceiver.getFiredFlags() & mask) == mask) {
350377
mReceiver.resetFiredFlags();
378+
writeOutput(String.format("undiscoverable() completed in %d ms",
379+
(System.currentTimeMillis() - s)));
351380
return;
352381
}
353382
} else {
@@ -358,10 +387,9 @@ private void undiscoverable(BluetoothAdapter adapter) {
358387

359388
int firedFlags = mReceiver.getFiredFlags();
360389
mReceiver.resetFiredFlags();
361-
fail("undiscoverable() timeout: " +
362-
"scanMode=" + scanMode + " (expected " +
363-
BluetoothAdapter.SCAN_MODE_CONNECTABLE + ") " +
364-
"flags=" + firedFlags + " (expected " + mask + ")");
390+
fail(String.format("undiscoverable() timeout: scanMode=%d (expected %d), flags=0x%x "
391+
+ "(expected 0x%x)", scanMode, BluetoothAdapter.SCAN_MODE_CONNECTABLE, firedFlags,
392+
mask));
365393
}
366394

367395
private void startScan(BluetoothAdapter adapter) {
@@ -382,16 +410,17 @@ private void startScan(BluetoothAdapter adapter) {
382410
while (System.currentTimeMillis() - s < START_DISCOVERY_TIMEOUT) {
383411
if (adapter.isDiscovering() && ((mReceiver.getFiredFlags() & mask) == mask)) {
384412
mReceiver.resetFiredFlags();
413+
writeOutput(String.format("startScan() completed in %d ms",
414+
(System.currentTimeMillis() - s)));
385415
return;
386416
}
387417
sleep(POLL_TIME);
388418
}
389419

390420
int firedFlags = mReceiver.getFiredFlags();
391421
mReceiver.resetFiredFlags();
392-
fail("startScan() timeout: " +
393-
"isDiscovering=" + adapter.isDiscovering() + " " +
394-
"flags=" + firedFlags + " (expected " + mask + ")");
422+
fail(String.format("startScan() timeout: isDiscovering=%b, flags=0x%x (expected 0x%x)",
423+
adapter.isDiscovering(), firedFlags, mask));
395424
}
396425

397426
private void stopScan(BluetoothAdapter adapter) {
@@ -414,16 +443,31 @@ private void stopScan(BluetoothAdapter adapter) {
414443
while (System.currentTimeMillis() - s < CANCEL_DISCOVERY_TIMEOUT) {
415444
if (!adapter.isDiscovering() && ((mReceiver.getFiredFlags() & mask) == mask)) {
416445
mReceiver.resetFiredFlags();
446+
writeOutput(String.format("stopScan() completed in %d ms",
447+
(System.currentTimeMillis() - s)));
417448
return;
418449
}
419450
sleep(POLL_TIME);
420451
}
421452

422453
int firedFlags = mReceiver.getFiredFlags();
423454
mReceiver.resetFiredFlags();
424-
fail("stopScan() timeout: " +
425-
"isDiscovering=" + adapter.isDiscovering() + " " +
426-
"flags=" + firedFlags + " (expected " + mask + ")");
455+
fail(String.format("stopScan() timeout: isDiscovering=%b, flags=0x%x (expected 0x%x)",
456+
adapter.isDiscovering(), firedFlags, mask));
457+
458+
}
459+
460+
private void writeOutput(String s) {
461+
if (mOutputWriter == null) {
462+
return;
463+
}
464+
try {
465+
Log.i(TAG, s);
466+
mOutputWriter.write(s + "\n");
467+
mOutputWriter.flush();
468+
} catch (IOException e) {
469+
Log.w(TAG, "Could not write to output file", e);
470+
}
427471
}
428472

429473
private void sleep(long time) {
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
* Copyright (C) 2010 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.bluetooth;
18+
19+
import junit.framework.TestSuite;
20+
21+
import android.os.Bundle;
22+
import android.test.InstrumentationTestRunner;
23+
import android.test.InstrumentationTestSuite;
24+
25+
public class BluetoothTestRunner extends InstrumentationTestRunner {
26+
public static int sEnableIterations = 100;
27+
public static int sDiscoverableIterations = 1000;
28+
public static int sScanIterations = 1000;
29+
30+
@Override
31+
public TestSuite getAllTests() {
32+
TestSuite suite = new InstrumentationTestSuite(this);
33+
suite.addTestSuite(BluetoothStressTest.class);
34+
return suite;
35+
}
36+
37+
@Override
38+
public ClassLoader getLoader() {
39+
return BluetoothTestRunner.class.getClassLoader();
40+
}
41+
42+
@Override
43+
public void onCreate(Bundle arguments) {
44+
super.onCreate(arguments);
45+
46+
String val = arguments.getString("enable_iterations");
47+
if (val != null) {
48+
try {
49+
sEnableIterations = Integer.parseInt(val);
50+
} catch (NumberFormatException e) {
51+
// Invalid argument, fall back to default value
52+
}
53+
}
54+
55+
val = arguments.getString("discoverable_iterations");
56+
if (val != null) {
57+
try {
58+
sDiscoverableIterations = Integer.parseInt(val);
59+
} catch (NumberFormatException e) {
60+
// Invalid argument, fall back to default value
61+
}
62+
}
63+
64+
val = arguments.getString("scan_iterations");
65+
if (val != null) {
66+
try {
67+
sScanIterations = Integer.parseInt(val);
68+
} catch (NumberFormatException e) {
69+
// Invalid argument, fall back to default value
70+
}
71+
}
72+
}
73+
}

0 commit comments

Comments
 (0)