|
1 // Copyright (c) 2005-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 // |
|
15 |
|
16 #include <bookmarkdatabase.h> |
|
17 #include "testutils.h" |
|
18 |
|
19 _LIT(KFilename, "tbookmarktests.cpp"); |
|
20 |
|
21 _LIT(KTxtFolder1, "Folder 1"); |
|
22 _LIT(KTxtFolder1_1, "Folder 1.1"); |
|
23 _LIT(KTxtFolder1_2, "Folder 1.2"); |
|
24 _LIT(KTxtFolder1_2_1, "Folder 1.2.1"); |
|
25 _LIT(KTxtFolder1_2_1_1, "Folder 1.2.1.1"); |
|
26 |
|
27 _LIT(KTxtBookmark1, "Bookmark 1"); |
|
28 _LIT(KTxtBookmark2, "Bookmark 2"); |
|
29 _LIT(KTxtBookmark3, "Bookmark 3"); |
|
30 _LIT(KTxtBookmark4, "Bookmark 4"); |
|
31 _LIT(KTxtBookmark5, "Bookmark 5"); |
|
32 _LIT(KTxtBookmark6, "Bookmark 6"); |
|
33 _LIT(KTxtBookmark7, "Bookmark 7"); |
|
34 _LIT(KTxtBookmark8, "Bookmark 8"); |
|
35 _LIT(KTxtBookmark9, "Bookmark 9"); |
|
36 _LIT(KTxtBookmark10, "Bookmark 10"); |
|
37 |
|
38 _LIT8(KIconData, "****4644372131**7*&879"); |
|
39 |
|
40 LOCAL_D CTestWrapper* gTestWrapper; |
|
41 LOCAL_D RBkDatabase gDatabase; |
|
42 |
|
43 LOCAL_D void CreateBookmarkTreeL() |
|
44 { |
|
45 Bookmark::TAttachmentId iconId = gDatabase.CreateIconL(KIconData); |
|
46 |
|
47 // Create the first two bookmarks in the root |
|
48 RBkBookmark bmrk = gDatabase.CreateBookmarkL(); |
|
49 CleanupClosePushL(bmrk); |
|
50 bmrk.SetTitleL(KTxtBookmark1); |
|
51 bmrk.SetIconIdL(iconId); |
|
52 CleanupStack::PopAndDestroy(&bmrk); |
|
53 |
|
54 bmrk = gDatabase.CreateBookmarkL(); |
|
55 CleanupClosePushL(bmrk); |
|
56 bmrk.SetTitleL(KTxtBookmark2); |
|
57 |
|
58 CleanupStack::PopAndDestroy(&bmrk); |
|
59 |
|
60 // Create a folder and put 2 bookmarks into it |
|
61 RBkFolder folder = gDatabase.CreateFolderL(KTxtFolder1); |
|
62 CleanupClosePushL(folder); |
|
63 bmrk = gDatabase.CreateBookmarkL(&folder); |
|
64 CleanupClosePushL(bmrk); |
|
65 bmrk.SetTitleL(KTxtBookmark3); |
|
66 CleanupStack::PopAndDestroy(&bmrk); |
|
67 bmrk = gDatabase.CreateBookmarkL(&folder); |
|
68 CleanupClosePushL(bmrk); |
|
69 bmrk.SetTitleL(KTxtBookmark4); |
|
70 CleanupStack::PopAndDestroy(&bmrk); |
|
71 |
|
72 // Create two more folders in this folder and place a bookmark in each one |
|
73 RBkFolder folderL2 = gDatabase.CreateFolderL(KTxtFolder1_1, &folder); |
|
74 CleanupClosePushL(folderL2); |
|
75 bmrk = gDatabase.CreateBookmarkL(&folderL2); |
|
76 CleanupClosePushL(bmrk); |
|
77 bmrk.SetTitleL(KTxtBookmark5); |
|
78 CleanupStack::PopAndDestroy(&bmrk); |
|
79 CleanupStack::PopAndDestroy(&folderL2); |
|
80 |
|
81 folderL2 = gDatabase.CreateFolderL(KTxtFolder1_2, &folder); |
|
82 CleanupClosePushL(folderL2); |
|
83 bmrk = gDatabase.CreateBookmarkL(&folderL2); |
|
84 CleanupClosePushL(bmrk); |
|
85 bmrk.SetTitleL(KTxtBookmark6); |
|
86 CleanupStack::PopAndDestroy(&bmrk); |
|
87 |
|
88 // Create a folder under 1_2 and add a bookmark |
|
89 RBkFolder folderL3 = gDatabase.CreateFolderL(KTxtFolder1_2_1, &folderL2); |
|
90 CleanupClosePushL(folderL3); |
|
91 bmrk = gDatabase.CreateBookmarkL(&folderL3); |
|
92 CleanupClosePushL(bmrk); |
|
93 bmrk.SetTitleL(KTxtBookmark7); |
|
94 CleanupStack::PopAndDestroy(&bmrk); |
|
95 |
|
96 // Create a folder under 1_2_1 and add 3 bookmarks |
|
97 RBkFolder folderL4 = gDatabase.CreateFolderL(KTxtFolder1_2_1_1, &folderL3); |
|
98 CleanupClosePushL(folderL4); |
|
99 bmrk = gDatabase.CreateBookmarkL(&folderL4); |
|
100 CleanupClosePushL(bmrk); |
|
101 bmrk.SetTitleL(KTxtBookmark8); |
|
102 CleanupStack::PopAndDestroy(&bmrk); |
|
103 bmrk = gDatabase.CreateBookmarkL(&folderL4); |
|
104 CleanupClosePushL(bmrk); |
|
105 bmrk.SetTitleL(KTxtBookmark9); |
|
106 CleanupStack::PopAndDestroy(&bmrk); |
|
107 bmrk = gDatabase.CreateBookmarkL(&folderL4); |
|
108 CleanupClosePushL(bmrk); |
|
109 bmrk.SetTitleL(KTxtBookmark10); |
|
110 CleanupStack::PopAndDestroy(&bmrk); |
|
111 |
|
112 |
|
113 CleanupStack::PopAndDestroy(&folderL4); |
|
114 CleanupStack::PopAndDestroy(&folderL3); |
|
115 CleanupStack::PopAndDestroy(&folderL2); |
|
116 CleanupStack::PopAndDestroy(&folder); |
|
117 } |
|
118 |
|
119 void CreateSomeBookmarksL() |
|
120 { |
|
121 RBkDatabase database; |
|
122 database.OpenL(); |
|
123 CleanupClosePushL(gDatabase); |
|
124 |
|
125 // Create the first two bookmarks in the root |
|
126 RBkBookmark bmrk = database.CreateBookmarkL(); |
|
127 CleanupClosePushL(bmrk); |
|
128 bmrk.SetTitleL(KTxtBookmark1); |
|
129 CleanupStack::PopAndDestroy(&bmrk); |
|
130 |
|
131 bmrk = database.CreateBookmarkL(); |
|
132 CleanupClosePushL(bmrk); |
|
133 bmrk.SetTitleL(KTxtBookmark2); |
|
134 CleanupStack::PopAndDestroy(&bmrk); |
|
135 |
|
136 // Create a folder |
|
137 RBkFolder folder = database.CreateFolderL(KTxtFolder1); |
|
138 CleanupClosePushL(folder); |
|
139 |
|
140 // Put 2 bookmarks into the new folder |
|
141 bmrk = database.CreateBookmarkL(&folder); |
|
142 CleanupClosePushL(bmrk); |
|
143 bmrk.SetTitleL(KTxtBookmark3); |
|
144 CleanupStack::PopAndDestroy(&bmrk); |
|
145 bmrk = database.CreateBookmarkL(&folder); |
|
146 CleanupClosePushL(bmrk); |
|
147 bmrk.SetTitleL(KTxtBookmark4); |
|
148 CleanupStack::PopAndDestroy(&bmrk); |
|
149 |
|
150 // Close the folder |
|
151 CleanupStack::PopAndDestroy(&folder); |
|
152 |
|
153 // Commit the changes to the database and close it |
|
154 database.CommitL(); |
|
155 CleanupStack::PopAndDestroy(&database); |
|
156 } |
|
157 |
|
158 _LIT8(KFolderIconData, "****4644372131**7*&879"); |
|
159 _LIT8(KBookmarkIconData, "****46=-=-=4372131**7*&879"); |
|
160 _LIT(KTxtFolderDesc, "This is the folder description"); |
|
161 _LIT(KTxtBookmarkDesc, "This is the bookmark description"); |
|
162 _LIT8(KBookmarkUri, "www.ft.com"); |
|
163 _LIT8(KSearchUri, "www.google.com"); |
|
164 _LIT(KHomePageDesc, "This is the home page description"); |
|
165 |
|
166 void SettingStandardPropertiesL() |
|
167 { |
|
168 // Open the database |
|
169 RBkDatabase database; |
|
170 database.OpenL(); |
|
171 CleanupClosePushL(database); |
|
172 |
|
173 // Create some icons |
|
174 Bookmark::TAttachmentId folderIconId = database.CreateIconL(KFolderIconData); |
|
175 Bookmark::TAttachmentId bookmarkIconId = database.CreateIconL(KBookmarkIconData); |
|
176 |
|
177 // Create a folder |
|
178 RBkFolder folder = database.CreateFolderL(KTxtFolder1); |
|
179 CleanupClosePushL(folder); |
|
180 // Set some folder properties |
|
181 folder.SetDescriptionL(KTxtFolderDesc); |
|
182 folder.SetIconIdL(folderIconId); |
|
183 |
|
184 // Create a bookmark in the new folder |
|
185 RBkBookmark bmrk = database.CreateBookmarkL(&folder); |
|
186 CleanupClosePushL(bmrk); |
|
187 // Set some bookmark properties |
|
188 bmrk.SetTitleL(KTxtBookmark1); |
|
189 bmrk.SetDescriptionL(KTxtBookmarkDesc); |
|
190 bmrk.SetUriL(KBookmarkUri); |
|
191 bmrk.SetIconIdL(bookmarkIconId); |
|
192 |
|
193 // Set some database properties |
|
194 database.SetHomePageTextL(KHomePageDesc); |
|
195 database.SetHome(bmrk); |
|
196 database.SetSearchUriL(KSearchUri); |
|
197 |
|
198 // Close the bookmark |
|
199 CleanupStack::PopAndDestroy(&bmrk); |
|
200 |
|
201 // Close the folder |
|
202 CleanupStack::PopAndDestroy(&folder); |
|
203 |
|
204 // Commit the changes to the database and close it |
|
205 database.CommitL(); |
|
206 CleanupStack::PopAndDestroy(&database); |
|
207 } |
|
208 |
|
209 void CreatingAnIconL() |
|
210 { |
|
211 // Open the database |
|
212 RBkDatabase database; |
|
213 database.OpenL(); |
|
214 CleanupClosePushL(database); |
|
215 |
|
216 // Create an icon |
|
217 Bookmark::TAttachmentId bookmarkIconId = database.CreateIconL(KBookmarkIconData); |
|
218 |
|
219 // Create a bookmark in the root |
|
220 RBkBookmark bmrk = database.CreateBookmarkL(); |
|
221 CleanupClosePushL(bmrk); |
|
222 // Set some bookmark properties |
|
223 bmrk.SetIconIdL(bookmarkIconId); |
|
224 // Close the bookmark |
|
225 CleanupStack::PopAndDestroy(&bmrk); |
|
226 |
|
227 // Commit the changes to the database and close it |
|
228 database.CommitL(); |
|
229 CleanupStack::PopAndDestroy(&database); |
|
230 } |
|
231 |
|
232 void CreateBookmarkWithCustomPropertiesL() |
|
233 { |
|
234 TUid des16PropertyUid = {0x00001234}; |
|
235 TUid des8PropertyUid = {0x00001235}; |
|
236 TUid intPropertyUid = {0x00001236}; |
|
237 TUid realPropertyUid = {0x00001237}; |
|
238 |
|
239 _LIT(KTxtFirst16EditorName, "Joe Bloggs"); |
|
240 _LIT(KTxtSecond16EditorName, "Joe Bloggs II"); |
|
241 |
|
242 _LIT8(KTxtFirst8ReferrerName, "Krishna"); |
|
243 _LIT8(KTxtSecond8RefererName, "Ganesh"); |
|
244 |
|
245 // Register a custom propery |
|
246 gDatabase.RegisterBookmarkPropertyL(des16PropertyUid, Bookmark::EDataTypeDes16); |
|
247 |
|
248 // Register a custom propery |
|
249 gDatabase.RegisterBookmarkPropertyL(des8PropertyUid, Bookmark::EDataTypeDes8); |
|
250 |
|
251 // Register a custom propery |
|
252 gDatabase.RegisterBookmarkPropertyL(intPropertyUid, Bookmark::EDataTypeInt); |
|
253 |
|
254 // Register a custom propery |
|
255 gDatabase.RegisterBookmarkPropertyL(realPropertyUid, Bookmark::EDataTypeReal); |
|
256 |
|
257 // Create a bookmark in the root |
|
258 RBkBookmark bmrk = gDatabase.CreateBookmarkL(); |
|
259 CleanupClosePushL(bmrk); |
|
260 bmrk.SetTitleL(KTxtBookmark1); |
|
261 |
|
262 //Added twice to check for memory leak described in INC067424 |
|
263 |
|
264 // Set the custom property value |
|
265 bmrk.SetCustomPropertyL(des16PropertyUid, KTxtFirst16EditorName); |
|
266 bmrk.SetCustomPropertyL(des16PropertyUid, KTxtSecond16EditorName); |
|
267 |
|
268 // Set the custom property value |
|
269 bmrk.SetCustomPropertyL(des8PropertyUid, KTxtFirst8ReferrerName); |
|
270 bmrk.SetCustomPropertyL(des8PropertyUid, KTxtSecond8RefererName); |
|
271 |
|
272 |
|
273 // Set the custom property value |
|
274 bmrk.SetCustomPropertyL(intPropertyUid, 1); |
|
275 bmrk.SetCustomPropertyL(intPropertyUid, 2); |
|
276 |
|
277 // Set the custom property value |
|
278 bmrk.SetCustomPropertyL(realPropertyUid, 3.14159); |
|
279 bmrk.SetCustomPropertyL(realPropertyUid, 3.14159); |
|
280 |
|
281 |
|
282 CleanupStack::PopAndDestroy(&bmrk); |
|
283 |
|
284 } |
|
285 |
|
286 Bookmark::TItemId FindBookmarkIdL(const TDesC& aName, RBkFolder aFolder) |
|
287 { |
|
288 RBkNode item; |
|
289 Bookmark::TItemId id = Bookmark::KNullItemID; |
|
290 for (TInt index = aFolder.Count() - 1; index >= 0; --index) |
|
291 { |
|
292 // open the child item at this index value |
|
293 item = aFolder.OpenItemL(index); |
|
294 CleanupClosePushL(item); |
|
295 // check the title |
|
296 const TDesC& title = item.Title(); |
|
297 if (item.Type() == Bookmark::ETypeBookmark && title.Compare(aName) == 0) |
|
298 { |
|
299 id = item.Id(); |
|
300 } |
|
301 else if (item.Type() == Bookmark::ETypeFolder) |
|
302 { |
|
303 // if the item is a folder, open as a folder and call FindBookmarkL to |
|
304 // check its children |
|
305 RBkFolder folder = item.OpenFolderL(); |
|
306 CleanupClosePushL(folder); |
|
307 id = FindBookmarkIdL(aName, folder); |
|
308 // close the folder |
|
309 CleanupStack::PopAndDestroy(&folder); |
|
310 } |
|
311 // close the child item |
|
312 CleanupStack::PopAndDestroy(&item); |
|
313 if (id != Bookmark::KNullItemID) |
|
314 { |
|
315 // the bookmark has been found so no need to look at any more items |
|
316 break; |
|
317 } |
|
318 } |
|
319 return id; |
|
320 } |
|
321 |
|
322 |
|
323 LOCAL_D TBool IsItemInFolderL(const TDesC& aName, RBkFolder aFolder) |
|
324 { |
|
325 RBkNode item; |
|
326 TBool found = EFalse; |
|
327 TInt index = aFolder.Count() - 1; |
|
328 for (;index >= 0; --index) |
|
329 { |
|
330 item = aFolder.OpenItemL(index); |
|
331 CleanupClosePushL(item); |
|
332 const TDesC& title = item.Title(); |
|
333 if (title.Compare(aName) == 0) |
|
334 { |
|
335 found = ETrue; |
|
336 } |
|
337 CleanupStack::PopAndDestroy(&item); |
|
338 if (found) |
|
339 { |
|
340 break; |
|
341 } |
|
342 } |
|
343 return found; |
|
344 } |
|
345 |
|
346 LOCAL_D void CreateBookmarksL() |
|
347 { |
|
348 gTestWrapper->Next(_L("[Bookmark tests] create bookmarks")); |
|
349 |
|
350 CreateBookmarkTreeL(); |
|
351 |
|
352 // Check there are 3 items in the root (2 bookmarks and 1 folder) |
|
353 RBkFolder folder = gDatabase.OpenRootL(); |
|
354 CleanupClosePushL(folder); |
|
355 gTestWrapper->TEST(folder.Count() == 3); |
|
356 // Check the titles are correct |
|
357 gTestWrapper->TEST(IsItemInFolderL(KTxtFolder1, folder)); |
|
358 gTestWrapper->TEST(IsItemInFolderL(KTxtBookmark1, folder)); |
|
359 gTestWrapper->TEST(IsItemInFolderL(KTxtBookmark2, folder)); |
|
360 CleanupStack::PopAndDestroy(&folder); |
|
361 |
|
362 // Open folder 1 and check there are 4 items (2 bookmarks and 2 folders) |
|
363 folder = gDatabase.OpenFolderL(KTxtFolder1); |
|
364 CleanupClosePushL(folder); |
|
365 gTestWrapper->TEST(folder.Count() == 4); |
|
366 // Check the titles are correct |
|
367 gTestWrapper->TEST(IsItemInFolderL(KTxtFolder1_1, folder)); |
|
368 gTestWrapper->TEST(IsItemInFolderL(KTxtFolder1_2, folder)); |
|
369 gTestWrapper->TEST(IsItemInFolderL(KTxtBookmark3, folder)); |
|
370 gTestWrapper->TEST(IsItemInFolderL(KTxtBookmark4, folder)); |
|
371 CleanupStack::PopAndDestroy(&folder); |
|
372 |
|
373 // Open folder 1.1 and check there is 1 item |
|
374 folder = gDatabase.OpenFolderL(KTxtFolder1_1); |
|
375 CleanupClosePushL(folder); |
|
376 gTestWrapper->TEST(folder.Count() == 1); |
|
377 // Check the title is correct |
|
378 gTestWrapper->TEST(IsItemInFolderL(KTxtBookmark5, folder)); |
|
379 CleanupStack::PopAndDestroy(&folder); |
|
380 |
|
381 // Open folder 1.2 and check there are 2 items (1 bookmark and 1 folder) |
|
382 folder = gDatabase.OpenFolderL(KTxtFolder1_2); |
|
383 CleanupClosePushL(folder); |
|
384 gTestWrapper->TEST(folder.Count() == 2); |
|
385 // Check the titles are correct |
|
386 gTestWrapper->TEST(IsItemInFolderL(KTxtFolder1_2_1, folder)); |
|
387 gTestWrapper->TEST(IsItemInFolderL(KTxtBookmark6, folder)); |
|
388 CleanupStack::PopAndDestroy(&folder); |
|
389 |
|
390 // Open folder 1.2.1 and check there are 2 items (1 bookmark and 1 folder) |
|
391 folder = gDatabase.OpenFolderL(KTxtFolder1_2_1); |
|
392 CleanupClosePushL(folder); |
|
393 gTestWrapper->TEST(folder.Count() == 2); |
|
394 // Check the titles are correct |
|
395 gTestWrapper->TEST(IsItemInFolderL(KTxtFolder1_2_1_1, folder)); |
|
396 gTestWrapper->TEST(IsItemInFolderL(KTxtBookmark7, folder)); |
|
397 CleanupStack::PopAndDestroy(&folder); |
|
398 |
|
399 // Open folder 1.2.1.1 and check there are 3 items (3 bookmarks) |
|
400 folder = gDatabase.OpenFolderL(KTxtFolder1_2_1_1); |
|
401 CleanupClosePushL(folder); |
|
402 gTestWrapper->TEST(folder.Count() == 3); |
|
403 // Check the titles are correct |
|
404 gTestWrapper->TEST(IsItemInFolderL(KTxtBookmark8, folder)); |
|
405 gTestWrapper->TEST(IsItemInFolderL(KTxtBookmark9, folder)); |
|
406 gTestWrapper->TEST(IsItemInFolderL(KTxtBookmark10, folder)); |
|
407 CleanupStack::PopAndDestroy(&folder); |
|
408 } |
|
409 |
|
410 LOCAL_D void CreateDatabaseL() |
|
411 { |
|
412 gDatabase.OpenL(); |
|
413 CleanupClosePushL(gDatabase); |
|
414 CreateBookmarkTreeL(); |
|
415 CleanupStack::PopAndDestroy(&gDatabase); |
|
416 } |
|
417 |
|
418 LOCAL_D void OOMTestL() |
|
419 { |
|
420 gTestWrapper->Next(_L("[Bookmark tests] OOM testing")); |
|
421 TInt ret = KErrNoMemory; |
|
422 TInt failAt = 0; |
|
423 while(ret != KErrNone) |
|
424 { |
|
425 failAt++; |
|
426 gTestWrapper->Tester().Printf(_L("OOM test step: %d\n"),failAt); |
|
427 |
|
428 __UHEAP_SETFAIL(RHeap::EDeterministic, failAt); |
|
429 __UHEAP_MARK; |
|
430 TRAP(ret, CreateDatabaseL()); |
|
431 __UHEAP_MARKEND; |
|
432 __UHEAP_RESET; |
|
433 |
|
434 gTestWrapper->TESTE(ret == KErrNoMemory || ret == KErrNone, ret); |
|
435 } |
|
436 } |
|
437 |
|
438 void DoBookmarkTestsL(CTestWrapper* aTest) |
|
439 { |
|
440 gTestWrapper = aTest; |
|
441 gTestWrapper->SetFile(KFilename); |
|
442 gTestWrapper->Next(_L("*** Bookmark tests ***")); |
|
443 |
|
444 gDatabase.OpenL(); |
|
445 CleanupClosePushL(gDatabase); |
|
446 |
|
447 CreateBookmarksL(); |
|
448 CreateBookmarkWithCustomPropertiesL(); |
|
449 |
|
450 CleanupStack::PopAndDestroy(&gDatabase); |
|
451 |
|
452 OOMTestL(); |
|
453 |
|
454 |
|
455 } |