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

KSPathUtilsTest.lua

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

ADOBE CONFIDENTIAL
------------------
Copyright 2012 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.

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

local KSPathUtils = require 'KSPathUtils'
local KSTestUtils = require 'KSTestUtils'
local WFFileUtils = require 'WFFileUtils'
local KSData = require 'KSData'
local KSIdGenerator = require 'KSIdGenerator'
	
local WFPlatformInfo = require 'WFPlatformInfo'


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

local KSPathUtilsTest = {}

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

function KSPathUtilsTest.testStandardFilePath()

	local success = true
	local resultStr = "The following standard paths could not be found: \n"
	
	local tempResult
	local localSuccess

	localSuccess, tempResult = pcall( function() return KSPathUtils.getStandardFilePath( "app" ) end )
	if localSuccess == false or tempResult == "" then
		resultStr = resultStr ..  "App, "
		success = false
	end

	localSuccess, tempResult = pcall( function() return KSPathUtils.getStandardFilePath( "appBuiltInPlugins" ) end )
	if localSuccess == false or tempResult == "" then
		resultStr = resultStr ..  "appBuiltInPlugins, "
		success = false
	end
	
	if not WFPlatformInfo.isiOS then
		localSuccess, tempResult = pcall( function() return KSPathUtils.getStandardFilePath( "adobeAppData" ) end )
		if localSuccess == false or tempResult == "" then
			resultStr = resultStr ..  "adobeAppData, "
			success = false
		end
	end
	
	if not ( WFPlatformInfo.isAndroid ) then -- home and desktop are not available on some platforms
	
		localSuccess, tempResult = pcall( function() return KSPathUtils.getStandardFilePath( "home" ) end )
		if localSuccess == false or tempResult == "" then
			resultStr = resultStr ..  "home, "
			success = false
		end
		localSuccess, tempResult = pcall( function() return KSPathUtils.getStandardFilePath( "desktop" ) end )
		if localSuccess == false or tempResult == "" then
			resultStr = resultStr ..  "desktop, "
			success = false
		end
		
	end
	
	if ( WFPlatformInfo.isWindows or WFPlatformInfo.isMac ) then
		
		localSuccess, tempResult = pcall( function() return KSPathUtils.getStandardFilePath( "documents" ) end )
		if localSuccess == false or tempResult == "" then
			resultStr = resultStr .. "documents, "
			success = false
		end

	end
	
	localSuccess, tempResult = pcall( function() return KSPathUtils.getStandardFilePath( "caches" ) end )
	if localSuccess == false or tempResult == "" then
		resultStr = resultStr ..  "caches, "
		success = false
	end
	
	localSuccess, tempResult = pcall( function() return KSPathUtils.getStandardFilePath( "carouselDocuments" ) end )
	if localSuccess == false or tempResult == "" then
		resultStr = resultStr ..  "carouselDocuments, "
		success = false
	end
	
	localSuccess, tempResult = pcall( function() return KSPathUtils.getStandardFilePath( "carouselCaches" ) end )
	if localSuccess == false or tempResult == "" then
		resultStr = resultStr ..  "carouselCaches, "
		success = false
	end

	if not WFPlatformInfo.isiOS then -- Pictures dir not available on iOS devices.
		localSuccess, tempResult = pcall( function() return KSPathUtils.getStandardFilePath( "pictures" ) end )
		if localSuccess == false or tempResult == "" then
			resultStr = resultStr ..  "pictures, "
			success = false
		end
	end
	
	if not WFPlatformInfo.isAndroid then -- home and desktop are not available on Android
		localSuccess, tempResult = pcall( function() return KSPathUtils.getStandardFilePath( "applications" ) end )
		if localSuccess == false or tempResult == "" then
			resultStr = resultStr ..  "applications, "
			success = false
		end
	end
		
	localSuccess, tempResult = pcall( function() return KSPathUtils.getStandardFilePath( "temp" ) end )
	if localSuccess == false or tempResult == "" then
		resultStr = resultStr ..  "temp, "
		success = false
	end
	localSuccess, tempResult = pcall( function() return KSPathUtils.getStandardFilePath( "testdata" ) end )
	if localSuccess == false or tempResult == "" then
		resultStr = resultStr ..  "testdata, "
		success = false
	end
	localSuccess, tempResult = pcall( function() return KSPathUtils.getStandardFilePath( "Resources" ) end )
	if localSuccess == false or tempResult == "" then
		resultStr = resultStr ..  "Resources, "
		success = false
	end
	
	-- FIXME (maybe) the TranslatedStrings path isn't available unless you have resources built and the test runner
	-- does not currently build the resources so this test will always fail.
	--[[localSuccess, tempResult = pcall( function() return KSPathUtils.getStandardFilePath( "TranslatedStrings" ) end )
	if ( localSuccess == false or tempResult == "" ) then
		resultStr = resultStr ..  "TranslatedStrings"
		success = false
	end
	]]

	assert( success == true, resultStr )

