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.
10 from adb_profile_chrome import profiler
11 from adb_profile_chrome import ui
14 class FakeController(object):
15 def __init__(self, contents='fake-contents'):
16 self.contents = contents
21 def StartTracing(self, interval):
22 self.interval = interval
24 def StopTracing(self):
28 with tempfile.NamedTemporaryFile(delete=False) as f:
29 self.filename = f.name
30 f.write(self.contents)
37 class ProfilerTest(unittest.TestCase):
41 def testCaptureBasicProfile(self):
42 controller = FakeController()
44 result = profiler.CaptureProfile([controller], interval)
47 self.assertEquals(controller.interval, interval)
48 self.assertTrue(controller.stopped)
49 self.assertTrue(os.path.exists(result))
50 self.assertFalse(os.path.exists(controller.filename))
51 self.assertTrue(result.endswith('.html'))
55 def testCaptureJsonProfile(self):
56 controller = FakeController()
57 result = profiler.CaptureProfile([controller], 1, write_json=True)
60 self.assertFalse(result.endswith('.html'))
61 with open(result) as f:
62 self.assertEquals(f.read(), controller.contents)
66 def testCaptureMultipleProfiles(self):
67 controllers = [FakeController('c1'), FakeController('c2')]
68 result = profiler.CaptureProfile(controllers, 1, write_json=True)
71 self.assertTrue(result.endswith('.zip'))
72 self.assertTrue(zipfile.is_zipfile(result))
73 with zipfile.ZipFile(result) as f:
76 [controllers[0].filename[1:], controllers[1].filename[1:]])