// T_FILE.CPP
//
// Copyright (c) 1997-1999 Symbian Ltd.  All rights reserved.
//

#include <e32uid.h>
#include <f32file.h>
#include <fbs.h>
#include <apadef.h>
#include <apffndr.h>
#include <apfrec.h>
#include <apfctlf.h>
#include <apgctl.h>
//#include <apgaplst.h>
//#include <apaflrec.h>
//#include <apgcli.h>
//#include <apacmdln.h>
//#include <apsserv.h>
#include "tstapp.h"
//
#if !defined(__E32TEST_H__)
#include /**/ <e32test.h>
#endif

/////////////////////////////////////////////////

const TInt KTestCleanupStack=0x40;

LOCAL_D TFileName dllname=_L("tstapp.app");
LOCAL_D TFileName searchPath=_L("\\system\\apps\\");
LOCAL_D TFileName filePath=_L("z:\\test\\tstapp.doc");
LOCAL_D TFileName tempPath=_L("c:\\system\\temp\\");
LOCAL_D TFileName appPath=_L("z:\\system\\apps\\tstapp\\tstapp.app");
LOCAL_D TFileName icnPath=_L("z:\\system\\apps\\tstapp\\tstapp.aif");
LOCAL_D TFileName iniPath=_L("c:\\system\\apps\\tstapp\\tstapp.ini");

LOCAL_D TFileName ctlName=_L("m_ctrl.ctl");
LOCAL_D TFileName ctlPath=_L("z:\\system\\controls\\m_ctrl.ctl");
LOCAL_D TFileName ctlSearchPath=_L("\\system\\controls\\");
const TUid KUidMinimalControl={98};

const TUid KUidTExe={0x147};
#define KTestExeUidType TUidType(KExecutableImageUid,KNullUid,KUidTExe)
#define KTestExeFileName _L("z:\\system\\programs\\texe.dxe")
#define spuriousFileName _L("aFileName")
#define spuriousTailEnd _L8("a Tail End")

LOCAL_D RTest test(_L("T_FILE"));
LOCAL_D CTrapCleanup* TheTrapCleanup;
LOCAL_D RFs TheFs;
LOCAL_D CApaScanningFileRecognizer* TheRecognizer;

////////////////////

LOCAL_C void setupCleanup()
//
// Initialise the cleanup stack.
//
    {
	TheTrapCleanup=CTrapCleanup::New();
	TRAPD(r,\
		{\
		for (TInt i=KTestCleanupStack;i>0;i--)\
			CleanupStack::PushL((TAny*)1);\
		test(r==KErrNone);\
		CleanupStack::Pop(KTestCleanupStack);\
		});
	}


LOCAL_D void setup()
	{
#if defined(__EPOC32__)
	// if we're on the rack create the directories we need
	TParse parser;
	parser.Set(filePath,NULL,NULL);
	TheFs.MkDirAll(parser.DriveAndPath());
	parser.Set(tempPath,NULL,NULL);
	TheFs.MkDirAll(parser.DriveAndPath());
#endif
	}


LOCAL_D void DoTest(TInt aReturnValue)
	{
	if (aReturnValue!=KErrNone)
		test.Printf(_L("\nError: %D\n"),aReturnValue);
	test(aReturnValue==KErrNone);
	}


LOCAL_D void testFindAppL()
	{
	test.Next(_L("Testing CApaScanningAppFinder::FindApp()"));
	//
	// work out a search path
	TParse path;
	path.SetNoWild(dllname,&searchPath,NULL);
	//
	// finding with just a name
	TFileName fileName;
	CApaScanningAppFinder* finder=CApaScanningAppFinder::NewL(TheFs);
	TRAPD(ret, fileName=finder->FindAppL(path.FullName(),KNullUid) ); 
		DoTest(ret);
		test(fileName.CompareF(appPath)==0);
	fileName.SetLength(0);
	//
	// finding with a name and uid
	TRAP(ret, fileName=finder->FindAppL(path.FullName(),KUidTestApp) );
		DoTest(ret);
		test(fileName.CompareF(appPath)==0);
	fileName.SetLength(0);
	//
	// failing to find with wild name
	TRAP(ret, fileName=finder->FindAppL(_L("wild*"),KUidTestApp) );
		test(ret==KErrBadName);
	//
	delete finder;
	}


