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

WFProgressReporterTest.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.

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

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

	-- WF.core.util
local WFProgressReporter = require 'WFProgressReporter'


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

local WFProgressReporterTest = {}

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

function WFProgressReporterTest.rxTestProgressReporter( rxTest )

	local progressReporter = WFProgressReporter( "progress test" )
	
	local status = rxTest:observe( progressReporter:status() )

	progressReporter:reportSuccess()
	
	status:expectData(
		{
			processedCount = 1,
			description = "progress test",
			errorCount = 0,
			percentComplete = 1,
			totalCount = 1,
		} )
	
	progressReporter:setTotal( 5 )
	
	status:expectData(
		{
			processedCount = 1,
			description = "progress test",
			errorCount = 0,
			percentComplete = 0.2,
			totalCount = 5,
		} )
	
	progressReporter:reportSuccess()
	
	status:expectData(
		{
			processedCount = 2,
			description = "progress test",
			errorCount = 0,
			percentComplete = 0.4,
			totalCount = 5,
		} )
	
	progressReporter:reportError()
	
	status:expectData(
		{
			processedCount = 3,
			description = "progress test",
			errorCount = 1,
			percentComplete = 0.6,
			totalCount = 5,
		} )
	
	progressReporter:reportSuccess()
	progressReporter:reportSuccess()
	
	status:expectDataWithinTime( 1,
		{
			processedCount = 5,
			description = "progress test",
			errorCount = 1,
			percentComplete = 1,
			totalCount = 5,
		} )
	
	rxTest:expectEqual( false, progressReporter:isDone() )

	progressReporter:done()

	rxTest:expectEqual( true, progressReporter:isDone() )
	
	status:expectCompleted()

	progressReporter:reset()
		-- Validate reset allows you to re-use the
		-- progressReporter instance that has completed.

	status = nil

	rxTest:expectEqual( false, progressReporter:isDone() )

	local status2 = rxTest:observe( progressReporter:status() )
	
	progressReporter:setTotal( 1 )

	status2:expectData(
		{
			processedCount = 0,
			description = "progress test",
			errorCount = 0,
			percentComplete = 0,
			totalCount = 1,
		} )

	progressReporter:reportSuccess()

	status2:expectData(
		{
			processedCount = 1,
			description = "progress test",
			errorCount = 0,
			percentComplete = 1,
			totalCount = 1,
		} )

	progressReporter:done()

	status2:expectCompleted()

end

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

function WFProgressReporterTest.rxTestProgressReporter_ErrorInfo( rxTest )

	local progressReporter = WFProgressReporter( "progress test" )
	
	local status = rxTest:observe( progressReporter:status() )

	progressReporter:setTotal( 2 )
	
	status:expectData(
		{
			processedCount = 0,
			description = "progress test",
			errorCount = 0,
			percentComplete = 0,
			totalCount = 2,
		} )
	
	local errorInfo = {
		foo = 'bar',
		id = 1
	}
	
	progressReporter:reportError( errorInfo, true )
	
	status:expectData(
		{
			processedCount = 1,
			description = "progress test",
			errorCount = 1,
			extraInfo = {
				foo = "bar",
				id = 1,
			},
			percentComplete = 0.5,
			totalCount = 2,
		} )
	
	errorInfo = {
		foo = 'bar',
		id = 2
	}
	
	progressReporter:reportError( errorInfo, true )
	
	status:expectData(
		{
			processedCount = 2,
			description = "progress test",
			errorCount = 2,
			extraInfo = {
				foo = "bar",
				id = 2,
			},
			percentComplete = 1,
			totalCount = 2,
		} )
	
	rxTest:expectEqual( false, progressReporter:isDone() )

	progressReporter:done()

	rxTest:expectEqual( true, progressReporter:isDone() )
	
	status:expectCompleted()

end

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

function WFProgressReporterTest.rxTestProgressWithLateSubscriber( rxTest )

	rxTest:setTimeLimit( 5 )

	local progressReporter = WFProgressReporter( "progress test" )
	
	progressReporter:setTotal( 2 )
	
	progressReporter:reportSuccessMustDeliver()
	
	progressReporter:reportSuccessMustDeliver()
	
	progressReporter:done()
	
	rxTest:sleep( 1 )
	
	local status = rxTest:observe( progressReporter:status() )

	status:expectDataWithinTime( 1,
		{
			processedCount = 1,
			description = "progress test",
			errorCount = 0,
			percentComplete = 0.5,
			totalCount = 2,
		} )
	
	status:expectDataWithinTime( 1,
		{
			processedCount = 2,
			description = "progress test",
			errorCount = 0,
			percentComplete = 1,
			totalCount = 2,
		} )
	
	status:expectCompleted()

end

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

function WFProgressReporterTest.rxTestProgressWithLateSubscriberOrder( rxTest )

	rxTest:setTimeLimit( 5 )

	local progressReporter = WFProgressReporter( "progress test" )
	
	progressReporter:setTotalMustDeliver( 2 )

	progressReporter:reportSuccessMustDeliver()
	
	progressReporter:reportSuccessMustDeliver()
	
	progressReporter:done()
	
	rxTest:sleep( 1 )
	
	local status = rxTest:observe( progressReporter:status() )

	status:expectData(
		{
			processedCount = 0,
			description = "progress test",
			errorCount = 0,
			percentComplete = 0,
			totalCount = 2,
		} )

	status:expectData(
		{
			processedCount = 1,
			description = "progress test",
			errorCount = 0,
			percentComplete = 0.5,
			totalCount = 2,
		} )
	
	status:expectData(
		{
			processedCount = 2,
			description = "progress test",
			errorCount = 0,
			percentComplete = 1,
			totalCount = 2,
		} )
	
	status:expectCompleted()

