installationservices/swi/test/tasynccancel/asyncc.cpp
changeset 0 ba25891c3a9e
equal deleted inserted replaced
-1:000000000000 0:ba25891c3a9e
       
     1 /*
       
     2 * Copyright (c) 2004-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of the License "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description: 
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include "asyncc.h"
       
    20 #include "asynccancelstep.h"
       
    21 
       
    22 
       
    23 
       
    24 // REComSession::FinalClose() needs to be run in order for a memory leak 
       
    25 // not to occur if using logo in the sis file to be installed. 
       
    26 // This shouldn't be the case since the test is never using ECom 
       
    27 // explicitly, meaning that it should be dealt with within the 
       
    28 // installation source itself. The behaviour isn't related to cancellation 
       
    29 // since it occurs when installation isn't cancelled at all
       
    30 #include <ecom/ecom.h>
       
    31 
       
    32 
       
    33 extern CConsoleBase* gConsole; // write all your messages to this
       
    34 
       
    35 
       
    36 // Try uninstalling a program with the given UID
       
    37 // If the program is detected to be installed than cancellation failed => return error
       
    38 TInt TryUninstall(TUid aUid)
       
    39 	{
       
    40 	CUIScriptAdaptor* ui = CUIScriptAdaptor::NewLC();
       
    41 	CInstallPrefs* prefs = CInstallPrefs::NewLC();
       
    42 
       
    43 	TInt err = Swi::Launcher::Uninstall(*ui, aUid);
       
    44 	
       
    45 	CleanupStack::PopAndDestroy(prefs);
       
    46 	CleanupStack::PopAndDestroy(ui);
       
    47 	
       
    48 	// program not installed => cancelled correctly
       
    49 	if (err==KErrNotFound)
       
    50 		{
       
    51 		err=0;
       
    52 		}
       
    53 	else
       
    54 		{
       
    55 		err=1;
       
    56 		}
       
    57 	
       
    58 	return err;
       
    59 	}
       
    60 
       
    61 
       
    62 
       
    63 
       
    64 TInt CAsyncCancelStep::DoTestL(TInt aWhenToCancelLow, TInt aWhenToCancelHigh, TInt aHowManyCancelations, TInt aMinWhenToCancelInc,
       
    65 			 const TPtrC& aSisToInstall, TUid aUid,
       
    66 			 CInstallPrefs* aPrefs)
       
    67     {
       
    68     gConsole=Console::NewL(KTxtExampleCode,TSize(KConsFullScreen,KConsFullScreen));
       
    69     CleanupStack::PushL(gConsole);
       
    70 
       
    71 
       
    72 
       
    73 	MY_INFO_PRINTF1( _L("Installing sis file: ") );
       
    74 	MY_INFO_PRINTF1( aSisToInstall );
       
    75 	MY_INFO_PRINTF1( _L("\n") );
       
    76 	
       
    77 
       
    78 	CActiveScheduler* scheduler=new(ELeave) CActiveScheduler;
       
    79     CActiveScheduler::Install(scheduler);
       
    80     
       
    81 
       
    82     CCancelTimer* CancelTimer = CCancelTimer::NewL(aPrefs);
       
    83 
       
    84 	TInt err=0;
       
    85 	
       
    86 	// Run the installation for the first time (without cancelling)
       
    87 	// to see how long it takes
       
    88 	TInt firstRun=1;
       
    89 	TInt whenToCancel=100000000;
       
    90 	
       
    91 	// This initialisation doesn't make any difference since the variable
       
    92 	// is initialised later. But it removes compiler warnings
       
    93 	TInt whenToCancelInc=1;
       
    94 
       
    95 	
       
    96 	
       
    97 	if (aWhenToCancelLow==0) 
       
    98 		{
       
    99 		aWhenToCancelLow=1; // impossible to cancel at 0
       
   100 		}
       
   101 	
       
   102 	
       
   103 	// Uninstall if already installed
       
   104 	err=TryUninstall( aUid );
       
   105 	err=0; // ignore the error
       
   106 
       
   107 
       
   108 	do 
       
   109 		{
       
   110 		CleanupStack::PushL(CancelTimer);
       
   111 		// Set up installation
       
   112 		CancelTimer->StartL(aSisToInstall,whenToCancel,firstRun);
       
   113 		CleanupStack::Pop(CancelTimer);
       
   114 		
       
   115 		// it is useful to run the complete installation twice
       
   116 		// since when OCSP checking is enabled the first installation
       
   117 		// takes a lot more time to execute than the later ones
       
   118 		if (firstRun==1) 
       
   119 			{
       
   120 			MY_INFO_PRINTF1(_L("Running a complete installation (first time) : "));
       
   121 			}
       
   122 		else if (firstRun==2) 
       
   123 			{
       
   124 			MY_INFO_PRINTF1(_L("Running a complete installation (second time): "));
       
   125 			}
       
   126 		else
       
   127 			{
       
   128 			MY_INFO_PRINTF2(_L("Running installation, cancelling at %d microseconds: "),whenToCancel);
       
   129 			}
       
   130 	
       
   131 		// Start installation process	
       
   132 		scheduler->Start();
       
   133 		// Test ended => Deque the cancel timer
       
   134 		CancelTimer->Deque();
       
   135 		
       
   136 		
       
   137 		MY_INFO_PRINTF2(_L("\tiStatus: %d\t"),CancelTimer->InstallerStatus());
       
   138 		
       
   139 
       
   140 		if (firstRun>0)
       
   141 			{
       
   142 			MY_INFO_PRINTF2(_L("%d microseconds\n"),CancelTimer->Time());
       
   143 			
       
   144 			if (CancelTimer->CancelationSuccess()!=1)
       
   145 				{
       
   146 				MY_ERR_PRINTF2(_L("<<< FAILED >>> with %d error code\n"),CancelTimer->CancelationSuccess());
       
   147 				break;
       
   148 				}
       
   149 			
       
   150 			// Determine at what intervals the cancellation should be performed
       
   151 			// in order to achieve the wanted number of cancellations
       
   152 			if (aWhenToCancelHigh>CancelTimer->Time())
       
   153 				{
       
   154 				aWhenToCancelHigh=CancelTimer->Time();
       
   155 				}				
       
   156 			whenToCancelInc= (aWhenToCancelHigh-aWhenToCancelLow)/aHowManyCancelations;
       
   157 			if (whenToCancelInc<aMinWhenToCancelInc) 
       
   158 				{
       
   159 				whenToCancelInc=aMinWhenToCancelInc;
       
   160 				}
       
   161 			whenToCancel=aWhenToCancelLow;
       
   162 			
       
   163 			// Uninstall
       
   164 			err=TryUninstall( aUid  );
       
   165 			err=0;
       
   166 			}
       
   167 		else
       
   168 			{
       
   169 			MY_INFO_PRINTF1(_L("Completed: "));
       
   170 			whenToCancel+= whenToCancelInc;
       
   171 			
       
   172 			if (CancelTimer->FinishedInstallation())
       
   173 				{
       
   174 				err=TryUninstall( aUid );
       
   175 				}				
       
   176 		
       
   177 			if (CancelTimer->CancelationSuccess())
       
   178 				{
       
   179 				MY_INFO_PRINTF1(_L("OK\n"));
       
   180 				}				
       
   181 			else 
       
   182 				{
       
   183 				MY_ERR_PRINTF1(_L("\t<<< FAILED >>>\n"));
       
   184 				}
       
   185 			
       
   186 			if (
       
   187 				CancelTimer->CancelationSuccess()!=1 ||
       
   188 			   	CancelTimer->FinishedInstallation() ||
       
   189 			   	whenToCancel>aWhenToCancelHigh
       
   190 			   )
       
   191 				{
       
   192 				break;
       
   193 				}
       
   194 
       
   195 			}
       
   196 	
       
   197 
       
   198 		if (firstRun==1)
       
   199 			{
       
   200 			firstRun=2;
       
   201 			}
       
   202 		else 
       
   203 			{
       
   204 			firstRun=0;
       
   205 			}
       
   206 
       
   207 		} while (1);
       
   208 
       
   209 	
       
   210 		
       
   211 	err=0;
       
   212 	if (CancelTimer->CancelationSuccess()!=1) 
       
   213 		{
       
   214 		err=1;
       
   215 		}
       
   216 	else
       
   217 		{
       
   218 		err=0;
       
   219 		}
       
   220 	
       
   221 	
       
   222 
       
   223 
       
   224 	// if a delay is needed, edit this code
       
   225 	{
       
   226 	CDelayTimer* DelayTimer=new(ELeave) CDelayTimer; 
       
   227 	DelayTimer->ConstructL();
       
   228 	CActiveScheduler::Add(DelayTimer);
       
   229 	DelayTimer->After(TTimeIntervalMicroSeconds32(1));
       
   230 	CActiveScheduler::Start();
       
   231 	delete DelayTimer;
       
   232 	}
       
   233 
       
   234 	
       
   235 	
       
   236 	// clean-up
       
   237 	delete scheduler;
       
   238 	delete CancelTimer;
       
   239 	
       
   240 	if (err)
       
   241 		{
       
   242 		MY_ERR_PRINTF1(_L("\n\n\tAt least one test <<< FAILED >>>\n"));
       
   243 		}		
       
   244 	else 
       
   245 		{
       
   246 		MY_INFO_PRINTF1(_L("\n\n\tAll tests PASSED\n"));
       
   247 		}
       
   248 	
       
   249 	MY_INFO_PRINTF1(_L("\tCancelTest compleated execution\n"))	;
       
   250 #ifdef WAITFORINPUTATEND	
       
   251 	MY_INFO_PRINTF1(KTxtPressAnyKey);
       
   252 	gConsole->Getch(); // get and ignore character
       
   253 #endif
       
   254 	CleanupStack::PopAndDestroy(gConsole); // close gConsole
       
   255 
       
   256 
       
   257 	// for explanation see the top of this file, at #include <ecom/ecom.h>
       
   258 	REComSession::FinalClose();
       
   259 	//
       
   260 
       
   261 	return err;
       
   262     }
       
   263 
       
   264 
       
   265 // End of file