LOCAL_D void testFindAllAppsL()
	{
	test.Next(_L("Testing CApaScanningAppFinder::FindAllApps() et al"));
	CApaScanningAppFinder* finder=CApaScanningAppFinder::NewL(TheFs);
	//
	TRAPD(ret, finder->FindAllAppsL() );
		DoTest(ret);
	//
	TBool more=ETrue;
	TInt count=0;
	while (more) 
		{
		TApaAppEntry entry;
		TRAPD(ret, more=finder->NextL(entry) );
			DoTest(ret);
		if (more)
			count++;
		}
	test(count>0);
	test.Printf(_L("     Apps found: %D\n"),count);
	//
	delete finder;
	}


LOCAL_D void testFindControlL()
	{
	test.Next(_L("Testing CApaScanningControlFinder::FindApp()"));
	TheFs.Delete(_L("c:\\system\\controls\\m_ctrl.ctl"));
	//
	// work out a search path
	TParse path;
	path.SetNoWild(ctlName,&ctlSearchPath,NULL);
	//
	// finding with just a name
	TFileName fileName;
	CApaScanningControlFinder* finder=CApaScanningControlFinder::NewL(TheFs);
	TRAPD(ret, fileName=finder->FindAppL(path.FullName(),KNullUid) ); 
		DoTest(ret);
		test.Printf(path.FullName());
		test(fileName.CompareF(ctlPath)==0);
	fileName.SetLength(0);
	//
	// finding with a name and uid
	TRAP(ret, fileName=finder->FindAppL(path.FullName(),KUidMinimalControl) );
		DoTest(ret);
		test(fileName.CompareF(ctlPath)==0);
	fileName.SetLength(0);
	//
	// failing to find with wild name
	TRAP(ret, fileName=finder->FindAppL(_L("wild*"),KUidTestApp) );
		test(ret==KErrBadName);
	//
	delete finder;
	}


LOCAL_D void testFindAllControlsL()
	{
	test.Next(_L("Testing CApaScanningControlFinder::FindAllApps() et al"));
	CApaScanningControlFinder* finder=CApaScanningControlFinder::NewL(TheFs);
	//
	TRAPD(ret, finder->FindAllAppsL() );
		DoTest(ret);
	//
	TBool more=ETrue;
	TInt count=0;
	while (more) 
		{
		TApaAppEntry entry;
		TRAPD(ret, more=finder->NextL(entry) );
			DoTest(ret);
		if (more)
			count++;
		}
	test(count==1);
	//
	delete finder;
	}


