variant_test.rb 5.5 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
  test "resized variation of JPEG blob" do
    blob = create_file_blob(filename: "racecar.jpg")
9
    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
  test "resized and monochrome variation of JPEG blob" do
    blob = create_file_blob(filename: "racecar.jpg")
19
    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 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
  test "monochrome with default variant_processor" do
    begin
      ActiveStorage.variant_processor = nil

      blob = create_file_blob(filename: "racecar.jpg")
      variant = blob.variant(monochrome: true).processed
      image = read_image(variant)
      assert_match(/Gray/, image.colorspace)
    ensure
      ActiveStorage.variant_processor = :mini_magick
    end
  end

  test "disabled variation of JPEG blob" do
    blob = create_file_blob(filename: "racecar.jpg")
    variant = blob.variant(resize: "100x100", monochrome: false).processed
    assert_match(/racecar\.jpg/, variant.service_url)

    image = read_image(variant)
    assert_equal 100, image.width
    assert_equal 67, image.height
    assert_match(/RGB/, image.colorspace)
  end

  test "disabled variation of JPEG blob with :combine_options" do
    blob = create_file_blob(filename: "racecar.jpg")
    variant = ActiveSupport::Deprecation.silence do
      blob.variant(combine_options: {
        resize: "100x100",
        monochrome: false
      }).processed
    end
    assert_match(/racecar\.jpg/, variant.service_url)

    image = read_image(variant)
    assert_equal 100, image.width
    assert_equal 67, image.height
    assert_match(/RGB/, image.colorspace)
  end

  test "disabled variation using :combine_options" do
    begin
      ActiveStorage.variant_processor = nil
      blob = create_file_blob(filename: "racecar.jpg")
      variant = ActiveSupport::Deprecation.silence do
        blob.variant(combine_options: {
          crop: "100x100+0+0",
          monochrome: false
        }).processed
      end
      assert_match(/racecar\.jpg/, variant.service_url)

      image = read_image(variant)
      assert_equal 100, image.width
      assert_equal 100, image.height
      assert_match(/RGB/, image.colorspace)
    ensure
      ActiveStorage.variant_processor = :mini_magick
    end
  end

89
  test "center-weighted crop of JPEG blob using :combine_options" do
90
    begin
91
      ActiveStorage.variant_processor = nil
92 93 94 95 96 97 98 99 100
      blob = create_file_blob(filename: "racecar.jpg")
      variant = ActiveSupport::Deprecation.silence do
        blob.variant(combine_options: {
          gravity: "center",
          resize: "100x100^",
          crop: "100x100+0+0",
        }).processed
      end
      assert_match(/racecar\.jpg/, variant.service_url)
101

102 103 104 105
      image = read_image(variant)
      assert_equal 100, image.width
      assert_equal 100, image.height
    ensure
106
      ActiveStorage.variant_processor = :mini_magick
107
    end
108 109
  end

110 111 112 113 114 115 116 117 118 119
  test "center-weighted crop of JPEG blob using :resize_to_fill" do
    blob = create_file_blob(filename: "racecar.jpg")
    variant = blob.variant(resize_to_fill: [100, 100]).processed
    assert_match(/racecar\.jpg/, variant.service_url)

    image = read_image(variant)
    assert_equal 100, image.width
    assert_equal 100, image.height
  end

120 121 122 123 124 125 126 127 128 129 130
  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

G
George Claghorn 已提交
131 132 133 134 135 136 137 138 139 140 141
  test "resized variation of ICO blob" do
    blob = create_file_blob(filename: "favicon.ico", content_type: "image/vnd.microsoft.icon")
    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

142 143 144 145 146 147 148 149
  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

150
  test "variation of invariable blob" do
151
    assert_raises ActiveStorage::InvariableError do
152 153 154 155
      create_file_blob(filename: "report.pdf", content_type: "application/pdf").variant(resize: "100x100")
    end
  end

156
  test "service_url doesn't grow in length despite long variant options" do
157 158
    blob = create_file_blob(filename: "racecar.jpg")
    variant = blob.variant(font: "a" * 10_000).processed
159
    assert_operator variant.service_url.length, :<, 525
160
  end
161 162 163

  test "works for vips processor" do
    begin
164
      ActiveStorage.variant_processor = :vips
165 166 167 168 169 170 171 172 173
      blob = create_file_blob(filename: "racecar.jpg")
      variant = blob.variant(thumbnail_image: 100).processed

      image = read_image(variant)
      assert_equal 100, image.width
      assert_equal 67, image.height
    rescue LoadError
      # libvips not installed
    ensure
174
      ActiveStorage.variant_processor = :mini_magick
175 176
    end
  end
177
end