Home | History | Annotate | Download | only in testcase
      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.DnsSdServiceResponseListener;
     24 import android.util.Log;
     25 
     26 /**
     27  * The utility class for testing
     28  * android.net.wifi.p2p.WifiP2pManager.DnsSdServiceResponseListener callback function.
     29  */
     30 public class DnsSdResponseListenerTest extends ListenerTest
     31     implements DnsSdServiceResponseListener {
     32 
     33     private static final String TAG = "DnsSdResponseListenerTest";
     34 
     35     public static final List<ListenerArgument> NO_DNS_PTR
     36             = new ArrayList<ListenerArgument>();
     37 
     38     public static final List<ListenerArgument> ALL_DNS_PTR
     39             = new ArrayList<ListenerArgument>();
     40 
     41     public static final List<ListenerArgument> IPP_DNS_PTR
     42             = new ArrayList<ListenerArgument>();
     43 
     44     public static final List<ListenerArgument> AFP_DNS_PTR
     45             = new ArrayList<ListenerArgument>();
     46 
     47     /**
     48      * The target device address.
     49      */
     50     private String mTargetAddr;
     51 
     52     static {
     53         initialize();
     54     }
     55 
     56     public DnsSdResponseListenerTest(String targetAddr) {
     57         mTargetAddr = targetAddr;
     58     }
     59 
     60     @Override
     61     public void onDnsSdServiceAvailable(String instanceName,
     62             String registrationType, WifiP2pDevice srcDevice) {
     63         Log.d(TAG, instanceName + " " + registrationType +
     64                 " received from " + srcDevice.deviceAddress);
     65 
     66         /*
     67          * Check only the response from the target device.
     68          * The response from other devices are ignored.
     69          */
     70         if (srcDevice.deviceAddress.equalsIgnoreCase(mTargetAddr)) {
     71             receiveCallback(new Argument(instanceName, registrationType));
     72         }
     73     }
     74 
     75     private static void initialize() {
     76         String ippInstanceName = "MyPrinter";
     77         String ippRegistrationType = "_ipp._tcp.local.";
     78         String afpInstanceName = "Example";
     79         String afpRegistrationType = "_afpovertcp._tcp.local.";
     80 
     81         IPP_DNS_PTR.add(new Argument(ippInstanceName, ippRegistrationType));
     82         AFP_DNS_PTR.add(new Argument(afpInstanceName, afpRegistrationType));
     83         ALL_DNS_PTR.add(new Argument(ippInstanceName, ippRegistrationType));
     84         ALL_DNS_PTR.add(new Argument(afpInstanceName, afpRegistrationType));
     85     }
     86 
     87     /**
     88      * The container of the argument of {@link #onDnsSdServiceAvailable}.
     89      */
     90     static class Argument extends ListenerArgument {
     91 
     92         private String mInstanceName;
     93         private String mRegistrationType;
     94 
     95         /**
     96          * Set the argument of {@link #onDnsSdServiceAvailable}.
     97          *
     98          * @param instanceName instance name.
     99          * @param registrationType registration type.
    100          */
    101         Argument(String instanceName, String registrationType) {
    102             mInstanceName = instanceName;
    103             mRegistrationType = registrationType;
    104         }
    105 
    106         @Override
    107         public boolean equals(Object obj) {
    108             if (obj == null || !(obj instanceof Argument)) {
    109                 return false;
    110             }
    111             Argument arg = (Argument)obj;
    112             return equals(mInstanceName, arg.mInstanceName) &&
    113                 equals(mRegistrationType, arg.mRegistrationType);
    114         }
    115 
    116         private boolean equals(String s1, String s2) {
    117             if (s1 == null && s2 == null) {
    118                 return true;
    119             }
    120             if (s1 == null || s2 == null) {
    121                 return false;
    122             }
    123             return s1.equals(s2);
    124         }
    125 
    126         @Override
    127         public String toString() {
    128             StringBuilder sb = new StringBuilder();
    129             sb.append("[type=dns_ptr instant_name=");
    130             sb.append(mInstanceName);
    131             sb.append(" registration type=");
    132             sb.append(mRegistrationType);
    133             sb.append("\n");
    134 
    135             return "instanceName=" + mInstanceName +
    136                     " registrationType=" + mRegistrationType;
    137         }
    138     }
    139 }
    140