bugfixes
[swftools.git] / spec / spec_helper.rb
1 require 'spec'
2 require 'rubygems'
3 require 'RMagick'
4
5 class WrongColor < Exception
6     def initialize(pixel)
7         @pixel = pixel
8     end
9     def to_s
10         "Wrong color at #{@pixel}"
11     end
12 end
13 class AreaError < Exception
14     def initialize(area,problem)
15         @area,@problem = area,problem
16     end
17     def to_s
18         "Area at #{@area} #{@problem}"
19     end
20 end
21 class PixelError < Exception
22     def initialize(p1, relation,p2)
23         @p1,@p2,@relation = p1,p2,relation
24     end
25     def to_s
26         "Pixel #{@p1} #{@relation} #{@p2}"
27     end
28 end
29 class ConversionFailed < Exception
30     def initialize(output,file)
31         @output = output
32         @file = file
33         @exists = File.exists?(file)
34     end
35     def to_s
36         puts "-"*26+" Conversion failed "+"-"*27
37         (puts @output) if @output
38         puts "file #{@file} doesn't exist" if not @exists
39         puts "-"*72
40     end
41 end
42
43 class Area
44     def initialize(x1,y1,x2,y2,file)
45         @x1,@y1,@x2,@y2,@file = x1,y1,x2,y2,file
46     end
47     def should_be_plain_colored
48         @rgb = @file.get_area(@x1,@y1,@x2,@y2) unless @rgb
49         @rgb.minmax == [@rgb[0],@rgb[0]] or raise AreaError.new(self,"is not plain colored")
50     end
51     def should_not_be_plain_colored
52         @rgb = @file.get_area(@x1,@y1,@x2,@y2) unless @rgb
53         @rgb.minmax != [@rgb[0],@rgb[0]] or raise AreaError.new(self,"is plain colored")
54     end
55     def should_contain_text(text)
56         @file.get_text(@x1,@y1,@x2,@y2) == text or raise AreaError.new(self, "doesn't contain text #{text}")
57     end
58     def to_s
59         "(#{@x1},#{@y1},#{@x2},#{@y2})"
60     end
61 end
62
63 def rgb_to_int(rgb)
64     # ImageMagick rgb triples are 16 bit
65     (rgb.reverse+[0]).map {|c| c>>8}.pack("CCCC").unpack("i")[0]
66 end
67
68 class Pixel
69     attr :rgb
70     def initialize(x,y,rgb)
71         @x,@y,@rgb = x,y,rgb
72     end
73     def should_be_of_color(color2)
74         color1 = rgb_to_int(@rgb)
75         color1 == color2 or raise WrongColor.new(self)
76     end
77     def should_be_brighter_than(pixel)
78         gray1 = @rgb.inject(0) {|sum,e| sum+e}
79         gray2 = pixel.rgb.inject(0) {|sum,e| sum+e}
80         gray1 > gray2 or raise PixelError.new(self,"is not brighter than",pixel)
81     end
82     def should_be_darker_than(pixel)
83         gray1 = @rgb.inject(0) {|sum,e| sum+e}
84         gray2 = pixel.rgb.inject(0) {|sum,e| sum+e}
85         gray1 < gray2 or raise PixelError.new(self,"is not less bright than",pixel)
86     end
87     def should_be_the_same_as(pixel)
88         @rgb == pixel.rgb or raise PixelError.new(self,"is not the same as",pixel)
89     end
90     def to_s
91         "(#{@x},#{@y})"
92     end
93 end
94
95 $tempfiles = []
96 Kernel.at_exit do
97     $tempfiles.each do |file|
98         `rm -f #{file}`
99     end
100 end
101
102 class DocFile
103     def initialize(filename, page)
104         @filename = filename
105         @page = page
106     end
107     def convert()
108         return if @swfname
109         @swfname = @filename.gsub(/.pdf$/i,"")+".swf"
110         $tempfiles += [@swfname]
111         `pdfinfo #{@filename}` =~ /Page size:\s*([0-9]+) x ([0-9]+) pts/
112         width,height = $1,$2
113         dpi = (72.0 * 612 / width.to_i).to_i
114         output = `pdf2swf --flatten -s zoom=#{dpi} -p #{@page} #{@filename} -o #{@swfname} 2>&1`
115         #output = `pdf2swf -s zoom=#{dpi} --flatten -p #{@page} #{@filename} -o #{@swfname} 2>&1`
116         raise ConversionFailed.new(output,@swfname) unless File.exists?(@swfname)
117     end
118     def render()
119         return if @img
120         convert()
121         @pngname = @filename.gsub(/.pdf$/i,"")+".png"
122         begin
123             output = `swfrender --legacy #{@swfname} -o #{@pngname} 2>&1`
124             raise ConversionFailed.new(output,@pngname) unless File.exists?(@pngname)
125             @img = Magick::Image.read(@pngname).first
126         ensure
127             `rm -f #{@pngname}`
128         end
129     end
130     def get_text(x1,y1,x2,y2)
131         self.convert()
132         #puts "swfstrings -x #{x1} -y #{y1} -W #{x2-x1} -H #{y2-y1} #{@swfname}"
133         #puts `swfstrings -x #{x1} -y #{y1} -W #{x2-x1} -H #{y2-y1} #{@swfname}`
134         `swfstrings -x #{x1} -y #{y1} -W #{x2-x1} -H #{y2-y1} #{@swfname}`.chomp
135     end
136     def get_area(x1,y1,x2,y2)
137         self.render()
138         data = @img.export_pixels(x1, y1, x2-x1, y2-y1, "RGB")
139         Array.new(data.size/3) do |i| data.slice(i*3,3) end
140     end
141     def area_at(x1,y1,x2,y2)
142         return Area.new(x1,y1,x2,y2,self)
143     end
144     def width()
145         self.render()
146         return @img.columns
147     end
148     def height()
149         self.render()
150         return @img.rows
151     end
152     def pixel_at(x,y)
153         self.render()
154         data = @img.export_pixels(x, y, 1, 1, "RGB")
155         return Pixel.new(x,y,data)
156     end
157 end
158
159 module Spec
160     module Example
161         module ExampleMethods
162            def area_at(x1,y1,x2,y2)
163                @file.area_at(x1,y1,x2,y2)
164            end
165            def width
166                @file.width
167            end
168            def height
169                @file.height
170            end
171            def pixel_at(x,y)
172                @file.pixel_at(x,y)
173            end
174            def initialize(proxy,&impl)
175
176                # overriding the initialize() function saves us from having to
177                # set up the document in the test. The bad news, however, is that
178                # we have to be very careful not to raise exceptions here-
179                # rspec would miss those.
180
181                @_proxy = proxy
182                @_implementation = impl
183                @_backtrace = caller
184                @file = DocFile.new(proxy.description, (proxy.options[:page] or 1))
185            end
186         end
187         module ExampleGroupMethods
188             alias :convert_file :example
189             def page(nr)
190                 nr
191             end
192         end
193     end
194 end
195
196