Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / tools / telemetry / telemetry / value / value_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 unittest
6
7 from telemetry import value
8 from telemetry.page import page_set
9
10
11 class TestBase(unittest.TestCase):
12   def setUp(self):
13     self.page_set =  page_set.PageSet(file_path=os.path.dirname(__file__))
14     self.page_set.AddPageWithDefaultRunNavigate("http://www.bar.com/")
15     self.page_set.AddPageWithDefaultRunNavigate("http://www.baz.com/")
16     self.page_set.AddPageWithDefaultRunNavigate("http://www.foo.com/")
17
18   @property
19   def pages(self):
20     return self.page_set.pages
21
22 class ValueForTest(value.Value):
23   @classmethod
24   def MergeLikeValuesFromSamePage(cls, values):
25     pass
26
27   @classmethod
28   def MergeLikeValuesFromDifferentPages(cls, values,
29                                         group_by_name_suffix=False):
30     pass
31
32   def GetBuildbotDataType(self, output_context):
33     pass
34
35   def GetBuildbotValue(self):
36     pass
37
38   def GetBuildbotMeasurementAndTraceNameForComputedSummaryResult(
39       self, trace_tag):
40     pass
41
42   def GetRepresentativeNumber(self):
43     pass
44
45   def GetRepresentativeString(self):
46     pass
47
48   @staticmethod
49   def GetJSONTypeName():
50     pass
51
52 class ValueForAsDictTest(ValueForTest):
53   @staticmethod
54   def GetJSONTypeName():
55     return 'baz'
56
57 class ValueForFromDictTest(ValueForTest):
58   @staticmethod
59   def FromDict(value_dict, page_dict):
60     kwargs = value.Value.GetConstructorKwArgs(value_dict, page_dict)
61     return ValueForFromDictTest(**kwargs)
62
63   @staticmethod
64   def GetJSONTypeName():
65     return 'value_for_from_dict_test'
66
67 class ValueTest(TestBase):
68   def testCompat(self):
69     page0 = self.pages[0]
70     page1 = self.pages[0]
71
72     a = value.Value(page0, 'x', 'unit', important=False, description=None)
73     b = value.Value(page1, 'x', 'unit', important=False, description=None)
74     self.assertTrue(b.IsMergableWith(a))
75
76   def testIncompat(self):
77     page0 = self.pages[0]
78
79     a = value.Value(page0, 'x', 'unit', important=False, description=None)
80     b = value.Value(page0, 'x', 'incompatUnit', important=False,
81                     description=None)
82     self.assertFalse(b.IsMergableWith(a))
83
84     a = value.Value(page0, 'x', 'unit', important=False, description=None)
85     b = value.Value(page0, 'x', 'unit', important=True, description=None)
86     self.assertFalse(b.IsMergableWith(a))
87
88     a = value.Value(page0, 'x', 'unit', important=False, description=None)
89     b = ValueForTest(page0, 'x', 'unit', important=True, description=None)
90     self.assertFalse(b.IsMergableWith(a))
91
92   def testAsDictBaseKeys(self):
93     v = ValueForAsDictTest(None, 'x', 'unit', important=True, description=None)
94     d = v.AsDict()
95
96     self.assertEquals(d, {
97           'name': 'x',
98           'type': 'baz',
99           'units': 'unit',
100         })
101
102   def testAsDictWithPage(self):
103     page0 = self.pages[0]
104
105     v = ValueForAsDictTest(page0, 'x', 'unit', important=False,
106                            description=None)
107     d = v.AsDict()
108
109     self.assertIn('page_id', d)
110
111   def testAsDictWithoutPage(self):
112     v = ValueForAsDictTest(None, 'x', 'unit', important=False, description=None)
113     d = v.AsDict()
114
115     self.assertNotIn('page_id', d)
116
117   def testAsDictWithDescription(self):
118     v = ValueForAsDictTest(None, 'x', 'unit', important=False,
119                            description='Some description.')
120     d = v.AsDict()
121     self.assertEqual('Some description.', d['description'])
122
123   def testAsDictWithoutDescription(self):
124     v = ValueForAsDictTest(None, 'x', 'unit', important=False, description=None)
125     self.assertNotIn('description', v.AsDict())
126
127   def testFromDictBaseKeys(self):
128     d = {
129       'type': 'value_for_from_dict_test',
130       'name': 'x',
131       'units': 'unit'
132     }
133
134     v = value.Value.FromDict(d, None)
135     self.assertEquals(v.name, 'x')
136     self.assertTrue(isinstance(v, ValueForFromDictTest))
137     self.assertEquals(v.units, 'unit')
138
139   def testFromDictWithPage(self):
140     page0 = self.pages[0]
141     page_dict = {page0.id: page0}
142
143     d = {
144       'type': 'value_for_from_dict_test',
145       'name': 'x',
146       'units': 'unit',
147       'page_id': page0.id
148     }
149
150     v = value.Value.FromDict(d, page_dict)
151
152     self.assertEquals(v.page.id, page0.id)
153
154   def testFromDictWithoutPage(self):
155     d = {
156       'type': 'value_for_from_dict_test',
157       'name': 'x',
158       'units': 'unit'
159     }
160
161     v = value.Value.FromDict(d, {})
162
163     self.assertEquals(v.page, None)
164
165   def testFromDictWithDescription(self):
166     d = {
167           'type': 'value_for_from_dict_test',
168           'name': 'x',
169           'units': 'unit',
170           'description': 'foo'
171         }
172
173     v = value.Value.FromDict(d, {})
174     self.assertEquals(v.description, 'foo')
175
176   def testFromDictWithoutDescription(self):
177     d = {
178           'type': 'value_for_from_dict_test',
179           'name': 'x',
180           'units': 'unit'
181         }
182
183     v = value.Value.FromDict(d, {})
184     self.assertEquals(v.description, None)
185
186   def testListOfValuesFromListOfDicts(self):
187     d0 = {
188           'type': 'value_for_from_dict_test',
189           'name': 'x',
190           'units': 'unit'
191         }
192     d1 = {
193           'type': 'value_for_from_dict_test',
194           'name': 'y',
195           'units': 'unit'
196         }
197     vs = value.Value.ListOfValuesFromListOfDicts([d0, d1], {})
198     self.assertEquals(vs[0].name, 'x')
199     self.assertEquals(vs[1].name, 'y')