/*
 * Copyright (c) 2015, Harald Kuhr
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * * Redistributions of source code must retain the above copyright notice, this
 *   list of conditions and the following disclaimer.
 *
 * * Redistributions in binary form must reproduce the above copyright notice,
 *   this list of conditions and the following disclaimer in the documentation
 *   and/or other materials provided with the distribution.
 *
 * * Neither the name of the copyright holder nor the names of its
 *   contributors may be used to endorse or promote products derived from
 *   this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package com.twelvemonkeys.imageio.color;

import org.junit.Test;

import java.awt.color.*;
import java.io.IOException;
import java.util.Arrays;

import static org.junit.Assert.assertArrayEquals;

public class KCMSSanitizerStrategyTest {
    private static final byte[] XYZ = new byte[] {'X', 'Y', 'Z', ' '};

    @Test(expected = IllegalArgumentException.class)
    public void testFixProfileNullProfile() {
        new KCMSSanitizerStrategy().fixProfile(null);
    }

    @Test
    public void testFixProfile() {
        new KCMSSanitizerStrategy().fixProfile(((ICC_ColorSpace) ColorSpace.getInstance(ColorSpace.CS_sRGB)).getProfile());
    }

    @Test
    public void testFixProfileUpdateHeader() throws IOException {
        ICC_Profile profile = ICC_Profile.getInstance(getClass().getResourceAsStream("/profiles/adobe_rgb_1998.icc"));

        // Mangle the profile slightly...
        byte[] header = profile.getData(ICC_Profile.icSigHead);
        header[ICC_Profile.icHdrRenderingIntent + 3] = 42;

        new KCMSSanitizerStrategy().fixProfile(profile);

        assertArrayEquals(new byte[] {0, 0, 0, ICC_Profile.icPerceptual}, Arrays.copyOfRange(profile.getData(ICC_Profile.icSigHead), ICC_Profile.icHdrRenderingIntent, ICC_Profile.icHdrRenderingIntent + 4));
    }

    @Test
    public void testFixProfileCorbisRGB() throws IOException {
        ICC_Profile corbisRGB = ICC_Profile.getInstance(getClass().getResourceAsStream("/profiles/Corbis RGB.icc"));

        new KCMSSanitizerStrategy().fixProfile(corbisRGB);

        // Make sure all known affected tags have type 'XYZ '
        assertArrayEquals(XYZ, Arrays.copyOfRange(corbisRGB.getData(ICC_Profile.icSigMediaWhitePointTag), 0, 4));
        assertArrayEquals(XYZ, Arrays.copyOfRange(corbisRGB.getData(ICC_Profile.icSigRedColorantTag), 0, 4));
        assertArrayEquals(XYZ, Arrays.copyOfRange(corbisRGB.getData(ICC_Profile.icSigGreenColorantTag), 0, 4));
        assertArrayEquals(XYZ, Arrays.copyOfRange(corbisRGB.getData(ICC_Profile.icSigBlueColorantTag), 0, 4));
    }
}
@@ -1654,17 +1654,99 @@
 t.color.
-*
+ColorSpace;%0Aimport java.awt.color.ICC_ColorSpace;%0Aimport java.awt.color.ICC_Profile
 ;%0Aimport
@@ -1842,16 +1842,53 @@
 yEquals;
+%0Aimport static org.mockito.Mockito.*;
 %0A%0Apublic
@@ -2082,32 +2082,49 @@
 ileNullProfile()
+ throws Exception
  %7B%0A        new K
@@ -2213,16 +2213,33 @@
 rofile()
+ throws Exception
  %7B%0A     
@@ -2418,34 +2418,32 @@
 Header() throws 
-IO
 Exception %7B%0A    
@@ -2450,444 +2450,514 @@
     
-ICC_Profile profile = ICC_Profile.getInstance(getClass().getResourceAsStream(%22/profiles/adobe_rgb_1998.icc%22));%0A%0A        // Mangle the profile slightly...%0A        byte%5B%5D header = profile.getData(ICC_Profile.icSigHead);%0A        header%5BICC_Profile.icHdrRenderingIntent + 3%5D = 42;%0A%0A        new KCMSSanitizerStrategy().fixProfile(profile);%0A%0A        assertArrayEquals(new byte%5B%5D %7B0, 0, 0, ICC_Profile.icPerceptual%7D, Arrays.copyOfRange
+byte%5B%5D header = new byte%5B128%5D;%0A        header%5BICC_Profile.icHdrRenderingIntent + 3%5D = 1;%0A        ICC_Profile profile = mock(ICC_Profile.class);%0A        when(profile.getData(ICC_Profile.icSigHead)).thenReturn(header);%0A%0A        // Can't test that the values are actually changed, as the LCMS-backed implementation%0A        // of ICC_Profile does not change based on this invocation.%0A        new KCMSSanitizerStrategy().fixProfile(profile);%0A%0A        // Verify that the method was invoked%0A        verify
 (profile
 .get
@@ -2948,33 +2948,37 @@
 rify(profile
-.g
+).s
 etData(
+eq(
 ICC_Profile.
@@ -2993,78 +2993,24 @@
 d), 
-ICC_Profile.icHdrRenderingIntent, ICC_Profile.icHdrRenderingIntent + 4
+any(byte%5B%5D.class
 ));%0A
@@ -3081,32 +3081,135 @@
 s IOException %7B%0A
+        // TODO: Consider re-writing this using mocks, to avoid dependencies on the CMS implementation%0A
         ICC_Prof

Revert "Update test to pass on JDK 19."