PageRenderTime 42ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/tools/telemetry/telemetry/internal/platform/tracing_agent/chrome_tracing_agent_unittest.py

https://gitlab.com/jonnialva90/iridium-browser
Python | 277 lines | 246 code | 20 blank | 11 comment | 2 complexity | d4e402878faf54eaec9e8d23940b2c0c MD5 | raw file
  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. import os
  5. import platform
  6. import stat
  7. import unittest
  8. from telemetry import decorators
  9. from telemetry.internal.platform.tracing_agent import chrome_tracing_agent
  10. from telemetry.internal.platform.tracing_agent import (
  11. chrome_tracing_devtools_manager)
  12. from telemetry.timeline import tracing_category_filter
  13. from telemetry.timeline import tracing_config
  14. from telemetry.timeline import tracing_options
  15. from devil.android import device_utils
  16. class FakeTracingControllerBackend(object):
  17. def __init__(self):
  18. self.is_tracing_running = False
  19. class FakePlatformBackend(object):
  20. def __init__(self):
  21. self.tracing_controller_backend = FakeTracingControllerBackend()
  22. def GetOSName(self):
  23. return ''
  24. class FakeAndroidPlatformBackend(FakePlatformBackend):
  25. def __init__(self):
  26. super(FakeAndroidPlatformBackend, self).__init__()
  27. devices = device_utils.DeviceUtils.HealthyDevices(None)
  28. self.device = devices[0]
  29. def GetOSName(self):
  30. return 'android'
  31. class FakeDesktopPlatformBackend(FakePlatformBackend):
  32. def GetOSName(self):
  33. system = platform.system()
  34. if system == 'Linux':
  35. return 'linux'
  36. if system == 'Darwin':
  37. return 'mac'
  38. if system == 'Windows':
  39. return 'win'
  40. class FakeDevtoolsClient(object):
  41. def __init__(self, remote_port):
  42. self.is_alive = True
  43. self.is_tracing_running = False
  44. self.remote_port = remote_port
  45. self.will_raise_exception_in_stop_tracing = False
  46. def IsAlive(self):
  47. return self.is_alive
  48. def StartChromeTracing(self, _trace_options, _filter_string, _timeout=10):
  49. self.is_tracing_running = True
  50. def StopChromeTracing(self, _trace_data_builder):
  51. self.is_tracing_running = False
  52. if self.will_raise_exception_in_stop_tracing:
  53. raise Exception
  54. def IsChromeTracingSupported(self):
  55. return True
  56. class ChromeTracingAgentTest(unittest.TestCase):
  57. def setUp(self):
  58. self.platform1 = FakePlatformBackend()
  59. self.platform2 = FakePlatformBackend()
  60. self.platform3 = FakePlatformBackend()
  61. def StartTracing(self, platform_backend, enable_chrome_trace=True):
  62. assert chrome_tracing_agent.ChromeTracingAgent.IsSupported(platform_backend)
  63. agent = chrome_tracing_agent.ChromeTracingAgent(platform_backend)
  64. trace_options = tracing_options.TracingOptions()
  65. trace_options.enable_chrome_trace = enable_chrome_trace
  66. category_filter = tracing_category_filter.TracingCategoryFilter('foo')
  67. agent._platform_backend.tracing_controller_backend.is_tracing_running = True
  68. agent.Start(trace_options, category_filter, 10)
  69. return agent
  70. def StopTracing(self, agent):
  71. agent._platform_backend.tracing_controller_backend.is_tracing_running = (
  72. False)
  73. agent.Stop(None)
  74. def testRegisterDevtoolsClient(self):
  75. chrome_tracing_devtools_manager.RegisterDevToolsClient(
  76. FakeDevtoolsClient(1), self.platform1)
  77. chrome_tracing_devtools_manager.RegisterDevToolsClient(
  78. FakeDevtoolsClient(2), self.platform1)
  79. chrome_tracing_devtools_manager.RegisterDevToolsClient(
  80. FakeDevtoolsClient(3), self.platform1)
  81. tracing_agent_of_platform1 = self.StartTracing(self.platform1)
  82. chrome_tracing_devtools_manager.RegisterDevToolsClient(
  83. FakeDevtoolsClient(4), self.platform1)
  84. chrome_tracing_devtools_manager.RegisterDevToolsClient(
  85. FakeDevtoolsClient(5), self.platform2)
  86. self.StopTracing(tracing_agent_of_platform1)
  87. chrome_tracing_devtools_manager.RegisterDevToolsClient(
  88. FakeDevtoolsClient(6), self.platform1)
  89. def testIsSupportWithoutStartupTracingSupport(self):
  90. self.assertFalse(
  91. chrome_tracing_agent.ChromeTracingAgent.IsSupported(self.platform1))
  92. self.assertFalse(
  93. chrome_tracing_agent.ChromeTracingAgent.IsSupported(self.platform2))
  94. self.assertFalse(
  95. chrome_tracing_agent.ChromeTracingAgent.IsSupported(self.platform3))
  96. devtool1 = FakeDevtoolsClient(1)
  97. devtool2 = FakeDevtoolsClient(2)
  98. chrome_tracing_devtools_manager.RegisterDevToolsClient(
  99. devtool1, self.platform1)
  100. chrome_tracing_devtools_manager.RegisterDevToolsClient(
  101. devtool2, self.platform2)
  102. devtool2.is_alive = False
  103. # Chrome tracing is only supported on platform 1 since only platform 1 has
  104. # an alive devtool.
  105. self.assertTrue(
  106. chrome_tracing_agent.ChromeTracingAgent.IsSupported(self.platform1))
  107. self.assertFalse(
  108. chrome_tracing_agent.ChromeTracingAgent.IsSupported(self.platform2))
  109. self.assertFalse(
  110. chrome_tracing_agent.ChromeTracingAgent.IsSupported(self.platform3))
  111. @decorators.Enabled('linux', 'mac', 'win')
  112. def testIsSupportOnDesktopPlatform(self):
  113. # Chrome tracing is always supported on desktop platforms because of startup
  114. # tracing.
  115. desktop_platform = FakeDesktopPlatformBackend()
  116. self.assertTrue(
  117. chrome_tracing_agent.ChromeTracingAgent.IsSupported(desktop_platform))
  118. devtool = FakeDevtoolsClient(1)
  119. chrome_tracing_devtools_manager.RegisterDevToolsClient(
  120. devtool, desktop_platform)
  121. self.assertTrue(
  122. chrome_tracing_agent.ChromeTracingAgent.IsSupported(desktop_platform))
  123. def testStartAndStopTracing(self):
  124. devtool1 = FakeDevtoolsClient(1)
  125. devtool2 = FakeDevtoolsClient(2)
  126. devtool3 = FakeDevtoolsClient(3)
  127. devtool4 = FakeDevtoolsClient(2)
  128. # Register devtools 1, 2, 3 on platform1 and devtool 4 on platform 2
  129. chrome_tracing_devtools_manager.RegisterDevToolsClient(
  130. devtool1, self.platform1)
  131. chrome_tracing_devtools_manager.RegisterDevToolsClient(
  132. devtool2, self.platform1)
  133. chrome_tracing_devtools_manager.RegisterDevToolsClient(
  134. devtool3, self.platform1)
  135. chrome_tracing_devtools_manager.RegisterDevToolsClient(
  136. devtool4, self.platform2)
  137. devtool2.is_alive = False
  138. tracing_agent1 = self.StartTracing(self.platform1)
  139. with self.assertRaises(chrome_tracing_agent.ChromeTracingStartedError):
  140. self.StartTracing(self.platform1)
  141. self.assertTrue(devtool1.is_tracing_running)
  142. self.assertFalse(devtool2.is_tracing_running)
  143. self.assertTrue(devtool3.is_tracing_running)
  144. # Devtool 4 shouldn't have tracing started although it has the same remote
  145. # port as devtool 2
  146. self.assertFalse(devtool4.is_tracing_running)
  147. self.StopTracing(tracing_agent1)
  148. self.assertFalse(devtool1.is_tracing_running)
  149. self.assertFalse(devtool2.is_tracing_running)
  150. self.assertFalse(devtool3.is_tracing_running)
  151. self.assertFalse(devtool4.is_tracing_running)
  152. # Test that it should be ok to start & stop tracing on platform1 again.
  153. tracing_agent1 = self.StartTracing(self.platform1)
  154. self.StopTracing(tracing_agent1)
  155. tracing_agent2 = self.StartTracing(self.platform2)
  156. self.assertTrue(devtool4.is_tracing_running)
  157. self.StopTracing(tracing_agent2)
  158. self.assertFalse(devtool4.is_tracing_running)
  159. def testExceptionRaisedInStopTracing(self):
  160. devtool1 = FakeDevtoolsClient(1)
  161. devtool2 = FakeDevtoolsClient(2)
  162. # Register devtools 1, 2 on platform 1
  163. chrome_tracing_devtools_manager.RegisterDevToolsClient(
  164. devtool1, self.platform1)
  165. chrome_tracing_devtools_manager.RegisterDevToolsClient(
  166. devtool2, self.platform1)
  167. tracing_agent1 = self.StartTracing(self.platform1)
  168. self.assertTrue(devtool1.is_tracing_running)
  169. self.assertTrue(devtool2.is_tracing_running)
  170. devtool1.will_raise_exception_in_stop_tracing = True
  171. with self.assertRaises(chrome_tracing_agent.ChromeTracingStoppedError):
  172. self.StopTracing(tracing_agent1)
  173. # Tracing is stopped on both devtools clients even if there is exception.
  174. self.assertIsNone(tracing_agent1.trace_config)
  175. self.assertFalse(devtool1.is_tracing_running)
  176. self.assertFalse(devtool2.is_tracing_running)
  177. devtool1.is_alive = False
  178. devtool2.is_alive = False
  179. # Register devtools 3 on platform 1 should not raise any exception.
  180. devtool3 = FakeDevtoolsClient(3)
  181. chrome_tracing_devtools_manager.RegisterDevToolsClient(
  182. devtool3, self.platform1)
  183. # Start & Stop tracing on platform 1 should work just fine.
  184. tracing_agent2 = self.StartTracing(self.platform1)
  185. self.StopTracing(tracing_agent2)
  186. @decorators.Enabled('android')
  187. def testCreateAndRemoveTraceConfigFileOnAndroid(self):
  188. platform_backend = FakeAndroidPlatformBackend()
  189. agent = chrome_tracing_agent.ChromeTracingAgent(platform_backend)
  190. self.assertIsNone(agent.trace_config_file)
  191. config = tracing_config.TracingConfig(
  192. tracing_options.TracingOptions(),
  193. tracing_category_filter.TracingCategoryFilter())
  194. agent._CreateTraceConfigFile(config)
  195. self.assertIsNotNone(agent.trace_config_file)
  196. self.assertTrue(platform_backend.device.PathExists(agent.trace_config_file))
  197. config_file_str = platform_backend.device.ReadFile(agent.trace_config_file,
  198. as_root=True)
  199. self.assertEqual(agent._CreateTraceConfigFileString(config),
  200. config_file_str.strip())
  201. config_file_path = agent.trace_config_file
  202. agent._RemoveTraceConfigFile()
  203. self.assertFalse(platform_backend.device.PathExists(config_file_path))
  204. self.assertIsNone(agent.trace_config_file)
  205. # robust to multiple file removal
  206. agent._RemoveTraceConfigFile()
  207. self.assertFalse(platform_backend.device.PathExists(config_file_path))
  208. self.assertIsNone(agent.trace_config_file)
  209. @decorators.Enabled('linux', 'mac', 'win')
  210. def testCreateAndRemoveTraceConfigFileOnDesktop(self):
  211. platform_backend = FakeDesktopPlatformBackend()
  212. agent = chrome_tracing_agent.ChromeTracingAgent(platform_backend)
  213. self.assertIsNone(agent.trace_config_file)
  214. config = tracing_config.TracingConfig(
  215. tracing_options.TracingOptions(),
  216. tracing_category_filter.TracingCategoryFilter())
  217. agent._CreateTraceConfigFile(config)
  218. self.assertIsNotNone(agent.trace_config_file)
  219. self.assertTrue(os.path.exists(agent.trace_config_file))
  220. self.assertTrue(os.stat(agent.trace_config_file).st_mode & stat.S_IROTH)
  221. with open(agent.trace_config_file, 'r') as f:
  222. config_file_str = f.read()
  223. self.assertEqual(agent._CreateTraceConfigFileString(config),
  224. config_file_str.strip())
  225. config_file_path = agent.trace_config_file
  226. agent._RemoveTraceConfigFile()
  227. self.assertFalse(os.path.exists(config_file_path))
  228. self.assertIsNone(agent.trace_config_file)
  229. # robust to multiple file removal
  230. agent._RemoveTraceConfigFile()
  231. self.assertFalse(os.path.exists(config_file_path))
  232. self.assertIsNone(agent.trace_config_file)