services/sync/tests/unit/test_bookmark_livemarks.js

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

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

mercurial