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

GridRangeUtilsTest.lua

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

ADOBE CONFIDENTIAL
------------------
Copyright 2016 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'

	-- wf.managed_catalog.pixel_provider
local GridRangeUtils = require "GridRangeUtils"

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

local function rxTestGetBufferedRange( rxTest )

--[[
local function getBufferedRange( bufferMultiplier, prevVisibleRange,
									visibleRange, imageCount )
--]]

	rxTest:expectError( GridRangeUtils.getBufferedRange )

	rxTest:expectError( GridRangeUtils.getBufferedRange, 1 )

	rxTest:expectError( GridRangeUtils.getBufferedRange, 0, {}, {}, 0 )

	rxTest:expectEqual( {}, GridRangeUtils.getBufferedRange( 
										1, {}, {}, 0 ) )

	rxTest:expectEqual( {}, 
						GridRangeUtils.getBufferedRange( 
							9, { 1, 10 }, {}, 9 ) )

	rxTest:expectEqual( {}, 
						GridRangeUtils.getBufferedRange( 
							9, { 1, 2 }, {}, 50 ) )

	rxTest:expectEqual( { 1, 9 }, 
						GridRangeUtils.getBufferedRange( 
							9, {}, { 1, 2 }, 9 ) )

	rxTest:expectEqual( { 1, 9 }, 
						GridRangeUtils.getBufferedRange( 
							9, {}, { 2, 2 }, 9 ) )

	rxTest:expectEqual( { 1, 18 }, 
						GridRangeUtils.getBufferedRange( 
							9, {}, { 1, 2 }, 5000 ) )

	rxTest:expectEqual( { 1, 18 }, 
						GridRangeUtils.getBufferedRange( 
							9, {}, { 3, 4 }, 5000 ) )

	rxTest:expectEqual( { 2, 19 }, 
						GridRangeUtils.getBufferedRange( 
							9, {}, { 10, 11 }, 50 ) )

	rxTest:expectEqual( { 2, 19 }, 
						GridRangeUtils.getBufferedRange( 
							9, { 1, 2 }, { 10, 11 }, 50 ) )

	rxTest:expectEqual( { 2, 19 }, 
						GridRangeUtils.getBufferedRange( 
							9, { 1, 3 }, { 10, 11 }, 50 ) )

	rxTest:expectEqual( { 1, 50 }, 
						GridRangeUtils.getBufferedRange( 
							9, { 1, 15 }, { 10, 11 }, 50 ) )

	rxTest:expectEqual( { 1, 27 }, 
						GridRangeUtils.getBufferedRange( 
							9, { 1, 4 }, { 10, 11 }, 50 ) )

	rxTest:expectEqual( { 33, 50 }, 
						GridRangeUtils.getBufferedRange( 
							9, {}, { 49, 50 }, 50 ) )

	rxTest:expectEqual( { 33, 50 }, 
						GridRangeUtils.getBufferedRange( 
							9, {}, { 46, 47 }, 50 ) )

end

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

local function rxTestIsInRange( rxTest )

	rxTest:expectEqual( false, GridRangeUtils.isInRange() )

	rxTest:expectEqual( false, GridRangeUtils.isInRange( 1 ) )

	rxTest:expectEqual( false, GridRangeUtils.isInRange( 1, {} ) )

	rxTest:expectEqual( true, GridRangeUtils.isInRange( 1, { 1, 1 } ) )

	rxTest:expectEqual( false, GridRangeUtils.isInRange( 1, { 2, 1 } ) )

	rxTest:expectEqual( false, GridRangeUtils.isInRange( 1, { 2, 4 } ) )

	rxTest:expectEqual( false, GridRangeUtils.isInRange( 3, { 1, 2 } ) )

	rxTest:expectEqual( true, GridRangeUtils.isInRange( 3, { 1, 1000 } ) )

end

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

local function rxTestComputeScrollDirection( rxTest )

	rxTest:expectEqual( 0, GridRangeUtils.computeScrollDirection() )

	rxTest:expectEqual( 0, GridRangeUtils.computeScrollDirection(
							{ 2, 1 } ) )

	rxTest:expectEqual( 0, GridRangeUtils.computeScrollDirection(
							{ 1, 1 }, { 1, 1 } ) )

	rxTest:expectEqual( 0, GridRangeUtils.computeScrollDirection(
							{ 1, 2 }, { 1, 2 } ) )

	rxTest:expectEqual( 1, GridRangeUtils.computeScrollDirection(
							{ 1, 2 }, { 1, 3 } ) )

	rxTest:expectEqual( 0, GridRangeUtils.computeScrollDirection(
							{ 1, 2 }, { 2, 2 } ) )

	rxTest:expectEqual( 1, GridRangeUtils.computeScrollDirection(
							{ 1, 2 }, { 2, 4 } ) )

	rxTest:expectEqual( 0, GridRangeUtils.computeScrollDirection(
							{ 3, 6 }, { 3, 5 } ) )

	rxTest:expectEqual( -1, GridRangeUtils.computeScrollDirection(
							{ 3, 6 }, { 2, 6 } ) )

	rxTest:expectEqual( -1, GridRangeUtils.computeScrollDirection(
							{ 3, 6 }, { 2, 5 } ) )

	rxTest:expectEqual( 0, GridRangeUtils.computeScrollDirection(
							{ 3, 4 }, { 1, 6 } ) )

	rxTest:expectEqual( 1, GridRangeUtils.computeScrollDirection(
							{ 3, 4 }, { 2, 6 } ) )

	rxTest:expectEqual( -1, GridRangeUtils.computeScrollDirection(
							{ 3, 4 }, { 1, 5 } ) )

