jnicklas / uploadcolumn

Easily upload files to your Ruby on Rails application

This URL has Read+Write access

uploadcolumn / spec / rmagick_manipulator_spec.rb
100644 187 lines (130 sloc) 5.768 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
require File.join(File.dirname(__FILE__), 'spec_helper')
require File.join(File.dirname(__FILE__), '../lib/upload_column/manipulators/rmagick')
 
describe UploadColumn::Manipulators::RMagick, "#manipulate!" do
  
  before(:each) do
    @uploaded_file = class << self; self end # this is a singleton object
    @uploaded_file.extend( UploadColumn::Manipulators::RMagick )
    @uploaded_file.load_manipulator_dependencies
    @uploaded_file.stub!(:path).and_return('/some_path.png')
  end
  
  it "should yield the first frame of the image and then save the result, for a single-framed image" do
    a_frame = mock('a frame')
    Magick::Image.should_receive(:read).with('/some_path.png').and_return( [a_frame] )
    
    @uploaded_file.manipulate! do |img|
      img.should == a_frame
      img.should_receive(:write).with('/some_path.png')
      img
    end
  end
  
  it "should yield all frames and save the result, for a multi-framed image" do
    image = Magick::Image.read(file_path('netscape.gif'))
    Magick::Image.should_receive(:read).with('/some_path.png').and_return( image )
    
    imagelist = Magick::ImageList.new
    Magick::ImageList.should_receive(:new).and_return(imagelist)
    
    imagelist.should_receive(:<<).with(image[0]).exactly(:once).ordered
    imagelist.should_receive(:<<).with(image[1]).exactly(:once).ordered
    
    image[0].should_receive(:solarize)
    image[1].should_receive(:solarize)
    
    imagelist.should_receive(:write).with('/some_path.png')
    
    @uploaded_file.manipulate! do |img|
      img.solarize
      img
    end
    
  end
  
  it "should raise an more meaningful error if something goes wrong" do
    Magick::Image.should_receive(:read).and_raise(Magick::ImageMagickError.new('arrggh'))
    
    lambda do
      @uploaded_file.manipulate! do |img|
        img
      end
    end.should raise_error( UploadColumn::ManipulationError, "Failed to manipulate with rmagick, maybe it is not an image? Original Error: arrggh" )
 
  end
  
end
 
describe UploadColumn::Manipulators::RMagick, "#resize!" do
 
  before(:each) do
    @uploaded_file = class << self; self end
    @uploaded_file.extend( UploadColumn::Manipulators::RMagick )
    @uploaded_file.load_manipulator_dependencies
    @uploaded_file.stub!(:path).and_return('/some_path.png')
  end
 
  it "should use rmagick to resize the image to the appropriate size" do
    
    img = mock('an image frame')
    @uploaded_file.should_receive(:manipulate!).and_yield(img)
    
    geometry_img = mock('image returned by change_geometry')
    
    img.should_receive(:change_geometry).with("200x200").and_yield(20, 40, geometry_img)
    
    geometry_img.should_receive(:resize).with(20, 40)
    
    @uploaded_file.resize!("200x200")
  end
 
end
 
 
describe UploadColumn::Manipulators::RMagick, "#crop_resized!" do
 
  before(:each) do
    @uploaded_file = class << self; self end
    @uploaded_file.extend( UploadColumn::Manipulators::RMagick )
    @uploaded_file.load_manipulator_dependencies
    @uploaded_file.stub!(:path).and_return('/some_path.png')
  end
 
  it "should use rmagick to resize and crop the image to the appropriate size" do
    
    img = mock('an image frame')
    @uploaded_file.should_receive(:manipulate!).and_yield(img)
    
    img.should_receive(:crop_resized).with(200, 200)
    
    @uploaded_file.crop_resized!("200x200")
  end
 
end
 
describe UploadColumn::Manipulators::RMagick, "#convert!" do
 
  before(:each) do
    @uploaded_file = class << self; self end
    @uploaded_file.extend( UploadColumn::Manipulators::RMagick )
    @uploaded_file.load_manipulator_dependencies
    @uploaded_file.stub!(:path).and_return('/some_path.png')
  end
 
  it "should use rmagick to change the image format" do
    
    img = mock('an image frame')
    @uploaded_file.should_receive(:manipulate!).and_yield(img)
    
    img.should_receive(:format=).with("PNG")
    
    @uploaded_file.convert!(:png)
  end
 
end
 
describe UploadColumn::Manipulators::RMagick, "#process!" do
 
  before(:each) do
    @uploaded_file = class << self; self end
    @uploaded_file.extend( UploadColumn::Manipulators::RMagick )
    @uploaded_file.load_manipulator_dependencies
    @uploaded_file.stub!(:path).and_return('/some_path.png')
  end
 
  it "should resize the image if a string like '333x444' is passed" do
    @uploaded_file.should_receive(:resize!).with('333x444')
    @uploaded_file.process!('333x444')
  end
  
  it "should crop and resize the image if a string like 'c333x444' is passed" do
    @uploaded_file.should_receive(:crop_resized!).with('333x444')
    @uploaded_file.process!('c333x444')
  end
  
  it "should pass on a proc to manipulate!" do
    img_frame = mock('an image frame')
    proc = proc { |img| img.solarize }
    img_frame.should_receive(:solarize)
    
    @uploaded_file.should_receive(:manipulate!).and_yield(img_frame)
    
    @uploaded_file.process!(proc)
  end
  
  it "should yield to manipulate! if a block is given" do
    img_frame = mock('an image frame')
    img_frame.should_receive(:solarize)
    
    @uploaded_file.should_receive(:manipulate!).and_yield(img_frame)
    
    @uploaded_file.process! do |img|
      img.solarize
    end
  end
  
  it "should resize first and then yield to manipulate! if both a block and a size string are given" do
    img_frame = mock('an image frame')
    img_frame.should_receive(:solarize)
    
    @uploaded_file.should_receive(:resize!).with('200x200').ordered
    @uploaded_file.should_receive(:manipulate!).ordered.and_yield(img_frame)
    
    @uploaded_file.process!('200x200') do |img|
      img.solarize
    end
  end
  
  it "should do nothing if :none is passed" do
    @uploaded_file.should_not_receive(:manipulate!)
    @uploaded_file.process!(:none)
  end
 
end