messagingappbase/smsmtm/test/src/T_CSmsSettings.cpp
changeset 25 84d9eb65b26f
parent 23 238255e8b033
child 27 e4592d119491
child 37 518b245aa84c
child 79 2981cb3aa489
equal deleted inserted replaced
23:238255e8b033 25:84d9eb65b26f
     1 // Copyright (c) 2003-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 "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 // Class test for CSmsSettings.
       
    15 // To build: bldmake bldfiles
       
    16 // abld test build wins udeb
       
    17 // To run from windows command prompt:
       
    18 // cd epoc32\release\wins\udeb 
       
    19 // T_CSMSSETTINGS -dtextshell --
       
    20 // 
       
    21 //
       
    22  
       
    23 
       
    24 #include <e32cons.h>
       
    25 #include <e32test.h>
       
    26 #include <s32file.h>
       
    27 
       
    28 // Message Server headers for linking to msgs.dll. 
       
    29 #include "msvapi.h"
       
    30 #include "msvids.h"
       
    31 #include "msvuids.h"
       
    32 
       
    33 // Class under test.
       
    34 #include "smutset.h"
       
    35 #include <csmsaccount.h>
       
    36 
       
    37 // Test utility classes
       
    38 class CMyScheduler : public CActiveScheduler
       
    39 	{
       
    40 private:
       
    41 	void Error(TInt aError) const;
       
    42 	};
       
    43 void CMyScheduler::Error(TInt /*aError*/) const {}
       
    44 
       
    45 
       
    46 class CObserver : public MMsvSessionObserver
       
    47 	{
       
    48 	public:
       
    49 		void HandleSessionEvent(TMsvSessionEvent aEvent, TAny* aArg1, TAny* aArg2, TAny* aArg3);
       
    50 		void HandleSessionEventL(TMsvSessionEvent aEvent, TAny* aArg1, TAny* aArg2, TAny* aArg3);
       
    51 	};
       
    52 void CObserver::HandleSessionEvent(TMsvSessionEvent /*aEvent*/, TAny* /*aArg1*/, TAny* /*aArg2*/, TAny* /*aArg3*/) {}
       
    53 void CObserver::HandleSessionEventL(TMsvSessionEvent /*aEvent*/, TAny* /*aArg1*/, TAny* /*aArg2*/, TAny* /*aArg3*/)	{}
       
    54 
       
    55 
       
    56 // Test Case Prototypes
       
    57 LOCAL_C TBool TestConstructionL();
       
    58 LOCAL_C TBool TestRestoreWithNoStoreL();
       
    59 LOCAL_C TBool TestRestoreWhenOnlyExternalSettingsIsPresentL();
       
    60 LOCAL_C TBool TestRestoreWhenOnlyStoreSettingsIsPresentL();
       
    61 LOCAL_C TBool TestStoreL();
       
    62 LOCAL_C TBool TestSaveExternalL();
       
    63 LOCAL_C TBool TestRestoreWhenExternalSettingsIsMoreRecentL();
       
    64 LOCAL_C TBool TestRestoreWhenStoreSettingsIsMoreRecentL();
       
    65 
       
    66 
       
    67 // Test Harness Prototypes
       
    68 LOCAL_C void InitL();
       
    69 LOCAL_C void UninitL();
       
    70 LOCAL_C void doTest();
       
    71 LOCAL_C void doMainL();
       
    72 GLDEF_C TInt E32Main();
       
    73 
       
    74 
       
    75 // Test harness constants and global variables...
       
    76 _LIT(KTitle, "T_CSMSSETTINGS");
       
    77 _LIT(KTxtPressAnyKey, "Press any key to return.");
       
    78 CTrapCleanup* pcleanup = NULL;
       
    79 CMyScheduler* pscheduler = NULL;
       
    80 RTest test(KTitle);
       
    81 TMsvId gTestEntryId;
       
    82 RFs gFs;
       
    83 CMsvSession* pSession = NULL;
       
    84 CObserver* pObserver = NULL;
       
    85 CMsvEntry* pContext = NULL;
       
    86 const TUid KMsgFileSmsSettingsUid = {0x1000996E};
       
    87 _LIT(KExternalSettingsFilename, "c:\\system\\data\\sms_settings.dat");
       
    88 
       
    89 
       
    90 LOCAL_C TBool TestConstructionL()
       
    91 	{
       
    92 	// Test construction via NewL().
       
    93 	__UHEAP_MARK;
       
    94 	CSmsSettings* obj = NULL;
       
    95 	obj = CSmsSettings::NewL();
       
    96 	CleanupStack::PushL(obj);
       
    97 	test(obj != NULL);
       
    98 	CleanupStack::PopAndDestroy();
       
    99 	obj = NULL;
       
   100 	__UHEAP_MARKEND;
       
   101 
       
   102 	// Test for memory leaks during heap cell failure at specific allocations.
       
   103 	TInt failureRate = 0;
       
   104 	while (failureRate < 32)
       
   105 		{
       
   106 		__UHEAP_MARK;
       
   107 		__UHEAP_SETFAIL(RHeap::EDeterministic, failureRate++);
       
   108 		TRAPD(err, obj = CSmsSettings::NewL());
       
   109 		__UHEAP_RESET;
       
   110 		if (err == KErrNone)
       
   111 			{
       
   112 			delete obj;
       
   113 			obj = NULL;
       
   114 			}
       
   115 		else
       
   116 			{
       
   117 			test(err == KErrNoMemory);
       
   118 			}
       
   119 		__UHEAP_MARKEND;
       
   120 		}
       
   121 
       
   122 	return ETrue;
       
   123 	}
       
   124 
       
   125 
       
   126 LOCAL_C TBool TestRestoreWhenOnlyStoreSettingsIsPresentL()
       
   127 /**
       
   128 Calling CSmsSettings::RestoreL should return KErrNone, and the settings
       
   129 should be set to that of the store.
       
   130 */
       
   131 	{
       
   132 	// Make sure there is no external settings.
       
   133 	gFs.Delete(KExternalSettingsFilename);
       
   134 
       
   135 	__UHEAP_MARK;
       
   136 	CSmsAccount* account = CSmsAccount::NewLC();
       
   137 
       
   138 	// Save settings to store only.
       
   139 	CSmsSettings* obj = CSmsSettings::NewL();
       
   140 	CleanupStack::PushL(obj);
       
   141 	obj->SetDescriptionLength(3446238);
       
   142 	account->SaveSettingsL(*obj);
       
   143 	CleanupStack::PopAndDestroy(obj);
       
   144 
       
   145 	// Restore settings and check it is from the store.
       
   146 	obj = CSmsSettings::NewL();
       
   147 	CleanupStack::PushL(obj);
       
   148 	
       
   149 	// Test that the settings are un-initialised.
       
   150 	test(obj->DescriptionLength() != 3446238);
       
   151 	account->LoadSettingsL(*obj);
       
   152 	
       
   153 	// Test that the settings are coming from the store.
       
   154 	test(obj->DescriptionLength() == 3446238);
       
   155 	
       
   156 	CleanupStack::PopAndDestroy(obj);
       
   157 	CleanupStack::PopAndDestroy(account);
       
   158 	__UHEAP_MARKEND;
       
   159 	
       
   160 	return ETrue;
       
   161 	}
       
   162 	
       
   163 	
       
   164 
       
   165 
       
   166 LOCAL_C void InitL()
       
   167 	{
       
   168 	// Connect to the file system...
       
   169 	TInt err = gFs.Connect();
       
   170 
       
   171 	// Connect to the Message Server...
       
   172 	pObserver = new (ELeave) CObserver();
       
   173 	CleanupStack::PushL(pObserver);
       
   174 	pSession = CMsvSession::OpenSyncL(*pObserver);
       
   175 	CleanupStack::PushL(pSession);
       
   176 
       
   177 	// Create a test entry with some dummy values so the Message Server
       
   178 	// will not panic when the entry is created in the message store...
       
   179 	pContext = CMsvEntry::NewL(*pSession, KMsvRootIndexEntryIdValue, TMsvSelectionOrdering());
       
   180 	CleanupStack::PushL(pContext);
       
   181 	TMsvEntry testEntry;
       
   182 	TUid entryMtm;
       
   183 	entryMtm.iUid = 11;
       
   184 	testEntry.iMtm = entryMtm;
       
   185 	testEntry.iServiceId = pContext->OwningService();
       
   186 	testEntry.iType = KUidMsvServiceEntry;
       
   187 
       
   188 	// Create the entry in the message store and set the current context to it...
       
   189 	pContext->CreateL(testEntry);
       
   190 	gTestEntryId = testEntry.Id();
       
   191 	pContext->SetEntryL(gTestEntryId);
       
   192 	}
       
   193 
       
   194 
       
   195 LOCAL_C void UninitL()
       
   196 	{
       
   197 	gFs.Delete(KExternalSettingsFilename);
       
   198 	pContext->SetEntryL(KMsvRootIndexEntryIdValue);
       
   199 	pContext->DeleteL(gTestEntryId);
       
   200 	CleanupStack::PopAndDestroy(pContext);
       
   201 	CleanupStack::PopAndDestroy(pSession);
       
   202 	CleanupStack::PopAndDestroy(pObserver);
       
   203 	gFs.Close();
       
   204 	}
       
   205 
       
   206 
       
   207 LOCAL_C void doTest()
       
   208 	{
       
   209 	test.Start(_L("Testing construction/destruction"));
       
   210 	test(TestConstructionL());
       
   211 	test.Next(_L("Testing RestoreL when only store settings are present"));
       
   212 	test(TestRestoreWhenOnlyStoreSettingsIsPresentL());
       
   213 	test.End();
       
   214 	}
       
   215 
       
   216 
       
   217 LOCAL_C void doMainL()
       
   218 	{
       
   219 	InitL();
       
   220 	doTest();
       
   221 	test.Getch();
       
   222 	UninitL();
       
   223 	}
       
   224 
       
   225 
       
   226 GLDEF_C TInt E32Main()
       
   227 	{
       
   228 	pcleanup = CTrapCleanup::New();
       
   229 	pscheduler = new (ELeave) CMyScheduler();
       
   230 	CActiveScheduler::Install(pscheduler);
       
   231 	TRAPD(ret,doMainL());		
       
   232 	delete pcleanup;
       
   233 	return(KErrNone);
       
   234 	}