kerneltest/e32test/power/t_power.cpp
changeset 0 a41df078684a
child 33 0173bcd7697c
equal deleted inserted replaced
-1:000000000000 0:a41df078684a
       
     1 // Copyright (c) 2002-2009 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 the License "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 // e32test\power\t_power.cpp
       
    15 // Overview:
       
    16 // Test power down and wakeup event notification
       
    17 // API Information:
       
    18 // Power::PowerDown(), Power::RequestWakeupEventNotification()
       
    19 // Details:
       
    20 // - Arm a wakeup timer, enable wakeup events, enter standby mode, get woken up.
       
    21 // - Test RequestWakeupEventNotification(): arm a timer, request notification,
       
    22 // wait for timer to expire, verify event has been notified, issue another 
       
    23 // notification request, disable wakeup events, arm another timer, verify 
       
    24 // wakeup event has not been notified, cancel the notification request and
       
    25 // close the timer.
       
    26 // - Verify that a slave process panics because it hasn't and capability.
       
    27 // - Confirm the number of open handles and pending requests are as expected.
       
    28 // Platforms/Drives/Compatibility:
       
    29 // All.
       
    30 // Assumptions/Requirement/Pre-requisites:
       
    31 // Failures and causes:
       
    32 // Base Port information:
       
    33 // 
       
    34 //
       
    35 
       
    36 #define __E32TEST_EXTENSION__
       
    37 
       
    38 #include <e32power.h>
       
    39 #include <e32test.h>
       
    40 #include <e32kpan.h>
       
    41 #include <f32file.h>
       
    42 
       
    43 LOCAL_D RTest test(_L(" T_POWER "));
       
    44 
       
    45 void SetAbsoluteTimeout(RTimer& aTimer, TUint aUs, TRequestStatus& aStatus)
       
    46 	{
       
    47 	TTime wakeup;
       
    48 	wakeup.HomeTime();
       
    49 	wakeup += TTimeIntervalMicroSeconds(aUs);
       
    50 	aTimer.At(aStatus, wakeup);
       
    51 	}
       
    52 
       
    53 void PowerTests()
       
    54 	{
       
    55 	test.Next(_L("test PowerDown()"));
       
    56 
       
    57 	TInt r = Power::PowerDown();
       
    58 	test (r == KErrNotReady);
       
    59 
       
    60 	for (int i = 0; i < 4; ++i)
       
    61 		{
       
    62 		test.Printf(_L(" %d "), i);
       
    63 		// Arm an absolute timer wakeup event after 5 sec
       
    64 		TRequestStatus absstatus;
       
    65 		RTimer abstimer;
       
    66 		r = abstimer.CreateLocal();
       
    67 		test (r == KErrNone);
       
    68 		SetAbsoluteTimeout(abstimer, 5000000, absstatus); // 5 sec
       
    69 		// Go to standby 
       
    70 		r = Power::EnableWakeupEvents(EPwStandby);
       
    71 		test (r == KErrNone);
       
    72 		r = Power::PowerDown();
       
    73 		test (r == KErrNone);
       
    74 		User::WaitForRequest(absstatus);
       
    75 		abstimer.Close();
       
    76 		}
       
    77 	test.Printf(_L(" OK\n"));
       
    78 
       
    79 	test.Next(_L("test RequestWakeupEventNotification()"));
       
    80 
       
    81 		{
       
    82 		TInt r = Power::EnableWakeupEvents(EPwActive);
       
    83 		test (r == KErrArgument);
       
    84 
       
    85 		// Request wakup event notification and enable wakeup events
       
    86 		TRequestStatus status;
       
    87 		Power::RequestWakeupEventNotification(status);
       
    88 		test(status.Int() == KRequestPending);
       
    89 		r = Power::EnableWakeupEvents(EPwStandby);
       
    90 		test (r == KErrNone);
       
    91 		// Arm an absolute timer wakeup event
       
    92 		TRequestStatus absstatus;
       
    93 		RTimer abstimer;
       
    94 		r = abstimer.CreateLocal();
       
    95 		test (r == KErrNone);
       
    96 		SetAbsoluteTimeout(abstimer, 100000, absstatus); // 100ms
       
    97 		// Wait for the timer
       
    98 		User::WaitForRequest(absstatus);
       
    99 		test(absstatus.Int() == KErrNone);
       
   100 		// Wakup event has to be already notified
       
   101 		test(status.Int() == KErrNone);
       
   102 		User::WaitForRequest(status);	// collect it
       
   103 		// Issue another notification request
       
   104 		Power::RequestWakeupEventNotification(status);
       
   105 		test(status.Int() == KRequestPending);
       
   106 		// Disable wakeup events
       
   107 		Power::DisableWakeupEvents();
       
   108 		// Arm another absolute timer wakeup event
       
   109 		SetAbsoluteTimeout(abstimer, 100000, absstatus); // 100ms
       
   110 		// Wait for the timer
       
   111 		User::WaitForRequest(absstatus);
       
   112 		test(absstatus.Int() == KErrNone);
       
   113 		// Wakeup event has not to be notified
       
   114 		test(status.Int() == KRequestPending);
       
   115 		// Cancel the notification request
       
   116 		Power::CancelWakeupEventNotification();
       
   117 		test(status.Int() == KErrCancel);
       
   118 		User::WaitForRequest(status);	// collect it
       
   119 		// Cancel again just for fun ...
       
   120 		Power::CancelWakeupEventNotification();
       
   121 		test(status.Int() == KErrCancel);
       
   122 
       
   123 		abstimer.Close();
       
   124 		}
       
   125 	}
       
   126 
       
   127 _LIT(KSecuritySlavePath, "t_power_slave.exe");
       
   128 
       
   129 void ExecSlave(TUint aArg)
       
   130 	{
       
   131 	RProcess proc;
       
   132 	TInt r = proc.Create(KSecuritySlavePath, TPtrC((TUint16*) &aArg, sizeof(aArg)/sizeof(TUint16)));
       
   133 	test(r == KErrNone);
       
   134 	TRequestStatus status;
       
   135 	proc.Logon(status);
       
   136 	proc.Resume();
       
   137 	User::WaitForRequest(status);
       
   138 	// The slave must panic
       
   139 	test_Equal(EExitPanic, proc.ExitType());
       
   140 	test_Equal(EPlatformSecurityTrap, proc.ExitReason());
       
   141 	CLOSE_AND_WAIT(proc);
       
   142 	}
       
   143 
       
   144 GLDEF_C TInt E32Main()
       
   145 	{
       
   146 	test.Title();
       
   147 	test.Start(_L("Testing"));
       
   148 
       
   149 	// Turn off evil lazy dll unloading
       
   150 	RLoader l;
       
   151 	test(l.Connect()==KErrNone);
       
   152 	test(l.CancelLazyDllUnload()==KErrNone);
       
   153 	l.Close();
       
   154 
       
   155 	//
       
   156 	// Perform the number of iterations specifed by the command line argument.
       
   157 	//
       
   158 	// If no arguments - perform two iterations
       
   159 	//
       
   160 	TInt iter = 2;
       
   161 	TInt len = User::CommandLineLength();
       
   162 	if (len)
       
   163 		{
       
   164 		// Copy the command line in a buffer
       
   165 		HBufC* hb = HBufC::NewMax(len);
       
   166 		test(hb != NULL);
       
   167 		TPtr cmd((TUint16*) hb->Ptr(), len);
       
   168 		User::CommandLine(cmd);
       
   169 		// Extract the number of iterations
       
   170 		TLex l(cmd);
       
   171 		TInt i;
       
   172 		TInt r = l.Val(i);
       
   173 		if (r == KErrNone)
       
   174 			iter = i;
       
   175 		else
       
   176 			// strange command - silently ignore
       
   177 			{} 
       
   178 		delete hb;
       
   179 		}
       
   180 
       
   181 	test.Printf(_L("Go for %d iterations\n"), iter);
       
   182 
       
   183 	while (iter--)
       
   184 		{
       
   185 		// Remember the number of open handles. Just for a sanity check ....
       
   186 		TInt start_thc, start_phc;
       
   187 		RThread().HandleCount(start_phc, start_thc);
       
   188 
       
   189 		PowerTests();
       
   190 
       
   191 		test.Start(_L("test platform security"));
       
   192 		// The slave process must panic because it hasn't any capability 
       
   193 		if(!PlatSec::IsCapabilityEnforced(ECapabilityPowerMgmt))
       
   194 			test.Printf(_L("TESTS NOT RUN - PowerMgmt capability isn't enforced on system"));
       
   195 		else
       
   196 			{
       
   197 			test.Next(_L("PowerDown()"));
       
   198 			ExecSlave(0);
       
   199 			test.Next(_L("EnableWakeupEvents()"));
       
   200 			ExecSlave(1);
       
   201 			test.Next(_L("DisableWakeupEvents()"));
       
   202 			ExecSlave(2);
       
   203 			test.Next(_L("RequestWakeupEventNotification()"));
       
   204 			ExecSlave(3);
       
   205 			test.Next(_L("CancelWakeupEventNotification()"));
       
   206 			ExecSlave(4);
       
   207 			}
       
   208 		test.End();
       
   209 
       
   210 		// Sanity check for open handles
       
   211 		TInt end_thc, end_phc;
       
   212 		RThread().HandleCount(end_phc, end_thc);
       
   213 		test(start_thc == end_thc);
       
   214 		test(start_phc == end_phc);
       
   215 			// and also for pending requests ...
       
   216 		test(RThread().RequestCount() == 0);
       
   217 		}
       
   218 
       
   219 	test.End();
       
   220 
       
   221 	return KErrNone;
       
   222 	}