os/ossrv/lowlevellibsandfws/pluginfw/Framework/SimpleTests/t_downgrade.cpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/ossrv/lowlevellibsandfws/pluginfw/Framework/SimpleTests/t_downgrade.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,221 @@
     1.4 +// Copyright (c) 2002-2009 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 +#include <e32test.h>
    1.20 +#include <f32file.h>
    1.21 +#include <s32file.h>
    1.22 +#include <bautils.h>
    1.23 +#include <hal.h>
    1.24 +
    1.25 +#include "DowngradePath.h"
    1.26 +#include "../EcomTestUtils/EcomTestUtils.h"
    1.27 +
    1.28 +
    1.29 +/* test stuff */
    1.30 +
    1.31 +RTest test(_L("EComServer: RDowngradePath test"));
    1.32 +
    1.33 +// flag to indicate when OOM testing is in progress
    1.34 +TBool OomTest = EFalse;
    1.35 +
    1.36 +// globals
    1.37 +typedef RArray<TLanguage> Languages;
    1.38 +LOCAL_D RFs	TheFs;
    1.39 +
    1.40 +
    1.41 +// --------------------------------------------------------------
    1.42 +// Begin: General helper functions & macros
    1.43 +// --------------------------------------------------------------
    1.44 +
    1.45 +/** Test boolean result. */
    1.46 +#define TEST1(stmt)	test(stmt)
    1.47 +
    1.48 +/** Print formatted string to output. */
    1.49 +#define TEST_PRINTF	test.Printf
    1.50 +
    1.51 +/** Start next test. */
    1.52 +#define TEST_NEXT(title)	\
    1.53 +	{\
    1.54 +	if (!OomTest)\
    1.55 +		test.Next(title);\
    1.56 +	}
    1.57 +
    1.58 +// Used for supressing warning in OOM tests
    1.59 +#define __UNUSED_VAR(var) var = var
    1.60 +
    1.61 +/**
    1.62 +Conditionally test given statement.
    1.63 +if OOM testing is in progress then the error code is compared against KErrNoMemory.
    1.64 +If it matches then the code leaves with that error, otherwise the boolean result
    1.65 +of the statement will be tested.
    1.66 +*/
    1.67 +#define TEST2(err, stmt) \
    1.68 +	{\
    1.69 +	if (OomTest && err==KErrNoMemory) \
    1.70 +		{\
    1.71 +		User::LeaveNoMemory();\
    1.72 +		}\
    1.73 +	else\
    1.74 +		{\
    1.75 +		test(stmt);\
    1.76 +		}\
    1.77 +	}
    1.78 +
    1.79 +/**
    1.80 +@SYMTestCaseID		SYSLIB-ECOM-CT-1857
    1.81 +@SYMTestCaseDesc 	Test RDowngradePath under OOM conditions.
    1.82 +@SYMTestPriority 	High
    1.83 +@SYMTestActions  	Test the use of RDowngradePath under OOM conditions.
    1.84 +@SYMTestExpectedResults No panics caused by leaking code.
    1.85 +@SYMDEF				 DEF090862
    1.86 +*/
    1.87 +typedef void (*FuncPtr8L) ();
    1.88 +static void DoOOMTestL(FuncPtr8L aTestFunctionL)
    1.89 +	{
    1.90 +	OomTest = ETrue;
    1.91 +
    1.92 +	TInt err = KErrNone;
    1.93 +	TInt tryCount = 0;
    1.94 +	RThread thread;
    1.95 +
    1.96 +	do
    1.97 +		{
    1.98 +		__UHEAP_MARK;
    1.99 +
   1.100 +		// find out the number of open handles
   1.101 +		TInt startProcessHandleCount = 0;
   1.102 +		TInt startThreadHandleCount = 0;
   1.103 +		thread.HandleCount(startProcessHandleCount, startThreadHandleCount);
   1.104 +
   1.105 +		__UHEAP_SETFAIL(RHeap::EDeterministic, ++tryCount);
   1.106 +
   1.107 +		TRAP(err, aTestFunctionL());
   1.108 +		RDowngradePath::Reset();
   1.109 +		__UHEAP_SETFAIL(RHeap::ENone, 0);
   1.110 +
   1.111 +		// check that no handles have leaked
   1.112 +		TInt endProcessHandleCount = 0;
   1.113 +		TInt endThreadHandleCount = 0;
   1.114 +		thread.HandleCount(endProcessHandleCount, endThreadHandleCount);
   1.115 +		TEST1(startProcessHandleCount == endProcessHandleCount);
   1.116 +		TEST1(startThreadHandleCount  == endThreadHandleCount);
   1.117 +
   1.118 +		__UHEAP_MARKEND;
   1.119 +		} while(err == KErrNoMemory);
   1.120 +
   1.121 +	thread.Close();
   1.122 +
   1.123 + 	TEST1(err==KErrNone);
   1.124 +
   1.125 +	OomTest = EFalse;
   1.126 +	TEST_PRINTF(_L("- succeeded at heap failure rate of %i\n"), tryCount);
   1.127 +	}
   1.128 +
   1.129 +/**
   1.130 +@SYMTestCaseID          SYSLIB-ECOM-CT-0656
   1.131 +@SYMTestCaseDesc	    Tests for RDowngradePath::HasChangedL() function
   1.132 +@SYMTestPriority 	    Medium
   1.133 +@SYMTestActions  	    Tests for downgradepath consistency
   1.134 +@SYMTestExpectedResults The test must not fail.
   1.135 +@SYMREQ                 REQ0000
   1.136 +*/
   1.137 +LOCAL_C void TestDowngradePathL()
   1.138 +	{
   1.139 +	//we need to make sure that at start of the test the language is set to English
   1.140 +	EComTestUtils::SwitchToLanguageL(static_cast<TLanguage>(ELangEnglish));
   1.141 +
   1.142 +	//the first time this is call should return language has changed
   1.143 +	TInt error;
   1.144 +	TBool hasLanguageChanged=EFalse;
   1.145 +	TRAP(error,hasLanguageChanged=RDowngradePath::HasChangedL(TheFs));
   1.146 +	if (error==KErrNoMemory)
   1.147 +		{
   1.148 +		User::LeaveNoMemory();
   1.149 +		}
   1.150 +
   1.151 +	TEST1(hasLanguageChanged);
   1.152 +
   1.153 +	//if second time called and no language changed yet,it should return false
   1.154 +	TRAP(error,hasLanguageChanged=RDowngradePath::HasChangedL(TheFs));
   1.155 +	if (error==KErrNoMemory)
   1.156 +		{
   1.157 +		User::LeaveNoMemory();
   1.158 +		}
   1.159 +	TEST1(!hasLanguageChanged);
   1.160 +
   1.161 +	//now switch to another language, this will change the language downgrade path
   1.162 +	EComTestUtils::SwitchToLanguageL(static_cast<TLanguage>(ELangFrench));
   1.163 +
   1.164 +	//this should detect language has changed now
   1.165 +	TRAP(error,hasLanguageChanged=RDowngradePath::HasChangedL(TheFs));
   1.166 +	if (error==KErrNoMemory)
   1.167 +		{
   1.168 +		User::LeaveNoMemory();
   1.169 +		}
   1.170 +	TEST1(hasLanguageChanged);
   1.171 +
   1.172 +	//and finally call that function again it should return no language changed yet.
   1.173 +	TRAP(error,hasLanguageChanged=RDowngradePath::HasChangedL(TheFs));
   1.174 +	if (error==KErrNoMemory)
   1.175 +		{
   1.176 +		User::LeaveNoMemory();
   1.177 +		}
   1.178 +	TEST1(!hasLanguageChanged);
   1.179 +
   1.180 +	//reset the cached language settings
   1.181 +	RDowngradePath::Reset();
   1.182 +	//and finally when tests are completed switch back to English
   1.183 +	EComTestUtils::SwitchToLanguageL(static_cast<TLanguage>(ELangEnglish));
   1.184 +	}
   1.185 +
   1.186 +/**
   1.187 +Run all RDowngradePath tests.
   1.188 +*/
   1.189 +LOCAL_C void RunTestsL()
   1.190 +	{
   1.191 +	TEST_NEXT(_L(" @SYMTestCaseID:SYSLIB-ECOM-CT-0656"));
   1.192 +	TestDowngradePathL();
   1.193 +
   1.194 +	TEST_NEXT(_L(" @SYMTestCaseID:SYSLIB-ECOM-CT-1857 Test RDowngradePath in OOM conditions. "));
   1.195 +	DoOOMTestL(TestDowngradePathL);
   1.196 +	}
   1.197 +
   1.198 +/**
   1.199 +Executable entry point.
   1.200 +*/
   1.201 +GLDEF_C TInt E32Main()
   1.202 +	{
   1.203 +	__UHEAP_MARK;
   1.204 +
   1.205 +	test.Title();
   1.206 +	test.Start(_L("Running tests..."));
   1.207 +
   1.208 +	test(TheFs.Connect() == KErrNone);
   1.209 +	CTrapCleanup* cleanup = CTrapCleanup::New();
   1.210 +
   1.211 +	TRAPD(err, RunTestsL());
   1.212 +
   1.213 +	delete cleanup;
   1.214 +	TheFs.Close();
   1.215 +
   1.216 +	// check for errors
   1.217 +	test(err == KErrNone);
   1.218 +
   1.219 +	test.End();
   1.220 +	test.Close();
   1.221 +
   1.222 +	__UHEAP_MARKEND;
   1.223 +	return(0);
   1.224 +	}