Skip to content
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
2 changes: 1 addition & 1 deletion lib/capybara/screenshot/diff/drivers/vips_driver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def save_image_to(image, filename)
end

def resize_image_to(image, new_width, new_height)
image.resize(new_width.to_f / new_height)
image.resize(new_width.to_f / image.width, vscale: new_height.to_f / image.height)
end

def load_images(old_file_name, new_file_name)
Expand Down
27 changes: 22 additions & 5 deletions test/support/driver_contract_tests.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,19 @@ module DriverContractTests
end

# load_images -------------------------------------------------------------
# Strengthened (vs. #204's original): a.png/b.png share dimensions, so a
# swapped return would go undetected. Uses fixtures with DIFFERENT
# dimensions so slot identity is verifiable by content.

test "[contract] #load_images loads both images from disk in (old, new) order" do
test "[contract] #load_images returns [old_image, new_image] without swapping slots" do
driver = make_comparison(:a, :a).driver
old_path = TEST_IMAGES_DIR / "a.png" # 80x80
new_path = TEST_IMAGES_DIR / "a_cropped.png" # 80x60

old_image, new_image = driver.load_images(TEST_IMAGES_DIR / "a.png", TEST_IMAGES_DIR / "b.png")
old_image, new_image = driver.load_images(old_path, new_path)

assert_not_nil old_image
assert_not_nil new_image
assert_equal driver.dimension(old_image), driver.dimension(new_image)
assert_equal [80, 80], driver.dimension(old_image)
assert_equal [80, 60], driver.dimension(new_image)
end

# find_difference_region result shape --------------------------------------
Expand Down Expand Up @@ -196,5 +200,18 @@ module DriverContractTests
end
assert_match(/no new screenshot/, error.message)
end

# Resize behavior -------------------------------------------------------
# Behavioral (vs. #204's method-presence check above): asserts the actual
# output dimensions, not just that #resize_image_to responds.

test "[contract] resize_image_to resizes a non-square source to the exact requested non-square dimensions" do
driver = make_comparison(:a, :a).driver
source = driver.from_file(TEST_IMAGES_DIR / "portrait.png") # 3x6, non-square

resized = driver.resize_image_to(source, 40, 30)

assert_equal [40, 30], driver.dimension(resized)
end
end
end
11 changes: 11 additions & 0 deletions test/unit/screenshoter_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,17 @@ class ScreenshoterTest < ActiveSupport::TestCase

assert_nil screenshoter.prepare_page_for_screenshot(timeout: nil) # does not raise an error
end

test "#resize_if_needed halves a non-square retina screenshot to the expected window size via VipsDriver" do
screenshoter = Screenshoter.new({}, {driver: :vips})
retina_image = Vips::Image.black(2560, 1600) # 2x window size, non-square

resized = Screenshot.stub(:window_size, [1280, 1024]) do
screenshoter.send(:resize_if_needed, retina_image)
end

assert_equal [1280, 800], screenshoter.driver.dimension(resized)
end
end
end
end
Loading