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

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.self_test
local WFRxTestCase = require 'WFRxTestCase'

	-- WF.core.rxlua
local WFRxDisposeOnUnsubscribeObservable = require 'WFRxDisposeOnUnsubscribeObservable'
local WFRxScheduler = require 'WFRxScheduler'

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

local disposeOnUnsubscribe = WFRxDisposeOnUnsubscribeObservable

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

local WFRxDisposeOnUnsubscribeObservableTest = {}

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

function WFRxDisposeOnUnsubscribeObservableTest.rxTestRetrieveNotifications( rxTest )

	local unsubscribe = Rx.Observable.just( 42 )
		:call( disposeOnUnsubscribe )
		
	rxTest:expectSingleValue( unsubscribe, 42 )
	
	unsubscribe = Rx.Observable.empty()
		:call( disposeOnUnsubscribe )
		
	rxTest:expectOnlyCompleted( unsubscribe )
	
	unsubscribe = Rx.Observable.throw( 'bad' )
		:call( disposeOnUnsubscribe )
		
	rxTest:expectOnlyError( unsubscribe, ':ignore:' )
	
end

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

function WFRxDisposeOnUnsubscribeObservableTest.rxTestRetrieveDisposeOnDispose( rxTest )

	local gotDisposed = false
	
	local gotError = false
	local gotCompleted = false
	
	local unsubscribeObservable = Rx.Observable.timer( 10, WFRxScheduler.sharedScheduler() )
		:call( disposeOnUnsubscribe )
		
	unsubscribeObservable:addDisposable( Rx.Disposable.create( function()
		gotDisposed = true
	end ) )
	
	local subscription = unsubscribeObservable
		:tap(
			nil, 
			function()
				gotError = true
			end,
			function()
				gotCompleted = true
			end
		)
		:subscribe()
	
	rxTest:expectEqual( gotDisposed, false )
	
	subscription:dispose()
	
	rxTest:expectEqual( gotDisposed, true )
	rxTest:expectEqual( gotError, false )
	rxTest:expectEqual( gotCompleted, false )
	
end

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

function WFRxDisposeOnUnsubscribeObservableTest.rxTestDisposeOnError( rxTest )

	local gotDisposed = false
	
	local unsubscribeObservable = Rx.Observable.throw( 'bad' )
		:call( disposeOnUnsubscribe )
		
	unsubscribeObservable:addDisposable( Rx.Disposable.create( function()
		gotDisposed = true
	end ) )
	
	rxTest:expectEqual( gotDisposed, false )
	
	-- As opposed to the AutoDetachObserver, the WFRxTestCase.observer doesn't dispose
	-- its managed subscribtion on an error or complete message so we add an intermediate
	-- :map() that creates an AutoDetachObserver to do the job
	
	local obs = rxTest:observe( unsubscribeObservable:map(function(...) return ... end ) )
	
	obs:expectError( 'bad' )
	
	rxTest:expectEqual( gotDisposed, true )
	
end

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

function WFRxDisposeOnUnsubscribeObservableTest.rxTestDisposeOnCompleted( rxTest )

	local gotDisposed = false
	
	local unsubscribeObservable = Rx.Observable.empty()
		:call( disposeOnUnsubscribe )
		
	unsubscribeObservable:addDisposable( Rx.Disposable.create( function()
		gotDisposed = true
	end ) )
	
	rxTest:expectEqual( gotDisposed, false )
	
	-- As opposed to the AutoDetachObserver, the WFRxTestCase.observer doesn't dispose
	-- its managed subscribtion on an error or complete message so we add an intermediate
	-- :map() that creates an AutoDetachObserver to do the job
	
	local obs = rxTest:observe( unsubscribeObservable:map(function(...) return ... end ) )
	
	obs:expectCompleted()
	
	rxTest:expectEqual( gotDisposed, true )
	
end

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

function WFRxDisposeOnUnsubscribeObservableTest.rxTestRemoveDisposable( rxTest )

	local gotDisposed = false
	
	local unsubscribeObservable = Rx.Observable.empty()
		:call( disposeOnUnsubscribe )
		
	local disposable = Rx.Disposable.create( function()
		gotDisposed = true
	end )
	
	unsubscribeObservable:addDisposable( disposable )
	
	rxTest:expectEqual( gotDisposed, false )
	
	unsubscribeObservable:removeDisposable( disposable )
	
	rxTest:expectEqual( gotDisposed, true )
	
	local obs = rxTest:observe( unsubscribeObservable:map(function(...) return ... end ) )
	
	obs:expectCompleted()
	
end

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

function WFRxDisposeOnUnsubscribeObservableTest.rxTestMultipleSubscriptions( rxTest )

	local gotDisposed = 0
	
	local unsubscribeObservable = Rx.Observable.empty()
		:call( disposeOnUnsubscribe )
		
	unsubscribeObservable:addDisposable( Rx.Disposable.create( function()
		gotDisposed = gotDisposed + 1
	end ) )
	
	rxTest:expectEqual( gotDisposed, 0 )
	
	-- As opposed to the AutoDetachObserver, the WFRxTestCase.observer doesn't dispose
	-- its managed subscribtion on an error or complete message so we add an intermediate
	-- :map() that creates an AutoDetachObserver to do the job
	
	local obs = rxTest:observe( unsubscribeObservable:map(function(...) return ... end ) )
	obs:expectCompleted()
	
	obs = rxTest:observe( unsubscribeObservable:map(function(...) return ... end ) )
	obs:expectCompleted()
	
	rxTest:expectEqual( gotDisposed, 1 )
	
end

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

function WFRxDisposeOnUnsubscribeObservableTest.rxTestInvalidArgument( rxTest )

	local unsubscribeObservable = Rx.Observable.empty()
		:call( disposeOnUnsubscribe )
		
	rxTest:expectError( function()
		unsubscribeObservable:addDisposable( function() end )
	end )
	
end

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

return WFRxTestCase.wrapSuite( 'WFRxDisposeOnUnsubscribeObservableTest', WFRxDisposeOnUnsubscribeObservableTest )