// T_CMDLN.CPP
//
// Copyright (c) 1997-1999 Symbian Ltd.  All rights reserved.
//


#include <e32std.h>
#include <e32base.h>
#include <f32file.h>
#include <e32test.h>
#include <apacmdln.h>

const TInt KTestCleanupStack=0x40;

LOCAL_D RTest test(_L("Testing command lines"));
LOCAL_D CTrapCleanup* TheTrapCleanup;



LOCAL_C void testCmdLinesL()
	{ 
	// RDebug::Print(_L("Starting testCmdLinesL()"));
	//
	CArrayFixFlat<TPtrC>* array=new(ELeave) CArrayFixFlat<TPtrC>(5);
	CleanupStack::PushL(array);
	array->AppendL(_L(""));
	array->AppendL(_L("appname"));
	array->AppendL(_L("appname Odocname"));
	array->AppendL(_L("\"app name\" B"));
	array->AppendL(_L("\"app name\" Cdocname"));
	array->AppendL(_L("appname O\"doc name\""));
	array->AppendL(_L("\"app name\" O\"doc name\""));
	array->AppendL(_L("appname Odocname tail end"));
	array->AppendL(_L("\"app name\" O\"doc name\" \"tail end\""));
	CApaCommandLine* cmdLine=CApaCommandLine::NewLC();
	for (TInt i=0; i<array->Count(); i++)
		{
		TBuf<0x80> text=_L("\nSetting command line with: \n");
		test.Printf(text);
		TPtrC commandLine=(*array)[i];
		test.Printf(commandLine);
		cmdLine->SetFullCommandLineL(commandLine);
		test.Printf(_L("\n"));

		text=_L("    lib name= ");
		text.Append(cmdLine->LibraryName());
		test.Printf(text);
  		test.Printf(_L("\n"));

		text=_L("    doc name= ");
		text.Append(cmdLine->DocumentName());
		test.Printf(text);
  		test.Printf(_L("\n"));
		
		TBuf<0x80> tail;
		tail.Copy(cmdLine->TailEnd());
		text=_L("    tail end= ");
		text.Append(tail);
		test.Printf(text);
  		test.Printf(_L("\n"));

		text=_L("     command= ");
		switch (cmdLine->Command())
			{
		case EApaCommandOpen:
			text.Append(_L("open"));
			break;
		case EApaCommandCreate:
			text.Append(_L("create"));
			break;
		case EApaCommandRun:
			text.Append(_L("run"));
			break;
		case EApaCommandBackground:
			text.Append(_L("background"));
			break;
			}
		test.Printf(text);
		test.Printf(_L("\n"));
//		User::After(5000000);
		}

	// RDebug::Print(_L("Mark 1"));
	test.Next(_L("\n\nTesting setting and getting individual components"));
	CleanupStack::PopAndDestroy(); // cmdLine
	test.Printf(_L("\nCreating an empty command line\n"));
	cmdLine=CApaCommandLine::NewLC(); // start with an empty command line
	TPtrC libName;
	TPtrC docName;
	TApaCommand command=EApaCommandRun;
	TPtrC8  tailEnd;
	test(libName==cmdLine->LibraryName());
	test(command==cmdLine->Command());
	test(docName==cmdLine->DocumentName());
	test(tailEnd==cmdLine->TailEnd());

	// RDebug::Print(_L("Mark 2"));
	test.Printf(_L("\nSetting library name to <lib name>\n"));
	libName.Set(_L("lib name"));
	cmdLine->SetLibraryNameL(libName);
	test(libName==cmdLine->LibraryName());
	test(command==cmdLine->Command());
	test(docName==cmdLine->DocumentName());
	test(tailEnd==cmdLine->TailEnd());

	// RDebug::Print(_L("Mark 3"));
	test.Printf(_L("\nSetting command byte to EApaCommandOpen\n"));
	command=EApaCommandOpen;
	cmdLine->SetCommandL(command);
	test(libName==cmdLine->LibraryName());
	test(command==cmdLine->Command());
	test(docName==cmdLine->DocumentName());
	test(tailEnd==cmdLine->TailEnd());

	// RDebug::Print(_L("Mark 4"));
	test.Printf(_L("\nSetting document name to <doc name>\n"));
	docName.Set(_L("doc name"));
	cmdLine->SetDocumentNameL(docName);
	test(libName==cmdLine->LibraryName());
	test(command==cmdLine->Command());
	test(docName==cmdLine->DocumentName());
	test(tailEnd==cmdLine->TailEnd());

	// RDebug::Print(_L("Mark 5"));
	test.Printf(_L("\nSetting tail end to <tail end>\n"));
	tailEnd.Set(_L8("tail end"));
	cmdLine->SetTailEndL(tailEnd);
	test(libName==cmdLine->LibraryName());
	test(command==cmdLine->Command());
	test(docName==cmdLine->DocumentName());
	test(tailEnd==cmdLine->TailEnd());

	// RDebug::Print(_L("Mark 6"));
	test.Next(_L("\n\nTesting setting and getting individual components in reverse order"));
	CleanupStack::PopAndDestroy(); // cmdLine
	test.Printf(_L("\nCreating an empty command line\n"));
	cmdLine=CApaCommandLine::NewLC(); // start with an empty command line
	libName.Set(_L(""));
	command=EApaCommandRun;
	docName.Set(_L(""));
	tailEnd.Set(_L8(""));
 	test(libName==cmdLine->LibraryName());
	test(command==cmdLine->Command());
	test(docName==cmdLine->DocumentName());
	test(tailEnd==cmdLine->TailEnd());

	// RDebug::Print(_L("Mark 7"));
	test.Printf(_L("\nSetting tail end to <tail end>\n"));
	tailEnd.Set(_L8("tail end"));
	cmdLine->SetTailEndL(tailEnd);
	test(libName==cmdLine->LibraryName());
	test(command==cmdLine->Command());
	test(docName==cmdLine->DocumentName());
	test(tailEnd==cmdLine->TailEnd());

	// RDebug::Print(_L("Mark 8"));
	test.Printf(_L("\nSetting document name to <doc name>\n"));
	docName.Set(_L("doc name"));
	cmdLine->SetDocumentNameL(docName);
	test(libName==cmdLine->LibraryName());
	test(command==cmdLine->Command());
	test(docName==cmdLine->DocumentName());
	test(tailEnd==cmdLine->TailEnd());

	// RDebug::Print(_L("Mark 9"));
	test.Printf(_L("\nSetting command byte to EApaCommandOpen\n"));
	command=EApaCommandOpen;
	cmdLine->SetCommandL(command);
	test(libName==cmdLine->LibraryName());
	test(command==cmdLine->Command());
	test(docName==cmdLine->DocumentName());
	test(tailEnd==cmdLine->TailEnd());

	// RDebug::Print(_L("Mark 10"));
	test.Printf(_L("\nSetting library name to <lib name>\n"));
	libName.Set(_L("lib name"));
	cmdLine->SetLibraryNameL(libName);
	test(libName==cmdLine->LibraryName());
	test(command==cmdLine->Command());
	test(docName==cmdLine->DocumentName());
	test(tailEnd==cmdLine->TailEnd());

	// RDebug::Print(_L("Mark 11"));
	test.Next(_L("\n\nTesting replacing and getting individual components"));
 	test.Printf(_L("\nSetting library name to <new lib name>\n"));
	libName.Set(_L("new lib name"));
	cmdLine->SetLibraryNameL(libName);
	test(libName==cmdLine->LibraryName());
	test(command==cmdLine->Command());
	test(docName==cmdLine->DocumentName());
	test(tailEnd==cmdLine->TailEnd());

	// RDebug::Print(_L("Mark 12"));
 	test.Printf(_L("\nSetting command byte to EApaCommandCreate\n"));
	command=EApaCommandCreate;
	cmdLine->SetCommandL(command);
	test(libName==cmdLine->LibraryName());
	test(command==cmdLine->Command());
	test(docName==cmdLine->DocumentName());
	test(tailEnd==cmdLine->TailEnd());

 	// RDebug::Print(_L("Mark 13"));
	test.Printf(_L("\nSetting document name to <new doc name>\n"));
	docName.Set(_L("new doc name"));
	cmdLine->SetDocumentNameL(docName);
	test(libName==cmdLine->LibraryName());
	test(command==cmdLine->Command());
	test(docName==cmdLine->DocumentName());
	test(tailEnd==cmdLine->TailEnd());

	// RDebug::Print(_L("Mark 14"));
 	test.Printf(_L("\nSetting tail end to <new tail end>\n"));
	tailEnd.Set(_L8("new tail end"));
	cmdLine->SetTailEndL(tailEnd);
	test(libName==cmdLine->LibraryName());
	test(command==cmdLine->Command());
	test(docName==cmdLine->DocumentName());
	test(tailEnd==cmdLine->TailEnd());

	// RDebug::Print(_L("Mark 15"));
  	test.Next(_L("\n\nTesting replacing and getting individual components in reverse order"));
  	test.Printf(_L("\nSetting tail end to <another new tail end>\n"));
	tailEnd.Set(_L8("another new tail end"));
	cmdLine->SetTailEndL(tailEnd);
	test(libName==cmdLine->LibraryName());
	test(command==cmdLine->Command());
	test(docName==cmdLine->DocumentName());
	test(tailEnd==cmdLine->TailEnd());

	// RDebug::Print(_L("Mark 16"));
  	test.Printf(_L("\nSetting document name to <another new doc name>\n"));
	docName.Set(_L("another new doc name"));
	cmdLine->SetDocumentNameL(docName);
	test(libName==cmdLine->LibraryName());
	test(command==cmdLine->Command());
	test(docName==cmdLine->DocumentName());
	test(tailEnd==cmdLine->TailEnd());

	// RDebug::Print(_L("Mark 17"));
  	test.Printf(_L("\nSetting command byte to EApaCommandBackground\n"));
	command=EApaCommandBackground;
	cmdLine->SetCommandL(command);
	test(libName==cmdLine->LibraryName());
	test(command==cmdLine->Command());
	test(docName==cmdLine->DocumentName());
	test(tailEnd==cmdLine->TailEnd());

	// RDebug::Print(_L("Mark 18"));
 	test.Printf(_L("\nSetting library name to <another new lib name>\n"));
	libName.Set(_L("another new lib name"));
	cmdLine->SetLibraryNameL(libName);
	test(libName==cmdLine->LibraryName());
	test(command==cmdLine->Command());
	test(docName==cmdLine->DocumentName());
	test(tailEnd==cmdLine->TailEnd());


	CleanupStack::PopAndDestroy(2); // array & cmdLine
	}


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("Test setting full command lines"));
	//
	// run the testcode (inside an alloc heaven harness)

	__UHEAP_MARK;
	TRAPD(r,testCmdLinesL());
		test(r==KErrNone);
	__UHEAP_MARKEND;

 	test.End();
	test.Close();
	
	delete TheTrapCleanup;
	return KErrNone;
	}

