Skip to content

Commit

Permalink
Reworked image-matching assertion internals
Browse files Browse the repository at this point in the history
* Use exit-code from `compare` to check for success
* Fix some formatting issues
  • Loading branch information
radiantcapsule committed Jun 3, 2014
1 parent 5a4346d commit f7217c4
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 30 deletions.
15 changes: 5 additions & 10 deletions image_assertion.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
var ImageAsserter = (function() {

function ImageAsserter(tuneUpPath, outputPath, refImagesPath) {

if (!outputPath || !refImagesPath || !tuneUpPath) {

throw new AssertionException("output, refImages, tuneUp pathes can't be null");
}

Expand All @@ -13,19 +11,16 @@ var ImageAsserter = (function() {

var target = UIATarget.localTarget();
if (!target) {

throw new AssertionException("unable to get localTarget");
}

this.host = target.host();
if (!this.host) {

throw new AssertionException("unable to get current UAIHost");
}
}

ImageAsserter.prototype.assertImageNamed = function(imageName, threshold) {

var command,
taskResult,
assertSuccessfull = false,
Expand All @@ -38,22 +33,22 @@ var ImageAsserter = (function() {
args,
TIMEOUT);

assertSuccessfull = (taskResult.exitCode === SUCCESS_EXIT_CODE);
if (!assertSuccessfull) UIALogger.logError(taskResult.stderr);
assertSuccessful = (taskResult.exitCode === SUCCESS_EXIT_CODE);
if (!assertSuccessful) {
UIALogger.logError(taskResult.stderr);
}

return assertSuccessfull;
return assertSuccessful;
};

return ImageAsserter;
}());

function createImageAsserter(tuneUpPath, outputPath, refImagesPath) {

this.imageAsserter = new ImageAsserter(tuneUpPath, outputPath, refImagesPath);
}

function assertScreenMatchesImageNamed(imageName, message, threshold) {

if (!this.imageAsserter) {
throw new AssertionException("imageAsserter isn't created.");
}
Expand Down
41 changes: 21 additions & 20 deletions image_assertion.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ class ImageAssertion
DIFF_IMAGE_FOLDER_NAME = 'screens_diff'

def self.assert_image(test_output, ref_images_path, image_name, threshold)

return false unless (test_output && ref_images_path && image_name)

diff_images_path = File.join(test_output, DIFF_IMAGE_FOLDER_NAME)
Expand All @@ -22,24 +21,30 @@ def self.assert_image(test_output, ref_images_path, image_name, threshold)

print_status(create_status('started', "Asserting #{image_file_name}."))

if !File.exists?(received_path) || !File.exists?(expected_path)

error = "Expected or reference image #{image_file_name} not found."
if !File.exists?(received_path)
error = "No captured image file found at #{received_path}"
print_status(create_status('failed', error))
return false
elsif !File.exists?(expected_path)
error = "No reference image file found at #{expected_path}"
print_status(create_status('failed', error))
return false

else

result = im_compare(expected_path, received_path, diff_path)
return process_imagemagick_result(image_file_name, result, threshold)
result, exit_status = im_compare(expected_path, received_path, diff_path)
if exit_status == 0
return process_imagemagick_result(image_file_name, result, exit_status, threshold)
elsif exit_status == 1
print_status(create_status('failed'), "Images differ, check #{diff_images_path} for details")
else
print_status(create_status('failed'), "ImageMagick compare failed: #{result}")
end
end
end

private

# Iterte through folders with name Run* and return with latests run number
def self.find_last_folder(test_output)

folder_mask = "#{test_output}/Run";
run_folders = Dir.glob("#{folder_mask}*")

Expand All @@ -50,52 +55,48 @@ def self.find_last_folder(test_output)
end[0]
end

def self.process_imagemagick_result(image_file_name, stderr, threshold)

def self.process_imagemagick_result(image_file_name, result, threshold)
result_status = 'failed'
result_message = "#{image_file_name} is not equal to the reference."
assertionResult = false

#imagemagick outputs floating point metrics value when succeeds
compare_succeed = ( stderr.match(/[0-9]*\.?[0-9]+/).length > 0 )
compare_succeed = ( result.match(/[0-9]*\.?[0-9]+/).length > 0 )
threshold ||= MAX_ALLOWED_DIFF_VALUE

if compare_succeed
if stderr.to_f < threshold
if result.to_f < threshold

result_status = 'passed'
result_message = "#{image_file_name} asserted successfully."
assertionResult = true
else
print_status(create_status(result_status, "expected diff is smaller than #{threshold} but #{stderr.to_f}."))
print_status(create_status(result_status, "expected diff is smaller than #{threshold} but #{result.to_f}."))
end
else

result_message = stderr
result_message = result
end

print_status(create_status(result_status, result_message))
assertionResult
end

def self.create_status(status, message)

"#{Time.new} #{status}: #{message}"
end

def self.print_status(message)

$stderr.puts(message)
end

def self.im_compare(expected_path, received_path, diff_path)

command = '/usr/local/bin/compare -metric MAE '
command << Shellwords.escape(expected_path) + ' '
command << Shellwords.escape(received_path) + ' '
command << ( diff_path ? Shellwords.escape(diff_path) : 'null:' )

_, _, stderr = Open3.popen3(command)
stderr.read
_, _, stderr, wait_thr = Open3.popen3(command)
[stderr.read, wait_thr.value.exitstatus]
end
end

0 comments on commit f7217c4

Please sign in to comment.