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
5import datetime
6import exceptions
7import os
8import shutil
9import tempfile
10import unittest
11
12from tracing_build import html2trace
13from telemetry.timeline import trace_data
14
15
16class TraceDataTest(unittest.TestCase):
17  def testSerialize(self):
18    test_dir = tempfile.mkdtemp()
19    trace_path = os.path.join(test_dir, 'test_trace.json')
20    try:
21      ri = trace_data.CreateTraceDataFromRawData({'traceEvents': [1, 2, 3]})
22      ri.Serialize(trace_path)
23      with open(trace_path) as f:
24        json_traces = html2trace.ReadTracesFromHTMLFilePath(f)
25      self.assertEqual(json_traces, [{'traceEvents': [1, 2, 3]}])
26    finally:
27      shutil.rmtree(test_dir)
28
29  def testEmptyArrayValue(self):
30    # We can import empty lists and empty string.
31    d = trace_data.CreateTraceDataFromRawData([])
32    self.assertFalse(d.HasTracesFor(trace_data.CHROME_TRACE_PART))
33
34  def testInvalidTrace(self):
35    with self.assertRaises(AssertionError):
36      trace_data.CreateTraceDataFromRawData({'hello': 1})
37
38  def testListForm(self):
39    d = trace_data.CreateTraceDataFromRawData([{'ph': 'B'}])
40    self.assertTrue(d.HasTracesFor(trace_data.CHROME_TRACE_PART))
41    events = d.GetTracesFor(trace_data.CHROME_TRACE_PART)[0].get(
42        'traceEvents', [])
43    self.assertEquals(1, len(events))
44
45  def testStringForm(self):
46    d = trace_data.CreateTraceDataFromRawData('[{"ph": "B"}]')
47    self.assertTrue(d.HasTracesFor(trace_data.CHROME_TRACE_PART))
48    events = d.GetTracesFor(trace_data.CHROME_TRACE_PART)[0].get(
49        'traceEvents', [])
50    self.assertEquals(1, len(events))
51
52
53class TraceDataBuilderTest(unittest.TestCase):
54  def testBasicChrome(self):
55    builder = trace_data.TraceDataBuilder()
56    builder.AddTraceFor(trace_data.CHROME_TRACE_PART,
57                        {'traceEvents': [1, 2, 3]})
58    builder.AddTraceFor(trace_data.TAB_ID_PART, ['tab-7'])
59    builder.AddTraceFor(trace_data.BATTOR_TRACE_PART, 'battor data here')
60
61    d = builder.AsData()
62    self.assertTrue(d.HasTracesFor(trace_data.CHROME_TRACE_PART))
63    self.assertTrue(d.HasTracesFor(trace_data.TAB_ID_PART))
64    self.assertTrue(d.HasTracesFor(trace_data.BATTOR_TRACE_PART))
65
66    self.assertRaises(Exception, builder.AsData)
67
68  def testSetTraceFor(self):
69    telemetry_trace = {
70        'traceEvents': [1, 2, 3],
71        'metadata': {
72          'field1': 'value1'
73        }
74    }
75
76    builder = trace_data.TraceDataBuilder()
77    builder.AddTraceFor(trace_data.TELEMETRY_PART, telemetry_trace)
78    d = builder.AsData()
79
80    self.assertEqual(d.GetTracesFor(trace_data.TELEMETRY_PART),
81                     [telemetry_trace])
82
83  def testSetTraceForRaisesWithInvalidPart(self):
84    builder = trace_data.TraceDataBuilder()
85
86    self.assertRaises(exceptions.AssertionError,
87                      lambda: builder.AddTraceFor('not_a_trace_part', {}))
88
89  def testSetTraceForRaisesWithInvalidTrace(self):
90    builder = trace_data.TraceDataBuilder()
91
92    self.assertRaises(exceptions.AssertionError, lambda:
93        builder.AddTraceFor(trace_data.TELEMETRY_PART, datetime.time.min))
94
95  def testSetTraceForRaisesAfterAsData(self):
96    builder = trace_data.TraceDataBuilder()
97    builder.AsData()
98
99    self.assertRaises(exceptions.Exception,
100        lambda: builder.AddTraceFor(trace_data.TELEMETRY_PART, {}))
101