| OLD | NEW |
| 1 # Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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 os | 5 import os |
| 6 import tempfile | |
| 7 import unittest | 6 import unittest |
| 8 import zipfile | 7 import zipfile |
| 9 | 8 |
| 10 from profile_chrome import profiler | 9 from profile_chrome import profiler |
| 11 from profile_chrome import ui | 10 from profile_chrome import ui |
| 12 from systrace import trace_result | 11 from profile_chrome import fake_agent_1 |
| 13 | 12 from profile_chrome import fake_agent_2 |
| 14 | 13 from systrace import decorators |
| 15 class FakeAgent(object): | 14 from systrace import tracing_controller |
| 16 def __init__(self, contents='fake-contents'): | |
| 17 self.contents = contents | |
| 18 self.stopped = False | |
| 19 self.filename = None | |
| 20 self.config = None | |
| 21 self.timeout = None | |
| 22 | |
| 23 def StartAgentTracing(self, config, timeout=None): | |
| 24 self.config = config | |
| 25 self.timeout = timeout | |
| 26 return True | |
| 27 | |
| 28 # pylint: disable=unused-argument | |
| 29 def StopAgentTracing(self, timeout=None): | |
| 30 self.stopped = True | |
| 31 return True | |
| 32 | |
| 33 # pylint: disable=unused-argument | |
| 34 def GetResults(self, timeout=None): | |
| 35 trace_data = open(self._PullTrace()).read() | |
| 36 return trace_result.TraceResult('fakeData', trace_data) | |
| 37 | |
| 38 def _PullTrace(self): | |
| 39 with tempfile.NamedTemporaryFile(delete=False) as f: | |
| 40 self.filename = f.name | |
| 41 f.write(self.contents) | |
| 42 return f.name | |
| 43 | |
| 44 # pylint: disable=no-self-use | |
| 45 def SupportsExplicitClockSync(self): | |
| 46 return False | |
| 47 | |
| 48 # pylint: disable=unused-argument, no-self-use | |
| 49 def RecordClockSyncMarker(self, sync_id, did_record_sync_marker_callback): | |
| 50 print ('Clock sync marker cannot be recorded since explicit clock sync ' | |
| 51 'is not supported.') | |
| 52 | |
| 53 def __repr__(self): | |
| 54 return 'faketrace' | |
| 55 | 15 |
| 56 | 16 |
| 57 class ProfilerTest(unittest.TestCase): | 17 class ProfilerTest(unittest.TestCase): |
| 58 def setUp(self): | 18 def setUp(self): |
| 59 ui.EnableTestMode() | 19 ui.EnableTestMode() |
| 20 self._tracing_options = tracing_controller.TracingControllerConfig(None, |
| 21 None, None, None, None, None, None, None, None, None) |
| 60 | 22 |
| 23 @decorators.ClientOnlyTest |
| 61 def testCaptureBasicProfile(self): | 24 def testCaptureBasicProfile(self): |
| 62 agent = FakeAgent() | 25 result = profiler.CaptureProfile(self._tracing_options, 1, [fake_agent_1]) |
| 63 result = profiler.CaptureProfile(None, [agent], 1) | |
| 64 | 26 |
| 65 try: | 27 try: |
| 66 self.assertTrue(agent.stopped) | |
| 67 self.assertTrue(os.path.exists(result)) | 28 self.assertTrue(os.path.exists(result)) |
| 68 self.assertTrue(result.endswith('.html')) | 29 self.assertTrue(result.endswith('.html')) |
| 69 finally: | 30 finally: |
| 70 if os.path.exists(result): | 31 if os.path.exists(result): |
| 71 os.remove(result) | 32 os.remove(result) |
| 72 | 33 |
| 34 @decorators.ClientOnlyTest |
| 73 def testCaptureJsonProfile(self): | 35 def testCaptureJsonProfile(self): |
| 74 agent = FakeAgent() | 36 result = profiler.CaptureProfile(self._tracing_options, 1, |
| 75 result = profiler.CaptureProfile(None, [agent], 1, write_json=True) | 37 [fake_agent_2], write_json=True) |
| 76 | 38 |
| 77 try: | 39 try: |
| 78 self.assertFalse(result.endswith('.html')) | 40 self.assertFalse(result.endswith('.html')) |
| 79 with open(result) as f: | 41 with open(result) as f: |
| 80 self.assertEquals(f.read(), agent.contents) | 42 self.assertEquals(f.read(), 'fake-contents') |
| 81 finally: | 43 finally: |
| 82 if os.path.exists(result): | 44 if os.path.exists(result): |
| 83 os.remove(result) | 45 os.remove(result) |
| 84 | 46 |
| 47 @decorators.ClientOnlyTest |
| 85 def testCaptureMultipleProfiles(self): | 48 def testCaptureMultipleProfiles(self): |
| 86 agents = [FakeAgent('c1'), FakeAgent('c2')] | 49 result = profiler.CaptureProfile(self._tracing_options, 1, |
| 87 result = profiler.CaptureProfile(None, agents, 1, write_json=True) | 50 [fake_agent_1, fake_agent_2], |
| 51 write_json=True) |
| 88 | 52 |
| 89 try: | 53 try: |
| 90 self.assertTrue(result.endswith('.zip')) | 54 self.assertTrue(result.endswith('.zip')) |
| 91 self.assertTrue(zipfile.is_zipfile(result)) | 55 self.assertTrue(zipfile.is_zipfile(result)) |
| 92 finally: | 56 finally: |
| 93 if os.path.exists(result): | 57 if os.path.exists(result): |
| 94 os.remove(result) | 58 os.remove(result) |
| OLD | NEW |