1 /*
2  * Copyright (C) 2012 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 com.android.cts.verifier.p2p.testcase;
18 
19 import java.util.ArrayList;
20 import java.util.List;
21 
22 import android.net.wifi.p2p.WifiP2pDevice;
23 import android.net.wifi.p2p.WifiP2pManager.UpnpServiceResponseListener;
24 import android.util.Log;
25 
26 /**
27  * The utility class for testing
28  * android.net.wifi.p2p.WifiP2pManager.UpnpServiceResponseListener callback function.
29  */
30 public class UPnPServiceResponseListenerTest extends ListenerTest
31     implements UpnpServiceResponseListener {
32 
33     private static final String TAG = "UPnPServiceResponseListenerTest";
34 
35     public static final List<ListenerArgument> NO_UPNP_SERVICES
36             = new ArrayList<ListenerArgument>();
37 
38     public static final List<ListenerArgument> ALL_UPNP_SERVICES
39             = new ArrayList<ListenerArgument>();
40 
41     public static final List<ListenerArgument> UPNP_ROOT_DEVICE
42             = new ArrayList<ListenerArgument>();
43 
44     /**
45      * The target device address.
46      */
47     private String mTargetAddr;
48 
49     static {
initialize()50         initialize();
51     }
52 
UPnPServiceResponseListenerTest(String targetAddr)53     public UPnPServiceResponseListenerTest(String targetAddr) {
54         mTargetAddr = targetAddr;
55     }
56 
57     @Override
onUpnpServiceAvailable(List<String> uniqueServiceNames, WifiP2pDevice srcDevice)58     public void onUpnpServiceAvailable(List<String> uniqueServiceNames,
59             WifiP2pDevice srcDevice) {
60         Log.d(TAG, uniqueServiceNames + " received from " + srcDevice.deviceAddress);
61 
62         /*
63          * Check only the response from the target device.
64          * The response from other devices are ignored.
65          */
66         if (srcDevice.deviceAddress.equalsIgnoreCase(mTargetAddr)) {
67             for (String uniqueServiceName : uniqueServiceNames) {
68                 receiveCallback(new Argument(uniqueServiceName));
69             }
70         }
71     }
72 
initialize()73     private static void initialize() {
74         String uuid = "uuid:6859dede-8574-59ab-9332-123456789011";
75         ALL_UPNP_SERVICES.add(new Argument(uuid));
76         ALL_UPNP_SERVICES.add(new Argument(uuid +
77                 "::upnp:rootdevice"));
78         ALL_UPNP_SERVICES.add(new Argument(uuid +
79                 "::urn:schemas-upnp-org:device:MediaRenderer:1"));
80         ALL_UPNP_SERVICES.add(new Argument(uuid +
81                 "::urn:schemas-upnp-org:service:AVTransport:1"));
82         ALL_UPNP_SERVICES.add(new Argument(uuid +
83                 "::urn:schemas-upnp-org:service:ConnectionManager:1"));
84 
85         UPNP_ROOT_DEVICE.add(new Argument(uuid +
86                 "::upnp:rootdevice"));
87     }
88 
89     /**
90      * The container of the argument of {@link #onUpnpServiceAvailable}.
91      */
92     static class Argument extends ListenerArgument {
93         String mUniqueServiceName;
94 
Argument(String uniqueServiceName)95         Argument(String uniqueServiceName) {
96             mUniqueServiceName = uniqueServiceName;
97         }
98 
99         @Override
equals(Object obj)100         public boolean equals(Object obj) {
101             if (obj == null || !(obj instanceof Argument)) {
102                 return false;
103             }
104             Argument arg = (Argument)obj;
105             return equals(mUniqueServiceName, arg.mUniqueServiceName);
106         }
107 
equals(String s1, String s2)108         private boolean equals(String s1, String s2) {
109             if (s1 == null && s2 == null) {
110                 return true;
111             }
112             if (s1 == null || s2 == null) {
113                 return false;
114             }
115             return s1.equals(s2);
116         }
117 
118         @Override
toString()119         public String toString() {
120             return mUniqueServiceName.toString();
121         }
122     }
123 }
124