1# Copyright 2013 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.
4import os
5import unittest
6
7from telemetry.page import page_set
8from telemetry.value import list_of_scalar_values
9from telemetry.value import merge_values
10from telemetry.value import scalar
11
12class TestBase(unittest.TestCase):
13  def setUp(self):
14    self.page_set =  page_set.PageSet.FromDict({
15      "description": "hello",
16      "archive_path": "foo.wpr",
17      "pages": [
18        {"url": "http://www.bar.com/"},
19        {"url": "http://www.baz.com/"},
20        {"url": "http://www.foo.com/"}
21        ]
22      }, os.path.dirname(__file__))
23
24  @property
25  def pages(self):
26    return self.page_set.pages
27
28class MergeValueTest(TestBase):
29  def testSamePageMergeBasic(self):
30    page0 = self.pages[0]
31    page1 = self.pages[1]
32
33    all_values = [scalar.ScalarValue(page0, 'x', 'units', 1),
34                  scalar.ScalarValue(page1, 'x', 'units', 4),
35                  scalar.ScalarValue(page0, 'x', 'units', 2),
36                  scalar.ScalarValue(page1, 'x', 'units', 5)]
37
38    merged_values = merge_values.MergeLikeValuesFromSamePage(all_values)
39    # Sort the results so that their order is predictable for the subsequent
40    # assertions.
41    merged_values.sort(key=lambda x: x.page.url)
42
43    self.assertEquals(2, len(merged_values))
44
45    self.assertEquals((page0, 'x'),
46                      (merged_values[0].page, merged_values[0].name))
47    self.assertEquals([1, 2], merged_values[0].values)
48
49    self.assertEquals((page1, 'x'),
50                      (merged_values[1].page, merged_values[1].name))
51    self.assertEquals([4, 5], merged_values[1].values)
52
53  def testSamePageMergeOneValue(self):
54    page0 = self.pages[0]
55
56    all_values = [scalar.ScalarValue(page0, 'x', 'units', 1)]
57
58    # Sort the results so that their order is predictable for the subsequent
59    # assertions.
60    merged_values = merge_values.MergeLikeValuesFromSamePage(all_values)
61    self.assertEquals(1, len(merged_values))
62    self.assertEquals(all_values[0].name, merged_values[0].name)
63    self.assertEquals(all_values[0].units, merged_values[0].units)
64
65  def testDifferentPageMergeBasic(self):
66    page0 = self.pages[0]
67    page1 = self.pages[1]
68
69    all_values = [scalar.ScalarValue(page0, 'x', 'units', 1),
70                  scalar.ScalarValue(page1, 'x', 'units', 2),
71                  scalar.ScalarValue(page0, 'y', 'units', 10),
72                  scalar.ScalarValue(page1, 'y', 'units', 20)]
73
74    # Sort the results so that their order is predictable for the subsequent
75    # assertions.
76    merged_values = merge_values.MergeLikeValuesFromDifferentPages(all_values)
77    merged_values.sort(key=lambda x: x.name)
78    self.assertEquals(2, len(merged_values))
79
80    self.assertEquals((None, 'x'),
81                      (merged_values[0].page, merged_values[0].name))
82    self.assertEquals([1, 2], merged_values[0].values)
83
84    self.assertEquals((None, 'y'),
85                      (merged_values[1].page, merged_values[1].name))
86    self.assertEquals([10, 20], merged_values[1].values)
87
88  def testDifferentPageMergeSingleValueStillMerges(self):
89    page0 = self.pages[0]
90
91    all_values = [scalar.ScalarValue(page0, 'x', 'units', 1)]
92
93    # Sort the results so that their order is predictable for the subsequent
94    # assertions.
95    merged_values = merge_values.MergeLikeValuesFromDifferentPages(all_values)
96    self.assertEquals(1, len(merged_values))
97
98    self.assertEquals((None, 'x'),
99                      (merged_values[0].page, merged_values[0].name))
100    self.assertTrue(
101        isinstance(merged_values[0], list_of_scalar_values.ListOfScalarValues))
102    self.assertEquals([1], merged_values[0].values)
103
104  def testDifferentPageMergeBasicIgnoreTraceName(self):
105    page0 = self.pages[0]
106    page1 = self.pages[1]
107
108    all_values = [scalar.ScalarValue(page0, 'x.score', 'units', 1),
109                  scalar.ScalarValue(page1, 'y.score', 'units', 2)]
110    # Sort the results so that their order is predictable for the subsequent
111    # assertions.
112    merged_values = merge_values.MergeLikeValuesFromDifferentPages(
113        all_values,
114        group_by_name_suffix=True)
115    self.assertEquals(1, len(merged_values))
116
117    self.assertEquals((None, 'score'),
118                      (merged_values[0].page, merged_values[0].name))
119    self.assertEquals([1, 2], merged_values[0].values)
120