1 /*
2  * Copyright (C) 2011 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;
18 
19 import com.android.cts.verifier.TestListAdapter.TestListItem;
20 
21 import android.app.ListActivity;
22 import android.content.Intent;
23 import android.content.res.Configuration;
24 import android.graphics.Rect;
25 import android.os.Bundle;
26 import android.view.MotionEvent;
27 import android.view.View;
28 import android.view.Window;
29 import android.widget.ListView;
30 
31 /** {@link ListActivity} that displays a list of manual tests. */
32 public abstract class AbstractTestListActivity extends ListActivity {
33     private static final int LAUNCH_TEST_REQUEST_CODE = 9001;
34     //An invalid value which smaller than the edge of coordinate on the screen.
35     private static final float DEFAULT_CLICKED_COORDINATE = -1;
36 
37     protected TestListAdapter mAdapter;
38     // Start time of test case.
39     protected long mStartTime;
40     // End time of test case.
41     protected long mEndTime;
42     // X-axis of clicked coordinate when entering a test case.
43     protected float mCoordinateX;
44     // Y-axis of clicked coordinate when entering a test case.
45     protected float mCoornidateY;
46     // Whether test case was executed through automation.
47     protected boolean mIsAutomated;
48 
setTestListAdapter(TestListAdapter adapter)49     protected void setTestListAdapter(TestListAdapter adapter) {
50         mAdapter = adapter;
51         setListAdapter(mAdapter);
52         mAdapter.loadTestResults();
53         setOnTouchListenerToListView();
54     }
55 
getIntent(int position)56     private Intent getIntent(int position) {
57         TestListItem item = mAdapter.getItem(position);
58         return item.intent;
59     }
60 
61     @Override
onActivityResult(int requestCode, int resultCode, Intent data)62     protected final void onActivityResult(int requestCode, int resultCode, Intent data) {
63         super.onActivityResult(requestCode, resultCode, data);
64         handleActivityResult(requestCode, resultCode, data);
65     }
66 
67     /** Override this in subclasses instead of onActivityResult */
handleActivityResult(int requestCode, int resultCode, Intent data)68     protected void handleActivityResult(int requestCode, int resultCode, Intent data) {
69         switch (requestCode) {
70             case LAUNCH_TEST_REQUEST_CODE:
71                 handleLaunchTestResult(resultCode, data);
72                 break;
73 
74             default:
75                 throw new IllegalArgumentException("Unknown request code: " + requestCode);
76         }
77     }
78 
79     @Override
onCreate(Bundle savedInstanceState)80     protected void onCreate(Bundle savedInstanceState) {
81         super.onCreate(savedInstanceState);
82         if ((getResources().getConfiguration().uiMode & Configuration.UI_MODE_TYPE_MASK)
83               == Configuration.UI_MODE_TYPE_TELEVISION) {
84             getWindow().requestFeature(Window.FEATURE_OPTIONS_PANEL);
85         }
86         setContentView(R.layout.list_content);
87     }
88 
handleLaunchTestResult(int resultCode, Intent data)89     protected void handleLaunchTestResult(int resultCode, Intent data) {
90         if (resultCode == RESULT_OK) {
91             // If subtest didn't set end time, set current time
92             if (mEndTime == 0) {
93                 mEndTime = System.currentTimeMillis();
94             }
95             TestResult testResult = TestResult.fromActivityResult(resultCode, data);
96             testResult.getHistoryCollection().add(
97                 testResult.getName(), mStartTime, mEndTime, mIsAutomated);
98             mAdapter.setTestResult(testResult);
99         }
100         // Reset end time to avoid keeping same end time in retry.
101         mEndTime = 0;
102         // Reset mIsAutomated flag to false
103         mIsAutomated = false;
104         // Reset clicked coordinate.
105         mCoordinateX = DEFAULT_CLICKED_COORDINATE;
106         mCoornidateY = DEFAULT_CLICKED_COORDINATE;
107     }
108 
109     /** Launch the activity when its {@link ListView} item is clicked. */
110     @Override
onListItemClick(ListView listView, View view, int position, long id)111     protected final void onListItemClick(ListView listView, View view, int position, long id) {
112         super.onListItemClick(listView, view, position, id);
113         mStartTime = System.currentTimeMillis();
114         //Check whether the clicked coordinate is consistent with the center of the clicked Object.
115         Rect rect = new Rect();
116         view.getGlobalVisibleRect(rect);
117         mIsAutomated = (mCoordinateX == rect.centerX()) && (mCoornidateY == rect.centerY());
118         handleItemClick(listView, view, position, id);
119     }
120 
121     /** Override this in subclasses instead of onListItemClick */
handleItemClick(ListView listView, View view, int position, long id)122     protected void handleItemClick(ListView listView, View view, int position, long id) {
123         Intent intent = getIntent(position);
124         startActivityForResult(intent, LAUNCH_TEST_REQUEST_CODE);
125     }
126 
127     /** Set OnTouchListener to ListView to get the clicked Coordinate*/
setOnTouchListenerToListView()128     protected void setOnTouchListenerToListView() {
129         getListView().setOnTouchListener(null);
130         getListView().setOnTouchListener(new View.OnTouchListener(){
131             @Override
132             public boolean onTouch(View v, MotionEvent event) {
133                 if (event.getAction() == MotionEvent.ACTION_UP) {
134                     mCoordinateX = event.getRawX();
135                     mCoornidateY = event.getRawY();
136                 } else {
137                     // Reset clicked coordinate.
138                     mCoordinateX = DEFAULT_CLICKED_COORDINATE;
139                     mCoornidateY = DEFAULT_CLICKED_COORDINATE;
140                 }
141                 return false;
142             }
143         });
144     }
145 }
146