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 os 6import unittest 7import zipfile 8 9from profile_chrome import profiler 10from profile_chrome import ui 11from profile_chrome import fake_agent_1 12from profile_chrome import fake_agent_2 13from systrace import decorators 14from systrace import tracing_controller 15 16 17class ProfilerTest(unittest.TestCase): 18 def setUp(self): 19 ui.EnableTestMode() 20 self._tracing_options = tracing_controller.TracingControllerConfig(None, 21 None, None, None, None, None, None, None, None) 22 23 @decorators.ClientOnlyTest 24 def testCaptureBasicProfile(self): 25 result = profiler.CaptureProfile(self._tracing_options, 1, [fake_agent_1]) 26 27 try: 28 self.assertTrue(os.path.exists(result)) 29 self.assertTrue(result.endswith('.html')) 30 finally: 31 if os.path.exists(result): 32 os.remove(result) 33 34 @decorators.ClientOnlyTest 35 def testCaptureJsonProfile(self): 36 result = profiler.CaptureProfile(self._tracing_options, 1, 37 [fake_agent_2], write_json=True) 38 39 try: 40 self.assertFalse(result.endswith('.html')) 41 with open(result) as f: 42 self.assertEquals(f.read(), 'fake-contents') 43 finally: 44 if os.path.exists(result): 45 os.remove(result) 46 47 @decorators.ClientOnlyTest 48 def testCaptureMultipleProfiles(self): 49 result = profiler.CaptureProfile(self._tracing_options, 1, 50 [fake_agent_1, fake_agent_2], 51 write_json=True) 52 53 try: 54 self.assertTrue(result.endswith('.zip')) 55 self.assertTrue(zipfile.is_zipfile(result)) 56 finally: 57 if os.path.exists(result): 58 os.remove(result) 59