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

WFCatTaskTest.lua

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

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

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

	-- RxLua
local Rx = require 'Rx'

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

local WFRxScheduler = require 'WFRxScheduler'

local WFCatMockCatalog = require 'WFCatMockCatalog'

local WFCatTaskManager = require 'WFCatTaskManager'
local WFCatTask = require 'WFCatTask'

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

local WFCatTaskTest = {}

local kDefaultDescription = "WFCatTask"

local createFakeWFCatalog = WFCatMockCatalog.createFakeWFCatalog

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

function WFCatTaskTest.rxTestCreateTask( rxTest )
	
	local timeOut = 5
	
	rxTest:setTimeLimit( timeOut )
	
	local function actionFn( args, progressReporter, cancelObservable )
	
		progressReporter:setTotal( 2 )
		
		local rxTest = args.rxTest
		
		return rxTest:observe( 
		
			Rx.Observable.timer( 0.5, 0.5, WFRxScheduler.sharedScheduler() )
			
				:map( function()
					progressReporter:reportSuccess()
				end )
				
				:take( 2 )
				
				:finally( function()
					progressReporter:done()
				end )
			)
			
	end
	
	local fakeWFCatalog = createFakeWFCatalog( rxTest )
		
	local manager = WFCatTaskManager.getCatTaskManager( fakeWFCatalog )
	
	-- 1) creat a task and add to task queue
	local task = WFCatTask( {
		args = { 
			rxTest = rxTest 
		},
		
		action = actionFn, 
	} )
	
	manager:addToQueue( task )
	
	local taskObservable = task:taskObservable()
	
	local taskObs = rxTest:observe( taskObservable
		:map( function( data )
			return data
		end )
	)

	-- 2) observe the result data
	taskObs:expectDataWithinTime( timeOut, {
		description = kDefaultDescription,
		errorCount = 0,
		percentComplete = 0,
		processedCount = 0,
		totalCount = 2,
	})
	
	taskObs:expectDataWithinTime( timeOut, {
		description = kDefaultDescription,
		errorCount = 0,
		percentComplete = 0.5,
		processedCount = 1,
		totalCount = 2,
	})
	
	taskObs:expectDataWithinTime( timeOut, {
		description = kDefaultDescription,
		errorCount = 0,
		percentComplete = 1,
		processedCount = 2,
		totalCount = 2,
	})
	
	taskObs:expectCompleted()
	
end

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

function WFCatTaskTest.rxTestCancelTask( rxTest )

	local timeOut = 5
	
	rxTest:setTimeLimit( timeOut )
	
	local function actionFn( args, progressReporter, cancelObservable )
		
		progressReporter:setTotal( 10 )
		
		local rxTest = args.rxTest
		
		return rxTest:observe( 
		
			Rx.Observable.timer( 0.5, 1, WFRxScheduler.sharedScheduler() )
			
				:map( function()
					progressReporter:reportSuccess()
				end )
				
				:takeUntil( cancelObservable )
				
				:finally( function()
					progressReporter:done()
				end )
			)
			
	end
	
	
	local fakeWFCatalog = createFakeWFCatalog( rxTest )
		
	local manager = WFCatTaskManager.getCatTaskManager( fakeWFCatalog )
	
	-- 1) creat a task and add to task queue
	local task = WFCatTask( {
		args = { 
			rxTest = rxTest 
		},
		
		action = actionFn, 
	} )
	
	manager:addToQueue( task )
	
	local taskObservable = task:taskObservable()
	
	local taskObs = rxTest:observe( taskObservable
		:map( function( data )
			return data
		end )
	)

	-- 2) add a client subscription and send cancel after 1 second
	local clientSubscription = taskObservable:subscribe( 
		function(...)
		end,
		function(...)
		end,
		function()
		end )
		
	rxTest:observe( Rx.Observable.timer( 1, WFRxScheduler.sharedScheduler() )
		:map( function()
			clientSubscription:dispose()
		end )
	)

	-- 3) observe the result data
	taskObs:expectDataWithinTime( timeOut, {
		description = kDefaultDescription,
		errorCount = 0,
		percentComplete = 0,
		processedCount = 0,
		totalCount = 10,
	})
	
	taskObs:expectDataWithinTime( timeOut, {
		description = kDefaultDescription,
		errorCount = 0,
		percentComplete = 0.1,
		processedCount = 1,
		totalCount = 10,
	})
	
	taskObs:expectCompleted()

end

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

return WFRxTestCase.wrapSuite( WFCatTaskTest )


