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

WFCatImportMetadataTest.lua

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

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

NOTICE: All information contained herein is, and remains 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 are protected by all applicable
intellectual property laws, including trade secret and copyright laws.
Dissemination of this information or reproduction of this material is
strictly forbidden unless prior written permission is obtained from
Adobe Systems Incorporated.

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

local WFPlatformInfo = require "WFPlatformInfo"

-- Disabled on iOS and Android because WFXmp is not present there

if WFPlatformInfo.isAndroid or WFPlatformInfo.isiOS then
       return {}
end

	-- WF.oz_client.managed_catalog.metadata
local WFXMLNS = require 'WFXMLNS'

--==============================================================================

local McatImportRequestsTest = {}

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

function McatImportRequestsTest.testAuxMetadataExtraction()

	local WFXmp = assert( require 'WFXmp', "WFXmp namespace exists" )
	local WFCatImportMetadata = assert( require 'WFCatImportMetadata', "WFCatImportMetadata namespace exists" )

	assert( WFCatImportMetadata.convertXmpInfo, "convertXmpInfo() exists" )
	
	local metadata = WFXmp.WFXmp()
	metadata:set( WFXMLNS.aux, "Lens", "My Lens" )
	metadata:set( WFXMLNS.aux, "SerialNumber", "1235number6789" )

	local xmpDoc = WFCatImportMetadata.convertXmpInfo( metadata )
	
	assert( type( xmpDoc ) == 'table', 'Got xmp doc table' )
	assert( type( xmpDoc.aux ) == 'table', 'xmp.aux is table' )
	assert( xmpDoc.aux.Lens == "My Lens", 'xmp.aux.Lens is correct' )
	assert( xmpDoc.aux.SerialNumber == "1235number6789", 'xmp.aux.Lens is correct' )

end

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

function McatImportRequestsTest.testNoExtraTables()

	local WFXmp = assert( require 'WFXmp', "WFXmp namespace exists" )
	local WFCatImportMetadata = require 'WFCatImportMetadata'

	local metadata = WFXmp.WFXmp()

	local xmpDoc = WFCatImportMetadata.convertXmpInfo( metadata )
	
	assert( next( xmpDoc ) == nil, 'no extra fields' )

end

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

function McatImportRequestsTest.testIntegerField()

	local WFXmp = assert( require 'WFXmp', "WFXmp namespace exists" )
	local WFCatImportMetadata = require 'WFCatImportMetadata'

	local metadata = WFXmp.WFXmp()
	metadata:set( WFXMLNS.exif, 'FocalLengthIn35mmFilm', '101' )

	local xmpDoc = WFCatImportMetadata.convertXmpInfo( metadata )
	
	assert( type( xmpDoc.exif ) == 'table', 'xmpDoc.exif is table' )
	assert( xmpDoc.exif.FocalLengthIn35mmFilm == 101, 'xmpDoc.exif.FocalLengthIn35mmFilm is 101' )
end

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

