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