LOCAL_D void testScanningFileTheRecognizerL()
	{
	test.Next(_L("Testing the Scanning File-Recognizer"));
	//
	// construct - this scans for TheRecognizer plug-ins
	TRAPD(ret, TheRecognizer=CApaScanningFileRecognizer::NewL(TheFs,NULL) );
		DoTest(ret);
		test(TheRecognizer->RecognizerCount()==2);
		test(TheRecognizer->UpdateCounter()==2);
	//
	test.Next(_L("Testing the getter and setter functions"));
	//
	// check the getter function
	CApaScanningFileRecognizer::TRecognizer infApp;
	CApaScanningFileRecognizer::TRecognizer infExe;
	if ((*TheRecognizer)[0].iUid.iUid==268435783)
		{
		infApp = (*TheRecognizer)[0];
		infExe = (*TheRecognizer)[1];
		}
	else
		{
		infApp = (*TheRecognizer)[1];
		infExe = (*TheRecognizer)[0];		
		}
	//
		test(infApp.iUid.iUid=268435783);
		test(infExe.iUid.iUid=268435785);
		test(infApp.iDrive=25);
		test(infExe.iDrive=25);
		test(infApp.iName.CompareF(_L("RECAPP"))==0);
		test(infExe.iName.CompareF(_L("RECEXE"))==0);
	//
	// check the setter function with a rec that's already added
	TRAP(ret,TheRecognizer->SetRecognizerL(infApp));
		DoTest(ret);
		test(TheRecognizer->UpdateCounter()==2);
	//
	// check the setter function with a rec that doesnt exist
	CApaScanningFileRecognizer::TRecognizer dodgyInf;
	dodgyInf.iUid.iUid = 7009;
	dodgyInf.iDrive = 25;
	dodgyInf.iName = _L("Dodgy");
	TRAP(ret,TheRecognizer->SetRecognizerL(dodgyInf));
		test(ret==KErrNotFound);
		test(TheRecognizer->UpdateCounter()==2);
	//
	// do other stuff...
	test.Next(_L("Testing the App recognizer"));
	//
	// check test app exists and has correct uids
	TEntry entry;
	ret = TheFs.Entry(appPath,entry);
		DoTest(ret);
		test(entry.iType[1]==KUidApp);
		test(entry.iType[2]==KUidTestApp);
	//
	// check test doc exists and has correct uids
	ret = TheFs.Entry(filePath,entry);
		DoTest(ret);
		test(entry.iType[1]==KUidAppDllDoc);
		test(entry.iType[2]==KUidTestApp);
	//
	// recognise an app without uids
	CApaFileRecognizerType* type=NULL;
	TRAP(ret, type=TheRecognizer->RecognizeFileL(appPath) );
		DoTest(ret);
		test(type!=NULL);
		test(type->AppUid()==KUidTestApp);
		test(type->TypeUid()==KUidApp);

	TApaAppCapabilityBuf buf;
	type->Capability(buf);
		test(buf().iEmbeddability==TApaAppCapability::ENotEmbeddable);
		test(!buf().iSupportsNewFile);
	//
	// recognise an app with UidType
	TRAP(ret, type=TheRecognizer->RecognizeFileL(appPath,&KTestAppUidType) );
		DoTest(ret);
		test(type!=NULL);
		test(type->AppUid()==KUidTestApp);
		test(type->TypeUid()==KUidApp);
	//
	// recognise a doc without uids
	TRAP(ret, type=TheRecognizer->RecognizeFileL(filePath) );
		DoTest(ret);
		test(type!=NULL);
		test(type->AppUid()==KUidTestApp);
		test(type->TypeUid()==KUidAppDllDoc);
	//
	// do other stuff...
	test.Next(_L("Testing the Exe recognizer"));
	//
	// check test exe exists and has correct uids
	ret = TheFs.Entry(KTestExeFileName,entry);
		DoTest(ret);
		test(entry.iType[1]==KNullUid);
		test(entry.iType[2]==KUidTExe);
	//
	// recognise an exe with UidType
	TRAP(ret, type=TheRecognizer->RecognizeFileL(KTestExeFileName,&KTestExeUidType) );
		DoTest(ret);
		test(type!=NULL);
		test(type->AppUid()==KUidTExe);
		test(type->TypeUid()==KNullUid);
	//
	// recognise an exe without uids
	TRAP(ret, type=TheRecognizer->RecognizeFileL(KTestExeFileName) );
		DoTest(ret);
		test(type!=NULL);
		test(type->AppUid()==KUidTExe);
		test(type->TypeUid()==KNullUid);
	//
	// run the recognized exe with a given command line
	TRAP(ret, type->RunL(EApaCommandRun,&spuriousFileName,&spuriousTailEnd) );
		test(ret==KErrNone);
	//
	test.Printf(_L("\nPress any key...")); // stop the dll's being closed too early
	test.Getch(); // stop the dll's being closed too early
	delete TheRecognizer;
	}


GLDEF_C TInt E32Main()
	{
	setupCleanup();
	//
	test.Title();
	test.Start(_L("Testing the APFILE dll..."));
	//
	// set up an fbs
//	FbsStartup();
//	TInt ret=RFbsSession::Connect();
//		test(!ret);
	//
	// set up the directory structure
	TheFs.Connect();
	setup();
	//
	// run the testcode (inside an alloc heaven harness)

	__UHEAP_MARK;
	// RDebug::Print(_L("About to test CApaScanningAppFinder::FindApp()"));
	TRAPD(r,testFindAppL());
		test(r==KErrNone);
	__UHEAP_MARKEND;

 	__UHEAP_MARK;
	// RDebug::Print(_L("About to test CApaScanningAppFinder::FindAllApps()"));
	TRAP(r,testFindAllAppsL());
		test(r==KErrNone);
	__UHEAP_MARKEND;

	__UHEAP_MARK;
	// RDebug::Print(_L("About to test CApaScanningControlFinder::FindApp()"));
	TRAP(r,testFindControlL());
		test(r==KErrNone);
	__UHEAP_MARKEND;

 	__UHEAP_MARK;
	// RDebug::Print(_L("About to test CApaScanningControlFinder::FindAllApps()"));
	TRAP(r,testFindAllControlsL());
		test(r==KErrNone);
	__UHEAP_MARKEND;

 	__UHEAP_MARK;
	// RDebug::Print(_L("About to test the Scanning File TheRecognizer"));
	TRAP(r,testScanningFileTheRecognizerL());
		test(r==KErrNone);
	__UHEAP_MARKEND;


	test.End();
	test.Close();
	
	delete TheTrapCleanup;
	return KErrNone;
	}