end

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

local function rxTestGetRangeDeltas( rxTest )

	rxTest:expectEqual( { {}, {} }, 
						{ GridRangeUtils.getRangeDeltas() } )

	rxTest:expectEqual( { {}, { { 50, 60 }, } }, 
						{ GridRangeUtils.getRangeDeltas( { 50 , 60 } ) } )

	rxTest:expectEqual( { { { 50, 60 }, }, {} }, 
						{ GridRangeUtils.getRangeDeltas( {}, { 50 , 60 } ) } )

	rxTest:expectEqual( { { { 50, 60 }, }, { { 30, 40 } } }, 
						{ GridRangeUtils.getRangeDeltas( { 30, 40 }, 
															{ 50 , 60 } ) } )

	rxTest:expectEqual( { { { 30, 40 }, }, { { 50, 60 } } }, 
						{ GridRangeUtils.getRangeDeltas( { 50, 60 }, 
															{ 30 , 40 } ) } )

	rxTest:expectEqual( { { { 30, 40 }, }, { { 50, 60 } } }, 
						{ GridRangeUtils.getRangeDeltas( { 50, 60 }, 
															{ 30 , 40 } ) } )

	rxTest:expectEqual( { {}, {} }, 
						{ GridRangeUtils.getRangeDeltas( { 50, 60 }, 
															{ 50 , 60 } ) } )

	rxTest:expectEqual( { { { 30, 49 }, }, { { 51, 60 } } }, 
						{ GridRangeUtils.getRangeDeltas( { 50, 60 }, 
															{ 30 , 50 } ) } )

	rxTest:expectEqual( { { { 30, 49 }, }, { { 59, 60 } } }, 
						{ GridRangeUtils.getRangeDeltas( { 50, 60 }, 
															{ 30 , 58 } ) } )

	rxTest:expectEqual( { { { 61, 70 }, }, { { 50, 59 } } }, 
						{ GridRangeUtils.getRangeDeltas( { 50, 60 }, 
															{ 60 , 70 } ) } )

	rxTest:expectEqual( { { { 61, 70 }, }, { { 50, 55 } } }, 
						{ GridRangeUtils.getRangeDeltas( { 50, 60 }, 
															{ 56 , 70 } ) } )

	rxTest:expectEqual( { {}, { { 50, 55 }, { 59, 60 } } }, 
						{ GridRangeUtils.getRangeDeltas( { 50, 60 }, 
															{ 56 , 58 } ) } )

	rxTest:expectEqual( { { { 45, 49 }, { 61, 65 } }, {} }, 
						{ GridRangeUtils.getRangeDeltas( { 50, 60 }, 
															{ 45 , 65 } ) } )

end

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

local function rxTestGetFetchAheadAndBehindRanges( rxTest )

	rxTest:expectEqual( { {}, {} }, 
						{ GridRangeUtils.getFetchAheadAndBehindRanges() } )

	rxTest:expectEqual( { {}, {} }, 
						{ GridRangeUtils.getFetchAheadAndBehindRanges(
							{ 1, 10 } ) } )

	rxTest:expectEqual( { {}, {} }, 
						{ GridRangeUtils.getFetchAheadAndBehindRanges(
							{}, { 1, 10 } ) } )

	rxTest:expectEqual( { {}, {} }, 
						{ GridRangeUtils.getFetchAheadAndBehindRanges(
							{ 1, 10 }, { 1, 10 } ) } )

	rxTest:expectEqual( { {}, { 1, 1 } }, 
						{ GridRangeUtils.getFetchAheadAndBehindRanges(
							{ 2, 10 }, { 1, 10 } ) } )

	rxTest:expectEqual( { { 10, 10 }, {} }, 
						{ GridRangeUtils.getFetchAheadAndBehindRanges(
							{ 1, 9 }, { 1, 10 } ) } )

	rxTest:expectEqual( { { 7, 10 }, { 1, 3 } }, 
						{ GridRangeUtils.getFetchAheadAndBehindRanges(
							{ 4, 6 }, { 1, 10 } ) } )

end

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

return WFRxTestCase.wrapSuite( 'GridRangeUtilsTest', {

	rxTestGetBufferedRange = rxTestGetBufferedRange,

	rxTestIsInRange = rxTestIsInRange,

	rxTestComputeScrollDirection = rxTestComputeScrollDirection,

	rxTestGetRangeDeltas = rxTestGetRangeDeltas,

	rxTestGetFetchAheadAndBehindRanges = rxTestGetFetchAheadAndBehindRanges,

} )
