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

WFCatRenderSizeTest.lua

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

ADOBE CONFIDENTIAL
------------------
Copyright 2015 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
local WFRxTestCase = require 'WFRxTestCase'

local WFCatRenderSize = require 'WFCatRenderSize'

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

local function rxTestGetRenderSizeBadInputs( rxTest )

	local f = WFCatRenderSize.private_UnitTestExports.makeBoundedRenderSizeFunction( { 1, 2, 3 } )

	rxTest:expectError( f )

	rxTest:expectError( f, "full" )

	rxTest:expectError( f, "full", "full" )

end

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

local function makeDocValueWithCroppedWidthHeightPair( w, h )

	return { develop = { croppedWidth = w, croppedHeight = h } }

end

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

local function rxTestGetMaxCroppedSide( rxTest )

	local getMaxCroppedSide = WFCatRenderSize.getMaxCroppedSide

	rxTest:expectEqual( 1024, getMaxCroppedSide( "bogus" ) )

	rxTest:expectEqual( 1024, getMaxCroppedSide( {} ) )

	rxTest:expectEqual( 1024, 
						getMaxCroppedSide( 
							makeDocValueWithCroppedWidthHeightPair() ) )

	rxTest:expectEqual( 1234, 
						getMaxCroppedSide( 
							makeDocValueWithCroppedWidthHeightPair( 
								1234 ) ) )

	rxTest:expectEqual( 1234, 
						getMaxCroppedSide( 
							makeDocValueWithCroppedWidthHeightPair( 
								nil, 1234 ) ) )

	rxTest:expectEqual( 1234, 
						getMaxCroppedSide( 
							makeDocValueWithCroppedWidthHeightPair( 
								1231, 1234 ) ) )

	rxTest:expectEqual( 1234, 
						getMaxCroppedSide( 
							makeDocValueWithCroppedWidthHeightPair( 
								1234, 1231 ) ) )

end

--------------------------------------------------------------------------------
-- from PiL
local function permutationGenerator( a, n )

	n = n or #a

	if n <= 1 then

		coroutine.yield( a )

	else

		for i = 1, n do

			-- put i-th element as the last one
			a[n], a[i] = a[i], a[n]

			-- generate all permutations of the other elements
			permutationGenerator( a, n - 1 )

			-- restore i-th element
			a[n], a[i] = a[i], a[n]

		end

	end

end

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

local function permutations( a )

	local n = #a

	return coroutine.wrap(function () permutationGenerator( a, n ) end )

end

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

