// ICONTOOL.CPP
//
// Copyright (c) 1997-1999 Symbian Ltd.  All rights reserved.
//

#include <apgicnfl.h>
#include "tstapp.h"
#include /**/ <fbs.h>
 

#if !defined(__E32TEST_H__)
#include /**/ <e32test.h>
#endif

const TInt KTestCleanupStack=0x40;

LOCAL_D TFullName iconFilePath=_L("c:\\system\\apps\\tstapp\\tstapp.aif");
LOCAL_D TApaAppCaption KAppCaption=_L("Test App");

const TUid KUidTest1App={11};
const TUid KUidTOpbut={223};
#define KTest1AppIconFile _L("c:\\system\\apps\\test1\\Test1.aif")
#define KTest1DefaultCaption _L("Test1")
#define KTest1EnglishCaption _L("Word")
#define KTest1FrenchCaption _L("Mot")

#define KDefaultIconFileName _L("C:\\system\\data\\default.aif")
const TUid KUidDefaultIconFile={0x100000D5};

LOCAL_D RTest test(_L("ICONFILE"));
LOCAL_D CTrapCleanup* TheTrapCleanup;
//LOCAL_D CTestDeviceMap* TheMap;



LOCAL_D void CreateIconFileL()
	{
	// create a file writer
	CApaAppInfoFileWriter* writer=NULL;
	TRAPD(ret,{writer=CApaAppInfoFileWriter::NewLC(iconFilePath,KUidTestApp);
				CleanupStack::Pop();
				});
		test(ret==KErrNone);
	CleanupStack::PushL(writer);
	// add a caption
	TRAP(ret,writer->AddCaptionL(ELangEnglish,KAppCaption));
		test(ret==KErrNone);
	// add a couple of icons (each should be a .mbm file containing the icon at 0 and its mask at 1)
	TRAP(ret,writer->AddIconL(_L("z:\\system\\apps\\tstapp\\icon16.mbm")));
		test(ret==KErrNone);
	TRAP(ret,writer->AddIconL(_L("z:\\system\\apps\\tstapp\\icon64.mbm")));
		test(ret==KErrNone);
	// add some capabilities
	TApaAppCapability capability;
	capability.iEmbeddability = TApaAppCapability::EEmbeddable;
	capability.iSupportsNewFile = EFalse;
	capability.iAppIsHidden = EFalse;
	TApaAppCapabilityBuf buf(capability);
	ret = writer->SetCapability(buf);
		test(ret==KErrNone);
	// add some mime types
	TRAP(ret,writer->AddDataTypeL(TDataTypeWithPriority(TDataType(_L8("text/plain")),KDataTypePriorityNormal)));
		test(ret==KErrNone);
	TRAP(ret,writer->AddDataTypeL(TDataTypeWithPriority(TDataType(_L8("text/html")),KDataTypePriorityHigh)));
		test(ret==KErrNone);
	TRAP(ret,writer->AddDataTypeL(TDataTypeWithPriority(TDataType(_L8("something/else")),KDataTypePriorityLow)));
		test(ret==KErrNone);
	// close the file
	TRAP(ret,writer->StoreL());
		test(ret==KErrNone);
	CleanupStack::PopAndDestroy(); // writer;
	}


LOCAL_D void TestIconFileL()
	{
	// create a file reader
	CApaAppInfoFileReader* reader=NULL;
	TRAPD(ret,{reader=CApaAppInfoFileReader::NewLC(iconFilePath,KUidTestApp);
				CleanupStack::Pop();
				});
		test(ret==KErrNone);
	CleanupStack::PushL(reader);
	// read the caption
	TApaAppCaption caption=NULL;
	TRAP(ret,caption=reader->CaptionL(ELangEnglish));
		test(ret==KErrNone);
		test(caption==KAppCaption);
	// get an icon
	CApaMaskedBitmap* icon=NULL;
	TRAP(ret,icon=reader->CreateMaskedBitmapL(64)); // 64x64 icon
		test(ret==KErrNone);
		test(icon->SizeInPixels().iWidth==64);
	// check the capability
	TApaAppCapabilityBuf buf;
	reader->Capability(buf);
	TApaAppCapability capability=buf();
		test(capability.iEmbeddability==TApaAppCapability::EEmbeddable);
		test(!capability.iSupportsNewFile);
		test(!capability.iAppIsHidden);
	// clean up
	delete icon;
	CArrayFixFlat<TDataTypeWithPriority>* array=new(ELeave) CArrayFixFlat<TDataTypeWithPriority>(3);
	CleanupStack::PushL(array);
	reader->DataTypesSupportedL(*array);
	test(array->Count()==3);
	test(array->At(0).iDataType==TDataType(_L8("text/plain")));
	test(array->At(0).iPriority==KDataTypePriorityNormal);
	test(array->At(1).iDataType==TDataType(_L8("text/html")));
	test(array->At(1).iPriority==KDataTypePriorityHigh);
	test(array->At(2).iDataType==TDataType(_L8("something/else")));
	test(array->At(2).iPriority==KDataTypePriorityLow);
	CleanupStack::PopAndDestroy(2); // array then reader;
	}

