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

WFOzAssetInfoTest.lua

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

ADOBE CONFIDENTIAL
------------------
Copyright 2013 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.

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

local WFOzAssetInfo = require 'WFOzAssetInfo'
local WFOzContentTypes = require 'WFOzContentTypes'

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

local WFOzAssetInfoTest = {}

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

WFOzAssetInfoTest.test_makeAssetInfo = function( test )
	
	local makeAssetInfo = WFOzAssetInfo.makeAssetInfo
	assert( makeAssetInfo )
	assert( pcall( makeAssetInfo ) == false )
	local info = makeAssetInfo( "7c3ca81508d841b28f57ed83893c6ade", 3, "889de37475010a39510c419e5657b6a6", true, 1234 )
	assert( info.id == "7c3ca81508d841b28f57ed83893c6ade" )
	assert( info.aspectRatio == 3 )
	assert( info.thumbnailMD5 == "889de37475010a39510c419e5657b6a6" )
	assert( info.isVideo == true )
	assert( info.duration == 1234 )
	
end

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

local makeDb = function( testData )
	local WFSQLiteDatabaseSchema = require 'WFSQLiteDatabaseSchema'
	local WFSQLiteDatabase = require 'WFSQLiteDatabase'
	
	local schema = WFSQLiteDatabaseSchema( '1.0' )
	
	schema:addTable [[CREATE TABLE myInfo( id, aspectRatio, thumbnailMd5, ignoredFavoriteCount, captureDate, isVideo, duration )]]
	local db = WFSQLiteDatabase( ":memory:", schema )
	
	for i, row in ipairs( testData ) do
		db:exec( "INSERT INTO myInfo VALUES ( ?, ?, ?, ?, ?, ?, ? )", unpack( row ) )
	end
	
	return db
end

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

local function isVideoN( ctn )
	local ct = WFOzContentTypes.contentTypeForContentTypeNumber( ctn )
	return WFOzContentTypes.isVideo( ct )
end

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

WFOzAssetInfoTest.test_makeAssetInfoArray = function( test )
	local KSIdGenerator = require 'KSIdGenerator'
	local WFOzContentTypes = require 'WFOzContentTypes'
	
	local id = KSIdGenerator.uuidInLowercaseHex
	local testData = { }
	
	local tn = WFOzContentTypes.getContentTypeNumberForContentType
	
	local date, ctn, dur
	for i = 1, 200 do
		date = string.format( '2%03d-08-20T%02d:%02d:00', i, i%24, 2 * ( i%30 ) )
		ctn = i == 10 and tn( "video/*" ) or tn( "image/*" )
		dur = i < 20 and i * 3 or nil
		testData[ i ] = { id(), i/64, id(), 0, date, ctn, dur }
	end
	
	local db = makeDb( testData )
	
	local encoded = db:encodeResults( [[SELECT * FROM myInfo]], WFOzAssetInfo.encodeSql )
	local nativeArray = encoded.photos
	assert( encoded.count == 200, "Count wrong" )
	
	local numDateMismatch = 0
	local lastDate = "starting"
	
	for i = 1, 200 do
		local assetInfo = nativeArray:getAsset( i )
		local expected = testData[ i ]
		assert( assetInfo.id == expected[ 1 ], "Id mismatch" )
		assert( assetInfo.aspectRatio == expected[ 2 ], "aspectRatio mismatch" )
		assert( assetInfo.thumbnailMD5 == expected[ 3 ], "thumbnailMD5 mismatch" )
		assert( assetInfo.isVideo == isVideoN( expected[ 6 ] ), "isVideo mismatch" )
		assert( assetInfo.duration == expected[ 7 ], "duration mismatch" )
		date = nativeArray:approxDateForAssetAtIndex( i )
		if date == expected[ 5 ] then
			-- dates match, reset fudge factors
			lastDate = date
			numDateMismatch = 0
		else
			numDateMismatch = numDateMismatch + 1
			if numDateMismatch > 3 then
				assert( false, "Too many approximate asset dates fail to match" )
			end
			if lastDate ~= 'starting' then
				assert( date >= lastDate, "Date is not in ascending sequence" )
			end
		end
	end
end

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

WFOzAssetInfoTest.test_makeArray = function( test )
	
	local makeArray = WFOzAssetInfo.makeArray
	assert( makeArray )
	assert( pcall( makeArray ) == false )
	local info = makeArray( 20 )
	assert( type( info ) == 'table' )
			
end

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

return WFOzAssetInfoTest
