Enhance CTS-Verifier to detect and record whether the test item is automated.

- Detect whether is automated of each execution.
- Add isAutomated to record the detect result of each execution.

Bug: 158059049
Test: Perform tests through both manually and automatically to verify
      that the detection mechanism works as expected.
Change-Id: Ie904337b7474f968474da505fc397686b2077a00
Merged-In: Ie904337b7474f968474da505fc397686b2077a00
diff --git a/apps/CtsVerifier/src/com/android/cts/verifier/AbstractTestListActivity.java b/apps/CtsVerifier/src/com/android/cts/verifier/AbstractTestListActivity.java
index 15f2155..70a9593 100644
--- a/apps/CtsVerifier/src/com/android/cts/verifier/AbstractTestListActivity.java
+++ b/apps/CtsVerifier/src/com/android/cts/verifier/AbstractTestListActivity.java
@@ -21,7 +21,9 @@
 import android.app.ListActivity;
 import android.content.Intent;
 import android.content.res.Configuration;
+import android.graphics.Rect;
 import android.os.Bundle;
+import android.view.MotionEvent;
 import android.view.View;
 import android.view.Window;
 import android.widget.ListView;
@@ -29,17 +31,26 @@
 /** {@link ListActivity} that displays a list of manual tests. */
 public abstract class AbstractTestListActivity extends ListActivity {
     private static final int LAUNCH_TEST_REQUEST_CODE = 9001;
+    //An invalid value which smaller than the edge of coordinate on the screen.
+    private static final float DEFAULT_CLICKED_COORDINATE = -1;
 
     protected TestListAdapter mAdapter;
-    // Start time of test item.
+    // Start time of test case.
     protected long mStartTime;
-    // End time of test item.
+    // End time of test case.
     protected long mEndTime;
+    // X-axis of clicked coordinate when entering a test case.
+    protected float mCoordinateX;
+    // Y-axis of clicked coordinate when entering a test case.
+    protected float mCoornidateY;
+    // Whether test case was executed through automation.
+    protected boolean mIsAutomated;
 
     protected void setTestListAdapter(TestListAdapter adapter) {
         mAdapter = adapter;
         setListAdapter(mAdapter);
         mAdapter.loadTestResults();
+        setOnTouchListenerToListView();
     }
 
     private Intent getIntent(int position) {
@@ -83,11 +94,16 @@
             }
             TestResult testResult = TestResult.fromActivityResult(resultCode, data);
             testResult.getHistoryCollection().add(
-                testResult.getName(), mStartTime, mEndTime);
+                testResult.getName(), mStartTime, mEndTime, mIsAutomated);
             mAdapter.setTestResult(testResult);
         }
         // Reset end time to avoid keeping same end time in retry.
         mEndTime = 0;
+        // Reset mIsAutomated flag to false
+        mIsAutomated = false;
+        // Reset clicked coordinate.
+        mCoordinateX = DEFAULT_CLICKED_COORDINATE;
+        mCoornidateY = DEFAULT_CLICKED_COORDINATE;
     }
 
     /** Launch the activity when its {@link ListView} item is clicked. */
@@ -95,6 +111,10 @@
     protected final void onListItemClick(ListView listView, View view, int position, long id) {
         super.onListItemClick(listView, view, position, id);
         mStartTime = System.currentTimeMillis();
+        //Check whether the clicked coordinate is consistent with the center of the clicked Object.
+        Rect rect = new Rect();
+        view.getGlobalVisibleRect(rect);
+        mIsAutomated = (mCoordinateX == rect.centerX()) && (mCoornidateY == rect.centerY());
         handleItemClick(listView, view, position, id);
     }
 
@@ -103,4 +123,23 @@
         Intent intent = getIntent(position);
         startActivityForResult(intent, LAUNCH_TEST_REQUEST_CODE);
     }
