// TEIKCONS.CPP
//
// Copyright (c) 1997-1999 Symbian Ltd.  All rights reserved.
//

#include <e32std.h>
#include <e32base.h>
#include <e32test.h>

RTest test(_L("TEIKCONS"));

TInt SubThread(TAny*)
	{
	RTest subTest(_L("SubTest"));
	subTest.Start(_L("Start"));
	subTest.End();
	subTest.Close();
	return(KErrNone);
	}

void ThreadTest()
	{
	test.Printf(_L("Starting thread...\n"));
	RThread thread;
	TInt err=thread.Create(_L("Subthread"),SubThread,KDefaultStackSize,0x1000,0x10000,NULL);
	test(err==KErrNone);
	TRequestStatus stat;
	thread.Logon(stat);
	thread.Resume();
	User::WaitForRequest(stat);
	test(stat==KErrNone);
	thread.Close();
	test.Printf(_L("Thread exited OK\n"));
	}

void ChunkTestL()
	{ // taken from some code written by SteveG
	test.Printf(_L("Finding chunks...\n"));
    TFullName name=_L("*");
    TFindChunk find(name);

    while (find.Next(name)==KErrNone)
		{
		RChunk chunk;
        test.Printf(_L("Chunk name %S\n"),&name);
        TInt err=chunk.Open(find);
		if (err)
			test.Printf(_L("Error %d opening chunk"),err);
		else
        	{
			test.Printf(_L("Chunk size %8d bytes\n"),chunk.Size());
	        chunk.Close();
			}
		User::After(500000);
        }
	}

void AsynchReadTestL()
	{
	test.Printf(_L("Test asynchrounous keyboard echo - Escape to finish\n\n"));
	TInt ret=0;
	TBool repeat=ETrue;
	while (repeat) 
		{
		ret=test.Getch();
		switch (ret)
			{
		case EKeyUpArrow:
			test.Console()->SetCursorPosRel(TPoint(0, -1));
			break;
		case EKeyDownArrow:
			test.Console()->SetCursorPosRel(TPoint(0, 1));
			break;
		case EKeyLeftArrow:
			test.Console()->SetCursorPosRel(TPoint(-1, 0));
			break;
		case EKeyRightArrow:
			test.Console()->SetCursorPosRel(TPoint(1, 0));
			break;
		case EKeyBackspace:
			test.Console()->SetCursorPosRel(TPoint(-1, 0));
			test.Printf(_L("%c"), EKeySpace);
			test.Console()->SetCursorPosRel(TPoint(-1, 0));
			break;
		case EKeyEscape:
			repeat=EFalse;
			break;
		case EKeyEnter:
			test.Printf(_L("%c"), EKeyLineFeed);
			// deliberate fall through
		default:
			test.Printf(_L("%c"), ret);
			break;
			}
		};
	}

void RunTestsL()
	{ 
    test.Start(_L("Eikon console tests...\n\n"));
start:
	test.Printf(_L("\n\nChoose a test...\n"));
	test.Printf(_L("******************************************************\n"));
	test.Printf(_L("1) Chunk test\n"));
	test.Printf(_L("2) Asynchrounous keyboard read test\n"));
	test.Printf(_L("3) Thread test\n"));
	test.Printf(_L("q) Exit\n"));
	test.Printf(_L("******************************************************\n"));
	TInt ret=test.Getch();
	switch (ret)
		{
	case '1':
		test.Next(_L(""));
		ChunkTestL();
		break;
	case '2':
		AsynchReadTestL();
		break;
	case '3':
		ThreadTest();
		break;
	case 'q':
		test.Printf(_L("Goodbye\n"));
		goto end;
		break;
		}
	goto start;
end:
    test.End();
	}

#if defined(__WINS__)
EXPORT_C TInt EntryPoint()
#else
GLDEF_C TInt E32Main()
#endif
	{
	__UHEAP_MARK;
    CTrapCleanup *cleanup=CTrapCleanup::New();
	test.Title();
    TRAPD(err,RunTestsL());
	test(!err);
	test.Close();
    delete(cleanup);
	__UHEAP_MARKEND;
    return(0);
	}

#if defined(__WINS__)
GLDEF_C TInt E32Dll(TDllReason)
	{
	return(KErrNone);
	}
#endif
