|
1 /* Any copyright is dedicated to the Public Domain. |
|
2 http://creativecommons.org/publicdomain/zero/1.0/ */ |
|
3 |
|
4 Cu.import("resource://gre/modules/Log.jsm"); |
|
5 Cu.import("resource://services-sync/record.js"); |
|
6 Cu.import("resource://services-sync/engines.js"); |
|
7 Cu.import("resource://services-sync/engines/bookmarks.js"); |
|
8 Cu.import("resource://services-sync/util.js"); |
|
9 Cu.import("resource://services-sync/service.js"); |
|
10 Cu.import("resource://gre/modules/PlacesUtils.jsm"); |
|
11 Cu.import("resource://testing-common/services-common/utils.js"); |
|
12 |
|
13 const DESCRIPTION_ANNO = "bookmarkProperties/description"; |
|
14 |
|
15 let engine = Service.engineManager.get("bookmarks"); |
|
16 let store = engine._store; |
|
17 |
|
18 // Record borrowed from Bug 631361. |
|
19 let record631361 = { |
|
20 id: "M5bwUKK8hPyF", |
|
21 index: 150, |
|
22 modified: 1296768176.49, |
|
23 payload: |
|
24 {"id":"M5bwUKK8hPyF", |
|
25 "type":"livemark", |
|
26 "siteUri":"http://www.bbc.co.uk/go/rss/int/news/-/news/", |
|
27 "feedUri":"http://fxfeeds.mozilla.com/en-US/firefox/headlines.xml", |
|
28 "parentName":"Bookmarks Toolbar", |
|
29 "parentid":"toolbar", |
|
30 "title":"Latest Headlines", |
|
31 "description":"", |
|
32 "children": |
|
33 ["7oBdEZB-8BMO", "SUd1wktMNCTB", "eZe4QWzo1BcY", "YNBhGwhVnQsN", |
|
34 "92Aw2SMEkFg0", "uw0uKqrVFwd-", "x7mx2P3--8FJ", "d-jVF8UuC9Ye", |
|
35 "DV1XVtKLEiZ5", "g4mTaTjr837Z", "1Zi5W3lwBw8T", "FEYqlUHtbBWS", |
|
36 "qQd2u7LjosCB", "VUs2djqYfbvn", "KuhYnHocu7eg", "u2gcg9ILRg-3", |
|
37 "hfK_RP-EC7Ol", "Aq5qsa4E5msH", "6pZIbxuJTn-K", "k_fp0iN3yYMR", |
|
38 "59YD3iNOYO8O", "01afpSdAk2iz", "Cq-kjXDEPIoP", "HtNTjt9UwWWg", |
|
39 "IOU8QRSrTR--", "HJ5lSlBx6d1D", "j2dz5R5U6Khc", "5GvEjrNR0yJl", |
|
40 "67ozIBF5pNVP", "r5YB0cUx6C_w", "FtmFDBNxDQ6J", "BTACeZq9eEtw", |
|
41 "ll4ozQ-_VNJe", "HpImsA4_XuW7", "nJvCUQPLSXwA", "94LG-lh6TUYe", |
|
42 "WHn_QoOL94Os", "l-RvjgsZYlej", "LipQ8abcRstN", "74TiLvarE3n_", |
|
43 "8fCiLQpQGK1P", "Z6h4WkbwfQFa", "GgAzhqakoS6g", "qyt92T8vpMsK", |
|
44 "RyOgVCe2EAOE", "bgSEhW3w6kk5", "hWODjHKGD7Ph", "Cky673aqOHbT", |
|
45 "gZCYT7nx3Nwu", "iJzaJxxrM58L", "rUHCRv68aY5L", "6Jc1hNJiVrV9", |
|
46 "lmNgoayZ-ym8", "R1lyXsDzlfOd", "pinrXwDnRk6g", "Sn7TmZV01vMM", |
|
47 "qoXyU6tcS1dd", "TRLanED-QfBK", "xHbhMeX_FYEA", "aPqacdRlAtaW", |
|
48 "E3H04Wn2RfSi", "eaSIMI6kSrcz", "rtkRxFoG5Vqi", "dectkUglV0Dz", |
|
49 "B4vUE0BE15No", "qgQFW5AQrgB0", "SxAXvwOhu8Zi", "0S6cRPOg-5Z2", |
|
50 "zcZZBGeLnaWW", "B0at8hkQqVZQ", "sgPtgGulbP66", "lwtwGHSCPYaQ", |
|
51 "mNTdpgoRZMbW", "-L8Vci6CbkJY", "bVzudKSQERc1", "Gxl9lb4DXsmL", |
|
52 "3Qr13GucOtEh"]}, |
|
53 collection: "bookmarks" |
|
54 }; |
|
55 |
|
56 // Clean up after other tests. Only necessary in XULRunner. |
|
57 store.wipe(); |
|
58 |
|
59 function makeLivemark(p, mintGUID) { |
|
60 let b = new Livemark("bookmarks", p.id); |
|
61 // Copy here, because tests mutate the contents. |
|
62 b.cleartext = TestingUtils.deepCopy(p); |
|
63 |
|
64 if (mintGUID) |
|
65 b.id = Utils.makeGUID(); |
|
66 |
|
67 return b; |
|
68 } |
|
69 |
|
70 |
|
71 function run_test() { |
|
72 initTestLogging("Trace"); |
|
73 Log.repository.getLogger("Sync.Engine.Bookmarks").level = Log.Level.Trace; |
|
74 Log.repository.getLogger("Sync.Store.Bookmarks").level = Log.Level.Trace; |
|
75 |
|
76 run_next_test(); |
|
77 } |
|
78 |
|
79 add_test(function test_livemark_descriptions() { |
|
80 let record = record631361.payload; |
|
81 |
|
82 function doRecord(r) { |
|
83 store._childrenToOrder = {}; |
|
84 store.applyIncoming(r); |
|
85 store._orderChildren(); |
|
86 delete store._childrenToOrder; |
|
87 } |
|
88 |
|
89 // Attempt to provoke an error by messing around with the description. |
|
90 record.description = null; |
|
91 doRecord(makeLivemark(record)); |
|
92 record.description = ""; |
|
93 doRecord(makeLivemark(record)); |
|
94 |
|
95 // Attempt to provoke an error by adding a bad description anno. |
|
96 let id = store.idForGUID(record.id); |
|
97 PlacesUtils.annotations.setItemAnnotation(id, DESCRIPTION_ANNO, "", 0, |
|
98 PlacesUtils.annotations.EXPIRE_NEVER); |
|
99 |
|
100 run_next_test(); |
|
101 }); |
|
102 |
|
103 add_test(function test_livemark_invalid() { |
|
104 _("Livemarks considered invalid by nsLivemarkService are skipped."); |
|
105 |
|
106 _("Parent is 0, which is invalid. Will be set to unfiled."); |
|
107 let noParentRec = makeLivemark(record631361.payload, true); |
|
108 noParentRec._parent = 0; |
|
109 store.create(noParentRec); |
|
110 let recID = store.idForGUID(noParentRec.id, true); |
|
111 do_check_true(recID > 0); |
|
112 do_check_eq(PlacesUtils.bookmarks.getFolderIdForItem(recID), PlacesUtils.bookmarks.unfiledBookmarksFolder); |
|
113 |
|
114 _("Parent is unknown. Will be set to unfiled."); |
|
115 let lateParentRec = makeLivemark(record631361.payload, true); |
|
116 let parentGUID = Utils.makeGUID(); |
|
117 lateParentRec.parentid = parentGUID; |
|
118 lateParentRec._parent = store.idForGUID(parentGUID); // Usually done by applyIncoming. |
|
119 do_check_eq(-1, lateParentRec._parent); |
|
120 |
|
121 store.create(lateParentRec); |
|
122 recID = store.idForGUID(lateParentRec.id, true); |
|
123 do_check_true(recID > 0); |
|
124 do_check_eq(PlacesUtils.bookmarks.getFolderIdForItem(recID), |
|
125 PlacesUtils.bookmarks.unfiledBookmarksFolder); |
|
126 |
|
127 _("No feed URI, which is invalid. Will be skipped."); |
|
128 let noFeedURIRec = makeLivemark(record631361.payload, true); |
|
129 delete noFeedURIRec.cleartext.feedUri; |
|
130 store.create(noFeedURIRec); |
|
131 // No exception, but no creation occurs. |
|
132 do_check_eq(-1, store.idForGUID(noFeedURIRec.id, true)); |
|
133 |
|
134 _("Parent is a Livemark. Will be skipped."); |
|
135 let lmParentRec = makeLivemark(record631361.payload, true); |
|
136 lmParentRec._parent = recID; |
|
137 store.create(lmParentRec); |
|
138 // No exception, but no creation occurs. |
|
139 do_check_eq(-1, store.idForGUID(lmParentRec.id, true)); |
|
140 |
|
141 // Clear event loop. |
|
142 Utils.nextTick(run_next_test); |
|
143 }); |