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 import value
8from telemetry.page import page_set
9from telemetry.value import histogram as histogram_module
10
11
12class TestBase(unittest.TestCase):
13  def setUp(self):
14    self.page_set = page_set.PageSet(file_path=os.path.dirname(__file__))
15    self.page_set.AddPageWithDefaultRunNavigate("http://www.bar.com/")
16    self.page_set.AddPageWithDefaultRunNavigate("http://www.baz.com/")
17    self.page_set.AddPageWithDefaultRunNavigate("http://www.foo.com/")
18
19  @property
20  def pages(self):
21    return self.page_set.pages
22
23class ValueTest(TestBase):
24  def testHistogramBasic(self):
25    page0 = self.pages[0]
26    histogram = histogram_module.HistogramValue(
27        page0, 'x', 'counts',
28        raw_value_json='{"buckets": [{"low": 1, "high": 2, "count": 1}]}',
29        important=False)
30    self.assertEquals(
31      ['{"buckets": [{"low": 1, "high": 2, "count": 1}]}'],
32      histogram.GetBuildbotValue())
33    self.assertEquals(1.5,
34                      histogram.GetRepresentativeNumber())
35    self.assertEquals(
36      ['{"buckets": [{"low": 1, "high": 2, "count": 1}]}'],
37      histogram.GetBuildbotValue())
38
39    self.assertEquals(
40        'unimportant-histogram',
41        histogram.GetBuildbotDataType(value.SUMMARY_RESULT_OUTPUT_CONTEXT))
42    histogram.important = True
43    self.assertEquals(
44        'histogram',
45        histogram.GetBuildbotDataType(value.SUMMARY_RESULT_OUTPUT_CONTEXT))
46
47  def testBucketAsDict(self):
48    bucket = histogram_module.HistogramValueBucket(33, 45, 78)
49    d = bucket.AsDict()
50
51    self.assertEquals(d, {
52          'low': 33,
53          'high': 45,
54          'count': 78
55        })
56
57  def testAsDict(self):
58    histogram = histogram_module.HistogramValue(
59        None, 'x', 'counts',
60        raw_value_json='{"buckets": [{"low": 1, "high": 2, "count": 1}]}',
61        important=False)
62    d = histogram.AsDictWithoutBaseClassEntries()
63
64    self.assertEquals(['buckets'], d.keys())
65    self.assertTrue(isinstance(d['buckets'], list))
66    self.assertEquals(len(d['buckets']), 1)
67
68  def testFromDict(self):
69    d = {
70      'type': 'histogram',
71      'name': 'x',
72      'units': 'counts',
73      'buckets': [{'low': 1, 'high': 2, 'count': 1}]
74    }
75    v = value.Value.FromDict(d, {})
76
77    self.assertTrue(isinstance(v, histogram_module.HistogramValue))
78    self.assertEquals(
79      ['{"buckets": [{"low": 1, "high": 2, "count": 1}]}'],
80      v.GetBuildbotValue())
81