Add a higher-level test for coverage.

Review at http://gwt-code-reviews.appspot.com/1813803

Review by: skybrian@google.com

git-svn-id: https://google-web-toolkit.googlecode.com/svn/trunk@11232 8db76d5a-ed1c-0410-87a9-c151d255dfc7
diff --git a/dev/core/src/com/google/gwt/dev/js/BaselineCoverageGatherer.java b/dev/core/src/com/google/gwt/dev/js/BaselineCoverageGatherer.java
index 748c7dd..fd44cd1 100644
--- a/dev/core/src/com/google/gwt/dev/js/BaselineCoverageGatherer.java
+++ b/dev/core/src/com/google/gwt/dev/js/BaselineCoverageGatherer.java
@@ -32,6 +32,7 @@
 import com.google.gwt.dev.js.ast.JsExpression;
 import com.google.gwt.thirdparty.guava.common.base.Charsets;
 import com.google.gwt.thirdparty.guava.common.collect.HashMultimap;
+import com.google.gwt.thirdparty.guava.common.collect.ImmutableSet;
 import com.google.gwt.thirdparty.guava.common.collect.Multimap;
 import com.google.gwt.thirdparty.guava.common.collect.Sets;
 import com.google.gwt.thirdparty.guava.common.io.Files;
@@ -51,6 +52,9 @@
 
   private static Set<String> getCoveredSourceFiles() {
     String filename = System.getProperty("gwt.coverage");
+    if (filename.indexOf(',') != -1) {
+      return ImmutableSet.copyOf(filename.split(","));
+    }
     File instrumentationFile = new File(filename);
     try {
       return Sets.newHashSet(Files.readLines(instrumentationFile, Charsets.UTF_8));
@@ -134,4 +138,4 @@
     }.accept(jProgram);
     return instrumentableLines;
   }
-}
\ No newline at end of file
+}
diff --git a/user/build.xml b/user/build.xml
index 21f9801..3290ba5 100755
--- a/user/build.xml
+++ b/user/build.xml
@@ -559,6 +559,23 @@
     </gwt.junit>
   </target>
 
+  <target name="test.coverage.htmlunit"
+      depends="compile, compile.tests"
+      description="Run tests for coverage support">
+      <fileset id="test.coverage.htmlunit.tests" dir="${javac.junit.out}"
+          includes="com/google/gwt/dev/js/client/CoverageTest.class"
+          excludes="" />
+      <gwt.junit test.name="test.coverage.htmlunit"
+        test.args="${test.args} -draftCompile -out www -prod -runStyle HtmlUnit:FF3.6"
+        test.jvmargs="${test.jvmargs} -Dgwt.coverage=com/google/gwt/dev/js/client/CoverageTestModule.java,"
+        test.out="${junit.out}/coverage-htmlunit"
+        test.cases="test.coverage.htmlunit.tests" >
+      <extraclasspaths>
+        <path refid="test.extraclasspath" />
+      </extraclasspaths>
+    </gwt.junit>
+  </target>
+
   <target name="test"
       depends="compile, compile.tests"
       description="Run all tests for this project.">
@@ -593,6 +610,7 @@
       <antcall target="test.draft.htmlunit"/>
       <antcall target="test.nometa.htmlunit"/>
       <antcall target="test.nongwt"/>
+      <antcall target="test.coverage.htmlunit"/>
     </parallel>
     </limit>
   </target>
@@ -614,6 +632,7 @@
       <antcall target="test.web.htmlunit"/>
       <antcall target="test.draft.htmlunit"/>
       <antcall target="test.nometa.htmlunit"/>
+      <antcall target="test.coverage.htmlunit"/>
     </parallel>
     </limit>
   </target>
