/*
 * 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.ColorSpace;
import java.awt.color.ICC_ColorSpace;
import java.awt.color.ICC_Profile;
import java.io.IOException;
import java.util.Arrays;

import static org.junit.Assert.assertArrayEquals;
import static org.mockito.Mockito.*;

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

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

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

    @Test
    public void testFixProfileUpdateHeader() throws Exception {
        byte[] header = new byte[128];
        header[ICC_Profile.icHdrRenderingIntent + 3] = 1;
        ICC_Profile profile = mock(ICC_Profile.class);
        when(profile.getData(ICC_Profile.icSigHead)).thenReturn(header);

        // Can't test that the values are actually changed, as the LCMS-backed implementation
        // of ICC_Profile does not change based on this invocation.
        new KCMSSanitizerStrategy().fixProfile(profile);

        // Verify that the method was invoked
        verify(profile).setData(eq(ICC_Profile.icSigHead), any(byte[].class));
    }

    @Test
    public void testFixProfileCorbisRGB() throws IOException {
        // TODO: Consider re-writing this using mocks, to avoid dependencies on the CMS implementation
        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));
    }
}
@@ -1658,91 +1658,9 @@
 lor.
-ColorSpace;%0Aimport java.awt.color.ICC_ColorSpace;%0Aimport java.awt.color.ICC_Profile
+*
 ;%0Aim
@@ -1764,45 +1764,8 @@
 als;
-%0Aimport static org.mockito.Mockito.*;
 %0A%0Apu
@@ -1971,33 +1971,16 @@
 rofile()
- throws Exception
  %7B%0A     
@@ -2077,33 +2077,16 @@
 rofile()
- throws Exception
  %7B%0A     
@@ -2273,16 +2273,18 @@
  throws 
+IO
 Exceptio
@@ -2299,514 +2299,444 @@
     
-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
+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
 (profile
 ).se
@@ -2735,21 +2735,17 @@
 file
-).s
+.g
 etData(
-eq(
 ICC_
@@ -2768,24 +2768,78 @@
 d), 
-any(byte%5B%5D.class
+ICC_Profile.icHdrRenderingIntent, ICC_Profile.icHdrRenderingIntent + 4
 ));%0A
@@ -2922,111 +2922,8 @@
 n %7B%0A
-        // TODO: Consider re-writing this using mocks, to avoid dependencies on the CMS implementation%0A
     

Update test to pass on JDK 19.