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 package com.android.cts.verifier.p2p.testcase;
17 
18 import java.util.Timer;
19 import java.util.TimerTask;
20 
21 import android.content.Context;
22 
23 import com.android.cts.verifier.R;
24 
25 /**
26  * The service response test case.
27  *
28  * This test case sets bonjour and UPnP local services.
29  * The requester devices check whether it can search appropriate
30  * devices and services.
31  */
32 public class ServRespTestCase extends TestCase {
33 
34     private Timer mTimer;
35 
ServRespTestCase(Context context)36     public ServRespTestCase(Context context) {
37         super(context);
38     }
39 
40     @Override
setUp()41     protected void setUp() {
42         mTimer = new Timer(true);
43         super.setUp();
44     }
45 
46     @Override
executeTest()47     protected boolean executeTest() throws InterruptedException {
48         ActionListenerTest listenerTest = new ActionListenerTest();
49 
50         /*
51          * Add renderer service
52          */
53         mP2pMgr.addLocalService(mChannel, LocalServices.createRendererService(),
54                 listenerTest);
55         if (!listenerTest.check(ActionListenerTest.SUCCESS, TIMEOUT)) {
56             mReason = mContext.getString(R.string.p2p_add_local_service_error);
57             return false;
58         }
59 
60         /*
61          * Add IPP service
62          */
63         mP2pMgr.addLocalService(mChannel, LocalServices.createIppService(),
64                 listenerTest);
65         if (!listenerTest.check(ActionListenerTest.SUCCESS, TIMEOUT)) {
66             mReason = mContext.getString(R.string.p2p_add_local_service_error);
67             return false;
68         }
69 
70         /*
71          * Add AFP service
72          */
73         mP2pMgr.addLocalService(mChannel, LocalServices.createAfpService(),
74                 listenerTest);
75         if (!listenerTest.check(ActionListenerTest.SUCCESS, TIMEOUT)) {
76             mReason = mContext.getString(R.string.p2p_add_local_service_error);
77             return false;
78         }
79 
80         /*
81          * Start discover
82          */
83         mP2pMgr.discoverPeers(mChannel, listenerTest);
84         if (!listenerTest.check(ActionListenerTest.SUCCESS, TIMEOUT)) {
85             mReason = mContext.getString(R.string.p2p_discover_peers_error);
86             return false;
87         }
88 
89         /*
90          * Responder calls discoverPeers periodically.
91          */
92         mTimer.schedule(new TimerTask() {
93             @Override
94             public void run() {
95                 mP2pMgr.discoverPeers(mChannel, null);
96             }
97         }, 10000, 10000);
98 
99         return true;
100     }
101 
102 
103     @Override
tearDown()104     protected void tearDown() {
105         /*
106          * If the test is finished, local services will be unregistered.
107          * So, block the test before stop() is called.
108          */
109         synchronized(this) {
110             try {
111                 wait();
112             } catch (InterruptedException e) {
113                 e.printStackTrace();
114             }
115         }
116         if (mTimer != null) {
117             mTimer.cancel();
118         }
119         super.tearDown();
120     }
121 
122     @Override
getTestName()123     public String getTestName() {
124         return "Service discovery responder test";
125     }
126 }
127