--[[----------------------------------------------------------------------------

KSDataLuaRxTest.lua

--------------------------------------------------------------------------------

ADOBE CONFIDENTIAL
------------------
Copyright 2022 Adobe Systems Incorporated
All Rights Reserved.

NOTICE: All information contained herein is, and remain the property of
Adobe Systems Incorporated and its suppliers, if any. The intellectual and
technical concepts contained herein are proprietary to Adobe Systems
Incorporated and its suppliers and may be covered by U.S. and Foreign
Patents, patents in process, and are protected by trade secret or copyright
law.  Dissemination of this information or reproduction of this material is
strictly forbidden unless prior written permission is obtained from
Adobe Systems Incorporated.

------------------------------------------------------------------------------]]

	-- WF.core.self_test
local WFRxTestCase = require 'WFRxTestCase'

	-- WF.core.lua
local KSData = require 'KSData'

--------------------------------------------------------------------------------

local KSDataLuaRxTest = {}

--------------------------------------------------------------------------------

function KSDataLuaRxTest.rxTestAsyncCompressAndDecompress( rxTest )

	local stringData = assert( KSData.make( "Hello world." ) )

	assert( KSData.isData( stringData ) == true, "isData returned false for string data" )
	assert( KSData.length( stringData ) == 12, "isData returned incorrect length for string data" )

	local compressedData = rxTest:getFirstValue( KSData.asyncCompress( stringData ) )

	assert( KSData.isData( compressedData ) == true, "isData returned false for compressed data" )
	assert( KSData.length( compressedData ) ~= 12, "compress data length is equal to original data" )

	local decompressedData = rxTest:getFirstValue( KSData.asyncDecompress( compressedData ) )

	assert( KSData.isData( decompressedData ) == true, "isData returned false for decompressed data" )
	assert( KSData.length( decompressedData ) == 12, "decompressed data length is not equal to original data" )

	assert( tostring( decompressedData ) == "Hello world.", "compress and decompress breaks the string data" )

	rxTest:expectSingleValue(
		KSData.asyncCompress( stringData )
			:flatMap( function( compressedData )
				return KSData.asyncDecompress( compressedData )
			end )
			:map( function( decompressedData )
				return tostring( decompressedData )
			end ),
		"Hello world." )


	stringData:close()
	compressedData:close()
	decompressedData:close()

end

--------------------------------------------------------------------------------

-- The following xmp camera raw example is copied from WFCatAssetTestUtils. We
-- do not use WFCatAssetTestUtils here to avoid a dependency on the oz_client
-- WF module.

