2017-08-12 08:32:15 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-07-11 12:53:17 -04:00
|
|
|
require "test_helper"
|
|
|
|
require "database/setup"
|
|
|
|
|
2017-07-21 16:52:09 -04:00
|
|
|
class ActiveStorage::VariantTest < ActiveSupport::TestCase
|
2017-12-18 07:47:42 -05:00
|
|
|
test "resized variation of JPEG blob" do
|
|
|
|
blob = create_file_blob(filename: "racecar.jpg")
|
|
|
|
variant = blob.variant(resize: "100x100").processed
|
2017-08-14 09:17:50 -04:00
|
|
|
assert_match(/racecar\.jpg/, variant.service_url)
|
2017-07-26 14:58:59 -04:00
|
|
|
|
2017-09-28 16:43:37 -04:00
|
|
|
image = read_image(variant)
|
2017-07-26 14:58:59 -04:00
|
|
|
assert_equal 100, image.width
|
|
|
|
assert_equal 67, image.height
|
2017-07-21 16:51:31 -04:00
|
|
|
end
|
2017-07-11 12:53:17 -04:00
|
|
|
|
2017-12-18 07:47:42 -05:00
|
|
|
test "resized and monochrome variation of JPEG blob" do
|
|
|
|
blob = create_file_blob(filename: "racecar.jpg")
|
|
|
|
variant = blob.variant(resize: "100x100", monochrome: true).processed
|
2017-08-14 09:17:50 -04:00
|
|
|
assert_match(/racecar\.jpg/, variant.service_url)
|
2017-07-26 14:58:59 -04:00
|
|
|
|
2017-09-28 16:43:37 -04:00
|
|
|
image = read_image(variant)
|
2017-07-26 14:58:59 -04:00
|
|
|
assert_equal 100, image.width
|
|
|
|
assert_equal 67, image.height
|
2017-08-04 23:21:21 -04:00
|
|
|
assert_match(/Gray/, image.colorspace)
|
2017-07-11 12:53:17 -04:00
|
|
|
end
|
2017-10-04 15:26:04 -04:00
|
|
|
|
2017-12-22 10:44:34 -05:00
|
|
|
test "center-weighted crop of JPEG blob" do
|
|
|
|
blob = create_file_blob(filename: "racecar.jpg")
|
|
|
|
variant = blob.variant(combine_options: {
|
|
|
|
gravity: "center",
|
|
|
|
resize: "100x100^",
|
|
|
|
crop: "100x100+0+0",
|
|
|
|
}).processed
|
|
|
|
assert_match(/racecar\.jpg/, variant.service_url)
|
|
|
|
|
|
|
|
image = read_image(variant)
|
|
|
|
assert_equal 100, image.width
|
|
|
|
assert_equal 100, image.height
|
|
|
|
end
|
|
|
|
|
2017-12-18 07:47:42 -05:00
|
|
|
test "resized variation of PSD blob" do
|
|
|
|
blob = create_file_blob(filename: "icon.psd", content_type: "image/vnd.adobe.photoshop")
|
|
|
|
variant = blob.variant(resize: "20x20").processed
|
|
|
|
assert_match(/icon\.png/, variant.service_url)
|
|
|
|
|
|
|
|
image = read_image(variant)
|
|
|
|
assert_equal "PNG", image.type
|
|
|
|
assert_equal 20, image.width
|
|
|
|
assert_equal 20, image.height
|
|
|
|
end
|
|
|
|
|
2018-01-02 07:19:39 -05:00
|
|
|
test "optimized variation of GIF blob" do
|
|
|
|
blob = create_file_blob(filename: "image.gif", content_type: "image/gif")
|
|
|
|
|
|
|
|
assert_nothing_raised do
|
|
|
|
blob.variant(layers: "Optimize").processed
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-12-15 10:45:00 -05:00
|
|
|
test "variation of invariable blob" do
|
2018-01-10 12:12:04 -05:00
|
|
|
assert_raises ActiveStorage::InvariableError do
|
2017-12-15 10:45:00 -05:00
|
|
|
create_file_blob(filename: "report.pdf", content_type: "application/pdf").variant(resize: "100x100")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-10-04 15:26:04 -04:00
|
|
|
test "service_url doesn't grow in length despite long variant options" do
|
2017-12-18 07:47:42 -05:00
|
|
|
blob = create_file_blob(filename: "racecar.jpg")
|
|
|
|
variant = blob.variant(font: "a" * 10_000).processed
|
2018-01-15 13:06:17 -05:00
|
|
|
assert_operator variant.service_url.length, :<, 525
|
2017-10-04 15:26:04 -04:00
|
|
|
end
|
2017-07-11 12:53:17 -04:00
|
|
|
end
|