// T_MRU.CPP
//
// Copyright (c) 1997-1999 Symbian Ltd.  All rights reserved.
//

#include <f32file.h>
#include <apaid.h>
#include <apffndr.h>
#include <apgaplst.h>
#include <apgicnfl.h>
#include <apgdoor.h>
#include <apparc.h>
#include "tstapp.h"
#include <fbs.h>
#include <s32std.h> 
#include <s32stor.h> 
#include <s32file.h> 
#include <s32mem.h>
//
#if !defined(__E32TEST_H__)
#include <e32test.h>
#endif

/////////////////////////////////////////////////

const TInt KTestCleanupStack=0x40;

//LOCAL_D TUid testUid={1};
LOCAL_D TFileName dllname=_L("tstapp.app");
LOCAL_D TApaAppCaption KAppCaption=_L("Test App");
LOCAL_D TFullName searchPath=_L("\\system\\apps\\");
LOCAL_D TFullName filePath=_L("c:\\docs\\tstapp.doc");
LOCAL_D TFullName tempPath=_L("c:\\system\\temp\\");
LOCAL_D TFullName appPath=_L("z:\\system\\apps\\tstapp\\tstapp.app");
LOCAL_D TFullName icnPath=_L("z:\\system\\apps\\tstapp\\tstapp.aif");
LOCAL_D TFullName iniPath=_L("c:\\system\\apps\\tstapp\\tstapp.ini");

LOCAL_D TFileName spoofName=_L("spoof.app");
LOCAL_D TFullName spoofPath=_L("c:\\system\\apps\\spoof\\spoof.app");
LOCAL_D TFullName spoofIcn=_L("c:\\system\\apps\\spoof\\spoof.aif");
LOCAL_D TFullName hiddenIcn=_L("c:\\system\\apps\\spoof\\hidden.aif");
LOCAL_D TFullName hiddenApp=_L("c:\\system\\apps\\spoof\\hidden.app");