local xmpCameraRawStart = [[
<x:xmpmeta xmlns:x="adobe:ns:meta/" x:xmptk="Adobe XMP Core 5.6-c140 79.160451, 2017/05/06-01:08:21        ">
 <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
  <rdf:Description rdf:about=""
    xmlns:crs="http://ns.adobe.com/camera-raw-settings/1.0/"
   crs:Version="10.3"
   crs:ProcessVersion="10.0"
   crs:WhiteBalance="As Shot"
   crs:IncrementalTemperature="0"
   crs:IncrementalTint="0"
   crs:Saturation="0"
   crs:Sharpness="0"
   crs:LuminanceSmoothing="0"
   crs:ColorNoiseReduction="0"
   crs:VignetteAmount="0"
   crs:ShadowTint="0"
   crs:RedHue="0"
   crs:RedSaturation="0"
   crs:GreenHue="0"
   crs:GreenSaturation="0"
   crs:BlueHue="0"
   crs:BlueSaturation="0"
   crs:Vibrance="0"
   crs:HueAdjustmentRed="0"
   crs:HueAdjustmentOrange="0"
   crs:HueAdjustmentYellow="0"
   crs:HueAdjustmentGreen="0"
   crs:HueAdjustmentAqua="0"
   crs:HueAdjustmentBlue="0"
   crs:HueAdjustmentPurple="0"
   crs:HueAdjustmentMagenta="0"
   crs:SaturationAdjustmentRed="0"
   crs:SaturationAdjustmentOrange="0"
   crs:SaturationAdjustmentYellow="0"
   crs:SaturationAdjustmentGreen="0"
   crs:SaturationAdjustmentAqua="0"
   crs:SaturationAdjustmentBlue="0"
   crs:SaturationAdjustmentPurple="0"
   crs:SaturationAdjustmentMagenta="0"
   crs:LuminanceAdjustmentRed="0"
   crs:LuminanceAdjustmentOrange="0"
   crs:LuminanceAdjustmentYellow="0"
   crs:LuminanceAdjustmentGreen="0"
   crs:LuminanceAdjustmentAqua="0"
   crs:LuminanceAdjustmentBlue="0"
   crs:LuminanceAdjustmentPurple="0"
   crs:LuminanceAdjustmentMagenta="0"
   crs:SplitToningShadowHue="0"
   crs:SplitToningShadowSaturation="0"
   crs:SplitToningHighlightHue="0"
   crs:SplitToningHighlightSaturation="0"
   crs:SplitToningBalance="0"
   crs:ParametricShadows="0"
   crs:ParametricDarks="0"
   crs:ParametricLights="0"
   crs:ParametricHighlights="0"
   crs:ParametricShadowSplit="25"
   crs:ParametricMidtoneSplit="50"
   crs:ParametricHighlightSplit="75"
   crs:SharpenRadius="+1.0"
   crs:SharpenDetail="25"
   crs:SharpenEdgeMasking="0"
   crs:PostCropVignetteAmount="0"
   crs:GrainAmount="0"
   crs:LensProfileEnable="0"
   crs:LensManualDistortionAmount="0"
   crs:PerspectiveVertical="0"
   crs:PerspectiveHorizontal="0"
   crs:PerspectiveRotate="0.0"
   crs:PerspectiveScale="100"
   crs:PerspectiveAspect="0"
   crs:PerspectiveUpright="0"
   crs:PerspectiveX="0.00"
   crs:PerspectiveY="0.00"
   crs:AutoLateralCA="0"
   crs:Exposure2012="0.00"
   crs:Contrast2012="0"
   crs:Highlights2012="0"
   crs:Shadows2012="0"
   crs:Whites2012="0"
   crs:Blacks2012="0"
   crs:Clarity2012="0"
   crs:DefringePurpleAmount="0"
   crs:DefringePurpleHueLo="30"
   crs:DefringePurpleHueHi="70"
   crs:DefringeGreenAmount="0"
   crs:DefringeGreenHueLo="40"
   crs:DefringeGreenHueHi="60"
   crs:Dehaze="0"
   crs:ToneMapStrength="0"
   crs:ConvertToGrayscale="False"
   crs:OverrideLookVignette="False"
   crs:ToneCurveName="Linear"
   crs:ToneCurveName2012="Linear"
   crs:CameraProfile="Embedded"
   crs:CameraProfileDigest="54650A341B5B5CCAE8442D0B43A92BCE"
   crs:LensProfileSetup="LensDefaults"
   crs:UprightVersion="151388160"
   crs:UprightCenterMode="0"
   crs:UprightCenterNormX="0.5"
   crs:UprightCenterNormY="0.5"
   crs:UprightFocalMode="0"
   crs:UprightFocalLength35mm="35"
   crs:UprightPreview="False"
   crs:UprightTransformCount="6"
   crs:UprightFourSegmentsCount="0"
   crs:HasSettings="True"
   crs:AlreadyApplied="False">
   <crs:ToneCurve>
    <rdf:Seq>
     <rdf:li>0, 0</rdf:li>
     <rdf:li>255, 255</rdf:li>
    </rdf:Seq>
   </crs:ToneCurve>
   <crs:ToneCurveRed>
    <rdf:Seq>
     <rdf:li>0, 0</rdf:li>
     <rdf:li>255, 255</rdf:li>
    </rdf:Seq>
   </crs:ToneCurveRed>
   <crs:ToneCurveGreen>
    <rdf:Seq>
     <rdf:li>0, 0</rdf:li>
     <rdf:li>255, 255</rdf:li>
    </rdf:Seq>
   </crs:ToneCurveGreen>
   <crs:ToneCurveBlue>
    <rdf:Seq>
     <rdf:li>0, 0</rdf:li>
     <rdf:li>255, 255</rdf:li>
    </rdf:Seq>
   </crs:ToneCurveBlue>
   <crs:ToneCurvePV2012>
    <rdf:Seq>
     <rdf:li>0, 0</rdf:li>
     <rdf:li>255, 255</rdf:li>
    </rdf:Seq>
   </crs:ToneCurvePV2012>
   <crs:ToneCurvePV2012Red>
    <rdf:Seq>
     <rdf:li>0, 0</rdf:li>
     <rdf:li>255, 255</rdf:li>
    </rdf:Seq>
   </crs:ToneCurvePV2012Red>
   <crs:ToneCurvePV2012Green>
    <rdf:Seq>
     <rdf:li>0, 0</rdf:li>
     <rdf:li>255, 255</rdf:li>
    </rdf:Seq>
   </crs:ToneCurvePV2012Green>
   <crs:ToneCurvePV2012Blue>
    <rdf:Seq>
     <rdf:li>0, 0</rdf:li>
     <rdf:li>255, 255</rdf:li>
    </rdf:Seq>
   </crs:ToneCurvePV2012Blue>
   <crs:PaintBasedCorrections>
    <rdf:Seq>
     <rdf:li>
      <rdf:Description
       crs:What="Correction"
       crs:CorrectionAmount="1.000000"
       crs:CorrectionActive="true"
       crs:LocalExposure="0.000000"
       crs:LocalSaturation="0.000000"
       crs:LocalContrast="0.000000"
       crs:LocalClarity="0.000000"
       crs:LocalSharpness="0.000000"
       crs:LocalBrightness="0.000000"
       crs:LocalToningHue="0.000000"
       crs:LocalToningSaturation="0.000000"
       crs:LocalExposure2012="0.500000"
       crs:LocalContrast2012="0.000000"
       crs:LocalHighlights2012="0.000000"
       crs:LocalShadows2012="-0.042071"
       crs:LocalWhites2012="1.000000"
       crs:LocalBlacks2012="1.000000"
       crs:LocalClarity2012="0.000000"
       crs:LocalDehaze="0.000000"
       crs:LocalLuminanceNoise="0.000000"
       crs:LocalMoire="0.000000"
       crs:LocalDefringe="0.000000"
       crs:LocalTemperature="0.000000"
       crs:LocalTint="0.000000">
      <crs:CorrectionMasks>
       <rdf:Seq>
        <rdf:li>
         <rdf:Description
          crs:What="Mask/Paint"
          crs:MaskValue="1.000000"
          crs:Radius="0.013802"
          crs:Flow="0.500000"
          crs:CenterWeight="0.000000">
         <crs:Dabs>
          <rdf:Seq>
           <rdf:li>d 0.944824 0.911002</rdf:li>
           <rdf:li>d 0.940887 0.909293</rdf:li>
           <rdf:li>d 0.936950 0.907583</rdf:li>
]]

