#!/usr/bin/env ruby
#--
# Color
# Colour management with Ruby
# http://rubyforge.org/projects/color
#   Version 1.4.0
#
# Licensed under a MIT-style licence. See Licence.txt in the main
# distribution for full licensing information.
#
# Copyright (c) 2005 - 2007 Austin Ziegler and Matt Lyon
#
# $Id: test_all.rb 55 2007-02-03 23:29:34Z austin $
#++

$LOAD_PATH.unshift("#{File.dirname(__FILE__)}/../lib") if __FILE__ == $0
require 'test/unit'
require 'color'
require 'color/palette/adobecolor'

module TestColor
  module TestPalette
    class TestAdobeColor < Test::Unit::TestCase
      include Color::Palette

      # This is based on the Visibone Anglo-Centric Color Code List; this is
      # an Adobe Color swatch version 1 (RGB colours only).
      VISIBONE_V1 = <<-EOS
AAEA2AAA/wD/AP8AAAAAAMwAzADMAAAAAACZAJkAmQAAAAAAZgBmAGYAAAAA
ADMAMwAzAAAAAAAAAAAAAAAAAAAA/wAAAAAAAAAAAP8AMwAzAAAAAADMAAAA
AAAAAAAA/wBmAGYAAAAAAMwAMwAzAAAAAACZAAAAAAAAAAAA/wCZAJkAAAAA
AMwAZgBmAAAAAACZADMAMwAAAAAAZgAAAAAAAAAAAP8AzADMAAAAAADMAJkA
mQAAAAAAmQBmAGYAAAAAAGYAMwAzAAAAAAAzAAAAAAAAAAAA/wAzAAAAAAAA
AP8AZgAzAAAAAADMADMAAAAAAAAA/wCZAGYAAAAAAMwAZgAzAAAAAACZADMA
AAAAAAAA/wBmAAAAAAAAAP8AmQAzAAAAAADMAGYAAAAAAAAA/wDMAJkAAAAA
AMwAmQBmAAAAAACZAGYAMwAAAAAAZgAzAAAAAAAAAP8AmQAAAAAAAAD/AMwA
ZgAAAAAAzACZADMAAAAAAJkAZgAAAAAAAADMAJkAAAAAAAAA/wDMADMAAAAA
AP8AzAAAAAAAAAD/AP8AAAAAAAAA/wD/ADMAAAAAAMwAzAAAAAAAAAD/AP8A
ZgAAAAAAzADMADMAAAAAAJkAmQAAAAAAAAD/AP8AmQAAAAAAzADMAGYAAAAA
AJkAmQA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QDMAAAAAADMAGYA
mQAAAAAAmQAzAGYAAAAAAGYAAAAzAAAAAAD/AAAAZgAAAAAA/wBmAJkAAAAA
AMwAMwBmAAAAAACZAAAAMwAAAAAA/wAzAGYAAAAAAMwAAAAzAAAAAAD/AAAA
MwAAAA==
      EOS

      # This is based on the Visibone Anglo-Centric Color Code List; this is
      # an Adobe Color swatch version 2 (with names).
      VISIBONE_V2 = <<-EOS
[encoded data, 13908 characters]
/wAAADMAAAAAAAANAFIAZQBkAC0AUgBlAGQALQBQAGkAbgBrAAA=
      EOS

      EXERCISE = <<-EOS
AAIABwAAHgA8AFoAAAAAAAAEAFIARwBCAAAAAQKPGZn//wAAAAAABABIAFMA
QgAAAAIMzH//GZn//wAAAAUAQwBNAFkASwAAAAcbWOiQG1gAAAAAAAQATABB
AEIAAAAIFXwAAAAAAAAAAAAFAEcAcgBhAHkAAAAJCcQNrBGUBdwAAAAGAFcA
QwBNAFkASwAAAA0JxA2sEZQF3AAAAAsAVQBOAEsATgBPAFcATgAgADEAMwAA
      EOS

