png_unittest.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. # Copyright (C) 2012 Balazs Ankes (bank@inf.u-szeged.hu) University of Szeged
  2. #
  3. # Redistribution and use in source and binary forms, with or without
  4. # modification, are permitted provided that the following conditions
  5. # are met:
  6. # 1. Redistributions of source code must retain the above copyright
  7. # notice, this list of conditions and the following disclaimer.
  8. # 2. Redistributions in binary form must reproduce the above copyright
  9. # notice, this list of conditions and the following disclaimer in the
  10. # documentation and/or other materials provided with the distribution.
  11. #
  12. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  13. # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  14. # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  15. # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  16. # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  17. # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  18. # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  19. # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  20. # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  21. # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  22. # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  23. """Unit test for png.py."""
  24. import unittest2 as unittest
  25. from png import PNGChecker
  26. from webkitpy.common.system.filesystem_mock import MockFileSystem
  27. from webkitpy.common.system.systemhost_mock import MockSystemHost
  28. class MockSCMDetector(object):
  29. def __init__(self, scm, prop=None):
  30. self._scm = scm
  31. self._prop = prop
  32. def display_name(self):
  33. return self._scm
  34. def propget(self, pname, path):
  35. return self._prop
  36. class PNGCheckerTest(unittest.TestCase):
  37. """Tests PNGChecker class."""
  38. def test_init(self):
  39. """Test __init__() method."""
  40. def mock_handle_style_error(self):
  41. pass
  42. checker = PNGChecker("test/config", mock_handle_style_error, MockSCMDetector('git'), MockSystemHost())
  43. self.assertEqual(checker._file_path, "test/config")
  44. self.assertEqual(checker._handle_style_error, mock_handle_style_error)
  45. def test_check(self):
  46. errors = []
  47. def mock_handle_style_error(line_number, category, confidence, message):
  48. error = (line_number, category, confidence, message)
  49. errors.append(error)
  50. file_path = ''
  51. fs = MockFileSystem()
  52. scm = MockSCMDetector('svn')
  53. checker = PNGChecker(file_path, mock_handle_style_error, scm, MockSystemHost(filesystem=fs))
  54. checker.check()
  55. self.assertEqual(len(errors), 1)
  56. self.assertEqual(errors[0],
  57. (0, 'image/png', 5, 'Set the svn:mime-type property (svn propset svn:mime-type image/png ).'))
  58. files = {'/Users/mock/.subversion/config': 'enable-auto-props = yes\n*.png = svn:mime-type=image/png'}
  59. fs = MockFileSystem(files)
  60. scm = MockSCMDetector('git')
  61. errors = []
  62. checker = PNGChecker("config", mock_handle_style_error, scm, MockSystemHost(os_name='linux', filesystem=fs))
  63. checker.check()
  64. self.assertEqual(len(errors), 0)
  65. files = {'/Users/mock/.subversion/config': '#enable-auto-props = yes'}
  66. fs = MockFileSystem(files)
  67. scm = MockSCMDetector('git')
  68. errors = []
  69. checker = PNGChecker("config", mock_handle_style_error, scm, MockSystemHost(os_name='linux', filesystem=fs))
  70. checker.check()
  71. self.assertEqual(len(errors), 1)
  72. files = {'/Users/mock/.subversion/config': 'enable-auto-props = yes\n#enable-auto-props = yes\n*.png = svn:mime-type=image/png'}
  73. fs = MockFileSystem(files)
  74. scm = MockSCMDetector('git')
  75. errors = []
  76. checker = PNGChecker("config", mock_handle_style_error, scm, MockSystemHost(os_name='linux', filesystem=fs))
  77. checker.check()
  78. self.assertEqual(len(errors), 0)
  79. files = {'/Users/mock/.subversion/config': '#enable-auto-props = yes\nenable-auto-props = yes\n*.png = svn:mime-type=image/png'}
  80. fs = MockFileSystem(files)
  81. scm = MockSCMDetector('git')
  82. errors = []
  83. checker = PNGChecker("config", mock_handle_style_error, scm, MockSystemHost(os_name='linux', filesystem=fs))
  84. checker.check()
  85. self.assertEqual(len(errors), 0)
  86. files = {'/Users/mock/.subversion/config': 'enable-auto-props = no'}
  87. fs = MockFileSystem(files)
  88. scm = MockSCMDetector('git')
  89. errors = []
  90. checker = PNGChecker("config", mock_handle_style_error, scm, MockSystemHost(os_name='linux', filesystem=fs))
  91. checker.check()
  92. self.assertEqual(len(errors), 1)
  93. file_path = "foo.png"
  94. fs.write_binary_file(file_path, "Dummy binary data")
  95. scm = MockSCMDetector('git')
  96. errors = []
  97. checker = PNGChecker(file_path, mock_handle_style_error, scm, MockSystemHost(os_name='linux', filesystem=fs))
  98. checker.check()
  99. self.assertEqual(len(errors), 1)
  100. file_path = "foo-expected.png"
  101. fs.write_binary_file(file_path, "Dummy binary data")
  102. scm = MockSCMDetector('git')
  103. errors = []
  104. checker = PNGChecker(file_path, mock_handle_style_error, scm, MockSystemHost(os_name='linux', filesystem=fs))
  105. checker.check()
  106. self.assertEqual(len(errors), 2)
  107. self.assertEqual(errors[0], (0, 'image/png', 5, 'Image lacks a checksum. Generate pngs using run-webkit-tests to ensure they have a checksum.'))