iossim_util_test.py 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. #!/usr/bin/env vpython3
  2. # Copyright 2019 The Chromium Authors. All rights reserved.
  3. # Use of this source code is governed by a BSD-style license that can be
  4. # found in the LICENSE file.
  5. """Unittests for iossim_util.py."""
  6. import mock
  7. import unittest
  8. import iossim_util
  9. import test_runner
  10. import test_runner_test
  11. SIMULATORS_LIST = {
  12. 'devices': {
  13. 'com.apple.CoreSimulator.SimRuntime.iOS-11-4': [{
  14. 'isAvailable':
  15. True,
  16. 'name':
  17. 'iPhone 5s 11.4 test simulator',
  18. 'deviceTypeIdentifier':
  19. 'com.apple.CoreSimulator.SimDeviceType.iPhone-5s',
  20. 'state':
  21. 'Shutdown',
  22. 'udid':
  23. 'E4E66320-177A-450A-9BA1-488D85B7278E'
  24. }],
  25. 'com.apple.CoreSimulator.SimRuntime.iOS-13-2': [{
  26. 'isAvailable':
  27. True,
  28. 'name':
  29. 'iPhone X 13.2.2 test simulator',
  30. 'deviceTypeIdentifier':
  31. 'com.apple.CoreSimulator.SimDeviceType.iPhone-X',
  32. 'state':
  33. 'Shutdown',
  34. 'udid':
  35. 'E4E66321-177A-450A-9BA1-488D85B7278E'
  36. }, {
  37. 'isAvailable':
  38. True,
  39. 'name':
  40. 'iPhone 11 13.2.2 test simulator',
  41. 'deviceTypeIdentifier':
  42. 'com.apple.CoreSimulator.SimDeviceType.iPhone-11',
  43. 'state':
  44. 'Shutdown',
  45. 'udid':
  46. 'A4E66321-177A-450A-9BA1-488D85B7278E'
  47. }]
  48. },
  49. 'devicetypes': [
  50. {
  51. 'name': 'iPhone 5s',
  52. 'bundlePath': '/path/iPhone 4s/Content',
  53. 'identifier': 'com.apple.CoreSimulator.SimDeviceType.iPhone-5s'
  54. },
  55. {
  56. 'name': 'iPhone X',
  57. 'bundlePath': '/path/iPhone X/Content',
  58. 'identifier': 'com.apple.CoreSimulator.SimDeviceType.iPhone-X'
  59. },
  60. {
  61. 'name': 'iPhone 11',
  62. 'bundlePath': '/path/iPhone 11/Content',
  63. 'identifier': 'com.apple.CoreSimulator.SimDeviceType.iPhone-11'
  64. },
  65. ],
  66. 'pairs': [],
  67. 'runtimes': [
  68. {
  69. "buildversion": "15F79",
  70. "bundlePath": "/path/Runtimes/iOS 11.4.simruntime",
  71. "identifier": "com.apple.CoreSimulator.SimRuntime.iOS-11-4",
  72. "isAvailable": True,
  73. "name": "iOS 11.4",
  74. "version": "11.4"
  75. },
  76. {
  77. "buildversion": "17A844",
  78. "bundlePath": "/path/Runtimes/iOS 13.1.simruntime",
  79. "identifier": "com.apple.CoreSimulator.SimRuntime.iOS-13-1",
  80. "isAvailable": True,
  81. "name": "iOS 13.1",
  82. "version": "13.1"
  83. },
  84. {
  85. "buildversion": "17B102",
  86. "bundlePath": "/path/Runtimes/iOS.simruntime",
  87. "identifier": "com.apple.CoreSimulator.SimRuntime.iOS-13-2",
  88. "isAvailable": True,
  89. "name": "iOS 13.2",
  90. "version": "13.2.2"
  91. },
  92. ]
  93. }
  94. @mock.patch.object(
  95. iossim_util, 'get_simulator_list', return_value=SIMULATORS_LIST)
  96. class GetiOSSimUtil(test_runner_test.TestCase):
  97. """Tests for iossim_util.py."""
  98. def setUp(self):
  99. super(GetiOSSimUtil, self).setUp()
  100. def test_get_simulator_runtime_by_version(self, _):
  101. """Ensures correctness of filter."""
  102. self.assertEqual(
  103. 'com.apple.CoreSimulator.SimRuntime.iOS-13-2',
  104. iossim_util.get_simulator_runtime_by_version(
  105. iossim_util.get_simulator_list(), '13.2.2'))
  106. def test_get_simulator_runtime_by_version_not_found(self, _):
  107. """Ensures that SimulatorNotFoundError raises if no runtime."""
  108. with self.assertRaises(test_runner.SimulatorNotFoundError) as context:
  109. iossim_util.get_simulator_runtime_by_version(
  110. iossim_util.get_simulator_list(), '13.2')
  111. expected_message = ('Simulator does not exist: Not found '
  112. '"13.2" SDK in runtimes')
  113. self.assertTrue(expected_message in str(context.exception))
  114. def test_get_simulator_device_type_by_platform(self, _):
  115. """Ensures correctness of filter."""
  116. self.assertEqual(
  117. 'com.apple.CoreSimulator.SimDeviceType.iPhone-11',
  118. iossim_util.get_simulator_device_type_by_platform(
  119. iossim_util.get_simulator_list(), 'iPhone 11'))
  120. def test_get_simulator_device_type_by_platform_not_found(self, _):
  121. """Ensures that SimulatorNotFoundError raises if no platform."""
  122. with self.assertRaises(test_runner.SimulatorNotFoundError) as context:
  123. iossim_util.get_simulator_device_type_by_platform(
  124. iossim_util.get_simulator_list(), 'iPhone XI')
  125. expected_message = ('Simulator does not exist: Not found device '
  126. '"iPhone XI" in devicetypes')
  127. self.assertTrue(expected_message in str(context.exception))
  128. def test_get_simulator_runtime_by_device_udid(self, _):
  129. """Ensures correctness of filter."""
  130. self.assertEqual(
  131. 'com.apple.CoreSimulator.SimRuntime.iOS-13-2',
  132. iossim_util.get_simulator_runtime_by_device_udid(
  133. 'E4E66321-177A-450A-9BA1-488D85B7278E'))
  134. def test_get_simulator_runtime_by_device_udid_not_found(self, _):
  135. """Ensures that SimulatorNotFoundError raises if no device with UDID."""
  136. with self.assertRaises(test_runner.SimulatorNotFoundError) as context:
  137. iossim_util.get_simulator_runtime_by_device_udid('non_existing_UDID')
  138. expected_message = ('Simulator does not exist: Not found simulator with '
  139. '"non_existing_UDID" UDID in devices')
  140. self.assertTrue(expected_message in str(context.exception))
  141. def test_get_simulator_udids_by_platform_and_version(self, _):
  142. """Ensures correctness of filter."""
  143. self.assertEqual(['A4E66321-177A-450A-9BA1-488D85B7278E'],
  144. iossim_util.get_simulator_udids_by_platform_and_version(
  145. 'iPhone 11', '13.2.2'))
  146. def test_get_simulator_udids_by_platform_and_version_not_found(self, _):
  147. """Ensures that filter returns empty list if no device with version."""
  148. self.assertEqual([],
  149. iossim_util.get_simulator_udids_by_platform_and_version(
  150. 'iPhone 11', '13.1'))
  151. @mock.patch('subprocess.check_output', autospec=True)
  152. def test_create_device_by_platform_and_version(self, subprocess_mock, _):
  153. """Ensures that command is correct."""
  154. subprocess_mock.return_value = b'NEW_UDID'
  155. self.assertEqual(
  156. 'NEW_UDID',
  157. iossim_util.create_device_by_platform_and_version(
  158. 'iPhone 11', '13.2.2'))
  159. self.assertEqual([
  160. 'xcrun', 'simctl', 'create', 'iPhone 11 13.2.2 test simulator',
  161. 'com.apple.CoreSimulator.SimDeviceType.iPhone-11',
  162. 'com.apple.CoreSimulator.SimRuntime.iOS-13-2'
  163. ], subprocess_mock.call_args[0][0])
  164. @mock.patch('subprocess.check_output', autospec=True)
  165. def test_delete_simulator_by_udid(self, subprocess_mock, _):
  166. """Ensures that command is correct."""
  167. iossim_util.delete_simulator_by_udid('UDID')
  168. self.assertEqual(['xcrun', 'simctl', 'delete', 'UDID'],
  169. subprocess_mock.call_args[0][0])
  170. @mock.patch('subprocess.check_call', autospec=True)
  171. def test_wipe_simulator_by_platform_and_version(self, subprocess_mock, _):
  172. """Ensures that command is correct."""
  173. iossim_util.wipe_simulator_by_udid('A4E66321-177A-450A-9BA1-488D85B7278E')
  174. self.assertEqual(
  175. ['xcrun', 'simctl', 'erase', 'A4E66321-177A-450A-9BA1-488D85B7278E'],
  176. subprocess_mock.call_args[0][0])
  177. @mock.patch('subprocess.check_output', autospec=True)
  178. def test_get_home_directory(self, subprocess_mock, _):
  179. """Ensures that command is correct."""
  180. subprocess_mock.return_value = b'HOME_DIRECTORY'
  181. self.assertEqual('HOME_DIRECTORY',
  182. iossim_util.get_home_directory('iPhone 11', '13.2.2'))
  183. self.assertEqual([
  184. 'xcrun', 'simctl', 'getenv', 'A4E66321-177A-450A-9BA1-488D85B7278E',
  185. 'HOME'
  186. ], subprocess_mock.call_args[0][0])
  187. @mock.patch.object(iossim_util, 'create_device_by_platform_and_version')
  188. def test_no_new_sim_created_when_one_exists(self, mock_create, _):
  189. """Ensures no simulator is created when one in desired dimension exists."""
  190. self.assertEqual('A4E66321-177A-450A-9BA1-488D85B7278E',
  191. iossim_util.get_simulator('iPhone 11', '13.2.2'))
  192. self.assertFalse(mock_create.called)
  193. if __name__ == '__main__':
  194. unittest.main()