# http://www.visibone.com/swatches/VisiBone.aco
# http://www.visibone.com/swatches/VisiBone2.aco
# http://www.visibone.com/swatches/VisiBone2_km.psppalette
# http://www.visibone.com/swatches/VisiBone2_km.pal
# http://www.visibone.com/swatches/VisiBone2_vaccc.ai
# http://www.visibone.com/swatches/VisiBone.gimp
# http://www.visibone.com/swatches/VisiBone.act

      def setup
        @filename = "test#{Process.pid}.aco"
      end

      def teardown
        require 'fileutils'
        FileUtils.rm_f @filename if File.exist? @filename
      end

      def test_version1
        v1 = VISIBONE_V1.unpack("m*")[0]
        assert_nothing_raised { @aco = AdobeColor.new(v1) }
        assert_equal(216, @aco.size)
        assert_equal(1, @aco.version)
        assert_equal({:rgb => 216}, @aco.statistics)
        assert_equal(Color::RGB::White, @aco[0])
        assert_equal("#ff0033", @aco[-1].html)
        assert_equal(v1, @aco.to_aco)
      end

      def test_version2
        v2 = VISIBONE_V2.unpack("m*")[0]
        @aco = AdobeColor.new(v2)
        assert_equal(216, @aco.size)
        assert_equal(2, @aco.version)
        assert_equal({:rgb => 216}, @aco.statistics)
        assert_equal(Color::RGB::White, @aco[0])
        assert_equal(Color::RGB::White,
                     @aco["\000W\000h\000i\000t\000e"][0])
        assert_equal("#ff0033", @aco[-1].html)
        assert_equal("#ff0033",
                     @aco["\000R\000e\000d\000-\000R\000e\000d\000-\000P\000i\000n\000k"][0].html)
        assert_equal(v2, @aco.to_aco)
      end

      def test_bogus
        o = VISIBONE_V2.unpack("m*")[0]
        v = o.dup
        v[0, 2] = [ 322 ].pack("n") # break the version
        assert_raises(RuntimeError) { AdobeColor.new(v) }
        v = o.dup
        v[2, 2] = [ 217 ].pack("n") # break the colour count
        assert_raises(IndexError) { @aco = AdobeColor.new(v) }
        v = o.dup
        v[14, 2] = [ 99 ].pack("n") # break the NULL before the name
        assert_raises(IndexError) { @aco = AdobeColor.new(v) }
        v = o.dup
        v[16, 2] = [ 18 ].pack("n") # break the length of the name
        assert_raises(IndexError) { @aco = AdobeColor.new(v) }
        v = o.dup
        v[28, 2] = [ 99 ].pack("n") # break the trailing null of the name
      end

      def test_exercise
        assert_nothing_raised do
          File.open(@filename, "wb") do |f|
            f.write EXERCISE.unpack("m*")[0]
          end
        end
        assert_nothing_raised { @aco = AdobeColor.from_file(@filename) }
        assert_equal(5, @aco.size)
        assert_equal(7, @aco.instance_variable_get(:@order).size)
      end

      def test_each
        v1 = VISIBONE_V1.unpack("m*")[0]
        @aco = AdobeColor.new(v1)
        @aco.each { |c| assert_kind_of(Color::RGB, c) }
      end

      def test_each_name
        v2 = VISIBONE_V2.unpack("m*")[0]
        assert_nothing_raised { @aco = AdobeColor.new(v2) }
        assert_equal(216, @aco.size)
        assert_equal(2, @aco.version)
        @aco.each_name do |n, s|
          assert_equal(0, n[0]) if RUBY_VERSION < "1.9"
          assert_equal(1, s.size)
          assert_kind_of(Color::RGB, s[0])
        end
      end

      def test_values_at
        v2 = VISIBONE_V2.unpack("m*")[0]
        assert_nothing_raised { @aco = AdobeColor.new(v2) }
        assert_equal(216, @aco.size)
        assert_equal(2, @aco.version)
        assert_equal([Color::RGB::White, Color::RGB.from_html("#ff0033")],
                     @aco.values_at(0, -1))
      end
    end
  end
end
