[2] | 1 | require 'abstract_unit' |
---|
| 2 | require_gem 'rmagick' |
---|
| 3 | |
---|
| 4 | class Entry < ActiveRecord::Base |
---|
| 5 | end |
---|
| 6 | |
---|
| 7 | class AbstractRMagickTest < Test::Unit::TestCase |
---|
| 8 | def teardown |
---|
| 9 | FileUtils.rm_rf File.dirname(__FILE__)+"/public/entry/" |
---|
| 10 | end |
---|
| 11 | |
---|
| 12 | def test_truth |
---|
| 13 | assert true |
---|
| 14 | end |
---|
| 15 | |
---|
| 16 | private |
---|
| 17 | |
---|
| 18 | def read_image(path) |
---|
| 19 | Magick::Image::read(path).first |
---|
| 20 | end |
---|
| 21 | |
---|
| 22 | def assert_max_image_size(img, s) |
---|
| 23 | assert img.columns <= s, "img has #{img.columns} columns, expected: #{s}" |
---|
| 24 | assert img.rows <= s, "img has #{img.rows} rows, expected: #{s}" |
---|
| 25 | assert_equal s, [img.columns, img.rows].max |
---|
| 26 | end |
---|
| 27 | end |
---|
| 28 | |
---|
| 29 | class RMagickSimpleTest < AbstractRMagickTest |
---|
| 30 | def setup |
---|
| 31 | Entry.file_column :image, :magick => { :geometry => "100x100" } |
---|
| 32 | end |
---|
| 33 | |
---|
| 34 | def test_simple_resize_without_save |
---|
| 35 | e = Entry.new |
---|
| 36 | e.image = upload("kerb.jpg") |
---|
| 37 | |
---|
| 38 | img = read_image(e.image) |
---|
| 39 | assert_max_image_size img, 100 |
---|
| 40 | end |
---|
| 41 | |
---|
| 42 | def test_simple_resize_with_save |
---|
| 43 | e = Entry.new |
---|
| 44 | e.image = upload("kerb.jpg") |
---|
| 45 | assert e.save |
---|
| 46 | e.reload |
---|
| 47 | |
---|
| 48 | img = read_image(e.image) |
---|
| 49 | assert_max_image_size img, 100 |
---|
| 50 | end |
---|
| 51 | |
---|
| 52 | def test_resize_on_saved_image |
---|
| 53 | Entry.file_column :image, :magick => { :geometry => "100x100" } |
---|
| 54 | |
---|
| 55 | e = Entry.new |
---|
| 56 | e.image = upload("skanthak.png") |
---|
| 57 | assert e.save |
---|
| 58 | e.reload |
---|
| 59 | old_path = e.image |
---|
| 60 | |
---|
| 61 | e.image = upload("kerb.jpg") |
---|
| 62 | assert e.save |
---|
| 63 | assert "kerb.jpg", File.basename(e.image) |
---|
| 64 | assert !File.exists?(old_path), "old image '#{old_path}' still exists" |
---|
| 65 | |
---|
| 66 | img = read_image(e.image) |
---|
| 67 | assert_max_image_size img, 100 |
---|
| 68 | end |
---|
| 69 | |
---|
| 70 | def test_invalid_image |
---|
| 71 | e = Entry.new |
---|
| 72 | assert_nothing_raised { e.image = upload("invalid-image.jpg") } |
---|
| 73 | assert !e.valid? |
---|
| 74 | end |
---|
| 75 | |
---|
| 76 | def test_serializable |
---|
| 77 | e = Entry.new |
---|
| 78 | e.image = upload("skanthak.png") |
---|
| 79 | assert_nothing_raised { |
---|
| 80 | flash = Marshal.dump(e) |
---|
| 81 | e = Marshal.load(flash) |
---|
| 82 | } |
---|
| 83 | assert File.exists?(e.image) |
---|
| 84 | end |
---|
| 85 | end |
---|
| 86 | |
---|
| 87 | class RMagickAlternativesTest < AbstractRMagickTest |
---|
| 88 | def setup |
---|
| 89 | Entry.file_column :image, :magick => {:geometry => "200x200", |
---|
| 90 | :versions => { |
---|
| 91 | "thumb" => "50x50", |
---|
| 92 | "medium" => {:geometry => "100x100"} |
---|
| 93 | } |
---|
| 94 | } |
---|
| 95 | end |
---|
| 96 | |
---|
| 97 | def test_thumb_created |
---|
| 98 | e = Entry.new("image" => upload("kerb.jpg")) |
---|
| 99 | |
---|
| 100 | thumb_path = File.join(File.dirname(e.image), "kerb-thumb.jpg") |
---|
| 101 | assert_equal thumb_path, e.image("thumb") |
---|
| 102 | assert File.exists?(e.image("thumb")), "thumb-nail not created" |
---|
| 103 | |
---|
| 104 | assert_max_image_size read_image(e.image("thumb")), 50 |
---|
| 105 | end |
---|
| 106 | end |
---|