Python library for working with the PD Buddy Sink Serial Console Configuration Interface
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

__init__.py 25KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588
  1. """Unit tests for the top-level pdbuddy classes"""
  2. import unittest
  3. import pdbuddy
  4. class SinkTestCase(unittest.TestCase):
  5. def setUp(self):
  6. # Get devices
  7. pdbs_devices = list(pdbuddy.Sink.get_devices())
  8. # If there are no devices, skip the test
  9. if len(pdbs_devices) == 0:
  10. self.skipTest("No PD Buddy Sink devices found")
  11. # Open the first device
  12. self.pdbs = pdbuddy.Sink(pdbs_devices[0])
  13. self.obj_valid = pdbuddy.SinkConfig(status=pdbuddy.SinkStatus.VALID,
  14. flags=pdbuddy.SinkFlags.NONE, v=15000, vmin=None, vmax=None,
  15. i=3000, idim=pdbuddy.SinkDimension.CURRENT)
  16. self.obj_valid_gb = pdbuddy.SinkConfig(status=pdbuddy.SinkStatus.VALID,
  17. flags=pdbuddy.SinkFlags.GIVEBACK, v=15000, vmin=None,
  18. vmax=None, i=3000, idim=pdbuddy.SinkDimension.CURRENT)
  19. self.obj_range = pdbuddy.SinkConfig(status=pdbuddy.SinkStatus.VALID,
  20. flags=pdbuddy.SinkFlags.HV_PREFERRED, v=13800, vmin=12000,
  21. vmax=16000, i=2000, idim=pdbuddy.SinkDimension.CURRENT)
  22. self.obj_huge_v = pdbuddy.SinkConfig(status=pdbuddy.SinkStatus.VALID,
  23. flags=pdbuddy.SinkFlags.NONE, v=65536, vmin=None, vmax=None,
  24. i=1000, idim=pdbuddy.SinkDimension.CURRENT)
  25. self.obj_big_v = pdbuddy.SinkConfig(status=pdbuddy.SinkStatus.VALID,
  26. flags=pdbuddy.SinkFlags.NONE, v=21001, vmin=None, vmax=None,
  27. i=1000, idim=pdbuddy.SinkDimension.CURRENT)
  28. self.obj_neg_v = pdbuddy.SinkConfig(status=pdbuddy.SinkStatus.VALID,
  29. flags=pdbuddy.SinkFlags.NONE, v=-1, vmin=None, vmax=None,
  30. i=1000, idim=pdbuddy.SinkDimension.CURRENT)
  31. self.obj_inv_range = pdbuddy.SinkConfig(status=pdbuddy.SinkStatus.VALID,
  32. flags=pdbuddy.SinkFlags.HV_PREFERRED, v=13800, vmin=16000,
  33. vmax=12000, i=2000, idim=pdbuddy.SinkDimension.CURRENT)
  34. self.obj_huge_i = pdbuddy.SinkConfig(status=pdbuddy.SinkStatus.VALID,
  35. flags=pdbuddy.SinkFlags.NONE, v=5000, vmin=None, vmax=None,
  36. i=65536, idim=pdbuddy.SinkDimension.CURRENT)
  37. self.obj_big_i = pdbuddy.SinkConfig(status=pdbuddy.SinkStatus.VALID,
  38. flags=pdbuddy.SinkFlags.NONE, v=5000, vmin=None, vmax=None,
  39. i=5001, idim=pdbuddy.SinkDimension.CURRENT)
  40. self.obj_neg_i = pdbuddy.SinkConfig(status=pdbuddy.SinkStatus.VALID,
  41. flags=pdbuddy.SinkFlags.NONE, v=5000, vmin=None, vmax=None,
  42. i=-1, idim=pdbuddy.SinkDimension.CURRENT)
  43. def tearDown(self):
  44. # Close the connection to the PD Buddy Sink
  45. self.pdbs.close()
  46. def test_identify(self):
  47. self.pdbs.identify()
  48. def test_help(self):
  49. help_text = self.pdbs.help()
  50. self.assertTrue(len(help_text) > 0)
  51. self.assertTrue(len(help_text[0]) > 0)
  52. def test_license(self):
  53. license_text = self.pdbs.license()
  54. self.assertTrue(len(license_text) > 0)
  55. self.assertTrue(len(license_text[0]) > 0)
  56. def test_set_tmpcfg_valid(self):
  57. self.pdbs.set_tmpcfg(self.obj_valid)
  58. self.assertEqual(self.pdbs.get_tmpcfg(), self.obj_valid)
  59. def test_set_tmpcfg_valid_gb(self):
  60. self.pdbs.set_tmpcfg(self.obj_valid_gb)
  61. self.assertEqual(self.pdbs.get_tmpcfg(), self.obj_valid_gb)
  62. def test_set_tmpcfg_range(self):
  63. self.pdbs.set_tmpcfg(self.obj_range)
  64. self.assertEqual(self.pdbs.get_tmpcfg(), self.obj_range)
  65. def test_set_tmpcfg_huge_v(self):
  66. with self.assertRaises(ValueError):
  67. self.pdbs.set_tmpcfg(self.obj_huge_v)
  68. def test_set_tmpcfg_big_v(self):
  69. with self.assertRaises(ValueError):
  70. self.pdbs.set_tmpcfg(self.obj_big_v)
  71. def test_set_tmpcfg_neg_v(self):
  72. with self.assertRaises(ValueError):
  73. self.pdbs.set_tmpcfg(self.obj_neg_v)
  74. def test_set_tmpcfg_inv_range(self):
  75. with self.assertRaises(ValueError):
  76. self.pdbs.set_tmpcfg(self.obj_inv_range)
  77. def test_set_tmpcfg_huge_i(self):
  78. with self.assertRaises(ValueError):
  79. self.pdbs.set_tmpcfg(self.obj_huge_i)
  80. def test_set_tmpcfg_big_i(self):
  81. with self.assertRaises(ValueError):
  82. self.pdbs.set_tmpcfg(self.obj_big_i)
  83. def test_set_tmpcfg_neg_i(self):
  84. with self.assertRaises(ValueError):
  85. self.pdbs.set_tmpcfg(self.obj_neg_i)
  86. def test_write(self):
  87. self.test_set_tmpcfg_valid()
  88. self.pdbs.write()
  89. self.assertEqual(self.pdbs.get_cfg(), self.obj_valid)
  90. def test_get_cfg_index(self):
  91. self.assertIsInstance(self.pdbs.get_cfg(0), pdbuddy.SinkConfig)
  92. def test_get_cfg_index_bad(self):
  93. with self.assertRaises(IndexError):
  94. self.pdbs.get_cfg(-1)
  95. def test_load(self):
  96. # Write obj_valid to flash
  97. self.test_write()
  98. # Write obj_valid_gb to tmpcfg
  99. self.test_set_tmpcfg_valid_gb()
  100. self.assertNotEqual(self.pdbs.get_cfg(), self.pdbs.get_tmpcfg())
  101. # Load flash to tmpcfg
  102. self.pdbs.load()
  103. self.assertEqual(self.pdbs.get_cfg(), self.pdbs.get_tmpcfg())
  104. def test_erase(self):
  105. self.pdbs.erase()
  106. with self.assertRaises(KeyError):
  107. self.pdbs.load()
  108. def test_context_manager(self):
  109. self.pdbs.close()
  110. with pdbuddy.Sink(list(pdbuddy.Sink.get_devices())[0]) as pdbs:
  111. # Test something with the conext manager. For example, this is
  112. # essentially test_get_cfg_index.
  113. self.assertIsInstance(pdbs.get_cfg(0), pdbuddy.SinkConfig)
  114. def test_output(self):
  115. try:
  116. self.pdbs.output = False
  117. self.assertFalse(self.pdbs.output)
  118. self.pdbs.output = True
  119. self.assertTrue(self.pdbs.output)
  120. except KeyError:
  121. self.skipTest("Command output not supported")
  122. except ValueError:
  123. self.skipTest("Unknown value returned by PD Buddy Sink")
  124. def test_get_source_cap(self):
  125. self.assertIsInstance(self.pdbs.get_source_cap(), list)
  126. def test_send_command_invalid(self):
  127. with self.assertRaises(KeyError):
  128. self.pdbs.send_command("foo bar")
  129. class SinkConfigTestCase(unittest.TestCase):
  130. def setUp(self):
  131. self.obj_none = pdbuddy.SinkConfig(status=None, flags=None, v=None,
  132. vmin=None, vmax=None, i=None, idim=None)
  133. self.obj_empty = pdbuddy.SinkConfig(status=pdbuddy.SinkStatus.EMPTY,
  134. flags=None, v=None, vmin=None, vmax=None, i=None, idim=None)
  135. self.obj_valid = pdbuddy.SinkConfig(status=pdbuddy.SinkStatus.VALID,
  136. flags=pdbuddy.SinkFlags.NONE, v=15000, vmin=None, vmax=None,
  137. i=3000, idim=pdbuddy.SinkDimension.CURRENT)
  138. self.obj_invalid = pdbuddy.SinkConfig(status=pdbuddy.SinkStatus.INVALID,
  139. flags=pdbuddy.SinkFlags.NONE, v=15000, vmin=None, vmax=None,
  140. i=3000, idim=pdbuddy.SinkDimension.CURRENT)
  141. self.obj_valid_gb = pdbuddy.SinkConfig(status=pdbuddy.SinkStatus.VALID,
  142. flags=pdbuddy.SinkFlags.GIVEBACK, v=15000, vmin=None,
  143. vmax=None, i=3000, idim=pdbuddy.SinkDimension.CURRENT)
  144. self.obj_valid_5v = pdbuddy.SinkConfig(status=pdbuddy.SinkStatus.VALID,
  145. flags=pdbuddy.SinkFlags.NONE, v=5000, vmin=None, vmax=None,
  146. i=3000, idim=pdbuddy.SinkDimension.CURRENT)
  147. self.obj_valid_1a = pdbuddy.SinkConfig(status=pdbuddy.SinkStatus.VALID,
  148. flags=pdbuddy.SinkFlags.NONE, v=15000, vmin=None, vmax=None,
  149. i=1000, idim=pdbuddy.SinkDimension.CURRENT)
  150. self.obj_valid_range = pdbuddy.SinkConfig(status=pdbuddy.SinkStatus.VALID,
  151. flags=pdbuddy.SinkFlags.NONE, v=15000, vmin=12000, vmax=16000,
  152. i=1000, idim=pdbuddy.SinkDimension.CURRENT)
  153. self.obj_valid_hv = pdbuddy.SinkConfig(status=pdbuddy.SinkStatus.VALID,
  154. flags=pdbuddy.SinkFlags.HV_PREFERRED, v=15000, vmin=12000,
  155. vmax=16000, i=1000, idim=pdbuddy.SinkDimension.CURRENT)
  156. self.obj_valid_10w = pdbuddy.SinkConfig(status=pdbuddy.SinkStatus.VALID,
  157. flags=pdbuddy.SinkFlags.NONE, v=15000, vmin=None, vmax=None,
  158. i=10000, idim=pdbuddy.SinkDimension.POWER)
  159. self.obj_valid_10r = pdbuddy.SinkConfig(status=pdbuddy.SinkStatus.VALID,
  160. flags=pdbuddy.SinkFlags.NONE, v=15000, vmin=None, vmax=None,
  161. i=10000, idim=pdbuddy.SinkDimension.RESISTANCE)
  162. def test_str_none(self):
  163. self.assertEqual(str(self.obj_none), "No configuration")
  164. def test_str_empty(self):
  165. self.assertEqual(str(self.obj_empty), "status: empty")
  166. def test_str_valid(self):
  167. self.assertEqual(str(self.obj_valid),
  168. "status: valid\nflags: (none)\nv: 15.000 V\ni: 3.00 A")
  169. def test_str_invalid(self):
  170. self.assertEqual(str(self.obj_invalid),
  171. "status: invalid\nflags: (none)\nv: 15.000 V\ni: 3.00 A")
  172. def test_str_valid_gb(self):
  173. self.assertEqual(str(self.obj_valid_gb),
  174. "status: valid\nflags: GiveBack\nv: 15.000 V\ni: 3.00 A")
  175. def test_str_valid_5v(self):
  176. self.assertEqual(str(self.obj_valid_5v),
  177. "status: valid\nflags: (none)\nv: 5.000 V\ni: 3.00 A")
  178. def test_str_valid_1a(self):
  179. self.assertEqual(str(self.obj_valid_1a),
  180. "status: valid\nflags: (none)\nv: 15.000 V\ni: 1.00 A")
  181. def test_str_valid_range(self):
  182. self.assertEqual(str(self.obj_valid_range),
  183. "status: valid\nflags: (none)\nv: 15.000 V\nvmin: 12.000 V\n"
  184. "vmax: 16.000 V\ni: 1.00 A")
  185. def test_str_valid_hv(self):
  186. self.assertEqual(str(self.obj_valid_hv),
  187. "status: valid\nflags: HV_Preferred\nv: 15.000 V\n"
  188. "vmin: 12.000 V\nvmax: 16.000 V\ni: 1.00 A")
  189. def test_str_valid_10w(self):
  190. self.assertEqual(str(self.obj_valid_10w),
  191. "status: valid\nflags: (none)\nv: 15.000 V\np: 10.00 W")
  192. def test_str_valid_10r(self):
  193. self.assertEqual(str(self.obj_valid_10r),
  194. "status: valid\nflags: (none)\nv: 15.000 V\nr: 10.00 \u03A9")
  195. def test_from_text_none(self):
  196. ft_none = pdbuddy.SinkConfig.from_text([])
  197. self.assertEqual(ft_none, self.obj_none)
  198. def test_from_text_empty(self):
  199. ft_empty = pdbuddy.SinkConfig.from_text([b"status: empty"])
  200. self.assertEqual(ft_empty, self.obj_empty)
  201. def test_from_text_valid(self):
  202. ft_valid = pdbuddy.SinkConfig.from_text([b"status: valid",
  203. b"flags: (none)",
  204. b"v: 15.000 V",
  205. b"i: 3.00 A"])
  206. self.assertEqual(ft_valid, self.obj_valid)
  207. def test_from_text_invalid(self):
  208. ft_invalid = pdbuddy.SinkConfig.from_text([b"status: invalid",
  209. b"flags: (none)",
  210. b"v: 15.000 V",
  211. b"i: 3.00 A"])
  212. self.assertEqual(ft_invalid, self.obj_invalid)
  213. def test_from_text_valid_gb(self):
  214. ft_valid_gb = pdbuddy.SinkConfig.from_text([b"status: valid",
  215. b"flags: GiveBack",
  216. b"v: 15.000 V",
  217. b"i: 3.00 A"])
  218. self.assertEqual(ft_valid_gb, self.obj_valid_gb)
  219. def test_from_text_valid_5v(self):
  220. ft_valid_5v = pdbuddy.SinkConfig.from_text([b"status: valid",
  221. b"flags: (none)",
  222. b"v: 5.000 V",
  223. b"i: 3.00 A"])
  224. self.assertEqual(ft_valid_5v, self.obj_valid_5v)
  225. def test_from_text_valid_1a(self):
  226. ft_valid_1a = pdbuddy.SinkConfig.from_text([b"status: valid",
  227. b"flags: (none)",
  228. b"v: 15.000 V",
  229. b"i: 1.00 A"])
  230. self.assertEqual(ft_valid_1a, self.obj_valid_1a)
  231. def test_from_text_valid_range(self):
  232. ft_valid_range = pdbuddy.SinkConfig.from_text([b"status: valid",
  233. b"flags: (none)",
  234. b"v: 15.000 V",
  235. b"vmin: 12.000 V",
  236. b"vmax: 16.000 V",
  237. b"i: 1.00 A"])
  238. self.assertEqual(ft_valid_range, self.obj_valid_range)
  239. def test_from_text_valid_hv(self):
  240. ft_valid_hv = pdbuddy.SinkConfig.from_text([b"status: valid",
  241. b"flags: HV_Preferred",
  242. b"v: 15.000 V",
  243. b"vmin: 12.000 V",
  244. b"vmax: 16.000 V",
  245. b"i: 1.00 A"])
  246. self.assertEqual(ft_valid_hv, self.obj_valid_hv)
  247. def test_from_text_valid_10w(self):
  248. ft_valid_10w = pdbuddy.SinkConfig.from_text([b"status: valid",
  249. b"flags: (none)",
  250. b"v: 15.000 V",
  251. b"p: 10.00 W"])
  252. self.assertEqual(ft_valid_10w, self.obj_valid_10w)
  253. def test_from_text_valid_10r(self):
  254. ft_valid_10r = pdbuddy.SinkConfig.from_text([b"status: valid",
  255. b"flags: (none)",
  256. b"v: 15.000 V",
  257. b"r: 10.00 \u03A9"])
  258. self.assertEqual(ft_valid_10r, self.obj_valid_10r)
  259. def test_from_text_invalid_index(self):
  260. with self.assertRaises(IndexError):
  261. pdbuddy.SinkConfig.from_text([b"Invalid index"])
  262. def test_from_text_no_configuration(self):
  263. ft_no_config = pdbuddy.SinkConfig.from_text([b"No configuration"])
  264. self.assertEqual(ft_no_config, self.obj_none)
  265. def test_from_text_valid_extra(self):
  266. ft_valid = pdbuddy.SinkConfig.from_text([b"status: valid",
  267. b"flags: (none)",
  268. b"This is an extra line, which shouldn't hurt anything.",
  269. b"v: 15.000 V",
  270. b"i: 3.00 A"])
  271. self.assertEqual(ft_valid, self.obj_valid)
  272. class UnknownPDOTestCase(unittest.TestCase):
  273. def setUp(self):
  274. self.obj_zero = pdbuddy.UnknownPDO(value=0x00000000)
  275. self.obj_notzero = pdbuddy.UnknownPDO(value=0xFFFFFFFF)
  276. def test_str_zero(self):
  277. self.assertEqual(str(self.obj_zero), "00000000")
  278. def test_str_notzero(self):
  279. self.assertEqual(str(self.obj_notzero), "FFFFFFFF")
  280. class SrcFixedPDOTestCase(unittest.TestCase):
  281. def setUp(self):
  282. self.obj_everything = pdbuddy.SrcFixedPDO(True, True, True, True, True,
  283. True, 3, 20000, 5000)
  284. self.obj_minimal = pdbuddy.SrcFixedPDO(False, False, False, False,
  285. False, False, 0, 5000, 1500)
  286. def test_str_everything(self):
  287. self.assertEqual(str(self.obj_everything),
  288. "fixed\n\tdual_role_pwr: 1\n\tusb_suspend: 1\n"
  289. "\tunconstrained_pwr: 1\n\tusb_comms: 1\n\tdual_role_data: 1\n"
  290. "\tunchunked_ext_msg: 1\n\tpeak_i: 3\n\tv: 20.00 V\n"
  291. "\ti: 5.00 A")
  292. def test_str_minimal(self):
  293. self.assertEqual(str(self.obj_minimal),
  294. "fixed\n\tv: 5.00 V\n\ti: 1.50 A")
  295. class TypeCVirtualPDOTestCase(unittest.TestCase):
  296. def setUp(self):
  297. self.obj_1p5a = pdbuddy.TypeCVirtualPDO(1500)
  298. def test_str_1p5a(self):
  299. self.assertEqual(str(self.obj_1p5a), "typec_virtual\n\ti: 1.50 A")
  300. class ReadPDOTestCase(unittest.TestCase):
  301. def setUp(self):
  302. self.src_fixed_everything = pdbuddy.SrcFixedPDO(True, True, True, True,
  303. True, True, 3, 20000, 5000)
  304. self.src_fixed_minimal = pdbuddy.SrcFixedPDO(False, False, False,
  305. False, False, False, 0, 5000, 1500)
  306. self.unknown_zero = pdbuddy.UnknownPDO(value=0x00000000)
  307. self.unknown_notzero = pdbuddy.UnknownPDO(value=0xFFFFFFFF)
  308. self.typec_virtual = pdbuddy.TypeCVirtualPDO(1500)
  309. def test_read_src_fixed_everything(self):
  310. rp_src_fixed_everything = pdbuddy.read_pdo([b"PDO 1: fixed",
  311. b"\tdual_role_pwr: 1",
  312. b"\tusb_suspend: 1",
  313. b"\tunconstrained_pwr: 1",
  314. b"\tusb_comms: 1",
  315. b"\tdual_role_data: 1",
  316. b"\tunchunked_ext_msg: 1",
  317. b"\tpeak_i: 3",
  318. b"\tv: 20.00 V",
  319. b"\ti: 5.00 A"])
  320. self.assertEqual(self.src_fixed_everything, rp_src_fixed_everything)
  321. def test_read_src_fixed_minimal(self):
  322. rp_src_fixed_minimal = pdbuddy.read_pdo([b"PDO 1: fixed",
  323. b"\tv: 5.00 V",
  324. b"\ti: 1.50 A"])
  325. self.assertEqual(self.src_fixed_minimal, rp_src_fixed_minimal)
  326. def test_read_src_fixed_minimal_no_index(self):
  327. rp_src_fixed_minimal = pdbuddy.read_pdo([b"fixed",
  328. b"\tv: 5.00 V",
  329. b"\ti: 1.50 A"])
  330. self.assertEqual(self.src_fixed_minimal, rp_src_fixed_minimal)
  331. def test_read_unknown_zero(self):
  332. rp_unknown_zero = pdbuddy.read_pdo([b"PDO 1: 00000000"])
  333. self.assertEqual(self.unknown_zero, rp_unknown_zero)
  334. def test_read_unknown_notzero(self):
  335. rp_unknown_notzero = pdbuddy.read_pdo([b"PDO 1: FFFFFFFF"])
  336. self.assertEqual(self.unknown_notzero, rp_unknown_notzero)
  337. def test_read_typec_virtual(self):
  338. rp_typec_virtual = pdbuddy.read_pdo([b"PDO 5: typec_virtual",
  339. b"\ti: 1.50 A"])
  340. self.assertEqual(self.typec_virtual, rp_typec_virtual)
  341. def test_read_none(self):
  342. none_pdo = pdbuddy.read_pdo([b"No Source_Capabilities"])
  343. self.assertEqual(none_pdo, None)
  344. class ReadPDOListTestCase(unittest.TestCase):
  345. def setUp(self):
  346. self.src_fixed_everything = pdbuddy.SrcFixedPDO(True, True, True, True,
  347. True, True, 3, 20000, 5000)
  348. self.src_fixed_minimal = pdbuddy.SrcFixedPDO(False, False, False,
  349. False, False, False, 0, 5000, 1500)
  350. self.unknown_zero = pdbuddy.UnknownPDO(value=0x00000000)
  351. self.unknown_notzero = pdbuddy.UnknownPDO(value=0xFFFFFFFF)
  352. self.typec_virtual = pdbuddy.TypeCVirtualPDO(1500)
  353. def test_read_pdo_list(self):
  354. # It's not a legal list for USB Power Delivery, but it works fine for
  355. # testing this code
  356. text = [b"PDO 1: fixed",
  357. b"\tdual_role_pwr: 1",
  358. b"\tusb_suspend: 1",
  359. b"\tunconstrained_pwr: 1",
  360. b"\tusb_comms: 1",
  361. b"\tdual_role_data: 1",
  362. b"\tunchunked_ext_msg: 1",
  363. b"\tpeak_i: 3",
  364. b"\tv: 20.00 V",
  365. b"\ti: 5.00 A",
  366. b"PDO 2: fixed",
  367. b"\tv: 5.00 V",
  368. b"\ti: 1.50 A",
  369. b"PDO 3: 00000000",
  370. b"PDO 4: FFFFFFFF",
  371. b"PDO 5: typec_virtual",
  372. b"\ti: 1.50 A"]
  373. pdo_list = pdbuddy.read_pdo_list(text)
  374. self.assertEqual(pdo_list[0], self.src_fixed_everything)
  375. self.assertEqual(pdo_list[1], self.src_fixed_minimal)
  376. self.assertEqual(pdo_list[2], self.unknown_zero)
  377. self.assertEqual(pdo_list[3], self.unknown_notzero)
  378. self.assertEqual(pdo_list[4], self.typec_virtual)
  379. class PDOListCalculationsTestCase(unittest.TestCase):
  380. def setUp(self):
  381. self.src_fixed_5v_1p5a = pdbuddy.SrcFixedPDO(False, False, True,
  382. False, False, False, 0, 5000, 1500)
  383. self.src_fixed_5v_3a = pdbuddy.SrcFixedPDO(False, False, True, False,
  384. False, False, 0, 5000, 3000)
  385. self.src_fixed_9v_1p6a = pdbuddy.SrcFixedPDO(False, False, False,
  386. False, False, False, 0, 9000, 1600)
  387. self.src_fixed_9v_3a = pdbuddy.SrcFixedPDO(False, False, False, False,
  388. False, False, 0, 9000, 3000)
  389. self.src_fixed_10v_1p5a = pdbuddy.SrcFixedPDO(False, False, False,
  390. False, False, False, 0, 10000, 1500)
  391. self.src_fixed_12v_5a = pdbuddy.SrcFixedPDO(False, False, False, False,
  392. False, False, 0, 12000, 5000)
  393. self.src_fixed_15v_1p8a = pdbuddy.SrcFixedPDO(False, False, False,
  394. False, False, False, 0, 15000, 1800)
  395. self.src_fixed_15v_3a = pdbuddy.SrcFixedPDO(False, False, False, False,
  396. False, False, 0, 15000, 3000)
  397. self.src_fixed_20v_2p25a = pdbuddy.SrcFixedPDO(False, False, False,
  398. False, False, False, 0, 20000, 2250)
  399. self.src_fixed_20v_5a = pdbuddy.SrcFixedPDO(False, False, False, False,
  400. False, False, 0, 20000, 5000)
  401. self.typec_virtual_1p5a = pdbuddy.TypeCVirtualPDO(1500)
  402. def test_calculate_pdp_typec_virtual(self):
  403. self.assertEqual(pdbuddy.calculate_pdp([self.typec_virtual_1p5a]), 7.5)
  404. def test_calculate_pdp_15w(self):
  405. self.assertEqual(pdbuddy.calculate_pdp([self.src_fixed_5v_3a]), 15)
  406. self.assertEqual(pdbuddy.calculate_pdp([self.src_fixed_5v_3a,
  407. self.src_fixed_9v_1p6a]), 15)
  408. def test_calculate_pdp_27w(self):
  409. self.assertEqual(pdbuddy.calculate_pdp([self.src_fixed_5v_3a,
  410. self.src_fixed_9v_3a]), 27)
  411. self.assertEqual(pdbuddy.calculate_pdp([self.src_fixed_5v_3a,
  412. self.src_fixed_9v_3a, self.src_fixed_15v_1p8a]), 27)
  413. def test_calculate_pdp_45w(self):
  414. self.assertEqual(pdbuddy.calculate_pdp([self.src_fixed_5v_3a,
  415. self.src_fixed_9v_3a, self.src_fixed_15v_3a]), 45)
  416. self.assertEqual(pdbuddy.calculate_pdp([self.src_fixed_5v_3a,
  417. self.src_fixed_9v_3a, self.src_fixed_15v_3a,
  418. self.src_fixed_20v_2p25a]), 45)
  419. def test_calculate_pdp_100w(self):
  420. self.assertEqual(pdbuddy.calculate_pdp([self.src_fixed_5v_3a,
  421. self.src_fixed_9v_3a, self.src_fixed_15v_3a,
  422. self.src_fixed_20v_5a]), 100)
  423. self.assertEqual(pdbuddy.calculate_pdp([self.src_fixed_5v_1p5a,
  424. self.src_fixed_12v_5a, self.src_fixed_20v_5a]), 100)
  425. def test_follows_power_rules_true(self):
  426. # <= 15 W
  427. self.assertTrue(pdbuddy.follows_power_rules([]))
  428. self.assertTrue(pdbuddy.follows_power_rules([self.typec_virtual_1p5a]))
  429. self.assertTrue(pdbuddy.follows_power_rules([self.src_fixed_5v_1p5a]))
  430. self.assertTrue(pdbuddy.follows_power_rules([self.src_fixed_5v_3a]))
  431. self.assertTrue(pdbuddy.follows_power_rules([self.src_fixed_5v_3a,
  432. self.src_fixed_9v_1p6a]))
  433. # <= 27 W
  434. self.assertTrue(pdbuddy.follows_power_rules([self.src_fixed_5v_3a,
  435. self.src_fixed_9v_3a]))
  436. self.assertTrue(pdbuddy.follows_power_rules([self.src_fixed_5v_3a,
  437. self.src_fixed_9v_3a, self.src_fixed_15v_1p8a]))
  438. # <= 45 W
  439. self.assertTrue(pdbuddy.follows_power_rules([self.src_fixed_5v_3a,
  440. self.src_fixed_9v_3a, self.src_fixed_15v_3a]))
  441. self.assertTrue(pdbuddy.follows_power_rules([self.src_fixed_5v_3a,
  442. self.src_fixed_9v_3a, self.src_fixed_15v_3a,
  443. self.src_fixed_20v_2p25a]))
  444. # <= 100 W
  445. self.assertTrue(pdbuddy.follows_power_rules([self.src_fixed_5v_3a,
  446. self.src_fixed_9v_3a, self.src_fixed_15v_3a,
  447. self.src_fixed_20v_5a]))
  448. self.assertTrue(pdbuddy.follows_power_rules([self.src_fixed_5v_3a,
  449. self.src_fixed_9v_3a, self.src_fixed_10v_1p5a,
  450. self.src_fixed_12v_5a, self.src_fixed_15v_3a,
  451. self.src_fixed_20v_5a]))
  452. def test_follows_power_rules_false(self):
  453. # <= 15 W
  454. self.assertFalse(pdbuddy.follows_power_rules([self.src_fixed_10v_1p5a]))
  455. self.assertFalse(pdbuddy.follows_power_rules([self.src_fixed_5v_1p5a,
  456. self.src_fixed_10v_1p5a]))
  457. # <= 27 W
  458. self.assertFalse(pdbuddy.follows_power_rules([self.src_fixed_9v_3a]))
  459. self.assertFalse(pdbuddy.follows_power_rules([self.src_fixed_5v_1p5a,
  460. self.src_fixed_9v_3a]))
  461. self.assertFalse(pdbuddy.follows_power_rules([self.src_fixed_5v_3a,
  462. self.src_fixed_9v_1p6a, self.src_fixed_15v_1p8a]))
  463. # <= 45 W
  464. self.assertFalse(pdbuddy.follows_power_rules([self.src_fixed_20v_2p25a]))
  465. self.assertFalse(pdbuddy.follows_power_rules([self.src_fixed_5v_1p5a,
  466. self.src_fixed_9v_3a, self.src_fixed_15v_3a]))
  467. self.assertFalse(pdbuddy.follows_power_rules([self.src_fixed_5v_3a,
  468. self.src_fixed_9v_1p6a, self.src_fixed_15v_3a]))
  469. self.assertFalse(pdbuddy.follows_power_rules([self.src_fixed_5v_3a,
  470. self.src_fixed_9v_3a, self.src_fixed_15v_1p8a,
  471. self.src_fixed_20v_2p25a]))
  472. # <= 100 W
  473. self.assertFalse(pdbuddy.follows_power_rules([self.src_fixed_20v_5a]))
  474. self.assertFalse(pdbuddy.follows_power_rules([self.src_fixed_5v_1p5a,
  475. self.src_fixed_9v_3a, self.src_fixed_15v_3a,
  476. self.src_fixed_20v_5a]))
  477. self.assertFalse(pdbuddy.follows_power_rules([self.src_fixed_5v_3a,
  478. self.src_fixed_9v_1p6a, self.src_fixed_15v_3a,
  479. self.src_fixed_20v_5a]))
  480. self.assertFalse(pdbuddy.follows_power_rules([self.src_fixed_5v_3a,
  481. self.src_fixed_9v_3a, self.src_fixed_15v_1p8a,
  482. self.src_fixed_20v_5a]))
  483. self.assertFalse(pdbuddy.follows_power_rules([self.src_fixed_5v_3a,
  484. self.src_fixed_9v_3a, self.src_fixed_12v_5a,
  485. self.src_fixed_15v_3a, self.src_fixed_20v_2p25a]))