PageRenderTime 30ms CodeModel.GetById 18ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 0ms

/services/sync/tests/unit/test_bookmark_livemarks.js

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