Start using the NameMangleListener and write a test

Change-Id: Iccdb088c4cbb009524c1f6793d1acc2b09db2ee0
diff --git a/Android.mk b/Android.mk
index b4139e3..faef02f 100644
--- a/Android.mk
+++ b/Android.mk
@@ -47,7 +47,7 @@
 # Note that this is incompatible with `make dist`.  If you want to make
 # the distribution, you must run `tapas` with the individual target names.
 .PHONY: tradefed-all
-tradefed-all: tradefed tradefed-tests tf-prod-tests
+tradefed-all: tradefed tradefed-tests tf-prod-tests tf-prod-metatests
 
 # ====================================
 include $(CLEAR_VARS)
diff --git a/prod-tests/src/com/android/continuous/SmokeTest.java b/prod-tests/src/com/android/continuous/SmokeTest.java
index 34a082e..eaa3217 100644
--- a/prod-tests/src/com/android/continuous/SmokeTest.java
+++ b/prod-tests/src/com/android/continuous/SmokeTest.java
@@ -16,6 +16,7 @@
 
 package com.android.continuous;
 
+import com.android.ddmlib.testrunner.TestIdentifier;
 import com.android.tradefed.config.Option;
 import com.android.tradefed.config.Option.Importance;
 import com.android.tradefed.config.OptionClass;
@@ -28,6 +29,7 @@
 import com.android.tradefed.result.DeviceFileReporter;
 import com.android.tradefed.result.ITestInvocationListener;
 import com.android.tradefed.result.LogDataType;
+import com.android.tradefed.result.NameMangleListener;
 import com.android.tradefed.testtype.InstrumentationTest;
 
 import java.util.LinkedHashMap;
@@ -51,15 +53,41 @@
      */
     @Override
     public void run(final ITestInvocationListener listener) throws DeviceNotAvailableException {
+        // trimListener should be the first thing to receive any results.  It will pass the results
+        // through to the bugListener, which will forward them (after collecting any necessary
+        // bugreports) to the real Listener(s).
         final BugreportCollector bugListener = new BugreportCollector(listener, getDevice());
         bugListener.addPredicate(BugreportCollector.AFTER_FAILED_TESTCASES);
+        final ITestInvocationListener trimListener = new TrimListener(bugListener);
 
-        super.run(bugListener);
+        super.run(trimListener);
 
-        final DeviceFileReporter dfr = new DeviceFileReporter(getDevice(), bugListener);
+        final DeviceFileReporter dfr = new DeviceFileReporter(getDevice(), trimListener);
         dfr.addPatterns(mUploadFilePatterns);
         dfr.run();
     }
 
+    /**
+     * A class to adjust the test identifiers from something like this:
+     * com.android.smoketest.SmokeTestRunner$3#com.android.voicedialer.VoiceDialerActivity
+     * To this:
+     * SmokeFAST#com.android.voicedialer.VoiceDialerActivity
+     */
+    static class TrimListener extends NameMangleListener {
+        public TrimListener(ITestInvocationListener listener) {
+            super(listener);
+        }
+
+        /**
+         * {@inheritDoc}
+         */
+        @Override
+        protected TestIdentifier mangleTestId(TestIdentifier test) {
+            final String method = test.getTestName();
+            final String klass = test.getClassName().replaceFirst(
+                    "com.android.smoketest.SmokeTestRunner(?:\\$\\d+)?", "SmokeFAST");
+            return new TestIdentifier(klass, method);
+        }
+    }
 }
 
diff --git a/prod-tests/tests/Android.mk b/prod-tests/tests/Android.mk
new file mode 100644
index 0000000..67d958f
--- /dev/null
+++ b/prod-tests/tests/Android.mk
@@ -0,0 +1,44 @@
+# Copyright (C) 2012 The Android Open Source Project
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+LOCAL_PATH := $(call my-dir)
+
+include $(CLEAR_VARS)
+
+# Only compile source java files in this lib.
+LOCAL_SRC_FILES := $(call all-java-files-under, src)
+
+#LOCAL_JAVA_RESOURCE_DIRS := res
+
+LOCAL_JAVACFLAGS += -g -Xlint
+
+LOCAL_MODULE := tf-prod-metatests
+LOCAL_MODULE_TAGS := optional
+LOCAL_STATIC_JAVA_LIBRARIES := easymock
+LOCAL_JAVA_LIBRARIES := tradefed tf-prod-tests ddmlib-prebuilt
+
+include $(BUILD_HOST_JAVA_LIBRARY)
+
+# makefile rules to copy jars to HOST_OUT/tradefed
+# so tradefed.sh can automatically add to classpath
+
+DEST_JAR := $(HOST_OUT)/tradefed/$(LOCAL_MODULE).jar
+$(DEST_JAR): $(LOCAL_BUILT_MODULE)
+	$(copy-file-to-new-target)
+
+# this dependency ensure the above rule will be executed if module is built
+$(LOCAL_INSTALLED_MODULE) : $(DEST_JAR)
+
+# Build all sub-directories
+include $(call all-makefiles-under,$(LOCAL_PATH))
diff --git a/prod-tests/tests/src/com/android/continuous/SmokeTestTest.java b/prod-tests/tests/src/com/android/continuous/SmokeTestTest.java
new file mode 100644
index 0000000..62e058d
--- /dev/null
+++ b/prod-tests/tests/src/com/android/continuous/SmokeTestTest.java
@@ -0,0 +1,51 @@
+/*
+ * Copyright (C) 2012 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.continuous;
+
+import com.android.continuous.SmokeTest.TrimListener;
+import com.android.ddmlib.testrunner.TestIdentifier;
+import com.android.tradefed.result.ITestInvocationListener;
+
+import junit.framework.TestCase;
+
+import org.easymock.EasyMock;
+
+/**
+ * Unit tests for {@link SmokeTest}.
+ */
+public class SmokeTestTest extends TestCase {
+    private ITestInvocationListener mListener = null;
+
+    @Override
+    public void setUp() {
+        mListener = EasyMock.createMock(ITestInvocationListener.class);
+    }
+
+    public void testRewrite() {
+        TrimListener trim = new TrimListener(mListener);
+        TestIdentifier in = new TestIdentifier("com.android.smoketest.SmokeTestRunner$3",
+                "com.android.voicedialer.VoiceDialerActivity");
+        TestIdentifier out = new TestIdentifier("SmokeFAST",
+                "com.android.voicedialer.VoiceDialerActivity");
+        mListener.testStarted(EasyMock.eq(out));
+
+        EasyMock.replay(mListener);
+        trim.testStarted(in);
+        EasyMock.verify(mListener);
+    }
+}
+