diff --git a/user/src/com/google/gwt/junit/RunStyleHtmlUnit.java b/user/src/com/google/gwt/junit/RunStyleHtmlUnit.java
index 2c18533..fcf5571 100644
--- a/user/src/com/google/gwt/junit/RunStyleHtmlUnit.java
+++ b/user/src/com/google/gwt/junit/RunStyleHtmlUnit.java
@@ -236,7 +236,7 @@
   private static Map<String, BrowserVersion> createBrowserMap() {
     Map<String, BrowserVersion> browserMap = new HashMap<String, BrowserVersion>();
     for (BrowserVersion browser : new BrowserVersion[] {
-        BrowserVersion.FIREFOX_3, BrowserVersion.INTERNET_EXPLORER_6,
+        BrowserVersion.FIREFOX_3, BrowserVersion.FIREFOX_3_6, BrowserVersion.INTERNET_EXPLORER_6,
         BrowserVersion.INTERNET_EXPLORER_7}) {
       browserMap.put(browser.getNickname(), browser);
     }
diff --git a/user/test/com/google/gwt/dev/js/CoverageTestModule.gwt.xml b/user/test/com/google/gwt/dev/js/CoverageTestModule.gwt.xml
new file mode 100644
index 0000000..83e975c
--- /dev/null
+++ b/user/test/com/google/gwt/dev/js/CoverageTestModule.gwt.xml
@@ -0,0 +1,4 @@
+<module>
+  <inherits name="com.google.gwt.user.User"/>
+  <inherits name="com.google.gwt.json.JSON"/>
+</module>
\ No newline at end of file
diff --git a/user/test/com/google/gwt/dev/js/client/CoverageTest.java b/user/test/com/google/gwt/dev/js/client/CoverageTest.java
new file mode 100644
index 0000000..77edb5b
--- /dev/null
+++ b/user/test/com/google/gwt/dev/js/client/CoverageTest.java
@@ -0,0 +1,81 @@
+/*
+ * Copyright 2012 Google Inc.
+ *
+ * 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.google.gwt.dev.js.client;
+
+import com.google.gwt.json.client.JSONNumber;
+import com.google.gwt.json.client.JSONObject;
+import com.google.gwt.json.client.JSONParser;
+import com.google.gwt.junit.client.GWTTestCase;
+import com.google.gwt.storage.client.Storage;
+
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * Tests coverage instrumentation.
+ */
+public class CoverageTest extends GWTTestCase {
+  private static final Map<String, Double> EXPECTED_COVERAGE = new HashMap<String, Double>() { {
+      put("25", 1.0);
+      put("26", 1.0);
+      put("27", 1.0);
+      put("29", 1.0);
+      put("31", 0.0);
+  }};
+
+  @Override
+  public String getModuleName() {
+    return "com.google.gwt.dev.js.CoverageTestModule";
+  }
+
+  /*
+   * Trigger the onbeforeunload handler. It would be nice to do this by refreshing the page or
+   * something, but that causes the test to fail.
+   */
+  private static native void fireOnBeforeUnloadEvent() /*-{
+    for (var i = 0; i < $wnd.frames.length; i++) {
+      if (typeof $wnd.frames[i].onbeforeunload === 'function') {
+        $wnd.frames[i].onbeforeunload();
+      }
+    }
+  }-*/;
+
+  public void testCoverageDataIsFlushedToLocalStorageOnBeforeUnload() {
+    Storage localStorage = Storage.getLocalStorageIfSupported();
+    assertNotNull("Test browser does not support localStorage", localStorage);
+    // No coverage initially
+    assertNull("Found unexpected initial coverage", localStorage.getItem("gwt_coverage"));
+
+    CoverageTestModule.method();
+
+    // Trigger the onbeforeunload handler to flush the coverage information to localStorage.
+    fireOnBeforeUnloadEvent();
+    String coverageAsJson = localStorage.getItem("gwt_coverage");
+    assertNotNull("No coverage data found", coverageAsJson);
+    JSONObject coverage = JSONParser.parseStrict(coverageAsJson).isObject();
+    assertNotNull("Coverage data was not valid JSON", coverage);
+
+    JSONObject fileCoverage =
+        coverage.get("com/google/gwt/dev/js/client/CoverageTestModule.java").isObject();
+    assertNotNull(fileCoverage);
+    for (Map.Entry<String, Double> lineCoverage : EXPECTED_COVERAGE.entrySet()) {
+      assertTrue(fileCoverage.containsKey(lineCoverage.getKey()));
+      JSONNumber value = fileCoverage.get(lineCoverage.getKey()).isNumber();
+      assertNotNull(value);
+      assertEquals(lineCoverage.getValue(), value.doubleValue(), 0.0001);
+    }
+  }
+}
diff --git a/user/test/com/google/gwt/dev/js/client/CoverageTestModule.java b/user/test/com/google/gwt/dev/js/client/CoverageTestModule.java
new file mode 100644
index 0000000..bf6d2fc
--- /dev/null
+++ b/user/test/com/google/gwt/dev/js/client/CoverageTestModule.java
@@ -0,0 +1,34 @@
+/*
+ * Copyright 2012 Google Inc.
+ *
+ * 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.google.gwt.dev.js.client;
+
+import com.google.gwt.core.client.GWT;
+
+/**
+ * Dummy class used to verify that coverage is sane.
+ */
+public class CoverageTestModule {
+  public static void method() {
+    Integer x = new Integer(42);
+    int y = x + 10;
+    if (x < y) {
+      // This comment will be ignored
+      GWT.log("This line should be covered");
+    } else {
+      GWT.log("This line should not be covered");
+    }
+  }
+}