OLD | NEW |
---|---|
(Empty) | |
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 | |
5 import cStringIO | |
6 import json | |
7 import logging | |
8 import unittest | |
9 | |
10 from telemetry.core import util | |
11 from telemetry.timeline import trace_data | |
12 | |
13 class TraceDataTest(unittest.TestCase): | |
14 def testSerialize(self): | |
15 ri = trace_data.TraceData({'traceEvents': [1,2,3]}) | |
16 f = cStringIO.StringIO() | |
17 ri.Serialize(f) | |
18 d = f.getvalue() | |
19 | |
20 assert 'traceEvents' in d | |
slamm
2014/12/22 17:12:32
self.assertIn('traceEvents', d)
sullivan
2014/12/22 19:28:38
Done.
| |
21 assert '[1, 2, 3]' in d | |
slamm
2014/12/22 17:12:32
self.assertIn('[1, 2, 3]', d)
sullivan
2014/12/22 19:28:38
Done.
| |
22 | |
23 json.loads(d) | |
24 | |
25 def testValidate(self): | |
slamm
2014/12/22 17:12:32
Split into 2 tests.
def testValidateWithNonPrimat
sullivan
2014/12/22 19:28:38
Done.
| |
26 self.assertRaisesRegexp( | |
27 Exception, | |
28 'TraceData cannot be serialized: .* is not JSON serializable', | |
29 trace_data.TraceData, | |
30 ({'hello': TraceDataTest})) | |
slamm
2014/12/22 17:12:32
With a module-defined exception, this check is sim
sullivan
2014/12/22 19:28:38
Done.
| |
31 | |
32 a = [] | |
33 d = {'foo': a} | |
34 a.append(d) | |
35 self.assertRaisesRegexp( | |
36 Exception, | |
37 'TraceData cannot be serialized: Circular reference detected', | |
slamm
2014/12/22 17:12:32
I did not see this exception raised in the code.
sullivan
2014/12/22 19:28:38
Raised by json parsing.
| |
38 trace_data.TraceData, | |
39 d) | |
40 | |
41 def testEmptyArrayValue(self): | |
42 # We can import empty lists and empty string. | |
43 d = trace_data.TraceData([]) | |
44 assert d.HasEventsFor(trace_data.CHROME_TRACE_PART) == False | |
slamm
2014/12/22 17:12:32
self.assertFalse(d.HasEventsFor(trace_data.CHROME_
sullivan
2014/12/22 19:28:38
Done.
| |
45 | |
46 def testEmptyStringValue(self): | |
47 d = trace_data.TraceData('') | |
48 assert d.HasEventsFor(trace_data.CHROME_TRACE_PART) == False | |
49 | |
50 def testListForm(self): | |
51 d = trace_data.TraceData([{'ph': 'B'}]) | |
52 assert d.HasEventsFor(trace_data.CHROME_TRACE_PART) == True | |
53 assert len(d.GetEventsFor(trace_data.CHROME_TRACE_PART)) == 1 | |
54 | |
55 def testStringForm(self): | |
56 d = trace_data.TraceData('[{"ph": "B"}]') | |
57 assert d.HasEventsFor(trace_data.CHROME_TRACE_PART) == True | |
58 assert len(d.GetEventsFor(trace_data.CHROME_TRACE_PART)) == 1 | |
59 | |
60 def testStringForm2(self): | |
61 d = trace_data.TraceData('{"inspectorTimelineEvents": [1]}') | |
62 assert d.HasEventsFor(trace_data.INSPECTOR_TRACE_PART) | |
63 assert len(d.GetEventsFor(trace_data.INSPECTOR_TRACE_PART)) == 1 | |
64 | |
65 def testCorrectlyMalformedStringForm(self): | |
66 d = trace_data.TraceData("""[ | |
67 {"ph": "B"}""") | |
68 assert d.HasEventsFor(trace_data.CHROME_TRACE_PART) == True | |
69 | |
70 def testCorrectlyMalformedStringForm2(self): | |
71 d = trace_data.TraceData("""[ | |
72 {"ph": "B"},""") | |
73 assert d.HasEventsFor(trace_data.CHROME_TRACE_PART) == True | |
74 | |
75 class TraceDataBuilderTest(unittest.TestCase): | |
76 def testBasicChrome(self): | |
77 builder = trace_data.TraceDataBuilder() | |
78 builder.AddEventsTo(trace_data.CHROME_TRACE_PART, [1,2,3]) | |
79 builder.AddEventsTo(trace_data.TAB_ID_PART, ['tab-7']) | |
80 | |
81 d = builder.AsData() | |
82 self.assertTrue(d.HasEventsFor(trace_data.CHROME_TRACE_PART)) | |
83 self.assertTrue(d.HasEventsFor(trace_data.TAB_ID_PART)) | |
84 | |
85 self.assertRaises(Exception, builder.AsData) | |
OLD | NEW |