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.HashMap;
     21 import java.util.List;
     22 import java.util.Map;
     23 
     24 import android.net.wifi.p2p.WifiP2pDevice;
     25 import android.net.wifi.p2p.WifiP2pManager.DnsSdTxtRecordListener;
     26 import android.util.Log;
     27 
     28 /**
     29  * The utility class for
     30  * testing android.net.wifi.p2p.WifiP2pManager.DnsSdTxtRecordListener callback function.
     31  */
     32 public class DnsSdTxtRecordListenerTest extends ListenerTest
     33     implements DnsSdTxtRecordListener {
     34 
     35     private static final String TAG = "DnsSdTxtRecordListenerTest";
     36 
     37     public static final List<ListenerArgument> NO_DNS_TXT
     38             = new ArrayList<ListenerArgument>();
     39 
     40     public static final List<ListenerArgument> ALL_DNS_TXT
     41             = new ArrayList<ListenerArgument>();
     42 
     43     public static final List<ListenerArgument> IPP_DNS_TXT
     44             = new ArrayList<ListenerArgument>();
     45 
     46     public static final List<ListenerArgument> AFP_DNS_TXT
     47             = new ArrayList<ListenerArgument>();
     48 
     49     static {
     50         initialize();
     51     }
     52 
     53     /**
     54      * The target device address.
     55      */
     56     private String mTargetAddr;
     57 
     58     public DnsSdTxtRecordListenerTest(String targetAddr) {
     59         mTargetAddr = targetAddr;
     60     }
     61 
     62     @Override
     63     public void onDnsSdTxtRecordAvailable(String fullDomainName,
     64             Map<String, String> txtRecordMap, WifiP2pDevice srcDevice) {
     65         Log.d(TAG, fullDomainName + " " + txtRecordMap + " received from "
     66                 + srcDevice.deviceAddress);
     67 
     68         /*
     69          * Check only the response from the target device.
     70          * The response from other devices are ignored.
     71          */
     72         if (srcDevice.deviceAddress.equalsIgnoreCase(mTargetAddr)) {
     73             receiveCallback(new Argument(fullDomainName, txtRecordMap));
     74         }
     75     }
     76 
     77     private static void initialize() {
     78         String ippDomainName = "myprinter._ipp._tcp.local.";
     79         String afpDomainName = "example._afpovertcp._tcp.local.";
     80 
     81         Map<String, String> ippTxtRecord = new HashMap<String, String>();
     82         Map<String, String> afpTxtRecord = new HashMap<String, String>();
     83         ippTxtRecord.put("txtvers", "1");
     84         ippTxtRecord.put("pdl", "application/postscript");
     85 
     86         IPP_DNS_TXT.add(new Argument(ippDomainName, ippTxtRecord));
     87         AFP_DNS_TXT.add(new Argument(afpDomainName, afpTxtRecord));
     88         ALL_DNS_TXT.add(new Argument(ippDomainName, ippTxtRecord));
     89         ALL_DNS_TXT.add(new Argument(afpDomainName, afpTxtRecord));
     90     }
     91 
     92     /**
     93      * The container of the argument of {@link #onDnsSdTxtRecordAvailable}.
     94      */
     95     static class Argument extends ListenerArgument {
     96 
     97         private String mFullDomainName;
     98         private Map<String, String> mTxtRecordMap;
     99 
    100         /**
    101          * Set the argument of {@link #onDnsSdTxtRecordAvailable}.
    102          * @param fullDomainName full domain name.
    103          * @param txtRecordMap txt record map.
    104          */
    105         Argument(String fullDomainName, Map<String, String> txtRecordMap) {
    106             mFullDomainName = fullDomainName;
    107             mTxtRecordMap = txtRecordMap;
    108         }
    109 
    110         @Override
    111         public boolean equals(Object obj) {
    112             if (obj == null || !(obj instanceof Argument)) {
    113                 return false;
    114             }
    115             Argument arg = (Argument)obj;
    116             return equals(mFullDomainName, arg.mFullDomainName) &&
    117                     equals(mTxtRecordMap, arg.mTxtRecordMap);
    118         }
    119 
    120         private boolean equals(String s1, String s2) {
    121             if (s1 == null && s2 == null) {
    122                 return true;
    123             }
    124             if (s1 == null || s2 == null) {
    125                 return false;
    126             }
    127             return s1.equals(s2);
    128         }
    129 
    130         private boolean equals(Map<String, String> s1, Map<String, String> s2) {
    131             if (s1 == null && s2 == null) {
    132                 return true;
    133             }
    134             if (s1 == null || s2 == null) {
    135                 return false;
    136             }
    137             return s1.equals(s2);
    138         }
    139 
    140         @Override
    141         public String toString() {
    142             return "domainName=" + mFullDomainName + " record='" + mTxtRecordMap + "'";
    143         }
    144     }
    145 }
    146