Upstream version 11.40.271.0
[platform/framework/web/crosswalk.git] / src / tools / telemetry / telemetry / results / csv_pivot_table_output_formatter_unittest.py
1 # Copyright 2014 The Chromium Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file.
4 import os
5 import StringIO
6 import unittest
7
8 from telemetry.page import page_set
9 from telemetry.results import csv_pivot_table_output_formatter
10 from telemetry.results import page_test_results
11 from telemetry.value import scalar
12
13
14 def _MakePageSet():
15   ps = page_set.PageSet(file_path=os.path.dirname(__file__))
16   ps.AddPageWithDefaultRunNavigate('http://www.foo.com/')
17   ps.AddPageWithDefaultRunNavigate('http://www.bar.com/')
18   return ps
19
20
21 class CsvPivotTableOutputFormatterTest(unittest.TestCase):
22
23   # The line separator used by CSV formatter.
24   _LINE_SEPARATOR = '\r\n'
25
26   def setUp(self):
27     self._output = StringIO.StringIO()
28     self._page_set = _MakePageSet()
29     self._results = page_test_results.PageTestResults()
30     self._formatter = None
31     self.MakeFormatter()
32
33   def MakeFormatter(self, trace_tag=''):
34     self._formatter = (
35         csv_pivot_table_output_formatter.CsvPivotTableOutputFormatter(
36             self._output, trace_tag))
37
38   def SimulateBenchmarkRun(self, dict_of_values):
39     """Simulate one run of a benchmark, using the supplied values.
40
41     Args:
42       dict_of_values: dictionary w/ Page instance as key, a list of Values
43           as value.
44     """
45     for page, values in dict_of_values.iteritems():
46       self._results.WillRunPage(page)
47       for v in values:
48         v.page = page
49         self._results.AddValue(v)
50       self._results.DidRunPage(page)
51
52   def Format(self):
53     self._formatter.Format(self._results)
54     return self._output.getvalue()
55
56   def testSimple(self):
57     # Test a simple benchmark with only one value:
58     self.SimulateBenchmarkRun({
59         self._page_set[0]: [scalar.ScalarValue(None, 'foo', 'seconds', 3)]})
60     expected = self._LINE_SEPARATOR.join([
61         'page_set,page,name,value,units,run_index',
62         'page_set,http://www.foo.com/,foo,3,seconds,0',
63         ''])
64
65     self.assertEqual(expected, self.Format())
66
67   def testMultiplePagesAndValues(self):
68     self.SimulateBenchmarkRun({
69         self._page_set[0]: [scalar.ScalarValue(None, 'foo', 'seconds', 4)],
70         self._page_set[1]: [scalar.ScalarValue(None, 'foo', 'seconds', 3.4),
71                             scalar.ScalarValue(None, 'bar', 'km', 10),
72                             scalar.ScalarValue(None, 'baz', 'count', 5)]})
73
74     # Parse CSV output into list of lists.
75     csv_string = self.Format()
76     lines = csv_string.split(self._LINE_SEPARATOR)
77     values = map(lambda s : s.split(','), lines[1:-1])
78
79     self.assertEquals(len(values), 4)  # We expect 4 value in total.
80     self.assertEquals(len(set((v[1] for v in values))), 2)  # 2 pages.
81     self.assertEquals(len(set((v[2] for v in values))), 3)  # 3 value names.
82
83   def testTraceTag(self):
84     self.MakeFormatter(trace_tag='date,option')
85     self.SimulateBenchmarkRun({
86         self._page_set[0]: [scalar.ScalarValue(None, 'foo', 'seconds', 3),
87                             scalar.ScalarValue(None, 'bar', 'tons', 5)]})
88     output = self.Format().split(self._LINE_SEPARATOR)
89
90     self.assertTrue(output[0].endswith(',trace_tag_0,trace_tag_1'))
91     for line in output[1:-1]:
92       self.assertTrue(line.endswith(',date,option'))