image_comparator 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,267 @@
1
+ require 'minitest/autorun'
2
+ require 'image_comparator'
3
+ require 'fileutils'
4
+
5
+ class ImageComparatorTest < Minitest::Test
6
+
7
+ def setup
8
+ create_tmp_folder_if_required
9
+ @image_comparator = ImageComparator.new
10
+ end
11
+
12
+ def teardown
13
+ clean_images
14
+ end
15
+
16
+ def clean_images
17
+ FileUtils.rm_rf Dir.glob('./tmp/*.png')
18
+ end
19
+
20
+ def create_tmp_folder_if_required
21
+ folder = 'tmp'
22
+ FileUtils::mkdir_p folder unless File.directory?(folder)
23
+ end
24
+
25
+ def test_compare_pixel_perfect_almost
26
+ @image_comparator.define_images_to_compare('./images/almost-the-same-1.png', './images/almost-the-same-2.png')
27
+ compare = @image_comparator.compare
28
+ refute(compare, 'Images are not equal')
29
+ end
30
+
31
+ def test_compare_pixel_perfect
32
+ @image_comparator.define_images_to_compare('./images/base.png', './images/base-ok.png')
33
+ compare = @image_comparator.compare
34
+ assert(compare, 'Images are not equal')
35
+ end
36
+
37
+ def test_not_compare_pixel_perfect
38
+ @image_comparator.define_images_to_compare('./images/base.png', './images/base-nok.png')
39
+ compare = @image_comparator.compare
40
+ refute(compare, 'Images are equal')
41
+ end
42
+
43
+ def test_calculate_diff_image_ok
44
+ path_res = './tmp/ok_diff.png'
45
+ expected_fingerprint = 0
46
+
47
+ @image_comparator.define_images_to_compare('./images/base.png', './images/base-ok.png')
48
+ current_fingerprint = @image_comparator.calculate_diff_image(path_res)
49
+ fingerprint_diff_file = Phashion::Image.new(path_res).fingerprint
50
+
51
+ assert_equal(expected_fingerprint, current_fingerprint, 'Method fingerprint is not the expceted one')
52
+ assert_equal(expected_fingerprint, fingerprint_diff_file, 'Diff file fingerprint is not the expceted one')
53
+ end
54
+
55
+ def test_calculate_diff_image_fail
56
+ path_res = './tmp/fail_diff.png'
57
+ expected_fingerprint = 5450986487329114955
58
+
59
+ @image_comparator.define_images_to_compare('./images/base.png', './images/base-nok.png')
60
+ current_fingerprint = @image_comparator.calculate_diff_image(path_res)
61
+ fingerprint_diff_file = Phashion::Image.new(path_res).fingerprint
62
+
63
+ assert_equal(expected_fingerprint, current_fingerprint, 'Method fingerprint is not the expceted one')
64
+ assert_equal(expected_fingerprint, fingerprint_diff_file, 'Diff file fingerprint is not the expceted one')
65
+ end
66
+
67
+ def test_compare_and_generate_diff_ok
68
+ @image_comparator.define_images_to_compare('./images/base.png', './images/base-ok.png')
69
+ compare = @image_comparator.compare_and_generate_diff
70
+ assert(compare, 'Images are not equal')
71
+ end
72
+
73
+ def test_compare_and_generate_diff_nok
74
+ expected_fingerprint = 5450986487329114955
75
+ path_res = './tmp/fail_diff.png'
76
+
77
+ @image_comparator.define_images_to_compare('./images/base.png', './images/base-nok.png')
78
+ compare = @image_comparator.compare_and_generate_diff(path_res)
79
+ fingerprint_diff_file = Phashion::Image.new(path_res).fingerprint
80
+
81
+ refute(compare, 'Images are equal')
82
+ assert_equal(expected_fingerprint, fingerprint_diff_file, 'Diff file fingerprint is not the expceted one')
83
+ end
84
+
85
+ def test_compare_area_include
86
+ origin_coord = {'x':828, 'y':293}
87
+ end_coord = {'x':1173, 'y':688}
88
+ area = [{'origin': origin_coord, 'end': end_coord, 'exclude': false}]
89
+
90
+ @image_comparator.define_images_to_compare('./images/base.png', './images/base-nok-areas.png')
91
+ @image_comparator.set_areas(area)
92
+ compare = @image_comparator.compare
93
+
94
+ assert(compare, 'Images are not equals')
95
+ end
96
+
97
+ def test_compare_area_exclude
98
+ origin_coord = {'x':828, 'y':293}
99
+ end_coord = {'x':1173, 'y':688}
100
+ area = [{'origin': origin_coord, 'end': end_coord, 'exclude': true}]
101
+
102
+ @image_comparator.define_images_to_compare('./images/base.png', './images/base-nok.png')
103
+ @image_comparator.set_areas(area)
104
+ compare = @image_comparator.compare
105
+
106
+ assert(compare, 'Images are not equals')
107
+ end
108
+
109
+ def test_compare_several_areas_include
110
+ origin_coord = {'x':828, 'y':293}
111
+ end_coord = {'x':1173, 'y':688}
112
+ area1 = {'origin': origin_coord, 'end': end_coord, 'exclude': false}
113
+ origin_coord2 = {'x':0, 'y':0}
114
+ end_coord2 = {'x':828, 'y':293}
115
+ area2 = {'origin': origin_coord2, 'end': end_coord2, 'exclude': false}
116
+ areas = [area1, area2]
117
+
118
+ @image_comparator.define_images_to_compare('./images/base.png', './images/base-nok-areas.png')
119
+ @image_comparator.set_areas(areas)
120
+ compare = @image_comparator.compare
121
+
122
+ refute(compare, 'Images are compares')
123
+ end
124
+
125
+ def test_compare_several_areas_exclude
126
+ origin_coord = {'x':828, 'y':293}
127
+ end_coord = {'x':1173, 'y':688}
128
+ area1 = {'origin': origin_coord, 'end': end_coord, 'exclude': true}
129
+ origin_coord2 = {'x':0, 'y':0}
130
+ end_coord2 = {'x':828, 'y':293}
131
+ area2 = {'origin': origin_coord2, 'end': end_coord2, 'exclude': true}
132
+ areas = [area1, area2]
133
+
134
+ @image_comparator.define_images_to_compare('./images/base.png', './images/base-nok-areas.png')
135
+ @image_comparator.set_areas(areas)
136
+ compare = @image_comparator.compare
137
+
138
+ assert(compare, 'Images are not equals')
139
+ end
140
+
141
+ def test_compare_several_areas_mix_not_overlapping
142
+ origin_coord = {'x':828, 'y':293}
143
+ end_coord = {'x':1173, 'y':688}
144
+ area1 = {'origin': origin_coord, 'end': end_coord, 'exclude': false}
145
+ origin_coord2 = {'x':0, 'y':0}
146
+ end_coord2 = {'x':828, 'y':293}
147
+ area2 = {'origin': origin_coord2, 'end': end_coord2, 'exclude': true}
148
+ areas = [area1, area2]
149
+
150
+ @image_comparator.define_images_to_compare('./images/base.png', './images/base-nok-areas.png')
151
+ @image_comparator.set_areas(areas)
152
+ compare = @image_comparator.compare
153
+
154
+ assert(compare, 'Images are differents')
155
+ end
156
+
157
+ def test_compare_several_areas_mix_overlapping
158
+ origin_coord = {'x':828, 'y':293}
159
+ end_coord = {'x':1173, 'y':688}
160
+ area1 = {'origin': origin_coord, 'end': end_coord, 'exclude': false}
161
+ origin_coord2 = {'x':0, 'y':0}
162
+ end_coord2 = {'x':1000, 'y':490}
163
+ area2 = {'origin': origin_coord2, 'end': end_coord2, 'exclude': true}
164
+ areas = [area1, area2]
165
+
166
+ @image_comparator.define_images_to_compare('./images/base.png', './images/base-nok-areas.png')
167
+ @image_comparator.set_areas(areas)
168
+ compare = @image_comparator.compare
169
+
170
+ assert(compare, 'Images are differents')
171
+ end
172
+
173
+ def test_compare_and_generate_diff_area_include
174
+ origin_coord = {'x':828, 'y':293}
175
+ end_coord = {'x':1173, 'y':688}
176
+ area = [{'origin': origin_coord, 'end': end_coord, 'exclude': false}]
177
+ path_res = './tmp/ok_diff.png'
178
+
179
+ @image_comparator.define_images_to_compare('./images/base.png', './images/base-ok.png')
180
+ @image_comparator.set_areas(area)
181
+ compare = @image_comparator.compare_and_generate_diff(path_res)
182
+
183
+ assert(compare, 'Images are differents')
184
+ end
185
+
186
+ def test_compare_and_generate_diff_area_exclude
187
+ origin_coord = {'x':828, 'y':293}
188
+ end_coord = {'x':1173, 'y':688}
189
+ area = [{'origin': origin_coord, 'end': end_coord, 'exclude': true}]
190
+ path_res = './tmp/ok_diff.png'
191
+
192
+ @image_comparator.define_images_to_compare('./images/base.png', './images/base-nok.png')
193
+ @image_comparator.set_areas(area)
194
+ compare = @image_comparator.compare_and_generate_diff(path_res)
195
+
196
+ assert(compare, 'Images are differents')
197
+ end
198
+
199
+ def test_compare_and_generate_diff_several_areas_mix_overlapping
200
+ origin_coord = {'x':828, 'y':293}
201
+ end_coord = {'x':1173, 'y':688}
202
+ area1 = {'origin': origin_coord, 'end': end_coord, 'exclude': false}
203
+ origin_coord2 = {'x':0, 'y':0}
204
+ end_coord2 = {'x':1000, 'y':490}
205
+ area2 = {'origin': origin_coord2, 'end': end_coord2, 'exclude': true}
206
+ areas = [area1, area2]
207
+ path_res = './tmp/ok_diff.png'
208
+
209
+ @image_comparator.define_images_to_compare('./images/base.png', './images/base-nok-areas.png')
210
+ @image_comparator.set_areas(areas)
211
+ compare = @image_comparator.compare_and_generate_diff(path_res)
212
+
213
+ assert(compare, 'Images are differents')
214
+ end
215
+
216
+ def test_compare_and_generate_diff_area_include_fail
217
+ origin_coord = {'x':828, 'y':293}
218
+ end_coord = {'x':1173, 'y':688}
219
+ area = [{'origin': origin_coord, 'end': end_coord, 'exclude': false}]
220
+ path_res = './tmp/nok_diff.png'
221
+ expected_fingerprint = 5450986487329114955
222
+
223
+ @image_comparator.define_images_to_compare('./images/base.png', './images/base-nok.png')
224
+ @image_comparator.set_areas(area)
225
+ compare = @image_comparator.compare_and_generate_diff(path_res)
226
+ refute(compare, 'Images are equal')
227
+
228
+ fingerprint_diff_file = Phashion::Image.new(path_res).fingerprint
229
+ assert_equal(expected_fingerprint, fingerprint_diff_file, 'Diff file fingerprint is not the expected one')
230
+ end
231
+
232
+ def test_compare_and_generate_diff_area_exclude_fail
233
+ origin_coord = {'x':0, 'y':0}
234
+ end_coord = {'x':1000, 'y':490}
235
+ area = [{'origin': origin_coord, 'end': end_coord, 'exclude': true}]
236
+ path_res = './tmp/nok_diff.png'
237
+ expected_fingerprint = 5452075012271155531
238
+
239
+ @image_comparator.define_images_to_compare('./images/base.png', './images/base-nok.png')
240
+ @image_comparator.set_areas(area)
241
+ compare = @image_comparator.compare_and_generate_diff(path_res)
242
+ refute(compare, 'Images are equal')
243
+
244
+ fingerprint_diff_file = Phashion::Image.new(path_res).fingerprint
245
+ assert_equal(expected_fingerprint, fingerprint_diff_file, 'Diff file fingerprint is not the expected one')
246
+ end
247
+
248
+ def test_compare_and_generate_diff_several_areas_mix_overlapping_fail
249
+ origin_coord = {'x':828, 'y':293}
250
+ end_coord = {'x':1173, 'y':688}
251
+ area1 = {'origin': origin_coord, 'end': end_coord, 'exclude': true}
252
+ origin_coord2 = {'x':0, 'y':0}
253
+ end_coord2 = {'x':1000, 'y':490}
254
+ area2 = {'origin': origin_coord2, 'end': end_coord2, 'exclude': false}
255
+ areas = [area1, area2]
256
+ path_res = './tmp/nok_diff.png'
257
+ expected_fingerprint = 54086765383280
258
+
259
+ @image_comparator.define_images_to_compare('./images/base.png', './images/base-nok-areas.png')
260
+ @image_comparator.set_areas(areas)
261
+ compare = @image_comparator.compare_and_generate_diff(path_res)
262
+ refute(compare, 'Images are equal')
263
+
264
+ fingerprint_diff_file = Phashion::Image.new(path_res).fingerprint
265
+ assert_equal(expected_fingerprint, fingerprint_diff_file, 'Diff file fingerprint is not the expected one')
266
+ end
267
+ end
metadata ADDED
@@ -0,0 +1,143 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: image_comparator
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Alberto Costoya
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-11-10 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: phashion
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.1'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.1'
27
+ - !ruby/object:Gem::Dependency
28
+ name: chunky_png
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.3'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.3'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rmagick
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '2.15'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '2.15'
55
+ - !ruby/object:Gem::Dependency
56
+ name: yard
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '0.8'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '0.8'
69
+ - !ruby/object:Gem::Dependency
70
+ name: minitest
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '5.9'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '5.9'
83
+ description: This gem provides a simple api to compare 2 pngs and in case they are
84
+ different, get the differences on a png file
85
+ email: alberto.costoya@xing.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - README.md
92
+ - Rakefile
93
+ - doc/ImageComparator.html
94
+ - doc/_index.html
95
+ - doc/class_list.html
96
+ - doc/css/common.css
97
+ - doc/css/full_list.css
98
+ - doc/css/style.css
99
+ - doc/file.README.html
100
+ - doc/file_list.html
101
+ - doc/frames.html
102
+ - doc/index.html
103
+ - doc/js/app.js
104
+ - doc/js/full_list.js
105
+ - doc/js/jquery.js
106
+ - doc/method_list.html
107
+ - doc/top-level-namespace.html
108
+ - image_comparator.gemspec
109
+ - images/almost-the-same-1.png
110
+ - images/almost-the-same-2.png
111
+ - images/base-nok-areas.png
112
+ - images/base-nok.png
113
+ - images/base-ok.png
114
+ - images/base.png
115
+ - lib/image_comparator.rb
116
+ - test/image_comparator_test.rb
117
+ homepage: https://source.xing.com/jobs-team/image_comparison
118
+ licenses:
119
+ - GPL-3.0
120
+ metadata: {}
121
+ post_install_message:
122
+ rdoc_options: []
123
+ require_paths:
124
+ - lib
125
+ required_ruby_version: !ruby/object:Gem::Requirement
126
+ requirements:
127
+ - - ">="
128
+ - !ruby/object:Gem::Version
129
+ version: '0'
130
+ required_rubygems_version: !ruby/object:Gem::Requirement
131
+ requirements:
132
+ - - ">="
133
+ - !ruby/object:Gem::Version
134
+ version: '0'
135
+ requirements: []
136
+ rubyforge_project:
137
+ rubygems_version: 2.6.7
138
+ signing_key:
139
+ specification_version: 4
140
+ summary: Gem to compare to png images and get differences.
141
+ test_files:
142
+ - test/image_comparator_test.rb
143
+ has_rdoc: