// TSERVCOM.CPP
//
// Copyright (c) 1997-1999 Symbian Ltd.  All rights reserved.
//

//
// Common AppArc server test code
//

#include <f32file.h>
#include <fbs.h>
#include <apaid.h>
#include <apffndr.h>
#include <apgaplst.h>
#include <apaflrec.h>
#include <apgcli.h>
#include <apacmdln.h>
#include <apsserv.h>
#include <apfrec.h>
#include <datastor.h>
#include <apgicnfl.h>
#include <apasvst.h>
#include "tstapp.h"
//
#include <e32test.h>

LOCAL_D TApaAppCaption KAppCaption=_L("Test App");
LOCAL_D TFullName appPath=_L("z:\\system\\apps\\tstapp\\tstapp.app");
LOCAL_D TFullName exePath=_L("z:\\system\\programs\\texe.dxe");

LOCAL_C void AppInfoTest1(RApaLsSession& aLs)
	{
	// get the first app
	TApaAppInfo info;
	TInt ret = aLs.GetNextApp(info);
		test(ret==KErrNone);
		test(info.iUid==KUidTestApp);
//		test.Printf(info.iFullName);
		test(info.iFullName.CompareF(appPath)==0);
		test(info.iCaption.CompareF(KAppCaption)==0);
	}


LOCAL_C void AppInfoTest2(RApaLsSession& aLs)
	{
	// try to get another app (there arn't any more)
	TApaAppInfo info;
	TInt ret = aLs.GetNextApp(info);
		test(ret==RApaLsSession::ENoMoreAppsInList);
	//
	// get a list of all apps
	ret = aLs.GetAllApps();
		test(ret==KErrNone);
	}

LOCAL_C void AppInfoTest3(RApaLsSession& aLs)
	{
	// get the first app...
	TApaAppInfo info;
	TInt ret = aLs.GetNextApp(info);
		test(ret==KErrNone);
		test(info.iUid==KUidTestApp);
		test(info.iFullName.CompareF(appPath)==0);
		test(info.iCaption.CompareF(KAppCaption)==0);
	}

LOCAL_C void AppInfoTest4(RApaLsSession& aLs)
	{
	// Get info for an app that exists
	TApaAppInfo info;
	TInt ret = aLs.GetAppInfo(info,KUidTestApp);
		test(ret==KErrNone);
		test(info.iUid==KUidTestApp);
		test(info.iFullName.CompareF(appPath)==0);
		test(info.iCaption.CompareF(KAppCaption)==0);
	}

LOCAL_C void AppInfoTest5(RApaLsSession& aLs)
	{
	// get info for a non-existant app
	TApaAppInfo info;
	TInt ret = aLs.GetAppInfo(info,KNullUid);
		test(ret==KErrNotFound);
	}
		
LOCAL_C void DoAppInfoTests(RApaLsSession& aLs)
	{
	AppInfoTest1(aLs);
	AppInfoTest2(aLs);
	AppInfoTest3(aLs);
	AppInfoTest4(aLs);
	AppInfoTest5(aLs);
	}

LOCAL_C void DoStartAppTestsL(RApaLsSession& aLs)
	{
	test.Next(_L("Checking application launching"));
	//
	// start the test exe
	CApaCommandLine* cmdLn=CApaCommandLine::NewLC();
	cmdLn->SetLibraryNameL(exePath);
	cmdLn->SetTailEndL(_L8("dogfish"));
	TInt ret = aLs.StartApp(*cmdLn); // explicit
		test(ret==KErrNone);
	//
	// start a non-existant app
	cmdLn->SetLibraryNameL(_L("wibble"));
	ret = aLs.StartApp(*cmdLn);
		test(ret==KErrNotFound);
	//
	//
	CleanupStack::PopAndDestroy(); // cmdLn
	//
	// Create some bindings for the mime type recognition
	// first delete any ini file
	TheFs.Delete(_L("c:\\system\\data\\dtstor.ini"));
	CTypeStoreManager* mman = CTypeStoreManager::NewL(TheFs);
	CleanupStack::PushL(mman);
	TUid testUid={KTestAppUidValue};
	mman->InsertDataMappingL(TDataType(_L8("text/plain")),0,testUid);
	mman->StoreL();
	CleanupStack::PopAndDestroy(); // mman
	// Test checking this binding
	TUid uid;
	ret=aLs.AppForDataType(TDataType(_L8("text/plain")),uid);
	test(ret==KErrNone);
	test(uid==testUid);
	// these are the iDataStoreManager in the server, and 2 allocs in Store constucting that
	uid.iUid=0;
	TDataType data;
	ret=aLs.AppForDocument(_L("z:\\system\\data\\sdk.txt"),uid,data);
	test(ret==KErrNone);
	test(uid==testUid);
	// Test starting a text file
	TThreadId id;
	ret=aLs.StartDocument(_L("z:\\system\\data\\sdk.txt"),id);
	test(ret==KErrNone);
	// Test starting a non existent file
	ret=aLs.StartDocument(_L("z:\\system\\data\\not_here_thanks.foo"),id);
	test(ret==KErrNotFound);
	// Test starting a file with a known data type
	ret=aLs.StartDocument(_L("z:\\system\\data\\sdk.txt"),TDataType(_L8("text/plain")),id);
	test(ret==KErrNone);
	// Test starting a file with a known data type that we don't have a binding for
	ret=aLs.StartDocument(_L("z:\\system\\data\\sdk.txt"),TDataType(_L8("text/plain/no thanks")),id);
	test(ret==KErrNotFound);
	// Test starting a file with a known app Uid
	ret=aLs.StartDocument(_L("z:\\system\\data\\sdk.txt"),testUid,id);
	test(ret==KErrNone);
	//
	}

