test_utils.py 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. # coding: utf-8
  2. #
  3. import threading
  4. import time
  5. import pytest
  6. from PIL import Image
  7. from uiautomator2 import utils
  8. def test_list2cmdline():
  9. testdata = [
  10. [("echo", "hello"), "echo hello"],
  11. [("echo", "hello&world"), "echo 'hello&world'"],
  12. [("What's", "your", "name?"), """'What'"'"'s' your 'name?'"""],
  13. ["echo hello", "echo hello"],
  14. ]
  15. for args, expect in testdata:
  16. cmdline = utils.list2cmdline(args)
  17. assert cmdline == expect, "Args: %s, Expect: %s, Got: %s" % (args, expect, cmdline)
  18. def test_inject_call():
  19. def foo(a, b, c=2):
  20. return a*100+b*10+c
  21. ret = utils.inject_call(foo, a=2, b=4)
  22. assert ret == 242
  23. with pytest.raises(TypeError):
  24. utils.inject_call(foo, 2)
  25. def test_threadsafe_wrapper():
  26. class A:
  27. n = 0
  28. @utils.thread_safe_wrapper
  29. def call(self):
  30. v = self.n
  31. time.sleep(.5)
  32. self.n = v + 1
  33. a = A()
  34. th1 = threading.Thread(name="th1", target=a.call)
  35. th2 = threading.Thread(name="th2", target=a.call)
  36. th1.start()
  37. th2.start()
  38. th1.join()
  39. th2.join()
  40. assert 2 == a.n
  41. def test_is_version_compatiable():
  42. assert utils.is_version_compatiable("1.0.0", "1.0.0")
  43. assert utils.is_version_compatiable("1.0.0", "1.0.1")
  44. assert utils.is_version_compatiable("1.0.0", "1.2.0")
  45. assert utils.is_version_compatiable("1.0.1", "1.1.0")
  46. assert not utils.is_version_compatiable("1.0.1", "2.1.0")
  47. assert not utils.is_version_compatiable("1.3.1", "1.3.0")
  48. assert not utils.is_version_compatiable("1.3.1", "1.2.0")
  49. assert not utils.is_version_compatiable("1.3.1", "1.2.2")
  50. def test_naturalsize():
  51. assert utils.natualsize(1) == "0.0 KB"
  52. assert utils.natualsize(1024) == "1.0 KB"
  53. assert utils.natualsize(1<<20) == "1.0 MB"
  54. assert utils.natualsize(1<<30) == "1.0 GB"
  55. def test_image_convert():
  56. im = Image.new("RGB", (100, 100))
  57. im2 = utils.image_convert(im, "pillow")
  58. assert isinstance(im2, Image.Image)
  59. with pytest.raises(ValueError):
  60. utils.image_convert(im, "unknown")
  61. def test_depreacated():
  62. @utils.deprecated("use bar instead")
  63. def foo():
  64. pass
  65. with pytest.warns(DeprecationWarning):
  66. foo()