graphicsresourceservices/graphicsresourceimplementation/test/src/tgraphicsresourcesecondprocess.cpp
branchRCL_3
changeset 163 bbf46f59e123
equal deleted inserted replaced
150:57c618273d5c 163:bbf46f59e123
       
     1 // Copyright (c) 2007-2010 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 //
       
    15 
       
    16 /**
       
    17  @file
       
    18  @test
       
    19  @internalComponent - Graphics Resource API Conformance Test Suite
       
    20 */
       
    21 
       
    22 #include <e32debug.h>
       
    23 #include "tgraphicsresourcemultiprocessthread.h"
       
    24 #include "tgraphicsresourceteststepbase.h"
       
    25 
       
    26 /**
       
    27 Helper function to test the equivalence of two TSgImageInfo structures.
       
    28 
       
    29 @param   aInfo1 A TSgImageInfo structure to compare.
       
    30 @param   aInfo2 A TSgImageInfo structure to compare.
       
    31 
       
    32 @return  ETrue if the two are identical, EFalse otherwise.
       
    33 */
       
    34 TBool CompareInfos(TSgImageInfo& aInfo1, TSgImageInfo& aInfo2)
       
    35 	{
       
    36 	TBool result = EFalse;
       
    37 	if(aInfo1.iPixelFormat == aInfo2.iPixelFormat
       
    38 		&& aInfo1.iSizeInPixels == aInfo2.iSizeInPixels
       
    39 		&& aInfo1.iUsage == aInfo2.iUsage)
       
    40 		{
       
    41 		result = ETrue;
       
    42 		}
       
    43 	return result;
       
    44 	}
       
    45 
       
    46 /**
       
    47 Opens an image in a different process.
       
    48 @param aInfo The test information passed from outside the current thread
       
    49 @return The test result indicating which tests passed
       
    50 */
       
    51 TInt TestOpenImageL(TSgProcessTestInfo& aInfo)
       
    52 	{
       
    53 	TSgDrawableId id = aInfo.iDrawableId;
       
    54 	TSgImageInfo imageInfo1 = aInfo.iImageInfo;
       
    55 	
       
    56 	RSgImage image;
       
    57 	TInt result = 0;
       
    58 
       
    59 	if(KErrNone == image.Open(id))
       
    60 		{
       
    61 		result |=  EFirstTestPassed;
       
    62 		}
       
    63 	
       
    64 	TSgImageInfo imageInfo2;
       
    65 	TInt attribVal = KMaxTInt;
       
    66 	if(KErrNone == image.GetInfo(imageInfo2))
       
    67 		{
       
    68 		result |= ESecondTestPassed;
       
    69 		}
       
    70 	if(CompareInfos(imageInfo1, imageInfo2))
       
    71 		{
       
    72 		result |= EThirdTestPassed;
       
    73 		}
       
    74 	if (image.Id() != KSgNullDrawableId)
       
    75 		{
       
    76 		result |= EFourthTestPassed;
       
    77 		}
       
    78 	if(image.Id() == id)
       
    79 		{
       
    80 		result |= EFifthTestPassed;
       
    81 		}
       
    82 	TUid uid = { 0x12345678 };
       
    83 	if (KErrNotSupported == image.GetAttribute(uid, attribVal))
       
    84 	    {
       
    85 	    result |= ESixthTestPassed;
       
    86 	    }
       
    87 	if (KErrArgument == image.GetAttribute(KNullUid, attribVal))
       
    88 	    {
       
    89 	    result |= ESeventhTestPassed;
       
    90 	    }
       
    91 	image.Close();
       
    92 
       
    93 	return result;
       
    94 	}
       
    95 
       
    96 /**
       
    97 Opens an image in a different process into a RSgDrawable object.
       
    98 @param aInfo The test information passed from outside the current thread
       
    99 @return The test result indicating which tests passed
       
   100 */
       
   101 TInt TestOpenDrawableL(TSgProcessTestInfo& aInfo)
       
   102 	{
       
   103 	TSgDrawableId id = aInfo.iDrawableId;
       
   104 	
       
   105 	RSgDrawable drawable;
       
   106 	TInt result = 0;
       
   107 	
       
   108 	if(KErrNone == drawable.Open(id))
       
   109 		{
       
   110 		result |=  EFirstTestPassed;
       
   111 		}
       
   112 	TSgDrawableId id2 = drawable.Id();
       
   113 	if(id2 != KSgNullDrawableId)
       
   114 		{
       
   115 		result |= ESecondTestPassed;
       
   116 		}
       
   117 	if(id2 == id)
       
   118 		{
       
   119 		result |= EThirdTestPassed;
       
   120 		}
       
   121 
       
   122 	drawable.Close();
       
   123 	return result;
       
   124 	}
       
   125 
       
   126 /**
       
   127 Opens an image in a different process with different invalid operations.
       
   128 @param aInfo The test information passed from outside the current thread
       
   129 @return The test result indicating which tests passed
       
   130 */
       
   131 TInt TestOpenImageInvalidL(TSgProcessTestInfo& aInfo)
       
   132 	{	
       
   133 	TSgDrawableId id = aInfo.iDrawableId;
       
   134 	TSgImageInfo imageInfo = aInfo.iImageInfo;
       
   135 	
       
   136 	RSgImage image;
       
   137 	TInt result = 0;
       
   138 	
       
   139 	//create image
       
   140 	if(KErrNone == image.Create(imageInfo, NULL, 0))
       
   141 		{
       
   142 		result |= EFirstTestPassed;
       
   143 		}
       
   144 	//  non-empty handle
       
   145 	if(KErrInUse == image.Open(id))
       
   146 		{
       
   147 		result |= ESecondTestPassed;
       
   148 		}
       
   149 	image.Close();
       
   150 	
       
   151 	//  null drawable id	
       
   152 	if(KErrArgument == image.Open(KSgNullDrawableId))
       
   153 		{
       
   154 		result |= EThirdTestPassed;
       
   155 		}
       
   156 	image.Close();
       
   157 	
       
   158 	//  non-existing drawable id
       
   159 	TSgDrawableId fakeid = {0xFFFFFFFFFFFFFFFFU};
       
   160 	if(KErrNotFound == image.Open(fakeid))
       
   161 		{
       
   162 		result |= EFourthTestPassed;
       
   163 		}
       
   164 	image.Close();
       
   165 	
       
   166 	//Valid Drawable Id
       
   167 	if (KErrNone == image.Open(id))
       
   168 		{
       
   169 		result |= EFifthTestPassed;
       
   170 		}
       
   171 	image.Close();
       
   172 	return result;
       
   173 	}
       
   174 
       
   175 /**
       
   176 Opens a drawable in a different process with different invalid operations.
       
   177 @param aInfo The test information passed from outside the current thread
       
   178 @return The test result indicating which tests passed
       
   179 */
       
   180 TInt TestOpenDrawableInvalidL()
       
   181 	{	
       
   182 	RSgDrawable  drawable;
       
   183 	TInt result = 0;
       
   184 	
       
   185 	//null drawable id	
       
   186 	if(KErrArgument == drawable.Open(KSgNullDrawableId))
       
   187 		{
       
   188 		result |= EFirstTestPassed;
       
   189 		}
       
   190 	drawable.Close();
       
   191 	
       
   192 	//non-existing drawable id
       
   193 	TSgDrawableId fakeid = {0xFFFFFFFFFFFFFFFFU};
       
   194 	if(KErrNotFound == drawable.Open(fakeid))
       
   195 		{
       
   196 		result |= ESecondTestPassed;
       
   197 		}
       
   198 	drawable.Close();
       
   199 		
       
   200 	//non-empty handle
       
   201 	//create an image
       
   202 	TSgImageInfo info1;
       
   203 	info1.iSizeInPixels = TSize(8, 8);
       
   204 	info1.iUsage = ESgUsageBitOpenVgImage;
       
   205 	info1.iPixelFormat = EUidPixelFormatRGB_565;
       
   206 
       
   207 	
       
   208 	RSgImage image;
       
   209 	TInt err = image.Create(info1, NULL, 0);
       
   210 	if(KErrNoMemory == err)
       
   211 		{
       
   212 		result = KErrNoMemory;
       
   213 		}
       
   214 	else if(KErrNone != err)
       
   215 		{
       
   216 		result = err;
       
   217 		}
       
   218 	else
       
   219 		{		
       
   220 		if(KErrNone == drawable.Open(image.Id()))
       
   221 			{
       
   222 			result |= EThirdTestPassed;
       
   223 			
       
   224 			if (KErrInUse == drawable.Open(image.Id()))
       
   225 				{
       
   226 				result |= EFourthTestPassed;
       
   227 				}
       
   228 			}		
       
   229 		drawable.Close();
       
   230 		}
       
   231 	image.Close();
       
   232 	return result;
       
   233 	}
       
   234 
       
   235 TInt TestCloseDriverOpenResources(RSgDriver& aDriver)
       
   236 	{
       
   237 	RSgImage image;
       
   238 	TSgImageInfo info1;
       
   239 	info1.iSizeInPixels = TSize(8, 8);
       
   240 	info1.iUsage = ESgUsageBitOpenVgImage;
       
   241 	info1.iPixelFormat = EUidPixelFormatRGB_565;
       
   242 	
       
   243 	TInt result = image.Create(info1, KCrossImageData, KCrossImageDataStride);
       
   244 	
       
   245 	if (result == KErrNone)
       
   246 		{
       
   247 		//Close the driver without closing the image.
       
   248 		aDriver.Close(); //Should panic with SGRES2
       
   249 		}
       
   250 	
       
   251 	return result;
       
   252 	}
       
   253 
       
   254 /**
       
   255 Method executed by secondary thread for test TestOpenImageMulththreadedL
       
   256 */
       
   257 _LIT(KTestOpenImageMultithreadedSem1, "TestOpenImageMultithreadedSem1");
       
   258 _LIT(KTestOpenImageMultithreadedSem2, "TestOpenImageMultithreadedSem2");
       
   259 
       
   260 TInt OpenImageMultiSecondThread(TAny* aAny)
       
   261 	{
       
   262 	TInt err = KErrNone;
       
   263 	TSgProcessTestInfo* info = static_cast<TSgProcessTestInfo*>(aAny);	
       
   264 
       
   265 	RSemaphore sem[2];
       
   266 	err = sem[0].OpenGlobal(KTestOpenImageMultithreadedSem1, EOwnerThread);
       
   267 	if (err != KErrNone)
       
   268 		{
       
   269 		return err;
       
   270 		}
       
   271 	err = sem[1].OpenGlobal(KTestOpenImageMultithreadedSem2, EOwnerThread);
       
   272 	if (err != KErrNone)
       
   273 		{
       
   274 		sem[0].Close();
       
   275 		return err;
       
   276 		}
       
   277 
       
   278 	RSgImage sgImage; 
       
   279 	err = sgImage.Open(info->iDrawableId);
       
   280 
       
   281 	sem[0].Signal();
       
   282 	sem[1].Wait();
       
   283 
       
   284 	sgImage.Close();	
       
   285 	sem[0].Signal();
       
   286 	return err;	
       
   287 	}
       
   288 
       
   289 /**
       
   290 Creates a second thread which will initially open a handle to the passed TSgDrawableId.
       
   291 The main thread then opens a new handle to the image.
       
   292 The second thread will then close its handle to the image.
       
   293 The main thread will then attempt to access the data of the image.
       
   294  */
       
   295 TInt TestOpenImageMultithreadedL(TSgProcessTestInfo& aInfo)
       
   296 	{
       
   297 	TInt result = 0;
       
   298 	
       
   299 	//create two semaphores
       
   300 	RSemaphore sem[2];
       
   301 	User::LeaveIfError(sem[0].CreateGlobal(KTestOpenImageMultithreadedSem1, 0, EOwnerThread));
       
   302 	CleanupClosePushL(sem[0]);
       
   303 	User::LeaveIfError(sem[1].CreateGlobal(KTestOpenImageMultithreadedSem2, 0, EOwnerThread));
       
   304 	CleanupClosePushL(sem[1]);
       
   305 		
       
   306 	//create secondary thread
       
   307 	_LIT(KSecondaryThreadName, "TestOpenImageMultithreadedL");
       
   308 	RThread thread;
       
   309 	User::LeaveIfError(thread.Create(KSecondaryThreadName, OpenImageMultiSecondThread, KDefaultStackSize, KSecondThreadMinHeapSize, KSecondThreadMaxHeapSize, &aInfo));
       
   310 	thread.Resume();
       
   311 	
       
   312 	// Make the second thread open the image before this thread.
       
   313 	sem[0].Wait();
       
   314 
       
   315 	// Second thread has opened image, now primary thread opens image
       
   316 	RSgImage sgImage;
       
   317 	TInt err = sgImage.Open(aInfo.iDrawableId);
       
   318 	CleanupClosePushL(sgImage);
       
   319 	sem[1].Signal();
       
   320 	sem[0].Wait();
       
   321 		
       
   322 	// Second thread has closed image and terminated, now wait for thread to clean-up
       
   323 	User::After(100000);
       
   324 
       
   325 	if (err == KErrNone)
       
   326 		{
       
   327 		// Do something that requires data access of sgImage, in this case, creating a copy.
       
   328 		result |= EFirstTestPassed;
       
   329 		RSgImage sgImageCopy;
       
   330 		err = sgImageCopy.Create(aInfo.iImageInfo, sgImage);
       
   331 		sgImageCopy.Close();
       
   332 		if (err == KErrNone)
       
   333 			{
       
   334 			result |= ESecondTestPassed;
       
   335 			}	
       
   336 		}
       
   337 	
       
   338 	CleanupStack::PopAndDestroy(3); // sgImage, sem[0], sem[1]		
       
   339 	return result;	
       
   340 	}
       
   341 
       
   342 TInt MainL()
       
   343 	{				
       
   344 	TPckgBuf<TSgProcessTestInfo> infoPkg;
       
   345 	User::LeaveIfError(User::GetDesParameter(KSecondProcessParametersSlot, infoPkg));
       
   346 	TSgProcessTestInfo& info = infoPkg();
       
   347 	TSgresTestCase testCase = info.iTestCase;
       
   348 	TInt result = 0;
       
   349 
       
   350 	RSgDriver sgDriver;
       
   351 	CleanupClosePushL(sgDriver);
       
   352 	
       
   353 	if(KErrNone == sgDriver.Open())
       
   354 		{
       
   355 		switch(testCase)
       
   356 			{
       
   357 			case ESgresSecondProcessOpenImage:
       
   358 				result = TestOpenImageL(info);
       
   359 				break;
       
   360 			case ESgresSecondProcessOpenDrawable:
       
   361 				result = TestOpenDrawableL(info);
       
   362 				break;
       
   363 			case ESgresSecondProcessOpenImageInvalid:
       
   364 				result = TestOpenImageInvalidL(info);
       
   365 				break;
       
   366 			case ESgresSecondProcessOpenDrawableInvalid:
       
   367 				result = TestOpenDrawableInvalidL();
       
   368 				break;
       
   369 			case ESgresSecondProcessPanicDriverCloseOpenResources:
       
   370 				result = TestCloseDriverOpenResources(sgDriver);
       
   371 				break;
       
   372 			case ESgresSecondProcessOpenImageMultithreaded:
       
   373 				result = TestOpenImageMultithreadedL(info);
       
   374 				break;
       
   375 			}
       
   376 		}	
       
   377 	
       
   378 	CleanupStack::PopAndDestroy(&sgDriver);
       
   379 	
       
   380 	return result;
       
   381 	}
       
   382 
       
   383 GLDEF_C TInt E32Main()
       
   384 	{
       
   385 	__UHEAP_MARK;
       
   386 	CTrapCleanup* cleanupStack = CTrapCleanup::New();
       
   387 	if(cleanupStack == NULL)
       
   388 		{
       
   389 		return KErrNoMemory;
       
   390 		}
       
   391 	TInt ret = 0;
       
   392 	TRAP_IGNORE(ret=MainL());
       
   393 	delete cleanupStack;
       
   394 	__UHEAP_MARKEND;
       
   395 	return ret;
       
   396 	}