BluetoothStressTest.java revision 23c067692be74c4fcd50cea48f9d469ba5288213
114db8d9978daf070f5e66ac8f2cdb1f4ee0dfe06Eric Rowe/*
214db8d9978daf070f5e66ac8f2cdb1f4ee0dfe06Eric Rowe * Copyright (C) 2010 The Android Open Source Project
314db8d9978daf070f5e66ac8f2cdb1f4ee0dfe06Eric Rowe *
414db8d9978daf070f5e66ac8f2cdb1f4ee0dfe06Eric Rowe * Licensed under the Apache License, Version 2.0 (the "License");
514db8d9978daf070f5e66ac8f2cdb1f4ee0dfe06Eric Rowe * you may not use this file except in compliance with the License.
614db8d9978daf070f5e66ac8f2cdb1f4ee0dfe06Eric Rowe * You may obtain a copy of the License at
714db8d9978daf070f5e66ac8f2cdb1f4ee0dfe06Eric Rowe *
814db8d9978daf070f5e66ac8f2cdb1f4ee0dfe06Eric Rowe *      http://www.apache.org/licenses/LICENSE-2.0
914db8d9978daf070f5e66ac8f2cdb1f4ee0dfe06Eric Rowe *
1014db8d9978daf070f5e66ac8f2cdb1f4ee0dfe06Eric Rowe * Unless required by applicable law or agreed to in writing, software
1114db8d9978daf070f5e66ac8f2cdb1f4ee0dfe06Eric Rowe * distributed under the License is distributed on an "AS IS" BASIS,
1214db8d9978daf070f5e66ac8f2cdb1f4ee0dfe06Eric Rowe * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1314db8d9978daf070f5e66ac8f2cdb1f4ee0dfe06Eric Rowe * See the License for the specific language governing permissions and
1414db8d9978daf070f5e66ac8f2cdb1f4ee0dfe06Eric Rowe * limitations under the License.
1514db8d9978daf070f5e66ac8f2cdb1f4ee0dfe06Eric Rowe */
1614db8d9978daf070f5e66ac8f2cdb1f4ee0dfe06Eric Rowe
1714db8d9978daf070f5e66ac8f2cdb1f4ee0dfe06Eric Rowepackage android.bluetooth;
1814db8d9978daf070f5e66ac8f2cdb1f4ee0dfe06Eric Rowe
1914db8d9978daf070f5e66ac8f2cdb1f4ee0dfe06Eric Roweimport android.content.Context;
2014db8d9978daf070f5e66ac8f2cdb1f4ee0dfe06Eric Roweimport android.test.InstrumentationTestCase;
2114db8d9978daf070f5e66ac8f2cdb1f4ee0dfe06Eric Rowe
2214db8d9978daf070f5e66ac8f2cdb1f4ee0dfe06Eric Rowepublic class BluetoothStressTest extends InstrumentationTestCase {
23e1d666b632608a2f708cb3df06c796e16d5d1717Eric Rowe    private static final String TAG = "BluetoothStressTest";
24e1d666b632608a2f708cb3df06c796e16d5d1717Eric Rowe    private static final String OUTPUT_FILE = "BluetoothStressTestOutput.txt";
2514db8d9978daf070f5e66ac8f2cdb1f4ee0dfe06Eric Rowe
26185a0b04c611a287cbd866d27b0ceff77f12d97bEric Rowe    private BluetoothTestUtils mTestUtils;
2714db8d9978daf070f5e66ac8f2cdb1f4ee0dfe06Eric Rowe
2814db8d9978daf070f5e66ac8f2cdb1f4ee0dfe06Eric Rowe    @Override
2914db8d9978daf070f5e66ac8f2cdb1f4ee0dfe06Eric Rowe    protected void setUp() throws Exception {
3014db8d9978daf070f5e66ac8f2cdb1f4ee0dfe06Eric Rowe        super.setUp();
3114db8d9978daf070f5e66ac8f2cdb1f4ee0dfe06Eric Rowe
32185a0b04c611a287cbd866d27b0ceff77f12d97bEric Rowe        Context context = getInstrumentation().getTargetContext();
33185a0b04c611a287cbd866d27b0ceff77f12d97bEric Rowe        mTestUtils = new BluetoothTestUtils(context, TAG, OUTPUT_FILE);
3414db8d9978daf070f5e66ac8f2cdb1f4ee0dfe06Eric Rowe    }
3514db8d9978daf070f5e66ac8f2cdb1f4ee0dfe06Eric Rowe
3614db8d9978daf070f5e66ac8f2cdb1f4ee0dfe06Eric Rowe    @Override
3714db8d9978daf070f5e66ac8f2cdb1f4ee0dfe06Eric Rowe    protected void tearDown() throws Exception {
3814db8d9978daf070f5e66ac8f2cdb1f4ee0dfe06Eric Rowe        super.tearDown();
3914db8d9978daf070f5e66ac8f2cdb1f4ee0dfe06Eric Rowe
40185a0b04c611a287cbd866d27b0ceff77f12d97bEric Rowe        mTestUtils.close();
4114db8d9978daf070f5e66ac8f2cdb1f4ee0dfe06Eric Rowe    }
4214db8d9978daf070f5e66ac8f2cdb1f4ee0dfe06Eric Rowe
43e1d666b632608a2f708cb3df06c796e16d5d1717Eric Rowe    public void testEnable() {
44e1d666b632608a2f708cb3df06c796e16d5d1717Eric Rowe        int iterations = BluetoothTestRunner.sEnableIterations;
45826af6266c79d76b81fe850b9cd3d351597e4adbEric Rowe        if (iterations == 0) {
46826af6266c79d76b81fe850b9cd3d351597e4adbEric Rowe            return;
47826af6266c79d76b81fe850b9cd3d351597e4adbEric Rowe        }
48826af6266c79d76b81fe850b9cd3d351597e4adbEric Rowe
4914db8d9978daf070f5e66ac8f2cdb1f4ee0dfe06Eric Rowe        BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
5014db8d9978daf070f5e66ac8f2cdb1f4ee0dfe06Eric Rowe
51e1d666b632608a2f708cb3df06c796e16d5d1717Eric Rowe        for (int i = 0; i < iterations; i++) {
52185a0b04c611a287cbd866d27b0ceff77f12d97bEric Rowe            mTestUtils.writeOutput("enable iteration " + (i + 1) + " of " + iterations);
53185a0b04c611a287cbd866d27b0ceff77f12d97bEric Rowe            mTestUtils.enable(adapter);
54185a0b04c611a287cbd866d27b0ceff77f12d97bEric Rowe            mTestUtils.disable(adapter);
5514db8d9978daf070f5e66ac8f2cdb1f4ee0dfe06Eric Rowe        }
5614db8d9978daf070f5e66ac8f2cdb1f4ee0dfe06Eric Rowe    }
5714db8d9978daf070f5e66ac8f2cdb1f4ee0dfe06Eric Rowe
5814db8d9978daf070f5e66ac8f2cdb1f4ee0dfe06Eric Rowe    public void testDiscoverable() {
59e1d666b632608a2f708cb3df06c796e16d5d1717Eric Rowe        int iterations = BluetoothTestRunner.sDiscoverableIterations;
60826af6266c79d76b81fe850b9cd3d351597e4adbEric Rowe        if (iterations == 0) {
61826af6266c79d76b81fe850b9cd3d351597e4adbEric Rowe            return;
62826af6266c79d76b81fe850b9cd3d351597e4adbEric Rowe        }
63826af6266c79d76b81fe850b9cd3d351597e4adbEric Rowe
6414db8d9978daf070f5e66ac8f2cdb1f4ee0dfe06Eric Rowe        BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
65185a0b04c611a287cbd866d27b0ceff77f12d97bEric Rowe        mTestUtils.enable(adapter);
6614db8d9978daf070f5e66ac8f2cdb1f4ee0dfe06Eric Rowe
67e1d666b632608a2f708cb3df06c796e16d5d1717Eric Rowe        for (int i = 0; i < iterations; i++) {
68185a0b04c611a287cbd866d27b0ceff77f12d97bEric Rowe            mTestUtils.writeOutput("discoverable iteration " + (i + 1) + " of " + iterations);
69185a0b04c611a287cbd866d27b0ceff77f12d97bEric Rowe            mTestUtils.discoverable(adapter);
70185a0b04c611a287cbd866d27b0ceff77f12d97bEric Rowe            mTestUtils.undiscoverable(adapter);
7114db8d9978daf070f5e66ac8f2cdb1f4ee0dfe06Eric Rowe        }
7214db8d9978daf070f5e66ac8f2cdb1f4ee0dfe06Eric Rowe
73185a0b04c611a287cbd866d27b0ceff77f12d97bEric Rowe        mTestUtils.disable(adapter);
7414db8d9978daf070f5e66ac8f2cdb1f4ee0dfe06Eric Rowe    }
7514db8d9978daf070f5e66ac8f2cdb1f4ee0dfe06Eric Rowe
7614db8d9978daf070f5e66ac8f2cdb1f4ee0dfe06Eric Rowe    public void testScan() {
77e1d666b632608a2f708cb3df06c796e16d5d1717Eric Rowe        int iterations = BluetoothTestRunner.sScanIterations;
78826af6266c79d76b81fe850b9cd3d351597e4adbEric Rowe        if (iterations == 0) {
79826af6266c79d76b81fe850b9cd3d351597e4adbEric Rowe            return;
80826af6266c79d76b81fe850b9cd3d351597e4adbEric Rowe        }
81826af6266c79d76b81fe850b9cd3d351597e4adbEric Rowe
8214db8d9978daf070f5e66ac8f2cdb1f4ee0dfe06Eric Rowe        BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
83185a0b04c611a287cbd866d27b0ceff77f12d97bEric Rowe        mTestUtils.enable(adapter);
8414db8d9978daf070f5e66ac8f2cdb1f4ee0dfe06Eric Rowe
85e1d666b632608a2f708cb3df06c796e16d5d1717Eric Rowe        for (int i = 0; i < iterations; i++) {
86185a0b04c611a287cbd866d27b0ceff77f12d97bEric Rowe            mTestUtils.writeOutput("scan iteration " + (i + 1) + " of " + iterations);
87185a0b04c611a287cbd866d27b0ceff77f12d97bEric Rowe            mTestUtils.startScan(adapter);
88185a0b04c611a287cbd866d27b0ceff77f12d97bEric Rowe            mTestUtils.stopScan(adapter);
8914db8d9978daf070f5e66ac8f2cdb1f4ee0dfe06Eric Rowe        }
9014db8d9978daf070f5e66ac8f2cdb1f4ee0dfe06Eric Rowe
91185a0b04c611a287cbd866d27b0ceff77f12d97bEric Rowe        mTestUtils.disable(adapter);
9214db8d9978daf070f5e66ac8f2cdb1f4ee0dfe06Eric Rowe    }
93826af6266c79d76b81fe850b9cd3d351597e4adbEric Rowe
94826af6266c79d76b81fe850b9cd3d351597e4adbEric Rowe    public void testPair() {
95826af6266c79d76b81fe850b9cd3d351597e4adbEric Rowe        int iterations = BluetoothTestRunner.sPairIterations;
96826af6266c79d76b81fe850b9cd3d351597e4adbEric Rowe        if (iterations == 0) {
97826af6266c79d76b81fe850b9cd3d351597e4adbEric Rowe            return;
98826af6266c79d76b81fe850b9cd3d351597e4adbEric Rowe        }
99826af6266c79d76b81fe850b9cd3d351597e4adbEric Rowe
100826af6266c79d76b81fe850b9cd3d351597e4adbEric Rowe        BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
101826af6266c79d76b81fe850b9cd3d351597e4adbEric Rowe        BluetoothDevice device = adapter.getRemoteDevice(BluetoothTestRunner.sPairAddress);
102826af6266c79d76b81fe850b9cd3d351597e4adbEric Rowe        mTestUtils.enable(adapter);
103826af6266c79d76b81fe850b9cd3d351597e4adbEric Rowe
104826af6266c79d76b81fe850b9cd3d351597e4adbEric Rowe        for (int i = 0; i < iterations; i++) {
105826af6266c79d76b81fe850b9cd3d351597e4adbEric Rowe            mTestUtils.writeOutput("pair iteration " + (i + 1) + " of " + iterations);
106826af6266c79d76b81fe850b9cd3d351597e4adbEric Rowe            mTestUtils.pair(adapter, device, BluetoothTestRunner.sPairPasskey,
10723c067692be74c4fcd50cea48f9d469ba5288213Eric Rowe                    BluetoothTestRunner.sPairPin);
10823c067692be74c4fcd50cea48f9d469ba5288213Eric Rowe            mTestUtils.unpair(adapter, device);
10923c067692be74c4fcd50cea48f9d469ba5288213Eric Rowe        }
11023c067692be74c4fcd50cea48f9d469ba5288213Eric Rowe        mTestUtils.disable(adapter);
11123c067692be74c4fcd50cea48f9d469ba5288213Eric Rowe    }
11223c067692be74c4fcd50cea48f9d469ba5288213Eric Rowe
11323c067692be74c4fcd50cea48f9d469ba5288213Eric Rowe    public void testAcceptPair() {
11423c067692be74c4fcd50cea48f9d469ba5288213Eric Rowe        int iterations = BluetoothTestRunner.sPairIterations;
11523c067692be74c4fcd50cea48f9d469ba5288213Eric Rowe        BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
11623c067692be74c4fcd50cea48f9d469ba5288213Eric Rowe        BluetoothDevice device = adapter.getRemoteDevice(BluetoothTestRunner.sPairAddress);
11723c067692be74c4fcd50cea48f9d469ba5288213Eric Rowe        mTestUtils.enable(adapter);
11823c067692be74c4fcd50cea48f9d469ba5288213Eric Rowe
11923c067692be74c4fcd50cea48f9d469ba5288213Eric Rowe        for (int i = 0; i < iterations; i++) {
12023c067692be74c4fcd50cea48f9d469ba5288213Eric Rowe            mTestUtils.writeOutput("acceptPair iteration " + (i + 1) + " of " + iterations);
12123c067692be74c4fcd50cea48f9d469ba5288213Eric Rowe            mTestUtils.acceptPair(adapter, device, BluetoothTestRunner.sPairPasskey,
122826af6266c79d76b81fe850b9cd3d351597e4adbEric Rowe                    BluetoothTestRunner.sPairPin);
123826af6266c79d76b81fe850b9cd3d351597e4adbEric Rowe            mTestUtils.unpair(adapter, device);
124826af6266c79d76b81fe850b9cd3d351597e4adbEric Rowe        }
125826af6266c79d76b81fe850b9cd3d351597e4adbEric Rowe        mTestUtils.disable(adapter);
126826af6266c79d76b81fe850b9cd3d351597e4adbEric Rowe    }
127826af6266c79d76b81fe850b9cd3d351597e4adbEric Rowe
128826af6266c79d76b81fe850b9cd3d351597e4adbEric Rowe    public void testConnectA2dp() {
129826af6266c79d76b81fe850b9cd3d351597e4adbEric Rowe        int iterations = BluetoothTestRunner.sConnectA2dpIterations;
130826af6266c79d76b81fe850b9cd3d351597e4adbEric Rowe        if (iterations == 0) {
131826af6266c79d76b81fe850b9cd3d351597e4adbEric Rowe            return;
132826af6266c79d76b81fe850b9cd3d351597e4adbEric Rowe        }
133826af6266c79d76b81fe850b9cd3d351597e4adbEric Rowe
134826af6266c79d76b81fe850b9cd3d351597e4adbEric Rowe        BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
135826af6266c79d76b81fe850b9cd3d351597e4adbEric Rowe        BluetoothDevice device = adapter.getRemoteDevice(BluetoothTestRunner.sA2dpAddress);
136826af6266c79d76b81fe850b9cd3d351597e4adbEric Rowe        mTestUtils.enable(adapter);
137826af6266c79d76b81fe850b9cd3d351597e4adbEric Rowe        mTestUtils.pair(adapter, device, BluetoothTestRunner.sPairPasskey,
138826af6266c79d76b81fe850b9cd3d351597e4adbEric Rowe                BluetoothTestRunner.sPairPin);
139826af6266c79d76b81fe850b9cd3d351597e4adbEric Rowe
140826af6266c79d76b81fe850b9cd3d351597e4adbEric Rowe        for (int i = 0; i < iterations; i++) {
141826af6266c79d76b81fe850b9cd3d351597e4adbEric Rowe            mTestUtils.writeOutput("connectA2dp iteration " + (i + 1) + " of " + iterations);
142826af6266c79d76b81fe850b9cd3d351597e4adbEric Rowe            mTestUtils.connectProfile(adapter, device, BluetoothProfile.A2DP);
143826af6266c79d76b81fe850b9cd3d351597e4adbEric Rowe            mTestUtils.disconnectProfile(adapter, device, BluetoothProfile.A2DP);
144826af6266c79d76b81fe850b9cd3d351597e4adbEric Rowe        }
145826af6266c79d76b81fe850b9cd3d351597e4adbEric Rowe
146826af6266c79d76b81fe850b9cd3d351597e4adbEric Rowe        // TODO: Unpair from device if device can accept pairing after unpairing
147826af6266c79d76b81fe850b9cd3d351597e4adbEric Rowe        mTestUtils.disable(adapter);
148826af6266c79d76b81fe850b9cd3d351597e4adbEric Rowe    }
149826af6266c79d76b81fe850b9cd3d351597e4adbEric Rowe
150826af6266c79d76b81fe850b9cd3d351597e4adbEric Rowe    public void testConnectHeadset() {
151826af6266c79d76b81fe850b9cd3d351597e4adbEric Rowe        int iterations = BluetoothTestRunner.sConnectHeadsetIterations;
152826af6266c79d76b81fe850b9cd3d351597e4adbEric Rowe        if (iterations == 0) {
153826af6266c79d76b81fe850b9cd3d351597e4adbEric Rowe            return;
154826af6266c79d76b81fe850b9cd3d351597e4adbEric Rowe        }
155826af6266c79d76b81fe850b9cd3d351597e4adbEric Rowe
156826af6266c79d76b81fe850b9cd3d351597e4adbEric Rowe        BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
157826af6266c79d76b81fe850b9cd3d351597e4adbEric Rowe        BluetoothDevice device = adapter.getRemoteDevice(BluetoothTestRunner.sHeadsetAddress);
158826af6266c79d76b81fe850b9cd3d351597e4adbEric Rowe        mTestUtils.enable(adapter);
159826af6266c79d76b81fe850b9cd3d351597e4adbEric Rowe        mTestUtils.pair(adapter, device, BluetoothTestRunner.sPairPasskey,
160826af6266c79d76b81fe850b9cd3d351597e4adbEric Rowe                BluetoothTestRunner.sPairPin);
161826af6266c79d76b81fe850b9cd3d351597e4adbEric Rowe
162826af6266c79d76b81fe850b9cd3d351597e4adbEric Rowe        for (int i = 0; i < iterations; i++) {
163826af6266c79d76b81fe850b9cd3d351597e4adbEric Rowe            mTestUtils.writeOutput("connectHeadset iteration " + (i + 1) + " of " + iterations);
164826af6266c79d76b81fe850b9cd3d351597e4adbEric Rowe            mTestUtils.connectProfile(adapter, device, BluetoothProfile.HEADSET);
165826af6266c79d76b81fe850b9cd3d351597e4adbEric Rowe            mTestUtils.disconnectProfile(adapter, device, BluetoothProfile.HEADSET);
166826af6266c79d76b81fe850b9cd3d351597e4adbEric Rowe        }
167826af6266c79d76b81fe850b9cd3d351597e4adbEric Rowe
168826af6266c79d76b81fe850b9cd3d351597e4adbEric Rowe        // TODO: Unpair from device if device can accept pairing after unpairing
169826af6266c79d76b81fe850b9cd3d351597e4adbEric Rowe        mTestUtils.disable(adapter);
170826af6266c79d76b81fe850b9cd3d351597e4adbEric Rowe    }
17114db8d9978daf070f5e66ac8f2cdb1f4ee0dfe06Eric Rowe}
172