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