local function rxTestGetRenderSizeValidInputs( rxTest )

	local renditionSizes = { 10, 20, 30 }

	local fullSize = renditionSizes[ #renditionSizes ] + 10

	for rendSizes in permutations( renditionSizes ) do

		local getRenditionSize = 
				WFCatRenderSize.private_UnitTestExports.makeBoundedRenderSizeFunction( 
					rendSizes )

		for i = 1, #renditionSizes do

			local smallSize = ( i - 1 ) * 10 + 1

			local largeSize = 10 * i

			local expectedSize = 10 * i

			local nextLevelSize = i < #renditionSizes and 10 * ( i + 1 ) or "full"

			rxTest:expectEqual( expectedSize,
								getRenditionSize( fullSize, smallSize ) )

			rxTest:expectEqual( expectedSize,
								getRenditionSize( fullSize, largeSize ) )

			rxTest:expectEqual( "full",
								getRenditionSize( largeSize, smallSize ) )

			rxTest:expectEqual( largeSize,
								getRenditionSize( largeSize + 1, largeSize ) )

			rxTest:expectEqual( "full",
								getRenditionSize( largeSize, largeSize ) )

			rxTest:expectEqual( "full", 
								getRenditionSize( largeSize, largeSize + 1 ) )

			rxTest:expectEqual( largeSize, 
								getRenditionSize( fullSize, smallSize ) )

			rxTest:expectEqual( largeSize, 
								getRenditionSize( fullSize, largeSize ) )

			rxTest:expectEqual( nextLevelSize,
								getRenditionSize( fullSize, largeSize + 1 ) )

		end


	end


end

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

local function rxTestGetRenderSizeValuesByAssetType( rxTest )

	local defaultRenditionSizes = { 320, 640, 1280, 2048 }

	local getRenditionSize = 
			WFCatRenderSize.private_UnitTestExports.makeBoundedRenderSizeFunction( 
				defaultRenditionSizes )
		
	-- images
	local assetType = 'image'
	for i = 1, #defaultRenditionSizes do
	
		local maxLongSide = defaultRenditionSizes[ i ]
		
		for _, requestSize in ipairs( defaultRenditionSizes ) do
		
			local expectedSize = requestSize < maxLongSide and requestSize or 'full'
			
			rxTest:expectEqual( expectedSize,
				getRenditionSize( maxLongSide, requestSize, assetType ) )
		end

	end
	
	-- videos
	assetType = 'video'
	for i = 1, #defaultRenditionSizes do
	
		local maxLongSide = defaultRenditionSizes[ i ]
		
		for _, requestSize in ipairs( defaultRenditionSizes ) do
		
			local expectedSize = requestSize < maxLongSide and requestSize or 2048
			
			rxTest:expectEqual( expectedSize,
				getRenditionSize( maxLongSide, requestSize, assetType ) )
		end

				
	end
		
end

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

local function rxTestCanRenderWithProxy( rxTest )

	local canRenderWithProxy = WFCatRenderSize.canRenderWithProxy 

	rxTest:expectError( canRenderWithProxy, nil )

	rxTest:expectError( canRenderWithProxy, "bogus" )

	rxTest:expectError( canRenderWithProxy, {} )

	rxTest:expectEqual( true, canRenderWithProxy( "full" ) )

	rxTest:expectEqual( true, canRenderWithProxy( 1280 ) )

	local max = 2560

	rxTest:expectEqual( true, canRenderWithProxy( max ) )

	rxTest:expectEqual( false, canRenderWithProxy( max + 1 ) )

end

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

local function rxTestGetNormalizedRenderSize( rxTest )

	rxTest:expectEqual( WFCatRenderSize.getNormalizedRenderSize( -1 ),
							WFCatRenderSize.defaultRenderSizes[ 1 ] )

	rxTest:expectEqual( WFCatRenderSize.getNormalizedRenderSize( 0 ),
							WFCatRenderSize.defaultRenderSizes[ 1 ] )

	local totalRenderSizes = #WFCatRenderSize.defaultRenderSizes

	for i, normalizedRenderSize in ipairs( WFCatRenderSize.defaultRenderSizes ) do

		local nextBiggestNormalizedSize =
			WFCatRenderSize.defaultRenderSizes[ math.min( i + 1, totalRenderSizes ) ]

		rxTest:expectEqual( 
			WFCatRenderSize.getNormalizedRenderSize( normalizedRenderSize - 1 ),
			WFCatRenderSize.defaultRenderSizes[ i ] )

		rxTest:expectEqual( 
			WFCatRenderSize.getNormalizedRenderSize( normalizedRenderSize ),
			WFCatRenderSize.defaultRenderSizes[ i ] )

		rxTest:expectEqual( 
			WFCatRenderSize.getNormalizedRenderSize( normalizedRenderSize + 1 ),
			nextBiggestNormalizedSize )

	end

end

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

return WFRxTestCase.wrapSuite( 'WFCatRenderSizeTest', {

	rxTestGetRenderSizeBadInputs = rxTestGetRenderSizeBadInputs,

	rxTestGetMaxCroppedSide = rxTestGetMaxCroppedSide,

	rxTestGetRenderSizeValidInputs = rxTestGetRenderSizeValidInputs,
	
	rxTestGetRenderSizeValuesByAssetType = rxTestGetRenderSizeValuesByAssetType,

	rxTestCanRenderWithProxy = rxTestCanRenderWithProxy,

	rxTestGetNormalizedRenderSize = rxTestGetNormalizedRenderSize,

} )
