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

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.

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

	-- RxLua
local Rx = require 'Rx'

	-- WF.core.utils
local WFCloseSupport = assert( require "WFCloseSupport" )

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

	-- WF.core.rxlua
local WFRxShareReplayWithCloseSupport = require 'WFRxShareReplayWithCloseSupport'

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

-- @mark WFRxShareReplayWithCloseSupportTest namespace

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

local WFRxShareReplayWithCloseSupportTest = {}

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

local shareReplayWithCloseSupport = WFRxShareReplayWithCloseSupport

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

local classMethods = {
		
	WFCloseSupport.addCloseSupportMethods
}

local ObjectClass = Rx.Class( 'TestClass', classMethods )
	
--------------------------------------------------------------------------------

function WFRxShareReplayWithCloseSupportTest.rxTestCloseSupport( rxTest )

	rxTest:setTimeLimit( 5 )
	
	local obj = ObjectClass()
	local obj2 = ObjectClass()
	
	local subject = Rx.Subject()
	
	local observable = subject:call( shareReplayWithCloseSupport )
	
	rxTest:expectEqual( false, WFCloseSupport.hasBeenOrWillBeClosed( obj ) )
	
	local obs = rxTest:observe( 
		observable
		:map( function( o )
			local isClosed = WFCloseSupport.hasBeenOrWillBeClosed( o )
			return isClosed
		end )
	)
	
	subject:onNext( obj )
	
	obs:expectDataWithinTime( 1, false )
	
	subject:onNext( obj2 )
	
	rxTest:expectEqual( true, WFCloseSupport.hasBeenOrWillBeClosed( obj ) )
	
end

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

function WFRxShareReplayWithCloseSupportTest.rxTestCompareObjects( rxTest )

	local obj = ObjectClass()
	
	local subject = Rx.Subject()
	
	local observable = subject:call( shareReplayWithCloseSupport )
	
	rxTest:expectEqual( false, WFCloseSupport.hasBeenOrWillBeClosed( obj ) )
	
	local obs = rxTest:observe( 
		observable
		:map( function( o )
			local isClosed = WFCloseSupport.hasBeenOrWillBeClosed( o )
			return isClosed
		end )
	)
	
	subject:onNext( obj )
	
	obs:expectDataWithinTime( 1, false )
	
	subject:onNext( obj )
	
	rxTest:expectEqual( false, WFCloseSupport.hasBeenOrWillBeClosed( obj ) )
	
end

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

function WFRxShareReplayWithCloseSupportTest.rxTestInvalidArguments( rxTest )

	rxTest:expectError( function()
	
		local obj = ObjectClass()
		
		Rx.Observable.just( obj )
			:call( shareReplayWithCloseSupport, 2 )
	
	end )
	
end

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

function WFRxShareReplayWithCloseSupportTest.rxTestReplayNil( rxTest )

	local obj = ObjectClass()
	
	local subject = Rx.Subject()
	
	local observable = subject:call( shareReplayWithCloseSupport )
	
	rxTest:expectEqual( false, WFCloseSupport.hasBeenOrWillBeClosed( obj ) )
	
	local obs = rxTest:observe( 
		observable
		:map( function( o )
			local isClosed = WFCloseSupport.hasBeenOrWillBeClosed( o )
			return isClosed
		end )
	)
	
	subject:onNext( nil )
	
	obs:expectDataWithinTime( 1, true )
	
	subject:onNext( obj )
	
	rxTest:expectEqual( false, WFCloseSupport.hasBeenOrWillBeClosed( obj ) )
	
end

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

function WFRxShareReplayWithCloseSupportTest.rxTestReplayWithClosedObject( rxTest )

	local obj = ObjectClass()
	local obj2 = ObjectClass()
	
	local subject = Rx.Subject()
	
	local observable = subject:call( shareReplayWithCloseSupport )
	
	rxTest:expectEqual( false, WFCloseSupport.hasBeenOrWillBeClosed( obj ) )
	
	local obs = rxTest:observe( 
		observable
		:map( function( o )
			local isClosed = WFCloseSupport.hasBeenOrWillBeClosed( o )
			return isClosed
		end )
	)
	
	subject:onNext( obj )
	
	obs:expectDataWithinTime( 1, false )
	
	obj:close()
	
	subject:onNext( obj2 )
	
	rxTest:expectEqual( true, WFCloseSupport.hasBeenOrWillBeClosed( obj ) )
	rxTest:expectEqual( false, WFCloseSupport.hasBeenOrWillBeClosed( obj2 ) )
	
end

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

function WFRxShareReplayWithCloseSupportTest.rxTestVarArgs( rxTest )

	rxTest:setTimeLimit( 5 )
	
	local subscription
	
	local obj = ObjectClass()
	local obj2 = ObjectClass()
	local obj3 = ObjectClass()
	
	rxTest:expectEqual( false, WFCloseSupport.hasBeenOrWillBeClosed( obj ) )
	
	rxTest:expectError( function()
	
		subscription = Rx.Observable.just( obj, obj2 )
		
			:call( shareReplayWithCloseSupport )
			
			:subscribe()
		
	end )
	
	rxTest:expectEqual( false, WFCloseSupport.hasBeenOrWillBeClosed( obj ) )
	rxTest:expectEqual( false, WFCloseSupport.hasBeenOrWillBeClosed( obj2 ) )
	rxTest:expectEqual( false, WFCloseSupport.hasBeenOrWillBeClosed( obj3 ) )
	
end

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

return WFRxTestCase.wrapSuite( WFRxShareReplayWithCloseSupportTest )