end

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

function WFProgressReporterTest.rxTestCompletedWithLateSubscriber( rxTest )

	rxTest:setTimeLimit( 5 )

	-- Make sure late subscribers at least receive completed notification.

	local progressReporter = WFProgressReporter( "progress test" )
	
	progressReporter:done()
	
	rxTest:sleep( 1 )
	
	local status = rxTest:observe( progressReporter:status() )

	status:expectCompleted()

end

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

function WFProgressReporterTest.rxTestProgressReporterForBytesTransferred( rxTest )

	local progressReporter = WFProgressReporter( "bytes transferred test" )
	
	local status = rxTest:observe( progressReporter:status() )

	progressReporter:setTotal( 50 )
	
	status:expectData(
		{
			processedCount = 0,
			description = "bytes transferred test",
			errorCount = 0,
			percentComplete = 0,
			totalCount = 50,
		} )
	
	progressReporter:setProcessedCount( 10, true, { youAreSo = 'extra' } )
	
	status:expectData(
		{
			processedCount = 10,
			description = "bytes transferred test",
			errorCount = 0,
			percentComplete = 10/50,
			totalCount = 50,
			extraInfo = {
				youAreSo = 'extra'
			}
		} )

	progressReporter:reset()
		-- Validate you can reset the progressReporter when not done
		-- and not lose the subscription.

	progressReporter:setErrorCount( 1 )
	
	status:expectData(
		{
			processedCount = 0,
			description = "bytes transferred test",
			errorCount = 1,
			percentComplete = 0,
			totalCount = 0,
		} )

	progressReporter:setTotal( 50 )
	
	status:expectData(
		{
			processedCount = 0,
			description = "bytes transferred test",
			errorCount = 1,
			percentComplete = 0.0,
			totalCount = 50,
		} )
	
	progressReporter:setProcessedCount( 20 )

	status:expectData(
		{
			processedCount = 20,
			description = "bytes transferred test",
			errorCount = 1,
			percentComplete = 20/50,
			totalCount = 50,
		} )
	
	progressReporter:setProcessedCount( 50 )

	status:expectData(
		{
			processedCount = 50,
			description = "bytes transferred test",
			errorCount = 1,
			percentComplete = 1,
			totalCount = 50,
		} )
	
	progressReporter:done()
	
	status:expectCompleted()
	
end

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

function WFProgressReporterTest.rxTestProgressReporterForExtraInfo( rxTest )

	local progressReporter = WFProgressReporter( "progress test" )
	
	local status = rxTest:observe( progressReporter:status() )

	progressReporter:reportSuccess(
		{
			id = 123,
			info = 'success',
		} )
	
	status:expectData(
		{
			processedCount = 1,
			description = "progress test",
			errorCount = 0,
			percentComplete = 1,
			totalCount = 1,
			extraInfo =
				{
					id = 123,
					info = 'success',
				}
		} )
	
	progressReporter:setTotal( 5 )
	
	status:expectData(
		{
			processedCount = 1,
			description = "progress test",
			errorCount = 0,
			percentComplete = 0.2,
			totalCount = 5,
		} )
	
	progressReporter:reportSuccess()
	
	status:expectData(
		{
			processedCount = 2,
			description = "progress test",
			errorCount = 0,
			percentComplete = 0.4,
			totalCount = 5,
		} )
	
	progressReporter:reportError( 'Invalid request' )
	
	status:expectData(
		{
			processedCount = 3,
			description = "progress test",
			errorCount = 1,
			percentComplete = 0.6,
			totalCount = 5,
			extraInfo = 'Invalid request',
		} )
	
	progressReporter:reportSuccess()
	
	progressReporter:reportSuccess( "123" )
	
	status:expectDataWithinTime( 1,
		{
			processedCount = 5,
			description = "progress test",
			errorCount = 1,
			percentComplete = 1,
			totalCount = 5,
			extraInfo = "123",
		} )
	
	rxTest:expectEqual( false, progressReporter:isDone() )

	progressReporter:done()

	rxTest:expectEqual( true, progressReporter:isDone() )
	
	status:expectCompleted()

end

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

function WFProgressReporterTest.rxTestProgressReporterDoneWithExtraInfo( rxTest )

	local progressReporter = WFProgressReporter( "progress test" )
	
	local status = rxTest:observe( progressReporter:status() )

	progressReporter:reportSuccess(
		{
			id = 123,
			info = 'success',
		} )
	
	status:expectData(
		{
			processedCount = 1,
			description = "progress test",
			errorCount = 0,
			percentComplete = 1,
			totalCount = 1,
			extraInfo =
				{
					id = 123,
					info = 'success',
				}
		} )
	
	rxTest:expectEqual( false, progressReporter:isDone() )

	progressReporter:done( { foo = 'bar' } )

	status:expectData(
		{
			processedCount = 1,
			description = "progress test",
			errorCount = 0,
			percentComplete = 1,
			totalCount = 1,
			extraInfo =
				{
					foo = 'bar'
				}
		} )

	rxTest:expectEqual( true, progressReporter:isDone() )
	
	status:expectCompleted()

end

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

return WFRxTestCase.wrapSuite( WFProgressReporterTest )