function McatImportRequestsTest.testFractionField()

	local WFXmp = assert( require 'WFXmp', "WFXmp namespace exists" )
	local WFCatImportMetadata = require 'WFCatImportMetadata'

	local metadata = WFXmp.WFXmp()
	metadata:set( WFXMLNS.exif, 'ApertureValue', '4/1' )

	local xmpDoc = WFCatImportMetadata.convertXmpInfo( metadata )
	
	assert( type( xmpDoc.exif ) == 'table', 'xmp.exif is table' )
	assert( type( xmpDoc.exif.ApertureValue ) == 'table', 'xmpDoc.exif.ApertureValue is table' )
	assert( xmpDoc.exif.ApertureValue[ 1 ] == 4, 'xmpDoc.exif.ApertureValue[ 1 ] is correct' )
	assert( xmpDoc.exif.ApertureValue[ 2 ] == 1, 'xmpDoc.exif.ApertureValue[ 2 ] is correct' )

	metadata:set( WFXMLNS.exif, 'ApertureValue', '0/2' )
	xmpDoc = WFCatImportMetadata.convertXmpInfo( metadata )
	assert( xmpDoc.exif.ApertureValue[ 1 ] == 0, 'xmpDoc.exif.ApertureValue[ 1 ] is correct' )
	assert( xmpDoc.exif.ApertureValue[ 2 ] == 1, 'xmpDoc.exif.ApertureValue[ 2 ] is correct' )

	metadata:set( WFXMLNS.exif, 'ApertureValue', 'not a number' )
	xmpDoc = WFCatImportMetadata.convertXmpInfo( metadata )
	assert( xmpDoc.exif == nil, 'xmpDoc.exif.ApertureValue[ 1 ] is correct' )

	metadata:set( WFXMLNS.exif, 'ApertureValue', '1/0' )
	xmpDoc = WFCatImportMetadata.convertXmpInfo( metadata )
	assert( xmpDoc.exif == nil, 'xmpDoc.exif.ApertureValue[ 1 ] is correct' )

	metadata:set( WFXMLNS.exif, 'ApertureValue', '-1/-2' )
	xmpDoc = WFCatImportMetadata.convertXmpInfo( metadata )
	assert( xmpDoc.exif.ApertureValue[ 1 ] == 1, 'xmpDoc.exif.ApertureValue[ 1 ] is correct' )
	assert( xmpDoc.exif.ApertureValue[ 2 ] == 2, 'xmpDoc.exif.ApertureValue[ 2 ] is correct' )

	metadata:set( WFXMLNS.exif, 'SubjectDistance', '4294967295/1000' )
	xmpDoc = WFCatImportMetadata.convertXmpInfo( metadata )
	assert( xmpDoc.exif.SubjectDistance == "infinite", 'SubjectDistance 4294967295/<any> is infinite' )

	metadata:set( WFXMLNS.exif, 'SubjectDistance', '123456789/1000' )
	xmpDoc = WFCatImportMetadata.convertXmpInfo( metadata )
	assert( xmpDoc.exif.SubjectDistance[ 1 ] == 123456789, 'SubjectDistance 123456789/1000 is numerator is 123456789' )
	assert( xmpDoc.exif.SubjectDistance[ 2 ] == 1000, 'SubjectDistance 123456789/1000 is denominator is 1000' )
	
	metadata:set( WFXMLNS.exif, 'SubjectDistance', '0/1000' )
	xmpDoc = WFCatImportMetadata.convertXmpInfo( metadata )
	assert( xmpDoc.exif.SubjectDistance == "unknown", 'SubjectDistance 0/<any> is unknown' )

end

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

