1 require File.dirname(__FILE__) + '/abstract_unit'
3 require File.dirname(__FILE__) + '/fixtures/entry'
6 class AbstractRMagickTest < Test::Unit::TestCase
8 FileUtils.rm_rf File.dirname(__FILE__)+"/public/entry/"
18 Magick::Image::read(path).first
21 def assert_max_image_size(img, s)
22 assert img.columns <= s, "img has #{img.columns} columns, expected: #{s}"
23 assert img.rows <= s, "img has #{img.rows} rows, expected: #{s}"
24 assert_equal s, [img.columns, img.rows].max
28 class RMagickSimpleTest < AbstractRMagickTest
30 Entry.file_column :image, :magick => { :geometry => "100x100" }
33 def test_simple_resize_without_save
35 e.image = upload(f("kerb.jpg"))
37 img = read_image(e.image)
38 assert_max_image_size img, 100
41 def test_simple_resize_with_save
43 e.image = upload(f("kerb.jpg"))
47 img = read_image(e.image)
48 assert_max_image_size img, 100
51 def test_resize_on_saved_image
52 Entry.file_column :image, :magick => { :geometry => "100x100" }
55 e.image = upload(f("skanthak.png"))
60 e.image = upload(f("kerb.jpg"))
62 assert "kerb.jpg", File.basename(e.image)
63 assert !File.exists?(old_path), "old image '#{old_path}' still exists"
65 img = read_image(e.image)
66 assert_max_image_size img, 100
69 def test_invalid_image
71 assert_nothing_raised { e.image = upload(f("invalid-image.jpg")) }
77 e.image = upload(f("skanthak.png"))
78 assert_nothing_raised {
79 flash = Marshal.dump(e)
80 e = Marshal.load(flash)
82 assert File.exists?(e.image)
85 def test_imagemagick_still_usable
87 assert_nothing_raised {
88 img = e.load_image_with_rmagick(file_path("skanthak.png"))
89 assert img.kind_of?(Magick::Image)
94 class RMagickRequiresImageTest < AbstractRMagickTest
96 Entry.file_column :image, :magick => {
98 :image_required => false,
101 :large => {:size => "200x200>", :lazy => true}
106 def test_image_required_with_image
107 e = Entry.new(:image => upload(f("skanthak.png")))
108 assert_max_image_size read_image(e.image), 100
112 def test_image_required_with_invalid_image
113 e = Entry.new(:image => upload(f("invalid-image.jpg")))
114 assert e.valid?, "did not ignore invalid image"
115 assert FileUtils.identical?(e.image, f("invalid-image.jpg")), "uploaded file has not been left alone"
118 def test_versions_with_invalid_image
119 e = Entry.new(:image => upload(f("invalid-image.jpg")))
122 image_state = e.send(:image_state)
123 assert_nil image_state.create_magick_version_if_needed(:thumb)
124 assert_nil image_state.create_magick_version_if_needed(:large)
125 assert_nil image_state.create_magick_version_if_needed("300x300>")
129 class RMagickCustomAttributesTest < AbstractRMagickTest
130 def assert_image_property(img, property, value, text = nil)
131 assert File.exists?(img), "the image does not exist"
132 assert_equal value, read_image(img).send(property), text
135 def test_simple_attributes
136 Entry.file_column :image, :magick => { :attributes => { :quality => 20 } }
137 e = Entry.new("image" => upload(f("kerb.jpg")))
138 assert_image_property e.image, :quality, 20, "the quality was not set"
141 def test_version_attributes
142 Entry.file_column :image, :magick => {
144 :thumb => { :attributes => { :quality => 20 } }
147 e = Entry.new("image" => upload(f("kerb.jpg")))
148 assert_image_property e.image("thumb"), :quality, 20, "the quality was not set"
151 def test_lazy_attributes
152 Entry.file_column :image, :magick => {
154 :thumb => { :attributes => { :quality => 20 }, :lazy => true }
157 e = Entry.new("image" => upload(f("kerb.jpg")))
158 e.send(:image_state).create_magick_version_if_needed(:thumb)
159 assert_image_property e.image("thumb"), :quality, 20, "the quality was not set"
163 class RMagickVersionsTest < AbstractRMagickTest
165 Entry.file_column :image, :magick => {:geometry => "200x200",
168 :medium => {:geometry => "100x100", :name => "100_100"},
169 :large => {:geometry => "150x150", :lazy => true}
175 def test_should_create_thumb
176 e = Entry.new("image" => upload(f("skanthak.png")))
178 assert File.exists?(e.image("thumb")), "thumb-nail not created"
180 assert_max_image_size read_image(e.image("thumb")), 50
183 def test_version_name_can_be_different_from_key
184 e = Entry.new("image" => upload(f("skanthak.png")))
186 assert File.exists?(e.image("100_100"))
187 assert !File.exists?(e.image("medium"))
190 def test_should_not_create_lazy_versions
191 e = Entry.new("image" => upload(f("skanthak.png")))
192 assert !File.exists?(e.image("large")), "lazy versions should not be created unless needed"
195 def test_should_create_lazy_version_on_demand
196 e = Entry.new("image" => upload(f("skanthak.png")))
198 e.send(:image_state).create_magick_version_if_needed(:large)
200 assert File.exists?(e.image("large")), "lazy version should be created on demand"
202 assert_max_image_size read_image(e.image("large")), 150
205 def test_generated_name_should_not_change
206 e = Entry.new("image" => upload(f("skanthak.png")))
208 name1 = e.send(:image_state).create_magick_version_if_needed("50x50")
209 name2 = e.send(:image_state).create_magick_version_if_needed("50x50")
210 name3 = e.send(:image_state).create_magick_version_if_needed(:geometry => "50x50")
211 assert_equal name1, name2, "hash value has changed"
212 assert_equal name1, name3, "hash value has changed"
215 def test_should_create_version_with_string
216 e = Entry.new("image" => upload(f("skanthak.png")))
218 name = e.send(:image_state).create_magick_version_if_needed("32x32")
220 assert File.exists?(e.image(name))
222 assert_max_image_size read_image(e.image(name)), 32
225 def test_should_create_safe_auto_id
226 e = Entry.new("image" => upload(f("skanthak.png")))
228 name = e.send(:image_state).create_magick_version_if_needed("32x32")
230 assert_match /^[a-zA-Z0-9]+$/, name
234 class RMagickCroppingTest < AbstractRMagickTest
236 Entry.file_column :image, :magick => {:geometry => "200x200",
238 :thumb => {:crop => "1:1", :geometry => "50x50"}
243 def test_should_crop_image_on_upload
244 e = Entry.new("image" => upload(f("skanthak.png")))
246 img = read_image(e.image("thumb"))
248 assert_equal 50, img.rows
249 assert_equal 50, img.columns
254 class UrlForImageColumnTest < AbstractRMagickTest
255 include FileColumnHelper
258 Entry.file_column :image, :magick => {
259 :versions => {:thumb => "50x50"}
261 @request = RequestMock.new
264 def test_should_use_version_on_symbol_option
265 e = Entry.new(:image => upload(f("skanthak.png")))
267 url = url_for_image_column(e, "image", :thumb)
268 assert_match %r{^/entry/image/tmp/.+/thumb/skanthak.png$}, url
271 def test_should_use_string_as_size
272 e = Entry.new(:image => upload(f("skanthak.png")))
274 url = url_for_image_column(e, "image", "50x50")
276 assert_match %r{^/entry/image/tmp/.+/.+/skanthak.png$}, url
278 url =~ /\/([^\/]+)\/skanthak.png$/
281 assert_max_image_size read_image(e.image(dirname)), 50
284 def test_should_accept_version_hash
285 e = Entry.new(:image => upload(f("skanthak.png")))
287 url = url_for_image_column(e, "image", :size => "50x50", :crop => "1:1", :name => "small")
289 assert_match %r{^/entry/image/tmp/.+/small/skanthak.png$}, url
291 img = read_image(e.image("small"))
292 assert_equal 50, img.rows
293 assert_equal 50, img.columns
297 class RMagickPermissionsTest < AbstractRMagickTest
299 Entry.file_column :image, :magick => {:geometry => "200x200",
301 :thumb => {:crop => "1:1", :geometry => "50x50"}
303 }, :permissions => 0616
306 def check_permissions(e)
307 assert_equal 0616, (File.stat(e.image).mode & 0777)
308 assert_equal 0616, (File.stat(e.image("thumb")).mode & 0777)
311 def test_permissions_with_rmagick
312 e = Entry.new(:image => upload(f("skanthak.png")))
323 def transform_grey(img)
324 img.quantize(256, Magick::GRAYColorspace)
328 class RMagickTransformationTest < AbstractRMagickTest
329 def assert_transformed(image)
330 assert File.exists?(image), "the image does not exist"
331 assert 256 > read_image(image).number_colors, "the number of colors was not changed"
334 def test_simple_transformation
335 Entry.file_column :image, :magick => { :transformation => Proc.new { |image| image.quantize(256, Magick::GRAYColorspace) } }
336 e = Entry.new("image" => upload(f("skanthak.png")))
337 assert_transformed(e.image)
340 def test_simple_version_transformation
341 Entry.file_column :image, :magick => {
342 :versions => { :thumb => Proc.new { |image| image.quantize(256, Magick::GRAYColorspace) } }
344 e = Entry.new("image" => upload(f("skanthak.png")))
345 assert_transformed(e.image("thumb"))
348 def test_complex_version_transformation
349 Entry.file_column :image, :magick => {
351 :thumb => { :transformation => Proc.new { |image| image.quantize(256, Magick::GRAYColorspace) } }
354 e = Entry.new("image" => upload(f("skanthak.png")))
355 assert_transformed(e.image("thumb"))
358 def test_lazy_transformation
359 Entry.file_column :image, :magick => {
361 :thumb => { :transformation => Proc.new { |image| image.quantize(256, Magick::GRAYColorspace) }, :lazy => true }
364 e = Entry.new("image" => upload(f("skanthak.png")))
365 e.send(:image_state).create_magick_version_if_needed(:thumb)
366 assert_transformed(e.image("thumb"))
369 def test_simple_callback_transformation
370 Entry.file_column :image, :magick => :transform_grey
371 e = Entry.new(:image => upload(f("skanthak.png")))
372 assert_transformed(e.image)
375 def test_complex_callback_transformation
376 Entry.file_column :image, :magick => { :transformation => :transform_grey }
377 e = Entry.new(:image => upload(f("skanthak.png")))
378 assert_transformed(e.image)