michael@0: /* Any copyright is dedicated to the Public Domain. michael@0: http://creativecommons.org/publicdomain/zero/1.0/ */ michael@0: michael@0: Cu.import("resource://gre/modules/Log.jsm"); michael@0: Cu.import("resource://services-sync/record.js"); michael@0: Cu.import("resource://services-sync/engines.js"); michael@0: Cu.import("resource://services-sync/engines/bookmarks.js"); michael@0: Cu.import("resource://services-sync/util.js"); michael@0: Cu.import("resource://services-sync/service.js"); michael@0: Cu.import("resource://gre/modules/PlacesUtils.jsm"); michael@0: Cu.import("resource://testing-common/services-common/utils.js"); michael@0: michael@0: const DESCRIPTION_ANNO = "bookmarkProperties/description"; michael@0: michael@0: let engine = Service.engineManager.get("bookmarks"); michael@0: let store = engine._store; michael@0: michael@0: // Record borrowed from Bug 631361. michael@0: let record631361 = { michael@0: id: "M5bwUKK8hPyF", michael@0: index: 150, michael@0: modified: 1296768176.49, michael@0: payload: michael@0: {"id":"M5bwUKK8hPyF", michael@0: "type":"livemark", michael@0: "siteUri":"http://www.bbc.co.uk/go/rss/int/news/-/news/", michael@0: "feedUri":"http://fxfeeds.mozilla.com/en-US/firefox/headlines.xml", michael@0: "parentName":"Bookmarks Toolbar", michael@0: "parentid":"toolbar", michael@0: "title":"Latest Headlines", michael@0: "description":"", michael@0: "children": michael@0: ["7oBdEZB-8BMO", "SUd1wktMNCTB", "eZe4QWzo1BcY", "YNBhGwhVnQsN", michael@0: "92Aw2SMEkFg0", "uw0uKqrVFwd-", "x7mx2P3--8FJ", "d-jVF8UuC9Ye", michael@0: "DV1XVtKLEiZ5", "g4mTaTjr837Z", "1Zi5W3lwBw8T", "FEYqlUHtbBWS", michael@0: "qQd2u7LjosCB", "VUs2djqYfbvn", "KuhYnHocu7eg", "u2gcg9ILRg-3", michael@0: "hfK_RP-EC7Ol", "Aq5qsa4E5msH", "6pZIbxuJTn-K", "k_fp0iN3yYMR", michael@0: "59YD3iNOYO8O", "01afpSdAk2iz", "Cq-kjXDEPIoP", "HtNTjt9UwWWg", michael@0: "IOU8QRSrTR--", "HJ5lSlBx6d1D", "j2dz5R5U6Khc", "5GvEjrNR0yJl", michael@0: "67ozIBF5pNVP", "r5YB0cUx6C_w", "FtmFDBNxDQ6J", "BTACeZq9eEtw", michael@0: "ll4ozQ-_VNJe", "HpImsA4_XuW7", "nJvCUQPLSXwA", "94LG-lh6TUYe", michael@0: "WHn_QoOL94Os", "l-RvjgsZYlej", "LipQ8abcRstN", "74TiLvarE3n_", michael@0: "8fCiLQpQGK1P", "Z6h4WkbwfQFa", "GgAzhqakoS6g", "qyt92T8vpMsK", michael@0: "RyOgVCe2EAOE", "bgSEhW3w6kk5", "hWODjHKGD7Ph", "Cky673aqOHbT", michael@0: "gZCYT7nx3Nwu", "iJzaJxxrM58L", "rUHCRv68aY5L", "6Jc1hNJiVrV9", michael@0: "lmNgoayZ-ym8", "R1lyXsDzlfOd", "pinrXwDnRk6g", "Sn7TmZV01vMM", michael@0: "qoXyU6tcS1dd", "TRLanED-QfBK", "xHbhMeX_FYEA", "aPqacdRlAtaW", michael@0: "E3H04Wn2RfSi", "eaSIMI6kSrcz", "rtkRxFoG5Vqi", "dectkUglV0Dz", michael@0: "B4vUE0BE15No", "qgQFW5AQrgB0", "SxAXvwOhu8Zi", "0S6cRPOg-5Z2", michael@0: "zcZZBGeLnaWW", "B0at8hkQqVZQ", "sgPtgGulbP66", "lwtwGHSCPYaQ", michael@0: "mNTdpgoRZMbW", "-L8Vci6CbkJY", "bVzudKSQERc1", "Gxl9lb4DXsmL", michael@0: "3Qr13GucOtEh"]}, michael@0: collection: "bookmarks" michael@0: }; michael@0: michael@0: // Clean up after other tests. Only necessary in XULRunner. michael@0: store.wipe(); michael@0: michael@0: function makeLivemark(p, mintGUID) { michael@0: let b = new Livemark("bookmarks", p.id); michael@0: // Copy here, because tests mutate the contents. michael@0: b.cleartext = TestingUtils.deepCopy(p); michael@0: michael@0: if (mintGUID) michael@0: b.id = Utils.makeGUID(); michael@0: michael@0: return b; michael@0: } michael@0: michael@0: michael@0: function run_test() { michael@0: initTestLogging("Trace"); michael@0: Log.repository.getLogger("Sync.Engine.Bookmarks").level = Log.Level.Trace; michael@0: Log.repository.getLogger("Sync.Store.Bookmarks").level = Log.Level.Trace; michael@0: michael@0: run_next_test(); michael@0: } michael@0: michael@0: add_test(function test_livemark_descriptions() { michael@0: let record = record631361.payload; michael@0: michael@0: function doRecord(r) { michael@0: store._childrenToOrder = {}; michael@0: store.applyIncoming(r); michael@0: store._orderChildren(); michael@0: delete store._childrenToOrder; michael@0: } michael@0: michael@0: // Attempt to provoke an error by messing around with the description. michael@0: record.description = null; michael@0: doRecord(makeLivemark(record)); michael@0: record.description = ""; michael@0: doRecord(makeLivemark(record)); michael@0: michael@0: // Attempt to provoke an error by adding a bad description anno. michael@0: let id = store.idForGUID(record.id); michael@0: PlacesUtils.annotations.setItemAnnotation(id, DESCRIPTION_ANNO, "", 0, michael@0: PlacesUtils.annotations.EXPIRE_NEVER); michael@0: michael@0: run_next_test(); michael@0: }); michael@0: michael@0: add_test(function test_livemark_invalid() { michael@0: _("Livemarks considered invalid by nsLivemarkService are skipped."); michael@0: michael@0: _("Parent is 0, which is invalid. Will be set to unfiled."); michael@0: let noParentRec = makeLivemark(record631361.payload, true); michael@0: noParentRec._parent = 0; michael@0: store.create(noParentRec); michael@0: let recID = store.idForGUID(noParentRec.id, true); michael@0: do_check_true(recID > 0); michael@0: do_check_eq(PlacesUtils.bookmarks.getFolderIdForItem(recID), PlacesUtils.bookmarks.unfiledBookmarksFolder); michael@0: michael@0: _("Parent is unknown. Will be set to unfiled."); michael@0: let lateParentRec = makeLivemark(record631361.payload, true); michael@0: let parentGUID = Utils.makeGUID(); michael@0: lateParentRec.parentid = parentGUID; michael@0: lateParentRec._parent = store.idForGUID(parentGUID); // Usually done by applyIncoming. michael@0: do_check_eq(-1, lateParentRec._parent); michael@0: michael@0: store.create(lateParentRec); michael@0: recID = store.idForGUID(lateParentRec.id, true); michael@0: do_check_true(recID > 0); michael@0: do_check_eq(PlacesUtils.bookmarks.getFolderIdForItem(recID), michael@0: PlacesUtils.bookmarks.unfiledBookmarksFolder); michael@0: michael@0: _("No feed URI, which is invalid. Will be skipped."); michael@0: let noFeedURIRec = makeLivemark(record631361.payload, true); michael@0: delete noFeedURIRec.cleartext.feedUri; michael@0: store.create(noFeedURIRec); michael@0: // No exception, but no creation occurs. michael@0: do_check_eq(-1, store.idForGUID(noFeedURIRec.id, true)); michael@0: michael@0: _("Parent is a Livemark. Will be skipped."); michael@0: let lmParentRec = makeLivemark(record631361.payload, true); michael@0: lmParentRec._parent = recID; michael@0: store.create(lmParentRec); michael@0: // No exception, but no creation occurs. michael@0: do_check_eq(-1, store.idForGUID(lmParentRec.id, true)); michael@0: michael@0: // Clear event loop. michael@0: Utils.nextTick(run_next_test); michael@0: });