Sanity test: pixel-by-pixel comparison
parent
5ea9ce619f
commit
74865cb10f
|
|
@ -34,3 +34,4 @@ benchmarks/_output/
|
||||||
samples/_output/*.svg
|
samples/_output/*.svg
|
||||||
*.sublime-workspace
|
*.sublime-workspace
|
||||||
spec/samples/sanity.html
|
spec/samples/sanity.html
|
||||||
|
spec/samples/_diffs/*.png
|
||||||
|
|
@ -6,12 +6,7 @@
|
||||||
<title>Squib Sanity Test for v<%=Squib::VERSION%></title>
|
<title>Squib Sanity Test for v<%=Squib::VERSION%></title>
|
||||||
<style>
|
<style>
|
||||||
body{
|
body{
|
||||||
background-color: #dedede;
|
background:url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAoAAAAKCAYAAACNMs+9AAAAHElEQVQYlWO4ePFiAzGY4eLFiw0MxIBRhVRRCADsljgKUbBKPgAAAABJRU5ErkJggg==) repeat;
|
||||||
font-color: white;
|
|
||||||
}
|
|
||||||
table, td, tr{
|
|
||||||
border: 1px solid black;
|
|
||||||
border-collapse: true;
|
|
||||||
}
|
}
|
||||||
img{
|
img{
|
||||||
width: 30%;
|
width: 30%;
|
||||||
|
|
@ -20,14 +15,14 @@
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<% count = 0 %>
|
<% count = 0 %>
|
||||||
<% images.each do |exp, actual| %>
|
<% images.each do |exp, actual, diff| %>
|
||||||
<img src="<%=exp %>">
|
<img src="<%=exp %>">
|
||||||
<img src="<%=actual %>">
|
<img src="<%=actual %>">
|
||||||
|
<img src="<%=diff %>">
|
||||||
<br>
|
<br>
|
||||||
<%=exp %>
|
<%=exp %>
|
||||||
<hr>
|
<hr>
|
||||||
<% count += 1 %>
|
<% count += 1 %>
|
||||||
<% end %>
|
<% end %>
|
||||||
</table>
|
|
||||||
<body>
|
<body>
|
||||||
</html>
|
</html>
|
||||||
|
|
@ -1,19 +1,31 @@
|
||||||
require 'launchy'
|
require 'launchy'
|
||||||
require 'erb'
|
require 'erb'
|
||||||
|
require 'cairo'
|
||||||
|
require 'ruby-progressbar'
|
||||||
|
|
||||||
# An pixel-by-pixel comparison of sample images for visual regression testing
|
# An pixel-by-pixel comparison of sample images for visual regression testing
|
||||||
class Sanity
|
class Sanity
|
||||||
|
|
||||||
@@EXPECTED_DIR = "#{File.expand_path(File.dirname(__FILE__))}/expected/"
|
@@EXPECTED_DIR = "#{File.expand_path(File.dirname(__FILE__))}/expected/"
|
||||||
@@OUTPUT_DIR = "#{File.expand_path(File.dirname(__FILE__))}/../../samples/_output/"
|
@@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_ERB = "#{File.expand_path(File.dirname(__FILE__))}/sanity.html.erb"
|
||||||
@@SANITY_HTML = "#{File.expand_path(File.dirname(__FILE__))}/sanity.html"
|
@@SANITY_HTML = "#{File.expand_path(File.dirname(__FILE__))}/sanity.html"
|
||||||
|
|
||||||
def images
|
def images
|
||||||
images = {}
|
images = []
|
||||||
Dir[@@EXPECTED_DIR + "/**/*.png"].each do |exp_png|
|
exp_pngs = Dir[@@EXPECTED_DIR + "/**/*.png"]
|
||||||
images["file:///" + exp_png] = "file:///" + @@OUTPUT_DIR + File.basename(exp_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
|
end
|
||||||
|
bar.finish
|
||||||
return images
|
return images
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
@ -21,13 +33,33 @@ class Sanity
|
||||||
puts "Building sanity test..."
|
puts "Building sanity test..."
|
||||||
sanity_template = File.read(@@SANITY_ERB)
|
sanity_template = File.read(@@SANITY_ERB)
|
||||||
process_erb(sanity_template)
|
process_erb(sanity_template)
|
||||||
# render_markdown(sanity_template)
|
|
||||||
Launchy.open("file:///" + @@SANITY_HTML)
|
Launchy.open("file:///" + @@SANITY_HTML)
|
||||||
puts "Done."
|
puts "Done."
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
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)
|
def process_erb(sanity_template)
|
||||||
renderer = ERB.new(sanity_template)
|
renderer = ERB.new(sanity_template)
|
||||||
File.open(@@SANITY_HTML, 'w+') do |html|
|
File.open(@@SANITY_HTML, 'w+') do |html|
|
||||||
|
|
@ -35,11 +67,4 @@ class Sanity
|
||||||
end
|
end
|
||||||
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
|
end
|
||||||
|
|
@ -12,6 +12,10 @@
|
||||||
"name": "rake",
|
"name": "rake",
|
||||||
"shell_cmd": "rake",
|
"shell_cmd": "rake",
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"name": "rake sanity",
|
||||||
|
"shell_cmd": "rake sanity",
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"name": "rake run[text_options]",
|
"name": "rake run[text_options]",
|
||||||
"shell_cmd": "rake run[text_options]",
|
"shell_cmd": "rake run[text_options]",
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue