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