local xmpCameraRawEnd = [[
           <rdf:li>d 0.496560 0.297892</rdf:li>
          </rdf:Seq>
         </crs:Dabs>
         </rdf:Description>
        </rdf:li>
       </rdf:Seq>
      </crs:CorrectionMasks>
      <crs:CorrectionRangeMask
       crs:Version="+2"
       crs:Type="0"
       crs:ColorAmount="0.500000"
       crs:LumMin="0.000000"
       crs:LumMax="1.000000"
       crs:LumFeather="0.500000"/>
      </rdf:Description>
     </rdf:li>
    </rdf:Seq>
   </crs:PaintBasedCorrections>
   <crs:Look
    crs:Name=""/>
  </rdf:Description>
 </rdf:RDF>
</x:xmpmeta>
]]

local function getCameraRaw( useBigXmpDevelop )

	local xmpCameraRaw = xmpCameraRawStart
	if useBigXmpDevelop then
		local x = 0.900
		local y = 0.800

		for i = 1, 400 do
			x = x - .001
			y = y - .001
			local stroke =  string.format("           <rdf:li>d %f %f</rdf:li>\r\n", x, y )
			xmpCameraRaw = xmpCameraRaw .. stroke
		end
	end

	xmpCameraRaw = xmpCameraRaw .. xmpCameraRawEnd

	return xmpCameraRaw

end

--------------------------------------------------------------------------------

function KSDataLuaRxTest.rxTestAsyncCompressAndDecompress_xmpDevelop( rxTest )

	local xmpDevelopString = getCameraRaw( true )
	local xmpDevelopData = KSData.make( xmpDevelopString )
	local xmpDevelopFileSize = #xmpDevelopString

	local compressedData = rxTest:getFirstValue( KSData.asyncCompress( xmpDevelopData ) )

	rxTest:expectEqual( KSData.isData( compressedData ), true )
	rxTest:expectEqual( KSData.length( compressedData ) < xmpDevelopFileSize, true )

	local compressedDataLen = KSData.length( compressedData )
	local decompressedData = rxTest:getFirstValue( KSData.asyncDecompress( compressedData, compressedDataLen ) )

	rxTest:expectEqual( KSData.isData( decompressedData ), true )
	rxTest:expectEqual( KSData.length( decompressedData ), xmpDevelopFileSize )

	rxTest:expectEqual( tostring( decompressedData ), xmpDevelopString )

	rxTest:expectSingleValue(
		KSData.asyncCompress( xmpDevelopData )
			:flatMap( function( compressedData )
				return KSData.asyncDecompress( compressedData )
			end )
			:map( function( decompressedData )
				return tostring( decompressedData )
			end ),
		xmpDevelopString )

	xmpDevelopData:close()
	compressedData:close()
	decompressedData:close()

end

--------------------------------------------------------------------------------

return WFRxTestCase.wrapSuite( 'KSDataLuaRxTest', KSDataLuaRxTest )