end

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

function KSPathUtilsTest.testChild()

	local ftc = KSTestUtils.functionTestCase


	local testCases = {
		ftc "Simple" ( "Foo", "Baz.jpg" )( "Foo" .. WFPlatformInfo.folderSeparator .. "Baz.jpg" ),
		ftc "WithPath" ( "Foo/Bar", "Baz.jpg" )( "Foo/Bar" .. WFPlatformInfo.folderSeparator .. "Baz.jpg" ),
		ftc "WithPaths" ( "Foo/Bar", "Zoo/Baz.jpg" )( "Foo/Bar" .. WFPlatformInfo.folderSeparator .. "Zoo/Baz.jpg" ),
		ftc "WithTrailingSlash" ( "Foo/Bar" .. WFPlatformInfo.folderSeparator, "Zoo/Baz.jpg" )( "Foo/Bar" .. WFPlatformInfo.folderSeparator .. "Zoo/Baz.jpg" ),
		ftc "WithLeadingSlash" ( "Foo/Bar", WFPlatformInfo.folderSeparator .. "Zoo/Baz.jpg" )( "Foo/Bar" .. WFPlatformInfo.folderSeparator .. "Zoo/Baz.jpg" )
	}

	KSTestUtils.runFunctionTestCases( KSPathUtils.child, testCases )

end

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

function KSPathUtilsTest.testParent()

	local ftc = KSTestUtils.functionTestCase


	local testCases = {
		ftc "SingleItem" ( "Foo" )( "" ),
		ftc "SimplePath" ( "Foo/Bar" )( "Foo" ),
		ftc "SimplePathWithFile" ( "Foo/Bar/file.jpg" )( "Foo/Bar" ),
	}

	KSTestUtils.runFunctionTestCases( KSPathUtils.parent, testCases )

end

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

function KSPathUtilsTest.testLeaf()

	local ftc = KSTestUtils.functionTestCase


	local testCases = {
		ftc "SingleItem" ( "Foo" )( "Foo" ),
		ftc "SimplePath" ( "Foo/Bar" )( "Bar" ),
		ftc "SimplePathWithFile" ( "Foo/Bar/file.jpg" )( "file.jpg" ),
	}

	KSTestUtils.runFunctionTestCases( KSPathUtils.leafName, testCases )

end

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

function KSPathUtilsTest.testIsPathParentOf()

	local ftc = KSTestUtils.functionTestCase

	local testCases = {
		ftc "basicTrue" ( "Foo", "Foo/Bar" )( true ),
		ftc "basicFalse" ( "Foo/Bar", "Foo" )( false ),
		ftc "trueWithFile" ( "Foo/Bar/Baz", "Foo/Bar/Baz/file.jpg" )( true ),
		ftc "falseWithFile" ( "Foo/Bar/Baz.jpg", "Foo/Bar/Baz" )( false ),
	}

	KSTestUtils.runFunctionTestCases( KSPathUtils.isPathParentOf, testCases )

end

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

function KSPathUtilsTest.testShortPath()

	-- Filename should be a valid utf8 char in case of Android
	local folderName = "fˆˆ"
	if WFPlatformInfo.isAndroid then
		folderName = "f"
	end

	local prefsDirectory = KSPathUtils.child( KSPathUtils.getStandardFilePath( "adobeAppData" ), folderName )
	WFFileUtils.mkdir( prefsDirectory )

	local normalPath = KSPathUtils.child( prefsDirectory, "unicode.test" )

	local contents = KSIdGenerator.uuidInLowercaseHex()

	KSData.saveToFile( KSData.make( contents ), normalPath )
	
	local shortPath = KSPathUtils.shortPath( normalPath )

	local file = io.open( shortPath, "r" )
	local shortPathContents
	
	if file then
		shortPathContents = file:read( "*a" )
		file:close()
	else
		file = nil
	end
	
	KSTestUtils.assertEquals( contents == shortPathContents, "Contents should match." )
	
	WFFileUtils.delete( normalPath, true )
	
end

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

return KSPathUtilsTest