LOCAL_D RTest test(_L("T_MRU"));
LOCAL_D CTrapCleanup* TheTrapCleanup;
LOCAL_D RFs TheFs;


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 TestMRUL()
	{
	CApaAppFinder* finder=NULL;
	CApaProcess* process=NULL;
	CApaDocument* doc=NULL;
	//


	// Test 1
	test.Next(_L("SetMainDocFileName with no document"));

	finder = CApaScanningAppFinder::NewL(TheFs);

	__UHEAP_RESET;
	__UHEAP_MARK;
	
	process = CApaProcess::NewL(TheFs,*finder);
	process->SetMainDocFileName(_L("FileName 01"));


	delete process;

	__UHEAP_MARKEND;


	// test 2
	test.Next(_L("SetMainDocFileName after document construction"));


	__UHEAP_RESET;
	__UHEAP_MARK;


	finder = CApaScanningAppFinder::NewL(TheFs);
	
	process = CApaProcess::NewL(TheFs,*finder);

	// Create document

	doc = process->AddNewDocumentL(appPath);

	// Set file name into MRU list
	process->SetMainDocFileName(_L("FileName 02"));

	process->DestroyDocument(doc);
	
	delete process;

	__UHEAP_MARKEND;


	

	// test 3
	test.Next(_L("SetMainDocFileName x100 with same filename"));
	TInt count;

	__UHEAP_RESET;
	__UHEAP_MARK;


	finder = CApaScanningAppFinder::NewL(TheFs);
	
	process = CApaProcess::NewL(TheFs,*finder);

	// Create document

	doc = process->AddNewDocumentL(appPath);

	// Set file name into MRU list - this name should only appear once
	for (count=0; count<100; count++)
		process->SetMainDocFileName(_L("FileName 03"));

	process->DestroyDocument(doc);
	
	delete process;

	__UHEAP_MARKEND;


	// test 4
	test.Next(_L("SetMainDocFileName x100 with different filenames"));

	__UHEAP_RESET;
	__UHEAP_MARK;


	finder = CApaScanningAppFinder::NewL(TheFs);
	
	process = CApaProcess::NewL(TheFs,*finder);

	// Create document

	doc = process->AddNewDocumentL(appPath);

	// Set file name into MRU list - there should only be a maximum of 
	// 50 names in the list

	TBuf<20> fileName;

	for (count=0; count < 100; count++)
		{
		fileName.Zero();
		fileName.AppendFormat(_L("FileName %d"),count);

		process->SetMainDocFileName(fileName);
		}

	process->DestroyDocument(doc);
	
	delete process;

	__UHEAP_MARKEND;




	// test 5
	test.Next(_L("Opening MRU.DAT then calling SetMainDocFileName"));

	__UHEAP_RESET;
	__UHEAP_MARK;


	finder = CApaScanningAppFinder::NewL(TheFs);
	
	process = CApaProcess::NewL(TheFs,*finder);

	// Create document

	doc = process->AddNewDocumentL(appPath);


	// Set file name into MRU list

	TParsePtrC fileStoreName(_L("c:\\system\\data\\mru.dat"));

	CFileStore* store = NULL;
	RFs fsSession;

	fsSession.Connect();

	TRAPD(ret,
	store = CDirectFileStore::OpenL(fsSession,fileStoreName.FullName(),EFileRead);)

	// This filename should not appear in mru.dat since this file is open
	process->SetMainDocFileName(_L("FileOpen"));

	delete store;

	process->DestroyDocument(doc);
	
	delete process;

	__UHEAP_MARKEND;



		// test 6
	test.Next(_L("Testing SetMainDocFileName under OOM"));


	finder = CApaScanningAppFinder::NewL(TheFs);
	
	process = CApaProcess::NewL(TheFs,*finder);

	// Create document

	doc = process->AddNewDocumentL(appPath);


	for (count=0;count<100;count++)
		{
		__UHEAP_RESET;
		__UHEAP_SETFAIL(RHeap::EDeterministic,1);
		__UHEAP_MARK;

		// This filename should not appear in mru.dat, since no memory is available
		process->SetMainDocFileName(_L("FileName OOM"));

		test(ret==KErrNone || ret==KErrNoMemory);

		__UHEAP_MARKEND;
		}



	process->DestroyDocument(doc);
	
	delete process;


		// test 7
	test.Next(_L("Testing GetRecentFileL with KNullUid"));

	__UHEAP_RESET;
	__UHEAP_MARK;


	finder = CApaScanningAppFinder::NewL(TheFs);
	
	process = CApaProcess::NewL(TheFs,*finder);

	// Create document

	doc = process->AddNewDocumentL(appPath);

	TInt fileCount = 0;

	FOREVER
		{
		TApaLastUsedEntry appName = CApaRecentFile::GetRecentFileL(process->FsSession(),KNullUid, fileCount);
		if (appName.iFileName.Length() == 0)
			break;
		else
			{
			test.Printf(_L("%d:"),fileCount);
			test.Printf(appName.iFileName);
			test.Printf(_L("\n"));
			fileCount++;
			}
		}


	process->DestroyDocument(doc);
	
	delete process;

	__UHEAP_MARKEND;



		// test 8
	test.Next(_L("Testing GetRecentFileListL with KNullUid"));

	__UHEAP_RESET;
	__UHEAP_MARK;


	finder = CApaScanningAppFinder::NewL(TheFs);
	
	process = CApaProcess::NewL(TheFs,*finder);

	// Create document

	doc = process->AddNewDocumentL(appPath);

	CArrayFixFlat<TApaLastUsedEntry>* lastUsedTable = CApaRecentFile::GetRecentFileListL(process->FsSession(), KNullUid);


	TInt size = lastUsedTable->Count();

	test.Printf(_L("Size of table : %d elements\n"),size);

	delete lastUsedTable;

	process->DestroyDocument(doc);
	
	delete process;

	__UHEAP_MARKEND;



	}




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);\
		});
	}


GLDEF_C TInt E32Main()
	{
	setupCleanup();
	//
	test.Title();
	test.Start(_L("Testing Apparch..."));
	//
	// 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;
	TRAPD(r,TestMRUL());
		test(r==KErrNone);
	__UHEAP_MARKEND;
	

	test.End();
	test.Close();
	
	delete TheTrapCleanup;
	return KErrNone;
	}

