[Coverage] Add a TOTALS row in the html report.

Adds a TOTAL row at the bottom of the table in the html report to make
the report easier to interprete.

Bug: 799503
Change-Id: I00336f035d0277005b56cf171ab4242be0c78ef1
Reviewed-on: https://chromium-review.googlesource.com/853158
Commit-Queue: Yuke Liao <[email protected]>
Reviewed-by: Abhishek Arya <[email protected]>
Reviewed-by: Max Moroz <[email protected]>
Cr-Commit-Position: refs/heads/master@{#527666}
diff --git a/tools/code_coverage/coverage.py b/tools/code_coverage/coverage.py
index faab150..a8a6a37 100755
--- a/tools/code_coverage/coverage.py
+++ b/tools/code_coverage/coverage.py
@@ -162,6 +162,7 @@
 
     self._css_absolute_path = css_absolute_path
     self._table_entries = []
+    self._total_entry = {}
     template_dir = os.path.join(
         os.path.dirname(os.path.realpath(__file__)), 'html_templates')
 
@@ -176,27 +177,43 @@
 
     The link to be added is assumed to be an entry in this directory.
     """
-    table_entry = {
-        'href':
-            html_report_path,
-        'name':
-            name,
-        'is_dir':
-            os.path.basename(html_report_path) ==
-            DIRECTORY_COVERAGE_HTML_REPORT_NAME
-    }
+    table_entry = self._CreateTableEntryFromCoverageSummary(
+        summary, html_report_path, name,
+        os.path.basename(html_report_path) ==
+        DIRECTORY_COVERAGE_HTML_REPORT_NAME)
+    self._table_entries.append(table_entry)
+
+  def CreateTotalsEntry(self, summary):
+    """Creates an entry corresponds to the 'TOTALS' row in the html report."""
+    self._total_entry = self._CreateTableEntryFromCoverageSummary(summary)
+
+  def _CreateTableEntryFromCoverageSummary(self,
+                                           summary,
+                                           href=None,
+                                           name=None,
+                                           is_dir=None):
+    """Creates an entry to display in the html report."""
+    entry = {}
     summary_dict = summary.Get()
-    for feature in summary_dict.keys():
+    for feature in summary_dict:
       percentage = round((float(summary_dict[feature]['covered']
                                ) / summary_dict[feature]['total']) * 100, 2)
       color_class = self._GetColorClass(percentage)
-      table_entry[feature] = {
+      entry[feature] = {
           'total': summary_dict[feature]['total'],
           'covered': summary_dict[feature]['covered'],
           'percentage': percentage,
           'color_class': color_class
       }
-    self._table_entries.append(table_entry)
+
+    if href != None:
+      entry['href'] = href
+    if name != None:
+      entry['name'] = name
+    if is_dir != None:
+      entry['is_dir'] = is_dir
+
+    return entry
 
   def _GetColorClass(self, percentage):
     """Returns the css color class based on coverage percentage."""
@@ -231,7 +248,8 @@
     css_path = os.path.join(OUTPUT_DIR, os.extsep.join(['style', 'css']))
     html_header = self._header_template.render(
         css_path=os.path.relpath(css_path, os.path.dirname(output_path)))
-    html_table = self._table_template.render(entries=self._table_entries)
+    html_table = self._table_template.render(
+        entries=self._table_entries, total_entry=self._total_entry)
     html_footer = self._footer_template.render()
 
     with open(output_path, 'w') as html_file:
@@ -401,6 +419,7 @@
           entry_html_report_relative_path, os.path.basename(entry_path),
           per_file_coverage_summary[entry_path])
 
+  html_generator.CreateTotalsEntry(per_directory_coverage_summary[dir_path])
   html_generator.WriteHtmlCoverageReport(html_report_path)