variant_test.rb 2.3 KB
Newer Older
1 2
# frozen_string_literal: true

3 4 5
require "test_helper"
require "database/setup"

D
David Heinemeier Hansson 已提交
6
class ActiveStorage::VariantTest < ActiveSupport::TestCase
7 8 9
  test "resized variation of JPEG blob" do
    blob = create_file_blob(filename: "racecar.jpg")
    variant = blob.variant(resize: "100x100").processed
10
    assert_match(/racecar\.jpg/, variant.service_url)
11

G
George Claghorn 已提交
12
    image = read_image(variant)
13 14
    assert_equal 100, image.width
    assert_equal 67, image.height
15
  end
16

17 18 19
  test "resized and monochrome variation of JPEG blob" do
    blob = create_file_blob(filename: "racecar.jpg")
    variant = blob.variant(resize: "100x100", monochrome: true).processed
20
    assert_match(/racecar\.jpg/, variant.service_url)
21

G
George Claghorn 已提交
22
    image = read_image(variant)
23 24
    assert_equal 100, image.width
    assert_equal 67, image.height
Y
yuuji.yaginuma 已提交
25
    assert_match(/Gray/, image.colorspace)
26
  end
27

28 29 30 31 32 33 34 35 36 37 38 39 40 41
  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

42 43 44 45 46 47 48 49 50 51 52
  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

53 54 55 56 57 58 59 60
  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

61 62 63 64 65 66
  test "variation of invariable blob" do
    assert_raises ActiveStorage::Blob::InvariableError do
      create_file_blob(filename: "report.pdf", content_type: "application/pdf").variant(resize: "100x100")
    end
  end

67
  test "service_url doesn't grow in length despite long variant options" do
68 69
    blob = create_file_blob(filename: "racecar.jpg")
    variant = blob.variant(font: "a" * 10_000).processed
70 71
    assert_operator variant.service_url.length, :<, 500
  end
72
end