+
+    /** Set OnTouchListener to ListView to get the clicked Coordinate*/
+    protected void setOnTouchListenerToListView() {
+        getListView().setOnTouchListener(null);
+        getListView().setOnTouchListener(new View.OnTouchListener(){
+            @Override
+            public boolean onTouch(View v, MotionEvent event) {
+                if (event.getAction() == MotionEvent.ACTION_UP) {
+                    mCoordinateX = event.getRawX();
+                    mCoornidateY = event.getRawY();
+                } else {
+                    // Reset clicked coordinate.
+                    mCoordinateX = DEFAULT_CLICKED_COORDINATE;
+                    mCoornidateY = DEFAULT_CLICKED_COORDINATE;
+                }
+                return false;
+            }
+        });
+    }
 }
diff --git a/apps/CtsVerifier/src/com/android/cts/verifier/TestResultHistoryCollection.java b/apps/CtsVerifier/src/com/android/cts/verifier/TestResultHistoryCollection.java
index 0e7160c..f92d233 100644
--- a/apps/CtsVerifier/src/com/android/cts/verifier/TestResultHistoryCollection.java
+++ b/apps/CtsVerifier/src/com/android/cts/verifier/TestResultHistoryCollection.java
@@ -3,7 +3,6 @@
 import com.android.compatibility.common.util.TestResultHistory;
 
 import java.io.Serializable;
-import java.util.AbstractMap;
 import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
@@ -11,6 +10,7 @@
 
 public class TestResultHistoryCollection implements Serializable {
 
+    private static final long serialVersionUID = 0L;
     private final Set<TestResultHistory> mHistoryCollection = new HashSet<>();
 
     /**
@@ -23,30 +23,32 @@
     }
 
     /**
-     * Add a test result history with test name, start time and end time.
+     * Add a test result history with test name, start time, end time and isAutomated.
      *
      * @param test a string of test name.
      * @param start start time of a test.
      * @param end end time of a test.
+     * @param isAutomated whether test case was executed through automation.
      */
-    public void add(String test, long start, long end) {
-        Set<Map.Entry> duration = new HashSet<>();
-        duration.add(new AbstractMap.SimpleEntry<>(start, end));
-        mHistoryCollection.add(new TestResultHistory(test, duration));
+    public void add(String test, long start, long end, boolean isAutomated) {
+        Set<TestResultHistory.ExecutionRecord> executionRecords
+                = new HashSet<TestResultHistory.ExecutionRecord> ();
+        executionRecords.add(new TestResultHistory.ExecutionRecord(start, end, isAutomated));
+        mHistoryCollection.add(new TestResultHistory(test, executionRecords));
     }
 
     /**
-     * Add test result histories for tests containing test name and a set of execution time.
+     * Add test result histories for tests containing test name and a set of ExecutionRecords
      *
      * @param test test name.
-     * @param durations set of start and end time.
+     * @param executionRecords set of ExecutionRecords.
      */
-    public void addAll(String test, Set<Map.Entry> durations) {
-        TestResultHistory history = new TestResultHistory(test, durations);
+    public void addAll(String test, Set<TestResultHistory.ExecutionRecord> executionRecords) {
+        TestResultHistory history = new TestResultHistory(test, executionRecords);
         boolean match = false;
         for (TestResultHistory resultHistory: mHistoryCollection) {
             if (resultHistory.getTestName().equals(test)) {
-                resultHistory.getDurations().addAll(durations);
+                resultHistory.getExecutionRecords().addAll(executionRecords);
                 match = true;
                 break;
             }
@@ -63,10 +65,12 @@
      * @param resultHistoryCollection a set of test result histories.
      */
     public void merge(String prefix, TestResultHistoryCollection resultHistoryCollection) {
-       if (resultHistoryCollection != null) {
+        if (resultHistoryCollection != null) {
             resultHistoryCollection.asSet().forEach(t-> addAll(
-                prefix != null ? prefix + ":" + t.getTestName() : t.getTestName(), t.getDurations()));
-       }
+                prefix != null
+                        ? prefix + ":" + t.getTestName()
+                        : t.getTestName(), t.getExecutionRecords()));
+        }
     }
 
     /**