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 android.content.Context;
19 import android.net.wifi.WifiManager;
20 import android.net.wifi.p2p.WifiP2pManager;
21 import android.net.wifi.p2p.WifiP2pManager.Channel;
22 
23 import com.android.cts.verifier.R;
24 
25 /**
26  * A test case defines the fixture to run p2p tests. To define a test case <br>
27  *
28  * 1) implement a subclass of TestCase<br>
29  * 2) define instance variables that store the state of the fixture<br>
30  * 3) initialize the fixture state by overriding setUp if needed. At default, p2p channel
31  *  and p2p manager is initialized.<br>
32  * 4) implement test case by overriding executeTest. executeTest must return the message
33  * id to be shown in result text view.
34  * 5) clean-up after a test by overriding tearDown if needed. At default, all services
35  *  and requests are cleared.<br>
36  */
37 public abstract class TestCase {
38 
39     /*
40      * The test case id.
41      */
42     public static final String EXTRA_TEST_NAME =
43             "com.android.cts.verifier.p2p.testcase.EXTRA_TEST_NAME";
44 
45     protected static final int TIMEOUT = 25000;
46     protected static final int TIMEOUT_FOR_USER_ACTION = 60000;
47     protected static final int SUCCESS = 0;
48 
49     protected Context mContext;
50     protected String mReason;
51 
52     protected WifiP2pManager mP2pMgr;
53     protected WifiManager mWifiMgr;
54     protected Channel mChannel;
55     // this is used for multi-client test
56     protected Channel mSubChannel;
57 
58     private Thread mThread;
59     private TestCaseListener mListener;
60 
61     /**
62      * Constructor
63      * @param context Activity context cannot be null.
64      * @param handler Must be the handler of GUI thread. cannot be null.
65      * @param textView The result message to be shown. cannot be null.
66      * @param listener The test listener. can be null.
67      */
TestCase(Context context)68     public TestCase(Context context) {
69         mContext = context;
70     }
71 
72     /**
73      * Start test case.
74      *
75      * Test case is executed in another thread.
76      * After the test, the result message is shown in text view.
77      */
start(TestCaseListener listener)78     public void start(TestCaseListener listener) {
79         mListener = listener;
80 
81         stop();
82         mThread = new Thread(new Runnable() {
83             @Override
84             public void run() {
85                 mListener.onTestStarted();
86                 try {
87                     setUp();
88                 } catch(Exception e) {
89                     mListener.onTestFailed(mContext.getString(R.string.p2p_setup_error));
90                     return;
91                 }
92 
93                 try {
94                     if (executeTest()) {
95                         mListener.onTestSuccess();
96                     } else {
97                         mListener.onTestFailed(getReason());
98                     }
99                 } catch(Exception e) {
100                     e.printStackTrace();
101                     mListener.onTestFailed(
102                             mContext.getString(R.string.p2p_unexpected_error));
103                 } finally {
104                     tearDown();
105                 }
106             }});
107         mThread.start();
108     }
109 
110     /**
111      * Stop test case.
112      */
stop()113     public void stop() {
114         if (mThread != null) {
115             mThread.interrupt();
116             mThread = null;
117         }
118     }
119 
120     /**
121      * Return test name.
122      * @return test name.
123      */
getTestName()124     abstract public String getTestName();
125 
126     /**
127      * Return test id. It must be unique.
128      * @return test id.
129      */
getTestId()130     public String getTestId() {
131         return this.getClass().getName();
132     }
133 
134     /**
135      * Execute test case.
136      * @return the message id to be shown in text view.
137      * @throws InterruptedException
138      */
executeTest()139     abstract protected boolean executeTest() throws InterruptedException;
140 
141     /**
142      * Set up the test case.
143      */
setUp()144     protected void setUp() {
145         mP2pMgr = (WifiP2pManager) mContext.getSystemService(Context.WIFI_P2P_SERVICE);
146         mWifiMgr = (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE);
147         mChannel = mP2pMgr.initialize(mContext, mContext.getMainLooper(), null);
148         mSubChannel = mP2pMgr.initialize(mContext, mContext.getMainLooper(), null);
149     }
150 
151     /**
152      * Tear down the test case.
153      */
tearDown()154     protected void tearDown() {
155         mP2pMgr.clearLocalServices(mChannel, null);
156         mP2pMgr.clearServiceRequests(mChannel, null);
157         mP2pMgr.clearLocalServices(mSubChannel, null);
158         mP2pMgr.clearServiceRequests(mSubChannel, null);
159     }
160 
161     /**
162      * Notify a message to the application.
163      * @param id
164      */
notifyTestMsg(int id)165     protected void notifyTestMsg(int id) {
166         mListener.onTestMsgReceived(mContext.getString(id));
167     }
168 
169     /**
170      * Get reason for the failure.
171      * @return
172      */
getReason()173     private String getReason() {
174        if (mReason == null) {
175            return mContext.getString(R.string.p2p_unexpected_error);
176        }
177        return mReason;
178     }
179 
180     public static interface TestCaseListener {
181 
182         /**
183          * This function is invoked when the test case starts.
184          */
onTestStarted()185         public void onTestStarted();
186 
187         /**
188          * This function is invoked when the test notify a message to application.
189          * @param msg
190          */
onTestMsgReceived(String msg)191         public void onTestMsgReceived(String msg);
192 
193         /**
194          * This function is invoked when the test is success.
195          */
onTestSuccess()196         public void onTestSuccess();
197 
198         /**
199          * This function is invoked when the test is failed.
200          * @param reason
201          */
onTestFailed(String reason)202         public void onTestFailed(String reason);
203     }
204 }
205