Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: add and use parse_pixels in tests #876

Merged
merged 1 commit into from
Jun 2, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 50 additions & 50 deletions weasyprint/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

from .. import CSS, HTML, __main__, default_url_fetcher
from ..urls import path2url
from .test_draw import B, _, assert_pixels_equal, image_to_pixels, r
from .test_draw import assert_pixels_equal, image_to_pixels, parse_pixels
from .testing_utils import (
FakeHTML, assert_no_logs, capture_logs, http_server, resource_filename)

Expand Down Expand Up @@ -173,65 +173,65 @@ def check_css(css):

def check_png_pattern(png_bytes, x2=False, blank=False, rotated=False):
if blank:
expected_pixels = [
_ + _ + _ + _ + _ + _ + _ + _,
_ + _ + _ + _ + _ + _ + _ + _,
_ + _ + _ + _ + _ + _ + _ + _,
_ + _ + _ + _ + _ + _ + _ + _,
_ + _ + _ + _ + _ + _ + _ + _,
_ + _ + _ + _ + _ + _ + _ + _,
_ + _ + _ + _ + _ + _ + _ + _,
_ + _ + _ + _ + _ + _ + _ + _,
]
expected_pixels = '''
________
________
________
________
________
________
________
________
'''
size = 8
elif x2:
expected_pixels = [
_ + _ + _ + _ + _ + _ + _ + _ + _ + _ + _ + _ + _ + _ + _ + _,
_ + _ + _ + _ + _ + _ + _ + _ + _ + _ + _ + _ + _ + _ + _ + _,
_ + _ + _ + _ + _ + _ + _ + _ + _ + _ + _ + _ + _ + _ + _ + _,
_ + _ + _ + _ + _ + _ + _ + _ + _ + _ + _ + _ + _ + _ + _ + _,
_ + _ + _ + _ + r + r + B + B + B + B + B + B + _ + _ + _ + _,
_ + _ + _ + _ + r + r + B + B + B + B + B + B + _ + _ + _ + _,
_ + _ + _ + _ + B + B + B + B + B + B + B + B + _ + _ + _ + _,
_ + _ + _ + _ + B + B + B + B + B + B + B + B + _ + _ + _ + _,
_ + _ + _ + _ + B + B + B + B + B + B + B + B + _ + _ + _ + _,
_ + _ + _ + _ + B + B + B + B + B + B + B + B + _ + _ + _ + _,
_ + _ + _ + _ + B + B + B + B + B + B + B + B + _ + _ + _ + _,
_ + _ + _ + _ + B + B + B + B + B + B + B + B + _ + _ + _ + _,
_ + _ + _ + _ + _ + _ + _ + _ + _ + _ + _ + _ + _ + _ + _ + _,
_ + _ + _ + _ + _ + _ + _ + _ + _ + _ + _ + _ + _ + _ + _ + _,
_ + _ + _ + _ + _ + _ + _ + _ + _ + _ + _ + _ + _ + _ + _ + _,
_ + _ + _ + _ + _ + _ + _ + _ + _ + _ + _ + _ + _ + _ + _ + _,
]
expected_pixels = '''
________________
________________
________________
________________
____rrBBBBBB____
____rrBBBBBB____
____BBBBBBBB____
____BBBBBBBB____
____BBBBBBBB____
____BBBBBBBB____
____BBBBBBBB____
____BBBBBBBB____
________________
________________
________________
________________
'''
size = 16
elif rotated:
expected_pixels = [
_ + _ + _ + _ + _ + _ + _ + _,
_ + _ + _ + _ + _ + _ + _ + _,
_ + _ + B + B + B + B + _ + _,
_ + _ + B + B + B + B + _ + _,
_ + _ + B + B + B + B + _ + _,
_ + _ + r + B + B + B + _ + _,
_ + _ + _ + _ + _ + _ + _ + _,
_ + _ + _ + _ + _ + _ + _ + _,
]
expected_pixels = '''
________
________
__BBBB__
__BBBB__
__BBBB__
__rBBB__
________
________
'''
size = 8
else:
expected_pixels = [
_ + _ + _ + _ + _ + _ + _ + _,
_ + _ + _ + _ + _ + _ + _ + _,
_ + _ + r + B + B + B + _ + _,
_ + _ + B + B + B + B + _ + _,
_ + _ + B + B + B + B + _ + _,
_ + _ + B + B + B + B + _ + _,
_ + _ + _ + _ + _ + _ + _ + _,
_ + _ + _ + _ + _ + _ + _ + _,
]
expected_pixels = '''
________
________
__rBBB__
__BBBB__
__BBBB__
__BBBB__
________
________
'''
size = 8
surface = cairo.ImageSurface.create_from_png(io.BytesIO(png_bytes))
assert_pixels_equal('api_png', size, size,
image_to_pixels(surface, size, size),
b''.join(expected_pixels))
b"".join(parse_pixels(expected_pixels)))


@assert_no_logs
Expand Down
45 changes: 41 additions & 4 deletions weasyprint/tests/test_draw/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,56 @@
R = as_pixel(b'\xff\x00\x00\xff') # red
B = as_pixel(b'\x00\x00\xff\xff') # blue
G = as_pixel(b'\x00\xff\x00\xff') # lime green
V = as_pixel(b'\xBF\x00\x40\xff') # Average of 1*B and 3*r.
S = as_pixel(b'\xff\x3f\x3f\xff') # r above r above #fff
V = as_pixel(b'\xBF\x00\x40\xff') # Average of 1*B and 3*R.
S = as_pixel(b'\xff\x3f\x3f\xff') # R above R above #fff
r = as_pixel(b'\xff\x00\x00\xff') # red
g = as_pixel(b'\x00\x80\x00\xff') # half green
b = as_pixel(b'\x00\x00\x80\xff') # half blue
v = as_pixel(b'\x80\x00\x80\xff') # Average of B and r.
v = as_pixel(b'\x80\x00\x80\xff') # Average of B and R.
a = as_pixel(b'\x00\x00\xfe\xff') # JPG is lossy...
p = as_pixel(b'\xc0\x00\x3f\xff') # r above r above B above #fff.
p = as_pixel(b'\xc0\x00\x3f\xff') # R above R above B above #fff.

# NOTE: "r" is not half red on purpose. In the pixel strings it has
# better contrast with "B" than does "R". eg. "rBBBrrBrB" vs "RBBBRRBRB".
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@liZe Can you clarify if this is correct?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't remember the reason, but it looks like a plausible explanation.


PIXELS_BY_CHAR = {
"_": _,
"R": R,
"B": B,
"G": G,
"V": V,
"S": S,
"r": r,
"g": g,
"b": b,
"v": v,
"a": a,
"p": p,
}


def parse_pixels(pixels, pix_overrides=None):
if pix_overrides is None:
pix_by_char = PIXELS_BY_CHAR
else:
pix_by_char = PIXELS_BY_CHAR.copy()
pix_by_char.update(pix_overrides)

pixel_lines = []
for line in pixels.splitlines():
line = line.split("#")[0].strip()
if not line:
continue
line_pixels = b"".join(pix_by_char[char] for char in line)
pixel_lines.append(line_pixels)
return pixel_lines


def assert_pixels(name, expected_width, expected_height, expected_pixels,
html):
"""Helper testing the size of the image and the pixels values."""
if isinstance(expected_pixels, str):
expected_pixels = parse_pixels(expected_pixels)
assert len(expected_pixels) == expected_height
assert len(expected_pixels[0]) == expected_width * 4
expected_raw = b''.join(expected_pixels)
Expand Down
Loading