function McatImportRequestsTest.testDateField()

	local WFXmp = assert( require 'WFXmp', "WFXmp namespace exists" )
	local WFCatImportMetadata = require 'WFCatImportMetadata'

	local metadata = WFXmp.WFXmp()
	
	-- Here are some sample XMP dates from the "all raw" data set
	-- (numers were regex'd for uniformity)
	--	"2345-12-28T01:23:45"
	--	"2345-12-28T01:23:45+06:00"
	--	"2345-12-28T01:23:45-06:00"
	--	"2345-12-28T01:23:45.67"
	--	"2345-12-28T01:23:45Z"

	metadata:set( WFXMLNS.exif, 'DateTimeOriginal', '2345-12-28T01:23:45+06:00' )

	local xmpDoc = WFCatImportMetadata.convertXmpInfo( metadata )
	
	assert( xmpDoc.exif.DateTimeOriginal == '2345-12-28T01:23:45+06:00', 'xmpDoc.exif.DateTimeOriginal' )

	metadata:set( WFXMLNS.exif, 'DateTimeOriginal', '' )

	xmpDoc = WFCatImportMetadata.convertXmpInfo( metadata )
	
	assert( xmpDoc.exif == nil, 'xmpDoc.exif.DateTimeOriginal' )

end

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

function McatImportRequestsTest.testBooleanField()

	local WFXmp = assert( require 'WFXmp', "WFXmp namespace exists" )
	local WFCatImportMetadata = require 'WFCatImportMetadata'

	local metadata = WFXmp.WFXmp()
	local xmpDoc = WFCatImportMetadata.convertXmpInfo( metadata )
	assert( xmpDoc.xmpRights == nil, 'xmpDoc.xmp.Marked' )

	metadata:set( WFXMLNS.xapRights, "Marked", true )
	xmpDoc = WFCatImportMetadata.convertXmpInfo( metadata )
	assert( xmpDoc.xmpRights.Marked == true, 'xmpDoc.xmp.Marked' )

	metadata:set( WFXMLNS.xapRights, "Marked", false )
	xmpDoc = WFCatImportMetadata.convertXmpInfo( metadata )
	assert( xmpDoc.xmpRights.Marked == false, 'xmpDoc.xmp.Marked' )

	metadata:set( WFXMLNS.xapRights, "Marked", "1" )
	xmpDoc = WFCatImportMetadata.convertXmpInfo( metadata )
	assert( xmpDoc.xmpRights.Marked == true, 'xmpDoc.xmp.Marked' )

	metadata:set( WFXMLNS.xapRights, "Marked", "0" )
	xmpDoc = WFCatImportMetadata.convertXmpInfo( metadata )
	assert( xmpDoc.xmpRights.Marked == false, 'xmpDoc.xmp.Marked' )

end

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

function McatImportRequestsTest.testEnumField()

	local WFXmp = assert( require 'WFXmp', "WFXmp namespace exists" )
	local WFCatImportMetadata = require 'WFCatImportMetadata'

	local metadata = WFXmp.WFXmp()
	metadata:set( WFXMLNS.exif, 'ExposureProgram', "1" )

	local xmpDoc = WFCatImportMetadata.convertXmpInfo( metadata )
	assert( xmpDoc.exif.ExposureProgram == "manual", 'xmpDoc.exif.ExposureProgram' )

end

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

function McatImportRequestsTest.testConcatenatedStringList()

	local WFXmp = assert( require 'WFXmp', "WFXmp namespace exists" )
	local WFCatImportMetadata = require 'WFCatImportMetadata'

	local metadata = WFXmp.WFXmp()
	metadata:setStringList( WFXMLNS.dc, 'creator', {"user1", "user2"} )
	local xmpDoc = WFCatImportMetadata.convertXmpInfo( metadata )
	assert( type( xmpDoc.dc.creator ) == 'table', 'xmpDoc.dc.creator is array' )
	assert( xmpDoc.dc.creator[ 1 ] == "user1", 'xmpDoc.dc.creator[ 1 ]' )
	assert( xmpDoc.dc.creator[ 2 ] == "user2", 'xmpDoc.dc.creator[ 2 ]' )

end

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

function McatImportRequestsTest.testFirstArrayElementAsInteger()

	local WFXmp = assert( require 'WFXmp', "WFXmp namespace exists" )
	local WFCatImportMetadata = require 'WFCatImportMetadata'

	local metadata = WFXmp.WFXmp()
	metadata:parse [[<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
  <rdf:Description rdf:about=""
    xmlns:exif="http://ns.adobe.com/exif/1.0/">
   <exif:ISOSpeedRatings>
    <rdf:Seq>
     <rdf:li>50</rdf:li>
    </rdf:Seq>
   </exif:ISOSpeedRatings>
  </rdf:Description>
 </rdf:RDF>
]]

	local xmpDoc = WFCatImportMetadata.convertXmpInfo( metadata )
	assert( xmpDoc.exif.ISOSpeedRatings == 50, 'xmpDoc.exif.ISOSpeedRatings' )

end

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

function McatImportRequestsTest.testFieldType()

	local WFXmp = assert( require 'WFXmp', "WFXmp namespace exists" )
	local WFCatImportMetadata = require 'WFCatImportMetadata'

	local metadata = WFXmp.WFXmp()
	local fieldPath = metadata:composeStructFieldPath( WFXMLNS.exif, 'Flash', WFXMLNS.exif, 'Fired' )
	metadata:set( WFXMLNS.exif, fieldPath, true )
	
	local xmpDoc = WFCatImportMetadata.convertXmpInfo( metadata )
	assert( type( xmpDoc.exif ) == 'table', 'xmpDoc.exif should be present ' )
	assert( xmpDoc.exif.FlashFired == true, 'xmpDoc.exif.FlashFired should be true' )

end

--==============================================================================

return McatImportRequestsTest
