1 /*
2  * Copyright (C) 2018 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 android.content.Context;
19 import android.net.wifi.p2p.WifiP2pConfig;
20 import android.net.wifi.p2p.WifiP2pInfo;
21 
22 import com.android.cts.verifier.R;
23 
24 /**
25  * A test case which accepts a connection from p2p client.
26  *
27  * The requester device tries to join this device.
28  */
29 public class GoWithConfigTestCase extends TestCase {
30 
31     protected P2pBroadcastReceiverTest mReceiverTest;
32 
33     private int mGroupOperatingBand = WifiP2pConfig.GROUP_OWNER_BAND_AUTO;
34     private int mGroupOperatingFrequency = 0;
35     private String mNetworkName = "DIRECT-XY-HELLO";
36 
GoWithConfigTestCase(Context context)37     public GoWithConfigTestCase(Context context) {
38         super(context);
39     }
40 
GoWithConfigTestCase(Context context, int bandOrFrequency)41     public GoWithConfigTestCase(Context context, int bandOrFrequency) {
42         super(context);
43         StringBuilder builder = new StringBuilder(mNetworkName);
44         switch(bandOrFrequency) {
45             case WifiP2pConfig.GROUP_OWNER_BAND_AUTO:
46                 break;
47             case WifiP2pConfig.GROUP_OWNER_BAND_2GHZ:
48                 builder.append("-2.4G");
49                 mGroupOperatingBand = bandOrFrequency;
50                 break;
51             case WifiP2pConfig.GROUP_OWNER_BAND_5GHZ:
52                 builder.append("-5G");
53                 mGroupOperatingBand = bandOrFrequency;
54                 break;
55             default:
56                 builder.append("-" + bandOrFrequency + "MHz");
57                 mGroupOperatingFrequency = bandOrFrequency;
58         }
59         mNetworkName = builder.toString();
60     }
61 
62     @Override
setUp()63     protected void setUp() {
64         super.setUp();
65         mReceiverTest = new P2pBroadcastReceiverTest(mContext);
66         mReceiverTest.init(mChannel);
67     }
68 
69     @Override
executeTest()70     protected boolean executeTest() throws InterruptedException {
71 
72         ActionListenerTest listener = new ActionListenerTest();
73 
74         /*
75          * Add renderer service
76          */
77         mP2pMgr.addLocalService(mChannel, LocalServices.createRendererService(),
78                 listener);
79         if (!listener.check(ActionListenerTest.SUCCESS, TIMEOUT)) {
80             mReason = mContext.getString(R.string.p2p_add_local_service_error);
81             return false;
82         }
83 
84         /*
85          * Add IPP service
86          */
87         mP2pMgr.addLocalService(mChannel, LocalServices.createIppService(),
88                 listener);
89         if (!listener.check(ActionListenerTest.SUCCESS, TIMEOUT)) {
90             mReason = mContext.getString(R.string.p2p_add_local_service_error);
91             return false;
92         }
93 
94         /*
95          * Add AFP service
96          */
97         mP2pMgr.addLocalService(mChannel, LocalServices.createAfpService(),
98                 listener);
99         if (!listener.check(ActionListenerTest.SUCCESS, TIMEOUT)) {
100             mReason = mContext.getString(R.string.p2p_add_local_service_error);
101             return false;
102         }
103 
104         /*
105          * Start up an autonomous group owner.
106          */
107         WifiP2pConfig.Builder b = new WifiP2pConfig.Builder()
108                 .setNetworkName(mNetworkName)
109                 .setPassphrase("DEADBEEF");
110         if (mGroupOperatingBand > 0) {
111             b.setGroupOperatingBand(mGroupOperatingBand);
112         } else if (mGroupOperatingFrequency > 0) {
113             b.setGroupOperatingFrequency(mGroupOperatingFrequency);
114         }
115         WifiP2pConfig config = b.build();
116         mP2pMgr.createGroup(mChannel, config, listener);
117         if (!listener.check(ActionListenerTest.SUCCESS, TIMEOUT)) {
118             mReason = mContext.getString(R.string.p2p_ceate_group_error);
119             return false;
120         }
121 
122         /*
123          * Check whether createGroup() is succeeded.
124          */
125         WifiP2pInfo info = mReceiverTest.waitConnectionNotice(TIMEOUT);
126         if (info == null || !info.isGroupOwner) {
127             mReason = mContext.getString(R.string.p2p_ceate_group_error);
128             return false;
129         }
130 
131         // wait until p2p client is joining.
132         return true;
133     }
134 
135     @Override
tearDown()136     protected void tearDown() {
137 
138         // wait until p2p client is joining.
139         synchronized (this) {
140             try {
141                 wait();
142             } catch (InterruptedException e) {
143                 e.printStackTrace();
144             }
145         }
146 
147         if (mP2pMgr != null) {
148             mP2pMgr.cancelConnect(mChannel, null);
149             mP2pMgr.removeGroup(mChannel, null);
150         }
151         if (mReceiverTest != null) {
152             mReceiverTest.close();
153         }
154         super.tearDown();
155     }
156 
157     @Override
getTestName()158     public String getTestName() {
159         String testName = "Accept client connection test";
160         if (mGroupOperatingBand > 0) {
161             testName += " with " + mGroupOperatingBand + "G band";
162         } else if (mGroupOperatingFrequency > 0) {
163             testName += " with " + mGroupOperatingFrequency + " MHz";
164         }
165         return testName;
166     }
167 }
168