summaryrefslogtreecommitdiffstats
path: root/plugins/contacts/symbian/contactsmodel/tsrc/t_recoverview.cpp
blob: cfa79a1024b5ef72d1c4f3c016bba96e06884234 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
/*
* Copyright (c) 2003-2009 Nokia Corporation and/or its subsidiary(-ies).
* All rights reserved.
* This component and the accompanying materials are made available
* under the terms of "Eclipse Public License v1.0"
* which accompanies this distribution, and is available
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
*
* Initial Contributors:
* Nokia Corporation - initial contribution.
*
* Contributors:
*
* Description: 
* RecoverView Test module
*
*/



#include <e32test.h>
#include <cntviewbase.h>
#include <cntitem.h>
#include <cntfldst.h>

#include "ccontactvieweventqueue.h"

_LIT(KTestName, "T_RecoverView");

_LIT(KTestDbName, "c:T_RecoverView.cdb");

LOCAL_D RTest test(KTestName);


class CTestResources : public CBase
    {
    public:
        static CTestResources* NewLC(TBool aDamaged);
        void ConstructL(TBool aDamaged);
        ~CTestResources();

        CContactDatabase* iDb;
        CContactViewEventQueue* iViewEventQueue;
        RContactViewSortOrder iViewSortOrder;
        CContactLocalView* iLocalView;
    };

/**
 * Creates new test resources object.
 *
 * @param aDamaged  if true creates the database as initially damaged.
 */
CTestResources* CTestResources::NewLC(TBool aDamaged)
    {
    CTestResources* self = new(ELeave) CTestResources;
    CleanupStack::PushL(self);
    self->ConstructL(aDamaged);
    return self;
    }

/**
 * Creates a new, nondamaged DB.
 */
void CTestResources::ConstructL(TBool aDamaged)
    {
    if (aDamaged)
        {
        // Create an empty DB...
        CContactDatabase* db = CContactDatabase::ReplaceL(KTestDbName);
        CleanupStack::PushL(db);
        // Damage the DB...
        db->DamageDatabaseL(0x666);
        // Close the DB.
        CleanupStack::PopAndDestroy(db);
        // Open the damaged database. CContactDatabase::OpenL() does recovery
        // by calling RecoverL().
		iDb = CContactDatabase::OpenL(KTestDbName);
        }
    else
        {
        // Just create an empty DB
        iDb = CContactDatabase::ReplaceL(KTestDbName);
        }

    iViewEventQueue = CContactViewEventQueue::NewL();

    iViewSortOrder.AppendL(KUidContactFieldFamilyName);
    iViewSortOrder.AppendL(KUidContactFieldGivenName);
    iViewSortOrder.AppendL(KUidContactFieldCompanyName);
    
    iLocalView = CContactLocalView::NewL
        (*iViewEventQueue, *iDb, iViewSortOrder, EContactsOnly);
    }

CTestResources::~CTestResources()
    {
    if (iLocalView) iLocalView->Close(*iViewEventQueue);
    iViewSortOrder.Close();
    delete iViewEventQueue;
    delete iDb;
    TRAP_IGNORE(CContactDatabase::DeleteDatabaseL(KTestDbName));
    }

#if defined(_DEBUG)

