Browse Source

Sanity test: pixel-by-pixel comparison

dev
Andy Meneely 11 years ago
parent
commit
74865cb10f
  1. 3
      .gitignore
  2. 0
      spec/samples/_diffs/gitkeep.txt
  3. 11
      spec/samples/sanity.html.erb
  4. 47
      spec/samples/sanity.rb
  5. 4
      squib.sublime-project

3
.gitignore vendored

@ -33,4 +33,5 @@ benchmarks/_output/*.pdf
benchmarks/_output/
samples/_output/*.svg
*.sublime-workspace
spec/samples/sanity.html
spec/samples/sanity.html
spec/samples/_diffs/*.png

0
spec/samples/_diffs/gitkeep.txt

11
spec/samples/sanity.html.erb

@ -6,12 +6,7 @@
<title>Squib Sanity Test for v<%=Squib::VERSION%></title>
<style>
body{
background-color: #dedede;
font-color: white;
}
table, td, tr{
border: 1px solid black;
border-collapse: true;
background:url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAoAAAAKCAYAAACNMs+9AAAAHElEQVQYlWO4ePFiAzGY4eLFiw0MxIBRhVRRCADsljgKUbBKPgAAAABJRU5ErkJggg==) repeat;
}
img{
width: 30%;
@ -20,14 +15,14 @@
</head>
<body>
<% count = 0 %>
<% images.each do |exp, actual| %>
<% images.each do |exp, actual, diff| %>
<img src="<%=exp %>">&nbsp;
<img src="<%=actual %>">&nbsp;
<img src="<%=diff %>">&nbsp;
<br>
<%=exp %>
<hr>
<% count += 1 %>
<% end %>
</table>
<body>
</html>

47
spec/samples/sanity.rb

@ -1,19 +1,31 @@
require 'launchy'
require 'erb'
require 'cairo'
require 'ruby-progressbar'
# An pixel-by-pixel comparison of sample images for visual regression testing
class Sanity
@@EXPECTED_DIR = "#{File.expand_path(File.dirname(__FILE__))}/expected/"
@@OUTPUT_DIR = "#{File.expand_path(File.dirname(__FILE__))}/../../samples/_output/"
@@DIFF_DIR = "#{File.expand_path(File.dirname(__FILE__))}/_diffs/"
@@SANITY_ERB = "#{File.expand_path(File.dirname(__FILE__))}/sanity.html.erb"
@@SANITY_HTML = "#{File.expand_path(File.dirname(__FILE__))}/sanity.html"
def images
images = {}
Dir[@@EXPECTED_DIR + "/**/*.png"].each do |exp_png|
images["file:///" + exp_png] = "file:///" + @@OUTPUT_DIR + File.basename(exp_png)
images = []
exp_pngs = Dir[@@EXPECTED_DIR + "/**/*.png"]
bar = ProgressBar.create(title: 'Diffing images', total: exp_pngs.size)
exp_pngs.each do |exp_png|
row = []
actual_png = @@OUTPUT_DIR + File.basename(exp_png)
row << "file:///" + exp_png
row << "file:///" + actual_png #actual
row << "file:///" + diff_image(exp_png, actual_png)
images << row
bar.increment
end
bar.finish
return images
end
@ -21,13 +33,33 @@ class Sanity
puts "Building sanity test..."
sanity_template = File.read(@@SANITY_ERB)
process_erb(sanity_template)
# render_markdown(sanity_template)
Launchy.open("file:///" + @@SANITY_HTML)
puts "Done."
end
private
def diff_image(exp_file, actual_file)
exp = Cairo::ImageSurface.from_png(exp_file)
exp_data = exp.data.bytes
actual_data = Cairo::ImageSurface.from_png(actual_file).data.bytes
new_data = Array.new(exp_data.size, 255)
unless exp_data == actual_data # this seems to be the fastest initial comparison
(0..exp_data.size).each do |i|
empty = (i % 4 == 3) ? 0 : 255 # alpha channel is empty, others are just black
new_data[i] = (exp_data[i] == actual_data[i]) ? empty : 128
end
end
out = Cairo::ImageSurface.new(new_data.pack('c*'), exp.format, exp.width, exp.height, exp.stride)
out_file = @@DIFF_DIR + exp_file[exp_file.rindex("/")..-1]
out.write_to_png(out_file)
out_file
end
def diff_pixel(exp, actual)
(exp == actual) ? [0, 0, 0, 0] : [255, 0, 0, 255]
end
def process_erb(sanity_template)
renderer = ERB.new(sanity_template)
File.open(@@SANITY_HTML, 'w+') do |html|
@ -35,11 +67,4 @@ class Sanity
end
end
# def render_markdown(sanity_template)
# md = Redcarpet::Markdown.new(Redcarpet::Render::HTML, autolink: true, tables: true)
# File.open(@@SANITY_HTML, 'w+') do |html|
# html.write(md.render(sanity_template))
# end
# end
end

4
squib.sublime-project vendored

@ -12,6 +12,10 @@
"name": "rake",
"shell_cmd": "rake",
},
{
"name": "rake sanity",
"shell_cmd": "rake sanity",
},
{
"name": "rake run[text_options]",
"shell_cmd": "rake run[text_options]",

Loading…
Cancel
Save