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