os/graphics/graphicsresourceservices/graphicsresourceimplementation/test/src/tgraphicsresourcesecondprocess.cpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/graphics/graphicsresourceservices/graphicsresourceimplementation/test/src/tgraphicsresourcesecondprocess.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,396 @@
     1.4 +// Copyright (c) 2007-2010 Nokia Corporation and/or its subsidiary(-ies).
     1.5 +// All rights reserved.
     1.6 +// This component and the accompanying materials are made available
     1.7 +// under the terms of "Eclipse Public License v1.0"
     1.8 +// which accompanies this distribution, and is available
     1.9 +// at the URL "http://www.eclipse.org/legal/epl-v10.html".
    1.10 +//
    1.11 +// Initial Contributors:
    1.12 +// Nokia Corporation - initial contribution.
    1.13 +//
    1.14 +// Contributors:
    1.15 +//
    1.16 +// Description:
    1.17 +//
    1.18 +
    1.19 +/**
    1.20 + @file
    1.21 + @test
    1.22 + @internalComponent - Graphics Resource API Conformance Test Suite
    1.23 +*/
    1.24 +
    1.25 +#include <e32debug.h>
    1.26 +#include "tgraphicsresourcemultiprocessthread.h"
    1.27 +#include "tgraphicsresourceteststepbase.h"
    1.28 +
    1.29 +/**
    1.30 +Helper function to test the equivalence of two TSgImageInfo structures.
    1.31 +
    1.32 +@param   aInfo1 A TSgImageInfo structure to compare.
    1.33 +@param   aInfo2 A TSgImageInfo structure to compare.
    1.34 +
    1.35 +@return  ETrue if the two are identical, EFalse otherwise.
    1.36 +*/
    1.37 +TBool CompareInfos(TSgImageInfo& aInfo1, TSgImageInfo& aInfo2)
    1.38 +	{
    1.39 +	TBool result = EFalse;
    1.40 +	if(aInfo1.iPixelFormat == aInfo2.iPixelFormat
    1.41 +		&& aInfo1.iSizeInPixels == aInfo2.iSizeInPixels
    1.42 +		&& aInfo1.iUsage == aInfo2.iUsage)
    1.43 +		{
    1.44 +		result = ETrue;
    1.45 +		}
    1.46 +	return result;
    1.47 +	}
    1.48 +
    1.49 +/**
    1.50 +Opens an image in a different process.
    1.51 +@param aInfo The test information passed from outside the current thread
    1.52 +@return The test result indicating which tests passed
    1.53 +*/
    1.54 +TInt TestOpenImageL(TSgProcessTestInfo& aInfo)
    1.55 +	{
    1.56 +	TSgDrawableId id = aInfo.iDrawableId;
    1.57 +	TSgImageInfo imageInfo1 = aInfo.iImageInfo;
    1.58 +	
    1.59 +	RSgImage image;
    1.60 +	TInt result = 0;
    1.61 +
    1.62 +	if(KErrNone == image.Open(id))
    1.63 +		{
    1.64 +		result |=  EFirstTestPassed;
    1.65 +		}
    1.66 +	
    1.67 +	TSgImageInfo imageInfo2;
    1.68 +	TInt attribVal = KMaxTInt;
    1.69 +	if(KErrNone == image.GetInfo(imageInfo2))
    1.70 +		{
    1.71 +		result |= ESecondTestPassed;
    1.72 +		}
    1.73 +	if(CompareInfos(imageInfo1, imageInfo2))
    1.74 +		{
    1.75 +		result |= EThirdTestPassed;
    1.76 +		}
    1.77 +	if (image.Id() != KSgNullDrawableId)
    1.78 +		{
    1.79 +		result |= EFourthTestPassed;
    1.80 +		}
    1.81 +	if(image.Id() == id)
    1.82 +		{
    1.83 +		result |= EFifthTestPassed;
    1.84 +		}
    1.85 +	TUid uid = { 0x12345678 };
    1.86 +	if (KErrNotSupported == image.GetAttribute(uid, attribVal))
    1.87 +	    {
    1.88 +	    result |= ESixthTestPassed;
    1.89 +	    }
    1.90 +	if (KErrArgument == image.GetAttribute(KNullUid, attribVal))
    1.91 +	    {
    1.92 +	    result |= ESeventhTestPassed;
    1.93 +	    }
    1.94 +	image.Close();
    1.95 +
    1.96 +	return result;
    1.97 +	}
    1.98 +
    1.99 +/**
   1.100 +Opens an image in a different process into a RSgDrawable object.
   1.101 +@param aInfo The test information passed from outside the current thread
   1.102 +@return The test result indicating which tests passed
   1.103 +*/
   1.104 +TInt TestOpenDrawableL(TSgProcessTestInfo& aInfo)
   1.105 +	{
   1.106 +	TSgDrawableId id = aInfo.iDrawableId;
   1.107 +	
   1.108 +	RSgDrawable drawable;
   1.109 +	TInt result = 0;
   1.110 +	
   1.111 +	if(KErrNone == drawable.Open(id))
   1.112 +		{
   1.113 +		result |=  EFirstTestPassed;
   1.114 +		}
   1.115 +	TSgDrawableId id2 = drawable.Id();
   1.116 +	if(id2 != KSgNullDrawableId)
   1.117 +		{
   1.118 +		result |= ESecondTestPassed;
   1.119 +		}
   1.120 +	if(id2 == id)
   1.121 +		{
   1.122 +		result |= EThirdTestPassed;
   1.123 +		}
   1.124 +
   1.125 +	drawable.Close();
   1.126 +	return result;
   1.127 +	}
   1.128 +
   1.129 +/**
   1.130 +Opens an image in a different process with different invalid operations.
   1.131 +@param aInfo The test information passed from outside the current thread
   1.132 +@return The test result indicating which tests passed
   1.133 +*/
   1.134 +TInt TestOpenImageInvalidL(TSgProcessTestInfo& aInfo)
   1.135 +	{	
   1.136 +	TSgDrawableId id = aInfo.iDrawableId;
   1.137 +	TSgImageInfo imageInfo = aInfo.iImageInfo;
   1.138 +	
   1.139 +	RSgImage image;
   1.140 +	TInt result = 0;
   1.141 +	
   1.142 +	//create image
   1.143 +	if(KErrNone == image.Create(imageInfo, NULL, 0))
   1.144 +		{
   1.145 +		result |= EFirstTestPassed;
   1.146 +		}
   1.147 +	//  non-empty handle
   1.148 +	if(KErrInUse == image.Open(id))
   1.149 +		{
   1.150 +		result |= ESecondTestPassed;
   1.151 +		}
   1.152 +	image.Close();
   1.153 +	
   1.154 +	//  null drawable id	
   1.155 +	if(KErrArgument == image.Open(KSgNullDrawableId))
   1.156 +		{
   1.157 +		result |= EThirdTestPassed;
   1.158 +		}
   1.159 +	image.Close();
   1.160 +	
   1.161 +	//  non-existing drawable id
   1.162 +	TSgDrawableId fakeid = {0xFFFFFFFFFFFFFFFFU};
   1.163 +	if(KErrNotFound == image.Open(fakeid))
   1.164 +		{
   1.165 +		result |= EFourthTestPassed;
   1.166 +		}
   1.167 +	image.Close();
   1.168 +	
   1.169 +	//Valid Drawable Id
   1.170 +	if (KErrNone == image.Open(id))
   1.171 +		{
   1.172 +		result |= EFifthTestPassed;
   1.173 +		}
   1.174 +	image.Close();
   1.175 +	return result;
   1.176 +	}
   1.177 +
   1.178 +/**
   1.179 +Opens a drawable in a different process with different invalid operations.
   1.180 +@param aInfo The test information passed from outside the current thread
   1.181 +@return The test result indicating which tests passed
   1.182 +*/
   1.183 +TInt TestOpenDrawableInvalidL()
   1.184 +	{	
   1.185 +	RSgDrawable  drawable;
   1.186 +	TInt result = 0;
   1.187 +	
   1.188 +	//null drawable id	
   1.189 +	if(KErrArgument == drawable.Open(KSgNullDrawableId))
   1.190 +		{
   1.191 +		result |= EFirstTestPassed;
   1.192 +		}
   1.193 +	drawable.Close();
   1.194 +	
   1.195 +	//non-existing drawable id
   1.196 +	TSgDrawableId fakeid = {0xFFFFFFFFFFFFFFFFU};
   1.197 +	if(KErrNotFound == drawable.Open(fakeid))
   1.198 +		{
   1.199 +		result |= ESecondTestPassed;
   1.200 +		}
   1.201 +	drawable.Close();
   1.202 +		
   1.203 +	//non-empty handle
   1.204 +	//create an image
   1.205 +	TSgImageInfo info1;
   1.206 +	info1.iSizeInPixels = TSize(8, 8);
   1.207 +	info1.iUsage = ESgUsageBitOpenVgImage;
   1.208 +	info1.iPixelFormat = EUidPixelFormatRGB_565;
   1.209 +
   1.210 +	
   1.211 +	RSgImage image;
   1.212 +	TInt err = image.Create(info1, NULL, 0);
   1.213 +	if(KErrNoMemory == err)
   1.214 +		{
   1.215 +		result = KErrNoMemory;
   1.216 +		}
   1.217 +	else if(KErrNone != err)
   1.218 +		{
   1.219 +		result = err;
   1.220 +		}
   1.221 +	else
   1.222 +		{		
   1.223 +		if(KErrNone == drawable.Open(image.Id()))
   1.224 +			{
   1.225 +			result |= EThirdTestPassed;
   1.226 +			
   1.227 +			if (KErrInUse == drawable.Open(image.Id()))
   1.228 +				{
   1.229 +				result |= EFourthTestPassed;
   1.230 +				}
   1.231 +			}		
   1.232 +		drawable.Close();
   1.233 +		}
   1.234 +	image.Close();
   1.235 +	return result;
   1.236 +	}
   1.237 +
   1.238 +TInt TestCloseDriverOpenResources(RSgDriver& aDriver)
   1.239 +	{
   1.240 +	RSgImage image;
   1.241 +	TSgImageInfo info1;
   1.242 +	info1.iSizeInPixels = TSize(8, 8);
   1.243 +	info1.iUsage = ESgUsageBitOpenVgImage;
   1.244 +	info1.iPixelFormat = EUidPixelFormatRGB_565;
   1.245 +	
   1.246 +	TInt result = image.Create(info1, KCrossImageData, KCrossImageDataStride);
   1.247 +	
   1.248 +	if (result == KErrNone)
   1.249 +		{
   1.250 +		//Close the driver without closing the image.
   1.251 +		aDriver.Close(); //Should panic with SGRES2
   1.252 +		}
   1.253 +	
   1.254 +	return result;
   1.255 +	}
   1.256 +
   1.257 +/**
   1.258 +Method executed by secondary thread for test TestOpenImageMulththreadedL
   1.259 +*/
   1.260 +_LIT(KTestOpenImageMultithreadedSem1, "TestOpenImageMultithreadedSem1");
   1.261 +_LIT(KTestOpenImageMultithreadedSem2, "TestOpenImageMultithreadedSem2");
   1.262 +
   1.263 +TInt OpenImageMultiSecondThread(TAny* aAny)
   1.264 +	{
   1.265 +	TInt err = KErrNone;
   1.266 +	TSgProcessTestInfo* info = static_cast<TSgProcessTestInfo*>(aAny);	
   1.267 +
   1.268 +	RSemaphore sem[2];
   1.269 +	err = sem[0].OpenGlobal(KTestOpenImageMultithreadedSem1, EOwnerThread);
   1.270 +	if (err != KErrNone)
   1.271 +		{
   1.272 +		return err;
   1.273 +		}
   1.274 +	err = sem[1].OpenGlobal(KTestOpenImageMultithreadedSem2, EOwnerThread);
   1.275 +	if (err != KErrNone)
   1.276 +		{
   1.277 +		sem[0].Close();
   1.278 +		return err;
   1.279 +		}
   1.280 +
   1.281 +	RSgImage sgImage; 
   1.282 +	err = sgImage.Open(info->iDrawableId);
   1.283 +
   1.284 +	sem[0].Signal();
   1.285 +	sem[1].Wait();
   1.286 +
   1.287 +	sgImage.Close();	
   1.288 +	sem[0].Signal();
   1.289 +	return err;	
   1.290 +	}
   1.291 +
   1.292 +/**
   1.293 +Creates a second thread which will initially open a handle to the passed TSgDrawableId.
   1.294 +The main thread then opens a new handle to the image.
   1.295 +The second thread will then close its handle to the image.
   1.296 +The main thread will then attempt to access the data of the image.
   1.297 + */
   1.298 +TInt TestOpenImageMultithreadedL(TSgProcessTestInfo& aInfo)
   1.299 +	{
   1.300 +	TInt result = 0;
   1.301 +	
   1.302 +	//create two semaphores
   1.303 +	RSemaphore sem[2];
   1.304 +	User::LeaveIfError(sem[0].CreateGlobal(KTestOpenImageMultithreadedSem1, 0, EOwnerThread));
   1.305 +	CleanupClosePushL(sem[0]);
   1.306 +	User::LeaveIfError(sem[1].CreateGlobal(KTestOpenImageMultithreadedSem2, 0, EOwnerThread));
   1.307 +	CleanupClosePushL(sem[1]);
   1.308 +		
   1.309 +	//create secondary thread
   1.310 +	_LIT(KSecondaryThreadName, "TestOpenImageMultithreadedL");
   1.311 +	RThread thread;
   1.312 +	User::LeaveIfError(thread.Create(KSecondaryThreadName, OpenImageMultiSecondThread, KDefaultStackSize, KSecondThreadMinHeapSize, KSecondThreadMaxHeapSize, &aInfo));
   1.313 +	thread.Resume();
   1.314 +	
   1.315 +	// Make the second thread open the image before this thread.
   1.316 +	sem[0].Wait();
   1.317 +
   1.318 +	// Second thread has opened image, now primary thread opens image
   1.319 +	RSgImage sgImage;
   1.320 +	TInt err = sgImage.Open(aInfo.iDrawableId);
   1.321 +	CleanupClosePushL(sgImage);
   1.322 +	sem[1].Signal();
   1.323 +	sem[0].Wait();
   1.324 +		
   1.325 +	// Second thread has closed image and terminated, now wait for thread to clean-up
   1.326 +	User::After(100000);
   1.327 +
   1.328 +	if (err == KErrNone)
   1.329 +		{
   1.330 +		// Do something that requires data access of sgImage, in this case, creating a copy.
   1.331 +		result |= EFirstTestPassed;
   1.332 +		RSgImage sgImageCopy;
   1.333 +		err = sgImageCopy.Create(aInfo.iImageInfo, sgImage);
   1.334 +		sgImageCopy.Close();
   1.335 +		if (err == KErrNone)
   1.336 +			{
   1.337 +			result |= ESecondTestPassed;
   1.338 +			}	
   1.339 +		}
   1.340 +	
   1.341 +	CleanupStack::PopAndDestroy(3); // sgImage, sem[0], sem[1]		
   1.342 +	return result;	
   1.343 +	}
   1.344 +
   1.345 +TInt MainL()
   1.346 +	{				
   1.347 +	TPckgBuf<TSgProcessTestInfo> infoPkg;
   1.348 +	User::LeaveIfError(User::GetDesParameter(KSecondProcessParametersSlot, infoPkg));
   1.349 +	TSgProcessTestInfo& info = infoPkg();
   1.350 +	TSgresTestCase testCase = info.iTestCase;
   1.351 +	TInt result = 0;
   1.352 +
   1.353 +	RSgDriver sgDriver;
   1.354 +	CleanupClosePushL(sgDriver);
   1.355 +	
   1.356 +	if(KErrNone == sgDriver.Open())
   1.357 +		{
   1.358 +		switch(testCase)
   1.359 +			{
   1.360 +			case ESgresSecondProcessOpenImage:
   1.361 +				result = TestOpenImageL(info);
   1.362 +				break;
   1.363 +			case ESgresSecondProcessOpenDrawable:
   1.364 +				result = TestOpenDrawableL(info);
   1.365 +				break;
   1.366 +			case ESgresSecondProcessOpenImageInvalid:
   1.367 +				result = TestOpenImageInvalidL(info);
   1.368 +				break;
   1.369 +			case ESgresSecondProcessOpenDrawableInvalid:
   1.370 +				result = TestOpenDrawableInvalidL();
   1.371 +				break;
   1.372 +			case ESgresSecondProcessPanicDriverCloseOpenResources:
   1.373 +				result = TestCloseDriverOpenResources(sgDriver);
   1.374 +				break;
   1.375 +			case ESgresSecondProcessOpenImageMultithreaded:
   1.376 +				result = TestOpenImageMultithreadedL(info);
   1.377 +				break;
   1.378 +			}
   1.379 +		}	
   1.380 +	
   1.381 +	CleanupStack::PopAndDestroy(&sgDriver);
   1.382 +	
   1.383 +	return result;
   1.384 +	}
   1.385 +
   1.386 +GLDEF_C TInt E32Main()
   1.387 +	{
   1.388 +	__UHEAP_MARK;
   1.389 +	CTrapCleanup* cleanupStack = CTrapCleanup::New();
   1.390 +	if(cleanupStack == NULL)
   1.391 +		{
   1.392 +		return KErrNoMemory;
   1.393 +		}
   1.394 +	TInt ret = 0;
   1.395 +	TRAP_IGNORE(ret=MainL());
   1.396 +	delete cleanupStack;
   1.397 +	__UHEAP_MARKEND;
   1.398 +	return ret;
   1.399 +	}