merged in from code from the other master
[selectricity-live] / vendor / plugins / attachment_fu / test / processors / mini_magick_test.rb
1 require File.expand_path(File.join(File.dirname(__FILE__), '..', 'test_helper'))
2
3 class MiniMagickTest < Test::Unit::TestCase
4   attachment_model MiniMagickAttachment
5
6   if Object.const_defined?(:MiniMagick)
7     def test_should_resize_image
8       attachment = upload_file :filename => '/files/rails.png'
9       assert_valid attachment
10       assert attachment.image?
11       # test MiniMagick thumbnail
12       assert_equal 43, attachment.width
13       assert_equal 55, attachment.height
14       
15       thumb      = attachment.thumbnails.detect { |t| t.filename =~ /_thumb/ }
16       geo        = attachment.thumbnails.detect { |t| t.filename =~ /_geometry/ }
17       
18       # test exact resize dimensions
19       assert_equal 50, thumb.width
20       assert_equal 51, thumb.height
21       
22       # test geometry string
23       assert_equal 31, geo.width
24       assert_equal 40, geo.height
25     end
26
27     def test_should_crop_image(klass = ImageThumbnailCrop)
28       attachment_model klass
29       attachment = upload_file :filename => '/files/rails.png'
30       assert_valid attachment
31       assert  attachment.image?
32     #  has_attachment :thumbnails => { :square => "50x50c", :vertical => "30x60c", :horizontal => "60x30c"}
33
34       square      = attachment.thumbnails.detect { |t| t.filename =~ /_square/ }
35       vertical    = attachment.thumbnails.detect { |t| t.filename =~ /_vertical/ }
36       horizontal  = attachment.thumbnails.detect { |t| t.filename =~ /_horizontal/ }
37       
38       # test excat resize
39       assert_equal 50, square.width
40       assert_equal 50, square.height
41
42       assert_equal 30, vertical.width
43       assert_equal 60, vertical.height
44
45       assert_equal 60, horizontal.width
46       assert_equal 30, horizontal.height
47     end
48     
49     # tests the first step in resize, crop the image in original size to right format
50     def test_should_crop_image_right(klass = ImageThumbnailCrop)      
51       @@testcases.collect do |testcase| 
52         image_width, image_height, thumb_width, thumb_height = testcase[:data]
53         image_aspect, thumb_aspect = image_width/image_height, thumb_width/thumb_height
54         crop_comand = klass.calculate_offset(image_width, image_height, image_aspect, thumb_width, thumb_height,thumb_aspect)
55         # pattern matching on crop command
56         if testcase.has_key?(:height) 
57           assert crop_comand.match(/^#{image_width}x#{testcase[:height]}\+0\+#{testcase[:yoffset]}$/)
58         else 
59           assert crop_comand.match(/^#{testcase[:width]}x#{image_height}\+#{testcase[:xoffset]}\+0$/)
60         end
61       end
62     end
63
64   else
65     def test_flunk
66       puts "MiniMagick not loaded, tests not running"
67     end
68   end
69
70   @@testcases = [
71     # image_aspect <= 1 && thumb_aspect >= 1  
72     {:data => [10.0,40.0,2.0,1.0], :height => 5.0, :yoffset => 17.5}, #   1b
73     {:data => [10.0,40.0,1.0,1.0], :height => 10.0, :yoffset => 15.0}, #  1b
74
75     # image_aspect < 1 && thumb_aspect < 1
76     {:data => [10.0,40.0,1.0,2.0], :height => 20.0, :yoffset => 10.0}, # 1a
77     {:data => [2.0,3.0,1.0,2.0], :width => 1.5, :xoffset => 0.25}, # 1a
78
79     # image_aspect = thumb_aspect
80     {:data => [10.0,10.0,1.0,1.0], :height => 10.0, :yoffset => 0.0}, # QUADRAT 1c
81
82     # image_aspect >= 1 && thumb_aspect > 1     && image_aspect < thumb_aspect
83     {:data => [6.0,3.0,4.0,1.0], :height => 1.5, :yoffset => 0.75}, # 2b  
84     {:data => [6.0,6.0,4.0,1.0], :height => 1.5, :yoffset => 2.25}, # 2b  
85
86     # image_aspect > 1 && thumb_aspect > 1     && image_aspect > thumb_aspect
87     {:data => [9.0,3.0,2.0,1.0], :width => 6.0, :xoffset => 1.5}, # 2a
88
89     # image_aspect > 1 && thumb_aspect < 1 && image_aspect < thumb_aspect
90     {:data => [10.0,5.0,0.1,2.0], :width => 0.25, :xoffset => 4.875}, # 4
91     {:data => [10.0,5.0,1.0,2.0], :width => 2.5, :xoffset => 3.75}, # 4
92
93     # image_aspect > 1 && thumb_aspect > 1     && image_aspect > thumb_aspect
94     {:data => [9.0,3.0,2.0,1.0], :width => 6.0, :xoffset => 1.5}, # 3a    
95     # image_aspect > 1 && thumb_aspect > 1     && image_aspect < thumb_aspect
96     {:data => [9.0,3.0,5.0,1.0], :height => 1.8, :yoffset => 0.6} # 3a
97   ]
98
99
100
101
102
103 end

Benjamin Mako Hill || Want to submit a patch?