| OLD | NEW |
| 1 # Copyright 2016 The Chromium Authors. All rights reserved. | 1 # Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
| 4 | 4 |
| 5 import logging | 5 import logging |
| 6 import unittest | 6 import unittest |
| 7 | 7 |
| 8 import devtools_monitor | 8 import devtools_monitor |
| 9 | 9 |
| 10 from tracing import TracingTrack | 10 from tracing import (Event, TracingTrack) |
| 11 | |
| 12 | |
| 13 class StubConnection(object): | |
| 14 def RegisterListener(self, name, obj): | |
| 15 pass | |
| 16 | |
| 17 def SyncRequestNoResponse(self, method, params): | |
| 18 pass | |
| 19 | 11 |
| 20 | 12 |
| 21 class TracingTrackTestCase(unittest.TestCase): | 13 class TracingTrackTestCase(unittest.TestCase): |
| 14 _MIXED_EVENTS = [ |
| 15 {'ts': 3, 'ph': 'N', 'id': 1, 'args': {'name': 'A'}}, |
| 16 {'ts': 5, 'ph': 'X', 'dur': 1, 'args': {'name': 'B'}}, |
| 17 {'ts': 7, 'ph': 'D', 'id': 1}, |
| 18 {'ts': 10, 'ph': 'B', 'args': {'name': 'D'}}, |
| 19 {'ts': 10, 'ph': 'b', 'cat': 'X', 'id': 1, 'args': {'name': 'C'}}, |
| 20 {'ts': 11, 'ph': 'e', 'cat': 'X', 'id': 1}, |
| 21 {'ts': 12, 'ph': 'E'}, |
| 22 {'ts': 12, 'ph': 'N', 'id': 1, 'args': {'name': 'E'}}, |
| 23 {'ts': 13, 'ph': 'b', 'cat': 'X', 'id': 2, 'args': {'name': 'F'}}, |
| 24 {'ts': 14, 'ph': 'e', 'cat': 'X', 'id': 2}, |
| 25 {'ts': 15, 'ph': 'D', 'id': 1}] |
| 26 |
| 22 def setUp(self): | 27 def setUp(self): |
| 23 self.track = TracingTrack(StubConnection()) | 28 self.track = TracingTrack(None) |
| 24 | 29 |
| 25 def EventToMicroseconds(self, event): | 30 def EventToMicroseconds(self, event): |
| 26 if 'ts' in event: | 31 if 'ts' in event: |
| 27 event['ts'] *= 1000 | 32 event['ts'] *= 1000 |
| 28 if 'dur' in event: | 33 if 'dur' in event: |
| 29 event['dur'] *= 1000 | 34 event['dur'] *= 1000 |
| 30 return event | 35 return event |
| 31 | 36 |
| 32 def CheckTrack(self, timestamp, names): | 37 def CheckTrack(self, timestamp, names): |
| 33 self.assertEqual( | 38 self.assertEqual( |
| (...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 169 {'ts': 10, 'ph': 'N', 'id': 4, 'args': {'name': 'C'}}, | 174 {'ts': 10, 'ph': 'N', 'id': 4, 'args': {'name': 'C'}}, |
| 170 {'ts': 11, 'ph': 'D', 'id': 4}, | 175 {'ts': 11, 'ph': 'D', 'id': 4}, |
| 171 {'ts': 12, 'ph': 'D', 'id': 3}, | 176 {'ts': 12, 'ph': 'D', 'id': 3}, |
| 172 {'ts': 12, 'ph': 'N', 'id': 1, 'args': {'name': 'E'}}, | 177 {'ts': 12, 'ph': 'N', 'id': 1, 'args': {'name': 'E'}}, |
| 173 {'ts': 13, 'ph': 'N', 'id': 5, 'args': {'name': 'F'}}, | 178 {'ts': 13, 'ph': 'N', 'id': 5, 'args': {'name': 'F'}}, |
| 174 {'ts': 14, 'ph': 'D', 'id': 5}, | 179 {'ts': 14, 'ph': 'D', 'id': 5}, |
| 175 {'ts': 15, 'ph': 'D', 'id': 1}]) | 180 {'ts': 15, 'ph': 'D', 'id': 1}]) |
| 176 | 181 |
| 177 def testMixed(self): | 182 def testMixed(self): |
| 178 # A and E are objects, B complete, D a duration, and C and F async. | 183 # A and E are objects, B complete, D a duration, and C and F async. |
| 179 self.CheckIntervals([ | 184 self.CheckIntervals(self._MIXED_EVENTS) |
| 180 {'ts': 3, 'ph': 'N', 'id': 1, 'args': {'name': 'A'}}, | 185 |
| 181 {'ts': 5, 'ph': 'X', 'dur': 1, 'args': {'name': 'B'}}, | 186 def testEventSerialization(self): |
| 182 {'ts': 7, 'ph': 'D', 'id': 1}, | 187 for e in self._MIXED_EVENTS: |
| 183 {'ts': 10, 'ph': 'B', 'args': {'name': 'D'}}, | 188 event = Event(e) |
| 184 {'ts': 10, 'ph': 'b', 'cat': 'X', 'id': 1, 'args': {'name': 'C'}}, | 189 json_dict = event.ToJsonDict() |
| 185 {'ts': 11, 'ph': 'e', 'cat': 'X', 'id': 1}, | 190 deserialized_event = Event.FromJsonDict(json_dict) |
| 186 {'ts': 12, 'ph': 'E'}, | 191 self.assertEquals( |
| 187 {'ts': 12, 'ph': 'N', 'id': 1, 'args': {'name': 'E'}}, | 192 event.tracing_event, deserialized_event.tracing_event) |
| 188 {'ts': 13, 'ph': 'b', 'cat': 'X', 'id': 2, 'args': {'name': 'F'}}, | 193 |
| 189 {'ts': 14, 'ph': 'e', 'cat': 'X', 'id': 2}, | 194 def testTracingTrackSerialization(self): |
| 190 {'ts': 15, 'ph': 'D', 'id': 1}]) | 195 events = self._MIXED_EVENTS |
| 196 self.track.Handle('Tracing.dataCollected', |
| 197 {'params': {'value': [self.EventToMicroseconds(e) |
| 198 for e in events]}}) |
| 199 json_dict = self.track.ToJsonDict() |
| 200 self.assertTrue('events' in json_dict) |
| 201 deserialized_track = TracingTrack.FromJsonDict(json_dict) |
| 202 self.assertEquals( |
| 203 len(self.track._events), len(deserialized_track._events)) |
| 204 for (e1, e2) in zip(self.track._events, deserialized_track._events): |
| 205 self.assertEquals(e1.tracing_event, e2.tracing_event) |
| 191 | 206 |
| 192 | 207 |
| 193 if __name__ == '__main__': | 208 if __name__ == '__main__': |
| 194 unittest.main() | 209 unittest.main() |
| OLD | NEW |