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.
4import os
5import unittest
6
7from telemetry import value
8from telemetry.page import page_set
9from telemetry.value import none_values
10from telemetry.value import string
11
12
13class TestBase(unittest.TestCase):
14  def setUp(self):
15    self.page_set = page_set.PageSet(file_path=os.path.dirname(__file__))
16    self.page_set.AddPageWithDefaultRunNavigate("http://www.bar.com/")
17    self.page_set.AddPageWithDefaultRunNavigate("http://www.baz.com/")
18    self.page_set.AddPageWithDefaultRunNavigate("http://www.foo.com/")
19
20  @property
21  def pages(self):
22    return self.page_set.pages
23
24class StringValueTest(TestBase):
25  def testBuildbotValueType(self):
26    page0 = self.pages[0]
27    v = string.StringValue(page0, 'x', 'label', 'L1', important=True)
28    self.assertEquals('default', v.GetBuildbotDataType(
29        value.COMPUTED_PER_PAGE_SUMMARY_OUTPUT_CONTEXT))
30    self.assertEquals(['L1'], v.GetBuildbotValue())
31    self.assertEquals(('x', page0.display_name),
32                      v.GetChartAndTraceNameForPerPageResult())
33
34    v = string.StringValue(page0, 'x', 'label', 'L1', important=False)
35    self.assertEquals(
36        'unimportant',
37        v.GetBuildbotDataType(value.COMPUTED_PER_PAGE_SUMMARY_OUTPUT_CONTEXT))
38
39  def testStringSamePageMerging(self):
40    page0 = self.pages[0]
41    v0 = string.StringValue(page0, 'x', 'label', 'L1')
42    v1 = string.StringValue(page0, 'x', 'label', 'L2')
43    self.assertTrue(v1.IsMergableWith(v0))
44
45    vM = string.StringValue.MergeLikeValuesFromSamePage([v0, v1])
46    self.assertEquals(page0, vM.page)
47    self.assertEquals('x', vM.name)
48    self.assertEquals('label', vM.units)
49    self.assertEquals(True, vM.important)
50    self.assertEquals(['L1', 'L2'], vM.values)
51
52  def testStringDifferentPageMerging(self):
53    page0 = self.pages[0]
54    page1 = self.pages[1]
55    v0 = string.StringValue(page0, 'x', 'label', 'L1')
56    v1 = string.StringValue(page1, 'x', 'label', 'L2')
57
58    vM = string.StringValue.MergeLikeValuesFromDifferentPages([v0, v1])
59    self.assertEquals(None, vM.page)
60    self.assertEquals('x', vM.name)
61    self.assertEquals('label', vM.units)
62    self.assertEquals(True, vM.important)
63    self.assertEquals(['L1', 'L2'], vM.values)
64
65  def testStringWithNoneValueMerging(self):
66    page0 = self.pages[0]
67    v0 = string.StringValue(page0, 'x', 'unit', 'L1')
68    v1 = string.StringValue(page0, 'x', 'unit', None, none_value_reason='n')
69    self.assertTrue(v1.IsMergableWith(v0))
70
71    vM = string.StringValue.MergeLikeValuesFromSamePage([v0, v1])
72    self.assertEquals(None, vM.values)
73    self.assertEquals(none_values.MERGE_FAILURE_REASON,
74                      vM.none_value_reason)
75
76  def testStringWithNoneValueMustHaveNoneReason(self):
77    page0 = self.pages[0]
78    self.assertRaises(none_values.NoneValueMissingReason,
79                      lambda: string.StringValue(page0, 'x', 'unit', None))
80
81  def testStringWithNoneReasonMustHaveNoneValue(self):
82    page0 = self.pages[0]
83    self.assertRaises(none_values.ValueMustHaveNoneValue,
84                      lambda: string.StringValue(page0, 'x', 'unit', 'L1',
85                                                 none_value_reason='n'))
86
87  def testAsDict(self):
88    v = string.StringValue(None, 'x', 'unit', 'foo', important=False)
89    d = v.AsDictWithoutBaseClassEntries()
90
91    self.assertEquals(d, {
92          'value': 'foo'
93        })
94
95  def testNoneValueAsDict(self):
96    v = string.StringValue(None, 'x', 'unit', None, important=False,
97                           none_value_reason='n')
98    d = v.AsDictWithoutBaseClassEntries()
99
100    self.assertEquals(d, {
101          'value': None,
102          'none_value_reason': 'n'
103        })
104
105  def testFromDict(self):
106    d = {
107      'type': 'string',
108      'name': 'x',
109      'units': 'unit',
110      'value': 'foo'
111    }
112
113    v = value.Value.FromDict(d, {})
114
115    self.assertTrue(isinstance(v, string.StringValue))
116    self.assertEquals(v.value, 'foo')
117
118  def testFromDictNoneValue(self):
119    d = {
120      'type': 'string',
121      'name': 'x',
122      'units': 'unit',
123      'value': None,
124      'none_value_reason': 'n'
125    }
126
127    v = value.Value.FromDict(d, {})
128
129    self.assertTrue(isinstance(v, string.StringValue))
130    self.assertEquals(v.value, None)
131    self.assertEquals(v.none_value_reason, 'n')
132