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

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.

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

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

	-- WF.core.os
local WFFileUtils = require 'WFFileUtils'
local WFPlatformInfo = require 'WFPlatformInfo'

	-- WF.core.performance
local WFPerformanceLocalFileReportImp = require 'WFPerformanceLocalFileReportImp'

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

local WFPerformanceLocalFileReportImpTest = {}

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

if not WFPlatformInfo.isAndroid then

-- Android errors with: Unable to create directory ... Read-only file system"

function WFPerformanceLocalFileReportImpTest.rxTestCheckExistencePerformanceFile( rxTest )

	local perfMonitor = WFPerformanceLocalFileReportImp()
	
	rxTest:finally( function()
	
		perfMonitor:close()
		
	end )
	
	local filePath
	
	rxTest:expectSingleValue( 
	
		perfMonitor:filepath()
			:tap( function( localFilepath )
				filePath = localFilepath
			end ),
			
		':ignore:' )
		
	rxTest:expectEqual( nil, filePath )
			
	rxTest:expectOnlyCompleted( perfMonitor:newPerformanceReport() )
			
	rxTest:expectSingleValue( 

		perfMonitor:filepath()
			:tap( function( localFilepath )
				filePath = localFilepath
			end ),
		
	':ignore:' )
	
	rxTest:expectEqual( 'file', WFFileUtils.exists( filePath ) )
	
	rxTest:finally( function()
		
		pcall( WFFileUtils.delete, filePath )
		
	end )
	
end

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

function WFPerformanceLocalFileReportImpTest.rxTestWriteToPerformanceFile( rxTest )

	local perfMonitor = WFPerformanceLocalFileReportImp()
	
	rxTest:expectOnlyCompleted( perfMonitor:recordSingleValue( 'foo', { name = 'test' } ) )
	
	rxTest:expectSingleValue( perfMonitor:_sendPerfDataOnce( true ), true )
	
	local filePath
	
	rxTest:expectSingleValue( 
	
		perfMonitor:filepath()
			:tap( function( localFilepath )
				filePath = localFilepath
			end ),
			
		':ignore:' )
		
	local fa = WFFileUtils.fileAttributes( filePath )
	
	rxTest:expectEqual( true, fa.fileSize > 0 )
	
	rxTest:finally( function()
		
		pcall( WFFileUtils.delete, filePath )
		
		perfMonitor:close()
		
	end )
	
end

end

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

return WFRxTestCase.wrapSuite(  WFPerformanceLocalFileReportImpTest )

