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.view.MotionEvent; 25 import android.view.View; 26 import android.view.Window; 27 import android.widget.ListView; 28 29 import com.android.cts.verifier.TestListAdapter.TestListItem; 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 Intent intent = item.intent; 59 intent.putExtra(TestResult.TEST_START_TIME, mStartTime); 60 return item.intent; 61 } 62 63 @Override onActivityResult(int requestCode, int resultCode, Intent data)64 protected final void onActivityResult(int requestCode, int resultCode, Intent data) { 65 super.onActivityResult(requestCode, resultCode, data); 66 handleActivityResult(requestCode, resultCode, data); 67 } 68 69 /** Override this in subclasses instead of onActivityResult */ handleActivityResult(int requestCode, int resultCode, Intent data)70 protected void handleActivityResult(int requestCode, int resultCode, Intent data) { 71 switch (requestCode) { 72 case LAUNCH_TEST_REQUEST_CODE: 73 handleLaunchTestResult(resultCode, data); 74 break; 75 76 default: 77 throw new IllegalArgumentException("Unknown request code: " + requestCode); 78 } 79 } 80 81 @Override onCreate(Bundle savedInstanceState)82 protected void onCreate(Bundle savedInstanceState) { 83 super.onCreate(savedInstanceState); 84 if ((getResources().getConfiguration().uiMode & Configuration.UI_MODE_TYPE_MASK) 85 == Configuration.UI_MODE_TYPE_TELEVISION) { 86 getWindow().requestFeature(Window.FEATURE_OPTIONS_PANEL); 87 } 88 setContentView(R.layout.list_content); 89 } 90 handleLaunchTestResult(int resultCode, Intent data)91 protected void handleLaunchTestResult(int resultCode, Intent data) { 92 // The mStartTime can be the initial 0 if this Activity has been recreated. 93 if (mStartTime == 0 && data.hasExtra(TestResult.TEST_START_TIME)) { 94 mStartTime = data.getLongExtra(TestResult.TEST_START_TIME, 0); 95 } 96 97 if (resultCode == RESULT_OK) { 98 // If subtest didn't set end time, set current time 99 if (mEndTime == 0) { 100 mEndTime = System.currentTimeMillis(); 101 } 102 TestResult testResult = TestResult.fromActivityResult(resultCode, data); 103 testResult.getHistoryCollection().add( 104 testResult.getName(), mStartTime, mEndTime, mIsAutomated); 105 mAdapter.setTestResult(testResult); 106 } 107 // Reset end time to avoid keeping same end time in retry. 108 mEndTime = 0; 109 // Reset mIsAutomated flag to false 110 mIsAutomated = false; 111 // Reset clicked coordinate. 112 mCoordinateX = DEFAULT_CLICKED_COORDINATE; 113 mCoornidateY = DEFAULT_CLICKED_COORDINATE; 114 } 115 116 /** Launch the activity when its {@link ListView} item is clicked. */ 117 @Override onListItemClick(ListView listView, View view, int position, long id)118 protected final void onListItemClick(ListView listView, View view, int position, long id) { 119 super.onListItemClick(listView, view, position, id); 120 mStartTime = System.currentTimeMillis(); 121 //Check whether the clicked coordinate is consistent with the center of the clicked Object. 122 Rect rect = new Rect(); 123 view.getGlobalVisibleRect(rect); 124 mIsAutomated = (mCoordinateX == rect.centerX()) && (mCoornidateY == rect.centerY()); 125 handleItemClick(listView, view, position, id); 126 } 127 128 /** Override this in subclasses instead of onListItemClick */ handleItemClick(ListView listView, View view, int position, long id)129 protected void handleItemClick(ListView listView, View view, int position, long id) { 130 Intent intent = getIntent(position); 131 startActivityForResult(intent, LAUNCH_TEST_REQUEST_CODE); 132 } 133 134 /** Set OnTouchListener to ListView to get the clicked Coordinate*/ setOnTouchListenerToListView()135 protected void setOnTouchListenerToListView() { 136 getListView().setOnTouchListener(null); 137 getListView().setOnTouchListener(new View.OnTouchListener(){ 138 @Override 139 public boolean onTouch(View v, MotionEvent event) { 140 if (event.getAction() == MotionEvent.ACTION_UP) { 141 mCoordinateX = event.getRawX(); 142 mCoornidateY = event.getRawY(); 143 } else { 144 // Reset clicked coordinate. 145 mCoordinateX = DEFAULT_CLICKED_COORDINATE; 146 mCoornidateY = DEFAULT_CLICKED_COORDINATE; 147 } 148 return false; 149 } 150 }); 151 } 152 } 153