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

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

	-- WF.core.reactive
local Rx = require "Rx"

	-- WF.core.utils
local KSIdGenerator = require 'KSIdGenerator'

	-- WF.oz_client.sync_agent
local WFOzSyncConstants = require 'WFOzSyncConstants'

	-- WF.lrmobile
local LrmOzBinaryDownloaderUtils = require 'LrmOzBinaryDownloaderUtils'

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

local LrmOzBinaryDownloaderUtilsTest = {}

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

function LrmOzBinaryDownloaderUtilsTest.rxTestBinaryDownloadManagement( rxTest )
	
	local minimumFreeSpace = 600 * (1024 * 1024)
	
	local userId = KSIdGenerator.uuidInLowercaseHex()
	
	local isReachable = Rx.ReplaySubject( 1 )
	isReachable:onNext( 'wwan' )

	local freeSpace = Rx.ReplaySubject( 1 )
	freeSpace:onNext( minimumFreeSpace + 1 )

	local forceUpgrade = Rx.ReplaySubject( 1 )
	forceUpgrade:onNext( false )

	local isWiFiOnlySync = Rx.ReplaySubject( 1 )
	isWiFiOnlySync:onNext( false )

	local isSyncEnabled = Rx.ReplaySubject( 1 )
	isSyncEnabled:onNext( true )

	local isInLoupe = Rx.ReplaySubject( 1 )
	isInLoupe:onNext( false )

	local isAuthenticatedUser = Rx.ReplaySubject( 1 )
	isAuthenticatedUser:onNext( userId ~= WFOzSyncConstants.kGlobalDocumentId )

	local binaryDownloadStrategy = Rx.ReplaySubject( 1 )
	binaryDownloadStrategy:onNext( 'preferMaster' )

	rxTest:makeVerbose()

	local enableBinaryDownloader = Rx.Observable.combineLatest( 
				LrmOzBinaryDownloaderUtils.makeObserverBinaryDownloaderFn( {} ),
				isReachable, freeSpace, isWiFiOnlySync, isSyncEnabled, forceUpgrade, isInLoupe, isAuthenticatedUser,
				binaryDownloadStrategy )
		:map( function( download )
			return download
		end )
	
	local obs = rxTest:observe( enableBinaryDownloader )
		
	obs:expectData( true )
	
	-- Reachability
	
	isReachable:onNext( 'unreachable' )
	obs:expectData( false )
	
	isReachable:onNext( 'wifi' )
	obs:expectData( true )

	isReachable:onNext( 'wwan' )
	obs:expectData( true )
	
	-- WiFi only
	
	isWiFiOnlySync:onNext( true )
	obs:expectData( false )

	isReachable:onNext( 'wifi' )
	obs:expectData( true )
		
	-- free space
	
	freeSpace:onNext( minimumFreeSpace - 10 )
	obs:expectData( false )

	freeSpace:onNext( minimumFreeSpace + 10 )
	obs:expectData( true )
	
	-- force upgrade
	
	forceUpgrade:onNext( true )
	obs:expectData( false )

	forceUpgrade:onNext( false )
	obs:expectData( true )
	
	-- loupe
	
	isInLoupe:onNext( true )
	obs:expectData( false )

	isInLoupe:onNext( false )
	obs:expectData( true )
	
	-- authenticated user
	
	isAuthenticatedUser:onNext( false )
	obs:expectData( false )

	isAuthenticatedUser:onNext( true )
	obs:expectData( true )

end

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

return WFRxTestCase.wrapSuite( 'LrmOzBinaryDownloaderUtilsTest', LrmOzBinaryDownloaderUtilsTest )