LOCAL_C void TestRecoverWhenViewReadyL()
    {
	test.Next(_L("Recover database when view is ready"));

    CTestResources* res = CTestResources::NewLC(EFalse);
	
	//Creating a contact to test recovery
	_LIT(KPhoneNumLabel,"Phone Number");
	_LIT(KPhoneNum,		"+440000000000");

	CContactCard* contact		= CContactCard::NewLC();
	CContactItemField* field	= CContactItemField::NewLC(KStorageTypeText,KUidContactFieldPhoneNumber);
	
	field->SetLabelL(KPhoneNumLabel());
	field->TextStorage()->SetTextL(KPhoneNum());
	
	contact->AddFieldL(*field);
	
	TContactItemId aContactId = res->iDb->AddNewContactL(*contact);
	
	CleanupStack::Pop(field);

    // Wait 5 seconds for the view to get ready
    TContactViewEvent event;
    test(res->iViewEventQueue->ListenForEvent(5,event));
    test(event.iEventType == TContactViewEvent::EReady);

    // Damage the DB
	test.Next(_L("Damage the open database"));

    res->iDb->DamageDatabaseL(0x666);
    test(res->iDb->IsDamaged());
   
    // Damaging should not send view events
    test(!res->iViewEventQueue->ListenForEvent(5,event));

	//Attempt to read contact from the damaged database - this should fail with KErrNotReady
	test.Next(_L("Attempting to read from damaged database"));

	CContactItem* testCard = NULL;
	TRAPD(ret,testCard = res->iDb->ReadContactLC(aContactId));
	test(ret == KErrNotReady);
		
	// Recover the DB
	test.Next(_L("Recover the damaged database"));

    res->iDb->RecoverL();
		
    // View should be unavailable here
    test(res->iViewEventQueue->ListenForEvent(5,event));
    test(event.iEventType == TContactViewEvent::EUnavailable);

	//View should now be ready for use again
	test(res->iViewEventQueue->ListenForEvent(5,event));
	test(event.iEventType == TContactViewEvent::EReady);

	//Line below included to fix ARMV5 minor build warnings.
	testCard = res->iDb->ReadContactLC(aContactId);
	test(testCard != NULL);
	
	CleanupStack::PopAndDestroy(testCard);
	CleanupStack::PopAndDestroy(contact);
    CleanupStack::PopAndDestroy(res);
    }

LOCAL_C void TestOpenDamagedDbL()
    {
    test.Next(_L("Open damaged database"));

    CTestResources* res = CTestResources::NewLC(ETrue);

    // Wait 10 seconds for the view to get ready
    TContactViewEvent event;
    test(res->iViewEventQueue->ListenForEvent(10,event));
    test(event.iEventType == TContactViewEvent::EReady);
    CleanupStack::PopAndDestroy(res);
    }

LOCAL_C void TestOpenAndCloseTablesL()
    {
    test.Next(_L("Open and close tables"));

    CTestResources* res = CTestResources::NewLC(EFalse);

    // Wait 10 seconds for the view to get ready
    TContactViewEvent event;
    test(res->iViewEventQueue->ListenForEvent(5,event));
    test(event.iEventType == TContactViewEvent::EReady);

    // Close tables
    res->iDb->CloseTables();
   
    // View should get unavailable
    test(res->iViewEventQueue->ListenForEvent(5,event));
    test(event.iEventType == TContactViewEvent::EUnavailable);

    // Reopen tables
    res->iDb->OpenTablesL();

    // Wait 10 seconds for the view to get ready
    test(res->iViewEventQueue->ListenForEvent(10,event));
    test(event.iEventType == TContactViewEvent::EReady);
    CleanupStack::PopAndDestroy(res);
    }

#endif

/**

@SYMTestCaseID     PIM-T-RECOVERVIEW-0001

*/

LOCAL_C void DoTestsL()
    {
	test.Start(_L("@SYMTESTCaseID:PIM-T-RECOVERVIEW-0001 T_RecoverView"));


#if defined(_DEBUG)
	TestRecoverWhenViewReadyL();
	TestOpenDamagedDbL();
	TestOpenAndCloseTablesL();
#else
	test.Printf(_L("T_RecoverView only perform functionality testing in UDEB build"));

	// reference CntModel DLL to remove linker warning
    TRAP_IGNORE(CContactDatabase::DefaultContactDatabaseExistsL());
#endif

    test.End();
	test.Close();
    }

GLDEF_C TInt E32Main()
	{
	__UHEAP_MARK;
    // Init
    CTrapCleanup* cleanupStack = CTrapCleanup::New();
    if (!cleanupStack)
        {
        return KErrNoMemory;
        }

    CActiveScheduler* activeScheduler = new CActiveScheduler;
    if (!activeScheduler)
        {
        return KErrNoMemory;
        }
    CActiveScheduler::Install(activeScheduler);

    // Run the tests
    TRAPD(err, DoTestsL());
	User::LeaveIfError(err);

    // Cleanup
    delete activeScheduler;
    delete cleanupStack;
	__UHEAP_MARKEND;
	return err;
    }