LOCAL_C void TestLongFileName(CApaAppInfoFileWriter& aWFile)
	{
// Test adding caption thats too long
	TBuf<512> fName=_L("1234567890123456789012345678901234567890123456789012345678901234");
	fName+=fName;
	fName+=fName;
	fName+=_L("1");		// 257
	TRAPD(ret,aWFile.AddCaptionL(ELangFrench,fName));
	test(ret==KErrBadName);
	}

LOCAL_D void BillsTestsL()
	{
	TFileName testFile=KTest1AppIconFile;
	CApaAppInfoFileWriter* wFile=NULL;
	CApaAppInfoFileReader* rFile=NULL;
// Test minimum icon file
	test.Next(_L("Testing minimum icon file"));
	TRAPD(ret,{wFile=CApaAppInfoFileWriter::NewLC(testFile,KUidTest1App);
				CleanupStack::Pop();
				});
		test(ret==KErrNone);
		CleanupStack::PushL(wFile);
	TRAP(ret,wFile->StoreL());
		test(ret==KErrNone);
	CleanupStack::PopAndDestroy();			// wFile
	TRAP(ret,{rFile=CApaAppInfoFileReader::NewLC(testFile,KUidTest1App);
				CleanupStack::Pop();
				});
		test(ret==KErrNone);
		CleanupStack::PushL(rFile);
		test(rFile->CaptionL(ELangEnglish)==KTest1DefaultCaption);
	TRAP(ret,rFile->CreateMaskedBitmapL(32));
		test(ret==KErrNotFound);
	CleanupStack::PopAndDestroy();			// rFile;
// Test Captions and icons
	test.Next(_L("Testing captions"));
	TRAP(ret,{wFile=CApaAppInfoFileWriter::NewLC(testFile,KUidTest1App);
				CleanupStack::Pop();
				});
		test(ret==KErrNone);
		CleanupStack::PushL(wFile);
	TRAP(ret,wFile->AddCaptionL(ELangEnglish,KTest1EnglishCaption));
		test(ret==KErrNone);
	TRAP(ret,wFile->AddCaptionL(ELangFrench,KTest1FrenchCaption));
		test(ret==KErrNone);
	TestLongFileName(*wFile);
	test.Next(_L("Testing icons"));
	TRAP(ret,wFile->AddIconL(_L("z:\\system\\apps\\tstapp\\icon16.mbm")));
		test(ret==KErrNone);
	TRAP(ret,wFile->AddIconL(_L("z:\\system\\apps\\tstapp\\icon64.mbm")));
		test(ret==KErrNone);
	TRAP(ret,wFile->StoreL());
		test(ret==KErrNone);
	CleanupStack::PopAndDestroy();	// wFile;
	TRAP(ret,{rFile=CApaAppInfoFileReader::NewLC(testFile,KUidTest1App);
				CleanupStack::Pop();
				});
		test(ret==KErrNone);
		CleanupStack::PushL(wFile);
		test(rFile->CaptionL(ELangEnglish)==KTest1EnglishCaption);
		test(rFile->CaptionL(ELangFrench)==KTest1FrenchCaption);
		test(rFile->CaptionL(ELangGerman)==KTest1DefaultCaption);
	CApaMaskedBitmap* icon=NULL;
	TRAP(ret,icon=rFile->CreateMaskedBitmapL(16));
		test(ret==KErrNone);
		test(icon->SizeInPixels().iWidth==16);
	delete icon;
// Test stretching icons
//	test.Next(_L("Test stretching icons"));
//	TRAP(ret,icon=rFile->CreateMaskedBitmapL(100));
//	if (ret!=KErrNone)
//		test.Printf(_L(" error: %d\n"),ret);
//		test(ret==KErrNone);
//		test(icon->SizeInPixels().iWidth==100);
//	delete icon;
	CleanupStack::PopAndDestroy();	// rFile;
	}

LOCAL_C void setupCleanup()
//
// Initialise the cleanup stack.
//
    {
	TheTrapCleanup=CTrapCleanup::New();
	TRAPD(r,\
		{\
		for (TInt i=KTestCleanupStack;i>0;i--)\
			CleanupStack::PushL((TAny*)1);\
		CleanupStack::Pop(KTestCleanupStack);\
		});
	}


GLDEF_C TInt E32Main()
	{
	setupCleanup();
	test.Title();
	//
	// set up an fbs
	FbsStartup();
	TInt ret=RFbsSession::Connect();
	if (ret!=KErrNone)
		__DEBUGGER();
//	TheMap = new(ELeave) CTestDeviceMap(TPoint(10,10));
	//
	// run the tool
	test.Start(_L("Creating an icon file..."));
	TRAPD(r,CreateIconFileL());
		test(r==KErrNone);

	test.Next(_L("Testing the icon file..."));
	TRAP(r,TestIconFileL());
		test(r==KErrNone);

	test.Next(_L("Testing CApaAppInfoFile..."));
	TRAP(r,BillsTestsL());
		test(r==KErrNone);

 	test.End();
	test.Close();

//	delete TheMap;
	delete TheTrapCleanup;
	return KErrNone;
	}


