Created
December 1, 2010 08:08
-
-
Save borgand/723147 to your computer and use it in GitHub Desktop.
Testing Mmo#styles_fit
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'test_helper' | |
require 'mocha' | |
class MmoTest < ActiveSupport::TestCase | |
# Set up equivalence classes for the tests | |
def setup | |
# Equivalence classes | |
@styles = { | |
"video" => { | |
:low => { :w => 1, :h => 1}, | |
:medium => {:w => 384 + 1, :h => 288 + 1}, | |
:high => {:w => 768 + 1,:h => 576 + 1}, | |
:hd => {:w => 1152 + 1,:h => 864 + 1}, | |
}, | |
"image" => { | |
:low => {:w => 1,:h => 1}, | |
:medium => {:w => 384 + 1, :h => 288 + 1}, | |
:high => {:w => 960 + 1, :h => 720 + 1} | |
} | |
} | |
# Data table for equivalence classes | |
@geometries = { | |
"video" => {"w" => [], "h" => []}, | |
"image" => {"w" => [], "h" => []} | |
} | |
# Generate test data | |
%w{video image}.each{|mtype| | |
# Parse all styles for geometry values | |
Mmo::STYLES[mtype].select{|k| | |
# :thumb and :image "fit" always, so they do not create equivalence class | |
![:thumb, :image].include?(k) | |
}.each{|k,v| | |
v[:geometry].split(/x/).each_with_index{|n,i| | |
# Prepend with 1 smaller value | |
@geometries[mtype][i % 2 == 0 ? "w" : "h" ] << n.to_i - 1 | |
@geometries[mtype][i % 2 == 0 ? "w" : "h" ] << n.to_i | |
# Append 120% bigger value too | |
@geometries[mtype][i % 2 == 0 ? "w" : "h" ] << (n.to_i * 1.2).to_i | |
@styles[mtype] ||= {} | |
@styles[mtype][k] ||= {} | |
@styles[mtype][k][i % 2 == 0 ? "w" : "h" ] = n.to_i | |
} | |
} | |
} | |
# Prepend -1 and 0 to each array and append a big int | |
@geometries.each{|k,v| v.each{|k1,v1| | |
v1.unshift(0) | |
v1.unshift(-1) | |
v1.push(10000) | |
} | |
} | |
end | |
def test_video_styles | |
check_styles("video") | |
end | |
def test_image_styles | |
check_styles("image") | |
end | |
def test_audio_styles | |
m = Mmo.new | |
m.stubs(:media_type).returns("audio") | |
# Fake a selection of supported codecs | |
%w{mp3 mp4 wav wmv}.each{|c| | |
m.stubs(:info).returns({:codec => c}) | |
if c == "mp3" | |
assert_equal([:mp3, :mp4], m.styles_fit.keys, "MP3 should have both :mp3 and :mp4 style") | |
else | |
assert_equal([:mp4], m.styles_fit.keys, "Codec #{c} should have only :mp4 style") | |
end | |
} | |
end | |
def check_styles(mtype) | |
m = Mmo.new | |
m.stubs(:media_type).returns(mtype) | |
# Traverse all possible combinations | |
@geometries[mtype]['w'].each do |w| | |
@geometries[mtype]['h'].each do |h| | |
# Stub media info | |
m.stubs(:info).returns({ | |
:width => w, | |
:height => h, | |
:aspect => m.send(:aspect,w,h) | |
}) | |
assert_correct_styles(w,h,m.styles_fit, mtype) | |
end | |
end | |
end | |
def assert_correct_styles(w,h,styles, mtype) | |
if w < 1 || h < 1 | |
assert_equal({}, styles, "Zero and Negative size should yield empty result") | |
return | |
end | |
assert(styles.keys.include?(:thumb), "Thumbnail should be always generated. styles: w:#{w} h:#{h}, #{styles}") | |
assert(styles.keys.include?(:image), "Image should be always generated for videos") if mtype == "video" | |
if (styles.has_key?(:image)) | |
ws,hs = styles[:image][:geometry].split(/x/) | |
# Compensate for rounding errors | |
assert(w == ws.to_i || h == hs.to_i, "Image should have original size. Styles: #{styles}, w:#{w}, h:#{h}") | |
end | |
@styles[mtype].each do |k,v| | |
if w >= v[:w] || h >= v[:h] | |
assert(styles.keys.include?(k), "Style #{k} should be generated. o:#{w}x#{h}, w_min:#{v[:w]}, h_min:#{v[:h]}") | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment