1 /* 2 * Copyright (C) 2016 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 package com.android.tradefed; 17 18 import com.android.tradefed.build.IBuildInfo; 19 import com.android.tradefed.device.DeviceNotAvailableException; 20 import com.android.tradefed.device.ITestDevice; 21 import com.android.tradefed.log.LogUtil.CLog; 22 import com.android.tradefed.metrics.proto.MetricMeasurement.Metric; 23 import com.android.tradefed.result.ITestInvocationListener; 24 import com.android.tradefed.result.TestDescription; 25 import com.android.tradefed.testtype.IMultiDeviceTest; 26 import com.android.tradefed.testtype.IRemoteTest; 27 import com.android.tradefed.util.sl4a.Sl4aClient; 28 import com.android.tradefed.util.sl4a.Sl4aEventDispatcher.EventSl4aObject; 29 30 import org.json.JSONArray; 31 import org.json.JSONException; 32 import org.json.JSONObject; 33 import org.junit.Assert; 34 35 import java.io.IOException; 36 import java.util.ArrayList; 37 import java.util.HashMap; 38 import java.util.List; 39 import java.util.Map; 40 41 /** 42 * Bluetooth discovery test using Sl4A scripting layer to query some device APIs. 43 */ 44 public class Sl4aBluetoothDiscovery implements IRemoteTest, IMultiDeviceTest { 45 46 private ITestDevice mDut; 47 private ITestDevice mDiscoverer; 48 private Map<ITestDevice, IBuildInfo> mDevicesInfos; 49 50 private static final String BLUETOOTH_NAME = "TEST_NAME"; 51 52 @Override 53 public void setDeviceInfos(Map<ITestDevice, IBuildInfo> deviceInfos) { 54 mDevicesInfos = deviceInfos; 55 List<ITestDevice> listDevices = new ArrayList<>(mDevicesInfos.keySet()); 56 mDut = listDevices.get(0); 57 mDiscoverer = listDevices.get(1); 58 } 59 60 /** 61 * Setup the devices state before doing the actual test. 62 * 63 * @param clientDut sl4a client of the device under test (DUT) 64 * @param clientDiscoverer sl4a client of the device doing the discovery of the DUT 65 * @throws IOException 66 */ 67 public void setup(Sl4aClient clientDut, Sl4aClient clientDiscoverer) throws IOException { 68 clientDut.rpcCall("bluetoothToggleState", false); 69 clientDut.getEventDispatcher().clearAllEvents(); 70 clientDut.rpcCall("bluetoothToggleState", true); 71 72 clientDiscoverer.rpcCall("bluetoothToggleState", false); 73 clientDiscoverer.getEventDispatcher().clearAllEvents(); 74 clientDiscoverer.rpcCall("bluetoothToggleState", true); 75 76 EventSl4aObject response = clientDut.getEventDispatcher() 77 .popEvent("BluetoothStateChangedOn", 20000); 78 Assert.assertNotNull(response); 79 response = clientDiscoverer.getEventDispatcher() 80 .popEvent("BluetoothStateChangedOn", 20000); 81 Assert.assertNotNull(response); 82 83 Object rep = clientDut.rpcCall("bluetoothCheckState"); 84 Assert.assertEquals(true, rep); 85 86 clientDut.rpcCall("bluetoothSetLocalName", BLUETOOTH_NAME); 87 rep = clientDut.rpcCall("bluetoothGetLocalName"); 88 Assert.assertEquals(BLUETOOTH_NAME, rep); 89 } 90 91 @Override 92 public void run(ITestInvocationListener listener) throws DeviceNotAvailableException { 93 // We provide null path for the apk to assume it's already installed. 94 Sl4aClient dutClient = Sl4aClient.startSL4A(mDut, null); 95 Sl4aClient discovererClient = Sl4aClient.startSL4A(mDiscoverer, null); 96 97 TestDescription testId = 98 new TestDescription(this.getClass().getCanonicalName(), "bluetooth_discovery"); 99 100 long startTime = System.currentTimeMillis(); 101 listener.testRunStarted("sl4a_bluetooth", 1); 102 listener.testStarted(testId); 103 104 try { 105 setup(dutClient, discovererClient); 106 dutClient.rpcCall("bluetoothMakeDiscoverable"); 107 Object rep = dutClient.rpcCall("bluetoothGetScanMode"); 108 // 3 signifies CONNECTABLE and DISCOVERABLE 109 Assert.assertEquals(3, rep); 110 111 discovererClient.getEventDispatcher().clearAllEvents(); 112 discovererClient.rpcCall("bluetoothStartDiscovery"); 113 discovererClient.getEventDispatcher() 114 .popEvent("BluetoothDiscoveryFinished", 60000); 115 Object listDiscovered = discovererClient.rpcCall("bluetoothGetDiscoveredDevices"); 116 JSONArray response = (JSONArray) listDiscovered; 117 boolean found = false; 118 for (int i = 0; i < response.length(); i++) { 119 JSONObject j = response.getJSONObject(i); 120 if (j.has("name") && BLUETOOTH_NAME.equals(j.getString("name"))) { 121 found = true; 122 break; 123 } 124 } 125 if (!found) { 126 listener.testFailed(testId, "Did not find the bluetooth from DUT."); 127 } 128 } catch (IOException | JSONException e) { 129 CLog.e(e); 130 listener.testFailed(testId, "Exception " + e); 131 } finally { 132 listener.testEnded(testId, new HashMap<String, Metric>()); 133 listener.testRunEnded( 134 System.currentTimeMillis() - startTime, new HashMap<String, Metric>()); 135 dutClient.close(); 136 discovererClient.close(); 137 } 138 } 139 } 140