test/unit/generator/pdf/document/graphics/test_image.rb in thinreports-0.9.1 vs test/unit/generator/pdf/document/graphics/test_image.rb in thinreports-0.10.0

- old
+ new

@@ -1,150 +1,46 @@ -# coding: utf-8 +# frozen_string_literal: true require 'test_helper' - require 'base64' -require 'digest/md5' class Thinreports::Generator::PDF::Graphics::TestImage < Minitest::Test include Thinreports::TestHelper - class TestImage - include Thinreports::Generator::PDF::Graphics + def setup + format = Thinreports::Layout::Format.build(self.layout_file.path) + @document = Thinreports::Generator::PDF::Document.new.tap { |doc| + doc.start_new_page(format) + } end - def test_normalize_image_from_base64_in_disabled - Thinreports.config.convert_palleted_transparency_png = false - - test_image = TestImage.new - base64_image = Base64.encode64(read_data_file('image_normal.png')) - - # Should not be normalized - TestImage::PNGNormalizer.stubs(:load_blob).never - test_image.normalize_image_from_base64('image/png', base64_image) - - image_registry = test_image.temp_image_registry - assert_equal 1, image_registry.count + def test_image + each_image do |image_filename| + @document.image(data_file(image_filename), 0, 0, 100, 100) + @document.image(StringIO.new(read_data_file(image_filename)), 0, 100, 100, 100) + end + assert_equal 6, analyze_pdf_images(@document.render).count end - def test_normalize_image_from_file_in_disabled - Thinreports.config.convert_palleted_transparency_png = false - - original_image_path = data_file('image_pallete_based.png') - - test_image = TestImage.new - returned_image_path = test_image.normalize_image_from_file(original_image_path) - - # Should not be normalized - assert_empty test_image.temp_image_registry - assert_same original_image_path, returned_image_path - end - - def test_normalize_image_from_base64_with_normal_images - Thinreports.config.convert_palleted_transparency_png = true - - [ - ['image/png', 'image_normal.png'], - ['image/jpeg', 'image_normal.jpg'] - ] - .each do |(image_type, image_file)| - base64_normal_image = Base64.encode64(read_data_file(image_file)) - - test_image = TestImage.new - - normalized_image_path = test_image.normalize_image_from_base64(image_type, base64_normal_image) - - image_registry = test_image.temp_image_registry - assert_equal 1, image_registry.count - - image_id = Digest::MD5.hexdigest(base64_normal_image) - assert_includes image_registry.keys, image_id - assert_equal normalized_image_path, image_registry[image_id].path - - assert equal_image(data_file(image_file), normalized_image_path) - - assert_equal normalized_image_path, - test_image.normalize_image_from_base64(image_type, base64_normal_image) + def test_base64image + each_image do |image_filename| + @document.base64image(Base64.encode64(read_data_file(image_filename)), 0, 0, 100, 100) end + assert_equal 3, analyze_pdf_images(@document.render).count end - def test_normalize_image_from_base64_with_palette_transparency_png - Thinreports.config.convert_palleted_transparency_png = true - - base64_palleted_png = Base64.encode64(read_data_file('image_pallete_based.png')) - - test_image = TestImage.new - - normalized_image_path = test_image.normalize_image_from_base64('image/png', base64_palleted_png) - - image_registry = test_image.temp_image_registry - assert_equal 1, image_registry.count - - image_id = Digest::MD5.hexdigest(base64_palleted_png) - assert_includes image_registry.keys, image_id - assert_equal normalized_image_path, image_registry[image_id].path - - refute equal_image(data_file('image_pallete_based.png'), normalized_image_path) - - assert_not_palette_based_transparency_png File.read(normalized_image_path) - - # 2nd time - assert_equal normalized_image_path, - test_image.normalize_image_from_base64('image/png', base64_palleted_png) - assert_equal 1, test_image.temp_image_registry.count - end - - def test_normalize_image_from_file_with_normal_images - Thinreports.config.convert_palleted_transparency_png = true - - [ - data_file('image_normal.png'), - data_file('iamge_normal.jpg'), - data_file('image_normal_png_noext'), - data_file('image_normal_jpg_noext') - ] - .each do |original_image_path| - test_image = TestImage.new - image_path = test_image.normalize_image_from_file(original_image_path) - - assert_equal original_image_path, image_path - assert_empty test_image.temp_image_registry + def test_image_box + each_image do |image_filename| + @document.image_box(data_file(image_filename), 0, 0, 100, 100) + @document.image(StringIO.new(read_data_file(image_filename)), 0, 100, 100, 100) end + assert_equal 6, analyze_pdf_images(@document.render).count end - def test_normalize_image_from_file_with_palette_transparency_png - Thinreports.config.convert_palleted_transparency_png = true - - original_image_path = data_file('image_pallete_based.png') - - test_image = TestImage.new - - normalized_image_path = test_image.normalize_image_from_file(original_image_path) - image_registry = test_image.temp_image_registry - - assert_equal 1, image_registry.count - assert_includes image_registry.keys, original_image_path - - refute equal_image(original_image_path, normalized_image_path) - - assert_not_palette_based_transparency_png File.read(normalized_image_path) - - # It should never called in 2nd time or subsequent. - TestImage::PNGNormalizer.stubs(:load_file).never - assert_equal normalized_image_path, - test_image.normalize_image_from_file(original_image_path) - end - - def assert_not_palette_based_transparency_png(data) - image_data = ChunkyPNG::Image.from_blob(data) - datastream = ChunkyPNG::Datastream.from_blob(data) - - assert_equal ChunkyPNG::COLOR_INDEXED, image_data.palette.best_color_settings.first - assert_equal nil, datastream.transparency_chunk - end - - def equal_image(expect_path, actual_path) - expect_image = File.binread(expect_path) - actual_image = File.binread(actual_path) - expect_image == actual_image + def each_image(&block) + %w( + image_normal.png + image_normal.jpg + image_pallete_based.png + ).each { |image_filename| block.call(image_filename) } end end