LOCAL_C void DoEnquiryTestsL(RApaLsSession& aLs)
	{
	TInt temp=1234;
	TInt ret=aLs.SetAcceptedConfidence(temp);
	test(ret==KErrNone);
	temp=0;
	ret=aLs.GetAcceptedConfidence(temp);
	test(ret==KErrNone);
	test(temp==1234);
	ret=aLs.SetMaxDataBufSize(temp);
	test(ret==KErrNone);
	temp=0;
	ret=aLs.GetMaxDataBufSize(temp);
	CDataTypeArray* dataTypes=new(ELeave) CDataTypeArray(5);
	CleanupStack::PushL(dataTypes);
	ret=aLs.GetSupportedDataTypesL(*dataTypes);
	test(ret==KErrNone);
	test(dataTypes->Count()==4);
	CleanupStack::PopAndDestroy(); // dataTypes
	}


GLDEF_C TInt DoTestL()
	{
	test.Next(_L("Interogating the server"));
	// RDebug::Print(_L("Interogating the server"));
	//	
	// Connect to the server
    RApaLsSession ls;
	TInt ret = ls.Connect();
		test(ret==KErrNone);
	//
	// get the full app count
	TInt count=0;
	ret = ls.AppCount(count);
		test(ret==KErrNone);
		test(count==1);
	//
	// get the embedded app count
	count=0;
	ret = ls.EmbeddableAppCount(count);
		test(ret==KErrNone);
		test(count==1);
	//
	// prepare to get a list of embeddable apps
	ret = ls.GetEmbeddableApps();
		test(ret==KErrNone);
	//
	DoAppInfoTests(ls);
	//
	// Get the capability of an app that exists
	TApaAppCapabilityBuf buf;
	ret = ls.GetAppCapability(buf,KUidTestApp);
		test(ret==KErrNone);
	TApaAppCapability cap=buf();
	test(cap.iEmbeddability==TApaAppCapability::EEmbeddable);
		test(!cap.iSupportsNewFile);
		test(!cap.iAppIsHidden);
	// 
	// get the capability of a non-existant app
	ret = ls.GetAppCapability(buf,KNullUid);
		test(ret==KErrNotFound);
	//
	test.Next(_L("Checking data type information"));
	TBool isText;
	ret=ls.RecognizeSpecificData(_L("a file name.txt"),_L8("Some plain text"),TDataType(_L8("text/plain")),isText);
		test(ret==KErrNone);
		test(isText);
	TDataRecognitionResult result;
	ret=ls.RecognizeData(_L("a file name.txt"),_L8("Some plain text"),result);
		test(ret==KErrNone);
		test(result.iDataType==TDataType(_L8("text/plain")));
		test(result.iConfidence==CApaDataRecognizerType::EProbable);
	ret=ls.RecognizeSpecificData(_L("a file name.txt"),_L8("Some plain text"),TDataType(_L8("foo/bar")),isText);
		test(ret==KErrNone);
		test(!isText);

	test.Next(_L("Checking icon loading"));
	CApaMaskedBitmap* icon=CApaMaskedBitmap::NewLC();
	ret=ls.GetAppIcon(KUidTestApp,0,*icon);
		test(ret==KErrNone);
		test(icon->SizeInPixels()==TSize(16,16));
	ret=ls.GetAppIcon(KUidTestApp,1,*icon);
		test(ret==KErrNone);
		test(icon->SizeInPixels()==TSize(16,16));
	ret=ls.GetAppIcon(KUidTestApp,2,*icon);
		test(ret==KErrNone);
		test(icon->SizeInPixels()==TSize(16,16));
		TUid uid={9999};
	ret=ls.GetAppIcon(uid,0,*icon);
		test(ret==KErrNotFound);
	CleanupStack::PopAndDestroy(); // icon

	//
	DoStartAppTestsL(ls);
	
	DoEnquiryTestsL(ls);
	//
	// close the server session
	// RDebug::Print(_L("Tidying up"));
	// and we have done some type store reloading
	ls.Close();
	// RDebug::Print(_L("Finished"));
	return KErrNone;
	}
