toolkit/components/places/tests/bookmarks/test_nsINavBookmarkObserver.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

     1 /* Any copyright is dedicated to the Public Domain.
     2    http://creativecommons.org/publicdomain/zero/1.0/ */
     4 // Tests that each nsINavBookmarksObserver method gets the correct input.
     6 let gBookmarksObserver = {
     7   expected: [],
     8   validate: function (aMethodName, aArguments) {
     9     do_check_eq(this.expected[0].name, aMethodName);
    11     let args = this.expected.shift().args;
    12     do_check_eq(aArguments.length, args.length);
    13     for (let i = 0; i < aArguments.length; i++) {
    14       do_log_info(aMethodName + "(args[" + i + "]: " + args[i].name + ")");
    15       do_check_true(args[i].check(aArguments[i]));
    16     }
    18     if (this.expected.length == 0) {
    19       run_next_test();
    20     }
    21   },
    23   // nsINavBookmarkObserver
    24   onBeginUpdateBatch: function onBeginUpdateBatch()
    25     this.validate(arguments.callee.name, arguments),
    26   onEndUpdateBatch: function onEndUpdateBatch()
    27     this.validate(arguments.callee.name, arguments),
    28   onItemAdded: function onItemAdded()
    29     this.validate(arguments.callee.name, arguments),
    30   onItemRemoved: function onItemRemoved()
    31     this.validate(arguments.callee.name, arguments),
    32   onItemChanged: function onItemChanged()
    33     this.validate(arguments.callee.name, arguments),
    34   onItemVisited: function onItemVisited()
    35     this.validate(arguments.callee.name, arguments),
    36   onItemMoved: function onItemMoved()
    37     this.validate(arguments.callee.name, arguments),
    39   // nsISupports
    40   QueryInterface: XPCOMUtils.generateQI([Ci.nsINavBookmarkObserver]),
    41 }
    43 add_test(function batch() {
    44   gBookmarksObserver.expected = [
    45     { name: "onBeginUpdateBatch",
    46      args: [] },
    47     { name: "onEndUpdateBatch",
    48      args: [] },
    49   ];
    50   PlacesUtils.bookmarks.runInBatchMode({
    51     runBatched: function () {
    52       // Nothing.
    53     }
    54   }, null);
    55 });
    57 add_test(function onItemAdded_bookmark() {
    58   const TITLE = "Bookmark 1";
    59   let uri = NetUtil.newURI("http://1.mozilla.org/");
    60   gBookmarksObserver.expected = [
    61     { name: "onItemAdded",
    62       args: [
    63         { name: "itemId", check: function (v) typeof(v) == "number" && v > 0 },
    64         { name: "parentId", check: function (v) v === PlacesUtils.unfiledBookmarksFolderId },
    65         { name: "index", check: function (v) v === 0 },
    66         { name: "itemType", check: function (v) v === PlacesUtils.bookmarks.TYPE_BOOKMARK },
    67         { name: "uri", check: function (v) v instanceof Ci.nsIURI && v.equals(uri) },
    68         { name: "title", check: function (v) v === TITLE },
    69         { name: "dateAdded", check: function (v) typeof(v) == "number" && v > 0 },
    70         { name: "guid", check: function (v) typeof(v) == "string" && /^[a-zA-Z0-9\-_]{12}$/.test(v) },
    71         { name: "parentGuid", check: function (v) typeof(v) == "string" && /^[a-zA-Z0-9\-_]{12}$/.test(v) },
    72       ] },
    73   ];
    74   PlacesUtils.bookmarks.insertBookmark(PlacesUtils.unfiledBookmarksFolderId,
    75                                        uri, PlacesUtils.bookmarks.DEFAULT_INDEX,
    76                                        TITLE);
    77 });
    79 add_test(function onItemAdded_separator() {
    80   gBookmarksObserver.expected = [
    81     { name: "onItemAdded",
    82       args: [
    83         { name: "itemId", check: function (v) typeof(v) == "number" && v > 0 },
    84         { name: "parentId", check: function (v) v === PlacesUtils.unfiledBookmarksFolderId },
    85         { name: "index", check: function (v) v === 1 },
    86         { name: "itemType", check: function (v) v === PlacesUtils.bookmarks.TYPE_SEPARATOR },
    87         { name: "uri", check: function (v) v === null },
    88         { name: "title", check: function (v) v === null },
    89         { name: "dateAdded", check: function (v) typeof(v) == "number" && v > 0 },
    90         { name: "guid", check: function (v) typeof(v) == "string" && /^[a-zA-Z0-9\-_]{12}$/.test(v) },
    91         { name: "parentGuid", check: function (v) typeof(v) == "string" && /^[a-zA-Z0-9\-_]{12}$/.test(v) },
    92       ] },
    93   ];
    94   PlacesUtils.bookmarks.insertSeparator(PlacesUtils.unfiledBookmarksFolderId,
    95                                         PlacesUtils.bookmarks.DEFAULT_INDEX);
    96 });
    98 add_test(function onItemAdded_folder() {
    99   const TITLE = "Folder 1";
   100   gBookmarksObserver.expected = [
   101     { name: "onItemAdded",
   102       args: [
   103         { name: "itemId", check: function (v) typeof(v) == "number" && v > 0 },
   104         { name: "parentId", check: function (v) v === PlacesUtils.unfiledBookmarksFolderId },
   105         { name: "index", check: function (v) v === 2 },
   106         { name: "itemType", check: function (v) v === PlacesUtils.bookmarks.TYPE_FOLDER },
   107         { name: "uri", check: function (v) v === null },
   108         { name: "title", check: function (v) v === TITLE },
   109         { name: "dateAdded", check: function (v) typeof(v) == "number" && v > 0 },
   110         { name: "guid", check: function (v) typeof(v) == "string" && /^[a-zA-Z0-9\-_]{12}$/.test(v) },
   111         { name: "parentGuid", check: function (v) typeof(v) == "string" && /^[a-zA-Z0-9\-_]{12}$/.test(v) },
   112       ] },
   113   ];
   114   PlacesUtils.bookmarks.createFolder(PlacesUtils.unfiledBookmarksFolderId,
   115                                      TITLE,
   116                                      PlacesUtils.bookmarks.DEFAULT_INDEX);
   117 });
   119 add_test(function onItemChanged_title_bookmark() {
   120   let id = PlacesUtils.bookmarks.getIdForItemAt(PlacesUtils.unfiledBookmarksFolderId, 0);
   121   let uri = PlacesUtils.bookmarks.getBookmarkURI(id);
   122   const TITLE = "New title";
   123   gBookmarksObserver.expected = [
   124     { name: "onItemChanged", // This is an unfortunate effect of bug 653910.
   125       args: [
   126         { name: "itemId", check: function (v) typeof(v) == "number" && v > 0 },
   127         { name: "property", check: function (v) v === "title" },
   128         { name: "isAnno", check: function (v) v === false },
   129         { name: "newValue", check: function (v) v === TITLE },
   130         { name: "lastModified", check: function (v) typeof(v) == "number" && v > 0 },
   131         { name: "itemType", check: function (v) v === PlacesUtils.bookmarks.TYPE_BOOKMARK },
   132         { name: "parentId", check: function (v) v === PlacesUtils.unfiledBookmarksFolderId },
   133         { name: "guid", check: function (v) typeof(v) == "string" && /^[a-zA-Z0-9\-_]{12}$/.test(v) },
   134         { name: "parentGuid", check: function (v) typeof(v) == "string" && /^[a-zA-Z0-9\-_]{12}$/.test(v) },
   135       ] },
   136   ];
   137   PlacesUtils.bookmarks.setItemTitle(id, TITLE);
   138 });
   140 add_test(function onItemChanged_tags_bookmark() {
   141   let id = PlacesUtils.bookmarks.getIdForItemAt(PlacesUtils.unfiledBookmarksFolderId, 0);
   142   let uri = PlacesUtils.bookmarks.getBookmarkURI(id);
   143   const TITLE = "New title";
   144   const TAG = "tag"
   145   gBookmarksObserver.expected = [
   146     { name: "onBeginUpdateBatch", // Tag addition uses a batch.
   147      args: [] },
   148     { name: "onItemAdded", // This is the tag folder.
   149       args: [
   150         { name: "itemId", check: function (v) typeof(v) == "number" && v > 0 },
   151         { name: "parentId", check: function (v) v === PlacesUtils.tagsFolderId },
   152         { name: "index", check: function (v) v === 0 },
   153         { name: "itemType", check: function (v) v === PlacesUtils.bookmarks.TYPE_FOLDER },
   154         { name: "uri", check: function (v) v === null },
   155         { name: "title", check: function (v) v === TAG },
   156         { name: "dateAdded", check: function (v) typeof(v) == "number" && v > 0 },
   157         { name: "guid", check: function (v) typeof(v) == "string" && /^[a-zA-Z0-9\-_]{12}$/.test(v) },
   158         { name: "parentGuid", check: function (v) typeof(v) == "string" && /^[a-zA-Z0-9\-_]{12}$/.test(v) },
   159       ] },
   160     { name: "onItemAdded", // This is the tag.
   161       args: [
   162         { name: "itemId", check: function (v) typeof(v) == "number" && v > 0 },
   163         { name: "parentId", check: function (v) typeof(v) == "number" && v > 0 },
   164         { name: "index", check: function (v) v === 0 },
   165         { name: "itemType", check: function (v) v === PlacesUtils.bookmarks.TYPE_BOOKMARK },
   166         { name: "uri", check: function (v) v instanceof Ci.nsIURI && v.equals(uri) },
   167         { name: "title", check: function (v) v === null },
   168         { name: "dateAdded", check: function (v) typeof(v) == "number" && v > 0 },
   169         { name: "guid", check: function (v) typeof(v) == "string" && /^[a-zA-Z0-9\-_]{12}$/.test(v) },
   170         { name: "parentGuid", check: function (v) typeof(v) == "string" && /^[a-zA-Z0-9\-_]{12}$/.test(v) },
   171       ] },
   172     { name: "onItemChanged",
   173       args: [
   174         { name: "itemId", check: function (v) typeof(v) == "number" && v > 0 },
   175         { name: "property", check: function (v) v === "tags" },
   176         { name: "isAnno", check: function (v) v === false },
   177         { name: "newValue", check: function (v) v === "" },
   178         { name: "lastModified", check: function (v) typeof(v) == "number" && v > 0 },
   179         { name: "itemType", check: function (v) v === PlacesUtils.bookmarks.TYPE_BOOKMARK },
   180         { name: "parentId", check: function (v) v === PlacesUtils.unfiledBookmarksFolderId },
   181         { name: "guid", check: function (v) typeof(v) == "string" && /^[a-zA-Z0-9\-_]{12}$/.test(v) },
   182         { name: "parentGuid", check: function (v) typeof(v) == "string" && /^[a-zA-Z0-9\-_]{12}$/.test(v) },
   183       ] },
   184     { name: "onEndUpdateBatch",
   185       args: [] },
   186     { name: "onBeginUpdateBatch", // Tag removal uses a batch.
   187      args: [] },
   188     { name: "onItemRemoved", // This is the tag.
   189       args: [
   190         { name: "itemId", check: function (v) typeof(v) == "number" && v > 0 },
   191         { name: "parentId", check: function (v) typeof(v) == "number" && v > 0 },
   192         { name: "index", check: function (v) v === 0 },
   193         { name: "itemType", check: function (v) v === PlacesUtils.bookmarks.TYPE_BOOKMARK },
   194         { name: "uri", check: function (v) v instanceof Ci.nsIURI && v.equals(uri) },
   195         { name: "guid", check: function (v) typeof(v) == "string" && /^[a-zA-Z0-9\-_]{12}$/.test(v) },
   196         { name: "parentGuid", check: function (v) typeof(v) == "string" && /^[a-zA-Z0-9\-_]{12}$/.test(v) },
   197       ] },
   198     { name: "onItemRemoved", // This is the tag folder.
   199       args: [
   200         { name: "itemId", check: function (v) typeof(v) == "number" && v > 0 },
   201         { name: "parentId", check: function (v) v === PlacesUtils.tagsFolderId },
   202         { name: "index", check: function (v) v === 0 },
   203         { name: "itemType", check: function (v) v === PlacesUtils.bookmarks.TYPE_FOLDER },
   204         { name: "uri", check: function (v) v === null },
   205         { name: "guid", check: function (v) typeof(v) == "string" && /^[a-zA-Z0-9\-_]{12}$/.test(v) },
   206         { name: "parentGuid", check: function (v) typeof(v) == "string" && /^[a-zA-Z0-9\-_]{12}$/.test(v) },
   207       ] },
   208     { name: "onItemChanged",
   209       args: [
   210         { name: "itemId", check: function (v) typeof(v) == "number" && v > 0 },
   211         { name: "property", check: function (v) v === "tags" },
   212         { name: "isAnno", check: function (v) v === false },
   213         { name: "newValue", check: function (v) v === "" },
   214         { name: "lastModified", check: function (v) typeof(v) == "number" && v > 0 },
   215         { name: "itemType", check: function (v) v === PlacesUtils.bookmarks.TYPE_BOOKMARK },
   216         { name: "parentId", check: function (v) v === PlacesUtils.unfiledBookmarksFolderId },
   217         { name: "guid", check: function (v) typeof(v) == "string" && /^[a-zA-Z0-9\-_]{12}$/.test(v) },
   218         { name: "parentGuid", check: function (v) typeof(v) == "string" && /^[a-zA-Z0-9\-_]{12}$/.test(v) },
   219       ] },
   220     { name: "onEndUpdateBatch",
   221       args: [] },
   222   ];
   223   PlacesUtils.tagging.tagURI(uri, [TAG]);
   224   PlacesUtils.tagging.untagURI(uri, [TAG]);
   225 });
   227 add_test(function onItemMoved_bookmark() {
   228   let id = PlacesUtils.bookmarks.getIdForItemAt(PlacesUtils.unfiledBookmarksFolderId, 0);
   229   let uri = PlacesUtils.bookmarks.getBookmarkURI(id);
   230   gBookmarksObserver.expected = [
   231     { name: "onItemMoved",
   232       args: [
   233         { name: "itemId", check: function (v) typeof(v) == "number" && v > 0 },
   234         { name: "oldParentId", check: function (v) v === PlacesUtils.unfiledBookmarksFolderId },
   235         { name: "oldIndex", check: function (v) v === 0 },
   236         { name: "newParentId", check: function (v) v === PlacesUtils.toolbarFolderId },
   237         { name: "newIndex", check: function (v) v === 0 },
   238         { name: "itemType", check: function (v) v === PlacesUtils.bookmarks.TYPE_BOOKMARK },
   239         { name: "guid", check: function (v) typeof(v) == "string" && /^[a-zA-Z0-9\-_]{12}$/.test(v) },
   240         { name: "oldParentGuid", check: function (v) typeof(v) == "string" && /^[a-zA-Z0-9\-_]{12}$/.test(v) },
   241         { name: "newParentGuid", check: function (v) typeof(v) == "string" && /^[a-zA-Z0-9\-_]{12}$/.test(v) },
   242       ] },
   243     { name: "onItemMoved",
   244       args: [
   245         { name: "itemId", check: function (v) typeof(v) == "number" && v > 0 },
   246         { name: "oldParentId", check: function (v) v === PlacesUtils.toolbarFolderId },
   247         { name: "oldIndex", check: function (v) v === 0 },
   248         { name: "newParentId", check: function (v) v === PlacesUtils.unfiledBookmarksFolderId },
   249         { name: "newIndex", check: function (v) v === 0 },
   250         { name: "itemType", check: function (v) v === PlacesUtils.bookmarks.TYPE_BOOKMARK },
   251         { name: "guid", check: function (v) typeof(v) == "string" && /^[a-zA-Z0-9\-_]{12}$/.test(v) },
   252         { name: "oldParentGuid", check: function (v) typeof(v) == "string" && /^[a-zA-Z0-9\-_]{12}$/.test(v) },
   253         { name: "newParentGuid", check: function (v) typeof(v) == "string" && /^[a-zA-Z0-9\-_]{12}$/.test(v) },
   254       ] },
   255   ];
   256   PlacesUtils.bookmarks.moveItem(id, PlacesUtils.toolbarFolderId, 0);
   257   PlacesUtils.bookmarks.moveItem(id, PlacesUtils.unfiledBookmarksFolderId, 0);
   258 });
   260 add_test(function onItemMoved_bookmark() {
   261   let id = PlacesUtils.bookmarks.getIdForItemAt(PlacesUtils.unfiledBookmarksFolderId, 0);
   262   let uri = PlacesUtils.bookmarks.getBookmarkURI(id);
   263   gBookmarksObserver.expected = [
   264     { name: "onItemVisited",
   265       args: [
   266         { name: "itemId", check: function (v) typeof(v) == "number" && v > 0 },
   267         { name: "visitId", check: function (v) typeof(v) == "number" && v > 0 },
   268         { name: "time", check: function (v) typeof(v) == "number" && v > 0 },
   269         { name: "transitionType", check: function (v) v === PlacesUtils.history.TRANSITION_TYPED },
   270         { name: "uri", check: function (v) v instanceof Ci.nsIURI && v.equals(uri) },
   271         { name: "parentId", check: function (v) v === PlacesUtils.unfiledBookmarksFolderId },
   272         { name: "guid", check: function (v) typeof(v) == "string" && /^[a-zA-Z0-9\-_]{12}$/.test(v) },
   273         { name: "parentGuid", check: function (v) typeof(v) == "string" && /^[a-zA-Z0-9\-_]{12}$/.test(v) },
   274       ] },
   275   ];
   276   promiseAddVisits({ uri: uri, transition: TRANSITION_TYPED });
   277 });
   279 add_test(function onItemRemoved_bookmark() {
   280   let id = PlacesUtils.bookmarks.getIdForItemAt(PlacesUtils.unfiledBookmarksFolderId, 0);
   281   let uri = PlacesUtils.bookmarks.getBookmarkURI(id);
   282   gBookmarksObserver.expected = [
   283     { name: "onItemChanged", // This is an unfortunate effect of bug 653910.
   284       args: [
   285         { name: "itemId", check: function (v) typeof(v) == "number" && v > 0 },
   286         { name: "property", check: function (v) v === "" },
   287         { name: "isAnno", check: function (v) v === true },
   288         { name: "newValue", check: function (v) v === "" },
   289         { name: "lastModified", check: function (v) typeof(v) == "number" && v > 0 },
   290         { name: "itemType", check: function (v) v === PlacesUtils.bookmarks.TYPE_BOOKMARK },
   291         { name: "parentId", check: function (v) v === PlacesUtils.unfiledBookmarksFolderId },
   292         { name: "guid", check: function (v) typeof(v) == "string" && /^[a-zA-Z0-9\-_]{12}$/.test(v) },
   293         { name: "parentGuid", check: function (v) typeof(v) == "string" && /^[a-zA-Z0-9\-_]{12}$/.test(v) },
   294       ] },
   295     { name: "onItemRemoved",
   296       args: [
   297         { name: "itemId", check: function (v) typeof(v) == "number" && v > 0 },
   298         { name: "parentId", check: function (v) v === PlacesUtils.unfiledBookmarksFolderId },
   299         { name: "index", check: function (v) v === 0 },
   300         { name: "itemType", check: function (v) v === PlacesUtils.bookmarks.TYPE_BOOKMARK },
   301         { name: "uri", check: function (v) v instanceof Ci.nsIURI && v.equals(uri) },
   302         { name: "guid", check: function (v) typeof(v) == "string" && /^[a-zA-Z0-9\-_]{12}$/.test(v) },
   303         { name: "parentGuid", check: function (v) typeof(v) == "string" && /^[a-zA-Z0-9\-_]{12}$/.test(v) },
   304       ] },
   305   ];
   306   PlacesUtils.bookmarks.removeItem(id);
   307 });
   309 add_test(function onItemRemoved_separator() {
   310   let id = PlacesUtils.bookmarks.getIdForItemAt(PlacesUtils.unfiledBookmarksFolderId, 0);
   311   gBookmarksObserver.expected = [
   312     { name: "onItemChanged", // This is an unfortunate effect of bug 653910.
   313       args: [
   314         { name: "itemId", check: function (v) typeof(v) == "number" && v > 0 },
   315         { name: "property", check: function (v) v === "" },
   316         { name: "isAnno", check: function (v) v === true },
   317         { name: "newValue", check: function (v) v === "" },
   318         { name: "lastModified", check: function (v) typeof(v) == "number" && v > 0 },
   319         { name: "itemType", check: function (v) v === PlacesUtils.bookmarks.TYPE_SEPARATOR },
   320         { name: "parentId", check: function (v) typeof(v) == "number" && v > 0 },
   321         { name: "guid", check: function (v) typeof(v) == "string" && /^[a-zA-Z0-9\-_]{12}$/.test(v) },
   322         { name: "parentGuid", check: function (v) typeof(v) == "string" && /^[a-zA-Z0-9\-_]{12}$/.test(v) },
   323       ] },
   324     { name: "onItemRemoved",
   325       args: [
   326         { name: "itemId", check: function (v) typeof(v) == "number" && v > 0 },
   327         { name: "parentId", check: function (v) typeof(v) == "number" && v > 0 },
   328         { name: "index", check: function (v) v === 0 },
   329         { name: "itemType", check: function (v) v === PlacesUtils.bookmarks.TYPE_SEPARATOR },
   330         { name: "uri", check: function (v) v === null },
   331         { name: "guid", check: function (v) typeof(v) == "string" && /^[a-zA-Z0-9\-_]{12}$/.test(v) },
   332         { name: "parentGuid", check: function (v) typeof(v) == "string" && /^[a-zA-Z0-9\-_]{12}$/.test(v) },
   333       ] },
   334   ];
   335   PlacesUtils.bookmarks.removeItem(id);
   336 });
   338 add_test(function onItemRemoved_folder() {
   339   let id = PlacesUtils.bookmarks.getIdForItemAt(PlacesUtils.unfiledBookmarksFolderId, 0);
   340   const TITLE = "Folder 2";
   341   gBookmarksObserver.expected = [
   342     { name: "onItemChanged", // This is an unfortunate effect of bug 653910.
   343       args: [
   344         { name: "itemId", check: function (v) typeof(v) == "number" && v > 0 },
   345         { name: "property", check: function (v) v === "" },
   346         { name: "isAnno", check: function (v) v === true },
   347         { name: "newValue", check: function (v) v === "" },
   348         { name: "lastModified", check: function (v) typeof(v) == "number" && v > 0 },
   349         { name: "itemType", check: function (v) v === PlacesUtils.bookmarks.TYPE_FOLDER },
   350         { name: "parentId", check: function (v) typeof(v) == "number" && v > 0 },
   351         { name: "guid", check: function (v) typeof(v) == "string" && /^[a-zA-Z0-9\-_]{12}$/.test(v) },
   352         { name: "parentGuid", check: function (v) typeof(v) == "string" && /^[a-zA-Z0-9\-_]{12}$/.test(v) },
   353       ] },
   354     { name: "onItemRemoved",
   355       args: [
   356         { name: "itemId", check: function (v) typeof(v) == "number" && v > 0 },
   357         { name: "parentId", check: function (v) typeof(v) == "number" && v > 0 },
   358         { name: "index", check: function (v) v === 0 },
   359         { name: "itemType", check: function (v) v === PlacesUtils.bookmarks.TYPE_FOLDER },
   360         { name: "uri", check: function (v) v === null },
   361         { name: "guid", check: function (v) typeof(v) == "string" && /^[a-zA-Z0-9\-_]{12}$/.test(v) },
   362         { name: "parentGuid", check: function (v) typeof(v) == "string" && /^[a-zA-Z0-9\-_]{12}$/.test(v) },
   363       ] },
   364   ];
   365   PlacesUtils.bookmarks.removeItem(id);
   366 });
   368 function run_test() {
   369   PlacesUtils.bookmarks.addObserver(gBookmarksObserver, false);
   370   run_next_test();
   371 }
   373 do_register_cleanup(function () {
   374   PlacesUtils.bookmarks.removeObserver(gBookmarksObserver);
   375 });

mercurial