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;
17 
18 import java.util.ArrayList;
19 
20 import android.content.Context;
21 import android.content.Intent;
22 import android.database.DataSetObserver;
23 import android.os.Bundle;
24 
25 import com.android.cts.verifier.ArrayTestListAdapter;
26 import com.android.cts.verifier.PassFailButtons;
27 import com.android.cts.verifier.R;
28 import com.android.cts.verifier.TestListAdapter;
29 import com.android.cts.verifier.TestListAdapter.TestListItem;
30 import com.android.cts.verifier.p2p.testcase.ReqTestCase;
31 import com.android.cts.verifier.p2p.testcase.TestCase;
32 
33 /**
34  * A base class for requester test list activity.
35  *
36  * This class provides the feature to list all the requester test cases.
37  * A requester test list activity just have to extend this class and implement
38  * getTestSuite() and getRequesterActivityClass().
39  */
40 public abstract class RequesterTestListActivity extends
41      PassFailButtons.TestListActivity {
42 
43     /**
44      * Test list view adapter
45      */
46     protected TestListAdapter mAdapter;
47 
48     /**
49      * Return test suite to be listed.
50      * @param context
51      * @return test suite
52      */
getTestSuite(Context context)53     protected abstract ArrayList<ReqTestCase> getTestSuite(Context context);
54 
55     /**
56      * Return requester activity class.
57      * @return
58      */
getRequesterActivityClass()59     protected abstract Class<?> getRequesterActivityClass();
60 
61     @Override
onCreate(Bundle savedInstanceState)62     protected void onCreate(Bundle savedInstanceState) {
63         super.onCreate(savedInstanceState);
64 
65         setContentView(R.layout.pass_fail_list);
66         setPassFailButtonClickListeners();
67         getPassButton().setEnabled(false);
68 
69         mAdapter = getTestListAdapter();
70         setTestListAdapter(mAdapter);
71     }
72 
73     /**
74      * Get test list view adapter
75      * @return
76      */
getTestListAdapter()77     private TestListAdapter getTestListAdapter() {
78         ArrayTestListAdapter adapter = new ArrayTestListAdapter(this);
79 
80         for (ReqTestCase testcase: getTestSuite(this)) {
81             addTestCase(adapter, testcase);
82         }
83 
84         adapter.registerDataSetObserver(new DataSetObserver() {
85             @Override
86             public void onChanged() {
87                 updatePassButton();
88             }
89         });
90 
91         return adapter;
92     }
93 
94     /**
95      * Add test case to test list view adapter.
96      * @param adapter
97      * @param testcase
98      */
addTestCase(ArrayTestListAdapter adapter, TestCase testcase)99     private void addTestCase(ArrayTestListAdapter adapter, TestCase testcase) {
100         Intent intent = new Intent(this, getRequesterActivityClass());
101         intent.putExtra(TestCase.EXTRA_TEST_NAME,
102                 testcase.getTestId());
103         adapter.add(TestListItem.newTest(testcase.getTestName(), testcase.getTestId(),
104                 intent, null));
105     }
106 }
107