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

WFRxNativeObservableTest.lua

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

ADOBE CONFIDENTIAL
------------------
Copyright 2015 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.rxlua
local WFRxNativeObservable = require 'WFRxNativeObservable'
local WFRxNativeObservableExamples = require 'WFRxNativeObservableExamples'

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


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

local WFRxNativeObservableTest = {}

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

function WFRxNativeObservableTest.tearDown()

	WFRxNativeObservableExamples.clearCancelSpy()

end

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

local function nativeObsExample( name, ... )

	local nativeFunc = WFRxNativeObservableExamples[ name ]

	return WFRxNativeObservable( nativeFunc, ... )

end

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

function WFRxNativeObservableTest.rxTestImmediateEmpty( rxTest )

	rxTest:expectOnlyCompleted( nativeObsExample 'immediateEmpty' )

end

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

function WFRxNativeObservableTest.rxTestImmediateValueViaCargo( rxTest )

	rxTest:expectSingleValue( nativeObsExample 'immediateValueViaCargo',
							  "testing, testing, testing ..." )

end

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

function WFRxNativeObservableTest.rxTestImmediateDataViaCargo( rxTest )

	local KSData = require 'KSData'

	rxTest:expectSingleValue(
		nativeObsExample 'immediateDataViaCargo'
			:map( function( data )
				return type( data ), KSData.length( data ), tostring( data )
			end ),
		'userdata', 9, "some data" )

end

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

function WFRxNativeObservableTest.rxTestImmediateSequenceViaCargo( rxTest )

	local o = rxTest:observe( nativeObsExample 'immediateSequenceViaCargo' )

	o:expectData( "testing, testing, testing ..." )
	o:expectData( 42 )
	o:expectData( 43 )
	o:expectData( 44 )
	o:expectCompleted()

end

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

function WFRxNativeObservableTest.rxTestImmediateSequenceViaShortcuts( rxTest )

	local o = rxTest:observe( nativeObsExample 'immediateSequenceViaShortcuts' )

	o:expectData( "testing, testing, testing ..." )
	o:expectData( 42 )
	o:expectData( true )
	o:expectData( false )
	o:expectCompleted()

end

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

function WFRxNativeObservableTest.rxTestSequenceFromAnotherThread( rxTest )

	local o = rxTest:observe( nativeObsExample 'sequenceFromAnotherThread' )

	o:expectData( "from another thread" )
	o:expectData( 53 )
	o:expectData( true )
	o:expectData( false )
	o:expectCompleted()

end

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

function WFRxNativeObservableTest.rxTestCancel( rxTest )

	local didCancelSpy = rxTest:observe( nativeObsExample 'cancelSpy' )

	local o = rxTest:observe( nativeObsExample 'observableToCancel' )

	didCancelSpy:expectData( false )

	o:expectData( "from another thread" )
	o:dispose()

	didCancelSpy:expectData( true )
	didCancelSpy:expectCompleted()

	rxTest:expectEqual( true, WFRxNativeObservableExamples.lastCancelObservableIsDone() )

end

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

function WFRxNativeObservableTest.rxTestCancelHandlerCleanup( rxTest )

	local didCancelSpy = rxTest:observe( nativeObsExample 'cancelSpy' )

	local o = rxTest:observe( nativeObsExample 'observableToNotCancel' )

	didCancelSpy:expectData( false )

	o:expectData( "from another thread" )
	o:expectData( 53 )
	o:expectData( true )
	o:expectData( false )
	o:expectCompleted()

	didCancelSpy:expectData( false )
	didCancelSpy:expectCompleted()

	rxTest:expectEqual( true, WFRxNativeObservableExamples.lastCancelObservableIsDone() )

end

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

function WFRxNativeObservableTest.rxTestImmediateErrorViaCargo( rxTest )

	rxTest:expectOnlyError( nativeObsExample 'immediateErrorViaCargo',
							 "testing, testing, testing ..." )

end

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

function WFRxNativeObservableTest.rxTestImmediateErrorTupleValue( rxTest )

	rxTest:expectOnlyError( nativeObsExample 'immediateErrorValue',
							 "testing, testing, testing ..." )

end

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

function WFRxNativeObservableTest.rxTestImmediateSequenceWithError( rxTest )

	local o = rxTest:observe( nativeObsExample 'immediateSequenceWithError' )

	o:expectData( "testing, testing, testing ..." )
	o:expectData( 42 )
	o:expectData( 43 )
	o:expectData( 44 )
	o:expectError( "expected error" )

end

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

return WFRxTestCase.wrapSuite( 'WFRxNativeObservableTest', WFRxNativeObservableTest )
