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

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

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

	-- RxLua
local Rx = require 'Rx'
local TestScheduler = Rx.testing.TestScheduler

	-- WF.core.reactive
local KSPromises = require 'KSPromises'

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

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


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

local asKSPromise = WFRxPromiseBridge

local onNext = Rx.testing.ReactiveTest.onNext
local onError = Rx.testing.ReactiveTest.onError
local onCompleted = Rx.testing.ReactiveTest.onCompleted
local subscribe = Rx.testing.ReactiveTest.subscribe


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

local WFRxPromiseBridgeTest = {}

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

function WFRxPromiseBridgeTest.testEmpty( test )

	local scheduler = TestScheduler()

	local xs = scheduler:createHotObservable(
		onCompleted( 390 )
	)

	WFAsyncTestCase( test )

		:waitFor( function()

			local result = xs:call( asKSPromise )
			scheduler:start()
			return result

		end )
		:continueWhenValueMatches( 'data', true )

		:waitFor( function()

			return KSPromises.resolvedPromiseP( 'data', xs._subscriptions )

		end )
		:continueWhenValueMatches( 'data', {
			subscribe( 0, 390 )
		} )

end

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

function WFRxPromiseBridgeTest.testOneValue( test )

	local scheduler = TestScheduler()

	local xs = scheduler:createHotObservable(
		onNext( 245, "hello" ),
		onCompleted( 390 )
	)

	WFAsyncTestCase( test )

		:waitFor( function()

			local result = xs:call( asKSPromise )
			scheduler:start()
			return result

		end )
		:continueWhenValueMatches( 'data', "hello" )

		:waitFor( function()

			return KSPromises.resolvedPromiseP( 'data', xs._subscriptions )

		end )
		:continueWhenValueMatches( 'data', {
			subscribe( 0, 245 )
		} )

end

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

function WFRxPromiseBridgeTest.testOneValueTuple( test )

	local scheduler = TestScheduler()

	local xs = scheduler:createHotObservable(
		onNext( 245, "hello", "and more", 42 ),
		onCompleted( 390 )
	)

	WFAsyncTestCase( test )

		:waitFor( function()

			local result = xs:call( asKSPromise )
			scheduler:start()
			return result

		end )
		:continueWhenValueMatches( 'data', "hello", "and more", 42 )

		:waitFor( function()

			return KSPromises.resolvedPromiseP( 'data', xs._subscriptions )

		end )
		:continueWhenValueMatches( 'data', {
			subscribe( 0, 245 )
		} )

end

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

function WFRxPromiseBridgeTest.testIgnoresMultipleValues( test )

	local scheduler = TestScheduler()

	local xs = scheduler:createHotObservable(
		onNext( 245, "hello" ),
		onNext( 252, "blah" ),
		onNext( 300, "and more" ),
		onNext( 344, "foo" ),
		onCompleted( 390 )
	)

	WFAsyncTestCase( test )

		:waitFor( function()

			local result = xs:call( asKSPromise )
			scheduler:start()
			return result

		end )
		:continueWhenValueMatches( 'data', "hello" )

		:waitFor( function()

			return KSPromises.resolvedPromiseP( 'data', xs._subscriptions )

		end )
		:continueWhenValueMatches( 'data', {
			subscribe( 0, 245 )
		} )

end

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

function WFRxPromiseBridgeTest.testIgnoresErrorAfterFirstValue( test )

	local scheduler = TestScheduler()

	local xs = scheduler:createHotObservable(
		onNext( 245, "hello" ),
		onNext( 252, "blah" ),
		onNext( 300, "and more" ),
		onError( 344, "foo" )
	)

	WFAsyncTestCase( test )

		:waitFor( function()

			local result = xs:call( asKSPromise )
			scheduler:start()
			return result

		end )
		:continueWhenValueMatches( 'data', "hello" )

		:waitFor( function()

			return KSPromises.resolvedPromiseP( 'data', xs._subscriptions )

		end )
		:continueWhenValueMatches( 'data', {
			subscribe( 0, 245 )
		} )

end

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

function WFRxPromiseBridgeTest.testOneErrorValue( test )

	local scheduler = TestScheduler()

	local xs = scheduler:createHotObservable(
		onError( 245, "totally failed, dude" )
	)

	WFAsyncTestCase( test )

		:waitFor( function()

			local result = xs:call( asKSPromise )
			scheduler:start()
			return result

		end )
		:continueWhenValueMatches( 'error', "totally failed, dude" )

		:waitFor( function()

			return KSPromises.resolvedPromiseP( 'data', xs._subscriptions )

		end )
		:continueWhenValueMatches( 'data', {
			subscribe( 0, 245 )
		} )

end

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

function WFRxPromiseBridgeTest.testErrorValueTuple( test )

	local scheduler = TestScheduler()

	local xs = scheduler:createHotObservable(
		onError( 245, "hello", "and more", 42 )
	)

	WFAsyncTestCase( test )

		:waitFor( function()

			local result = xs:call( asKSPromise )
			scheduler:start()
			return result

		end )
		:continueWhenValueMatches( 'error', "hello", "and more", 42 )

		:waitFor( function()

			return KSPromises.resolvedPromiseP( 'data', xs._subscriptions )

		end )
		:continueWhenValueMatches( 'data', {
			subscribe( 0, 245 )
		} )

end

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

return WFRxPromiseBridgeTest
