michael@0: var gLastFolderAction = ""; michael@0: var gLastBookmarkAction = ""; michael@0: var gLastRootAction = ""; michael@0: michael@0: function url(spec) { michael@0: return Services.io.newURI(spec, null, null); michael@0: } michael@0: michael@0: function test() { michael@0: // Some very basic tests on the tags root michael@0: var tags = Application.bookmarks.tags; michael@0: ok(tags, "Check access to bookmark tags root"); michael@0: ok(!tags.parent, "Check tags parent (should be null)"); michael@0: michael@0: //---------------------------------------------- michael@0: michael@0: // Some very basic tests on the unfiled root michael@0: var unfiled = Application.bookmarks.unfiled; michael@0: ok(unfiled, "Check access to bookmark unfiled root"); michael@0: ok(!unfiled.parent, "Check unfiled parent (should be null)"); michael@0: michael@0: //---------------------------------------------- michael@0: michael@0: // Some basic tests on the toolbar root michael@0: var toolbar = Application.bookmarks.toolbar; michael@0: ok(toolbar, "Check access to bookmark toolbar root"); michael@0: ok(!toolbar.parent, "Check toolbar parent (should be null)"); michael@0: michael@0: var toolbarKidCount = toolbar.children.length; michael@0: michael@0: // test adding folders michael@0: var testFolderToolbar = toolbar.addFolder("FUEL in Toolbar"); michael@0: ok(testFolderToolbar, "Check folder creation"); michael@0: is(testFolderToolbar.type, "folder", "Check 'folder.type' after creation"); michael@0: ok(testFolderToolbar.parent, "Check parent after folder creation"); michael@0: michael@0: toolbarKidCount++; michael@0: is(toolbar.children.length, toolbarKidCount, "Check toolbar folder child count after adding a child folder"); michael@0: michael@0: //---------------------------------------------- michael@0: michael@0: // Main testing is done on the bookmarks menu root michael@0: var root = Application.bookmarks.menu; michael@0: ok(root, "Check access to bookmark root"); michael@0: ok(!root.parent, "Check root parent (should be null)"); michael@0: michael@0: var rootKidCount = root.children.length; michael@0: michael@0: // test adding folders michael@0: var testFolder = root.addFolder("FUEL"); michael@0: ok(testFolder, "Check folder creation"); michael@0: is(testFolder.type, "folder", "Check 'folder.type' after creation"); michael@0: ok(testFolder.parent, "Check parent after folder creation"); michael@0: michael@0: rootKidCount++; michael@0: is(root.children.length, rootKidCount, "Check root folder child count after adding a child folder"); michael@0: michael@0: // test modifying a folder michael@0: testFolder.events.addListener("change", onFolderChange); michael@0: testFolder.description = "FUEL folder"; michael@0: is(testFolder.description, "FUEL folder", "Check setting 'folder.description'"); michael@0: is(gLastFolderAction, "bookmarkProperties/description", "Check event handler for setting 'folder.description'"); michael@0: michael@0: testFolder.title = "fuel-is-cool"; michael@0: is(testFolder.title, "fuel-is-cool", "Check setting 'folder.title'"); michael@0: is(gLastFolderAction, "title", "Check event handler for setting 'folder.title'"); michael@0: michael@0: testFolder.annotations.set("testing/folder", "annotate-this", 0); michael@0: ok(testFolder.annotations.has("testing/folder"), "Checking existence of added annotation"); michael@0: is(gLastFolderAction, "testing/folder", "Check event handler for setting annotation"); michael@0: gLastFolderAction = ""; michael@0: is(testFolder.annotations.get("testing/folder"), "annotate-this", "Checking existence of added annotation"); michael@0: testFolder.annotations.remove("testing/folder"); michael@0: ok(!testFolder.annotations.has("testing/folder"), "Checking existence of removed annotation"); michael@0: is(gLastFolderAction, "testing/folder", "Check event handler for removing annotation"); michael@0: michael@0: testFolder.events.addListener("addchild", onFolderAddChild); michael@0: testFolder.events.addListener("removechild", onFolderRemoveChild); michael@0: michael@0: // test adding a bookmark michael@0: var testBookmark = testFolder.addBookmark("Mozilla", url("https://www.mozilla.org/")); michael@0: ok(testBookmark, "Check bookmark creation"); michael@0: ok(testBookmark.parent, "Check parent after bookmark creation"); michael@0: is(gLastFolderAction, "addchild", "Check event handler for adding a child to a folder"); michael@0: is(testBookmark.type, "bookmark", "Check 'bookmark.type' after creation"); michael@0: is(testBookmark.title, "Mozilla", "Check 'bookmark.title' after creation"); michael@0: is(testBookmark.uri.spec, "https://www.mozilla.org/", "Check 'bookmark.uri' after creation"); michael@0: michael@0: is(testFolder.children.length, 1, "Check test folder child count after adding a child bookmark"); michael@0: michael@0: // test modifying a bookmark michael@0: testBookmark.events.addListener("change", onBookmarkChange); michael@0: testBookmark.description = "mozcorp"; michael@0: is(testBookmark.description, "mozcorp", "Check setting 'bookmark.description'"); michael@0: is(gLastBookmarkAction, "bookmarkProperties/description", "Check event handler for setting 'bookmark.description'"); michael@0: michael@0: testBookmark.keyword = "moz" michael@0: is(testBookmark.keyword, "moz", "Check setting 'bookmark.keyword'"); michael@0: is(gLastBookmarkAction, "keyword", "Check event handler for setting 'bookmark.keyword'"); michael@0: michael@0: testBookmark.title = "MozCorp" michael@0: is(testBookmark.title, "MozCorp", "Check setting 'bookmark.title'"); michael@0: is(gLastBookmarkAction, "title", "Check event handler for setting 'bookmark.title'"); michael@0: michael@0: testBookmark.uri = url("http://www.mozilla.org/"); michael@0: is(testBookmark.uri.spec, "http://www.mozilla.org/", "Check setting 'bookmark.uri'"); michael@0: is(gLastBookmarkAction, "uri", "Check event handler for setting 'bookmark.uri'"); michael@0: michael@0: // test adding and removing a bookmark annotation michael@0: testBookmark.annotations.set("testing/bookmark", "annotate-this", 0); michael@0: ok(testBookmark.annotations.has("testing/bookmark"), "Checking existence of added annotation"); michael@0: is(gLastBookmarkAction, "testing/bookmark", "Check event handler for setting annotation"); michael@0: gLastBookmarkAction = ""; michael@0: is(testBookmark.annotations.get("testing/bookmark"), "annotate-this", "Checking existence of added annotation"); michael@0: testBookmark.annotations.remove("testing/bookmark"); michael@0: ok(!testBookmark.annotations.has("testing/bookmark"), "Checking existence of removed annotation"); michael@0: is(gLastBookmarkAction, "testing/bookmark", "Check event handler for removing annotation"); michael@0: is(testBookmark.annotations.get("testing/bookmark"), null, "Check existence of a missing annotation"); michael@0: michael@0: // quick annotation type tests michael@0: testBookmark.annotations.set("testing/bookmark/string", "annotate-this", 0); michael@0: ok(testBookmark.annotations.has("testing/bookmark/string"), "Checking existence of added string annotation"); michael@0: is(testBookmark.annotations.get("testing/bookmark/string"), "annotate-this", "Checking value of added string annotation"); michael@0: is(gLastBookmarkAction, "testing/bookmark/string", "Check event handler for setting annotation"); michael@0: gLastBookmarkAction = ""; michael@0: testBookmark.annotations.set("testing/bookmark/int", 100, 0); michael@0: ok(testBookmark.annotations.has("testing/bookmark/int"), "Checking existence of added integer annotation"); michael@0: is(testBookmark.annotations.get("testing/bookmark/int"), 100, "Checking value of added integer annotation"); michael@0: is(gLastBookmarkAction, "testing/bookmark/int", "Check event handler for setting annotation"); michael@0: gLastBookmarkAction = ""; michael@0: testBookmark.annotations.set("testing/bookmark/double", 3.333, 0); michael@0: ok(testBookmark.annotations.has("testing/bookmark/double"), "Checking existence of added double annotation"); michael@0: is(testBookmark.annotations.get("testing/bookmark/double"), 3.333, "Checking value of added double annotation"); michael@0: is(gLastBookmarkAction, "testing/bookmark/double", "Check event handler for setting annotation"); michael@0: gLastBookmarkAction = ""; michael@0: michael@0: // test names array - NOTE: "bookmarkProperties/description" is an annotation too michael@0: var names = testBookmark.annotations.names; michael@0: ok(names.some(function (f) f == "bookmarkProperties/description"), "Checking for description annotation"); michael@0: ok(names.some(function (f) f == "testing/bookmark/string"), "Checking for string test annotation"); michael@0: ok(names.some(function (f) f == "testing/bookmark/int"), "Checking for int test annotation"); michael@0: ok(names.some(function (f) f == "testing/bookmark/double"), "Checking for double test annotation"); michael@0: michael@0: // test adding a separator michael@0: var testSeparator = testFolder.addSeparator(); michael@0: ok(testSeparator, "Check bookmark creation"); michael@0: ok(testSeparator.parent, "Check parent after separator creation"); michael@0: is(gLastFolderAction, "addchild", "Check event handler for adding a child separator to a folder"); michael@0: is(testSeparator.type, "separator", "Check 'bookmark.type' after separator creation"); michael@0: michael@0: is(testFolder.children.length, 2, "Check test folder child count after adding a child separator"); michael@0: michael@0: // test removing separator michael@0: testSeparator.events.addListener("remove", onBookmarkRemove); michael@0: testSeparator.remove(); michael@0: is(gLastBookmarkAction, "remove", "Check event handler for removing separator"); michael@0: is(gLastFolderAction, "removechild", "Check event handler for removing a child separator from a folder"); michael@0: is(testFolder.children.length, 1, "Check test folder child count after removing a child separator"); michael@0: michael@0: // test removing bookmark michael@0: testBookmark.events.addListener("remove", onBookmarkRemove); michael@0: testBookmark.remove(); michael@0: is(gLastBookmarkAction, "remove", "Check event handler for removing bookmark"); michael@0: is(gLastFolderAction, "removechild", "Check event handler for removing a child from a folder"); michael@0: is(testFolder.children.length, 0, "Check test folder child count after removing a child bookmark"); michael@0: michael@0: // test removing a folder michael@0: testFolder.events.addListener("remove", onFolderRemove); michael@0: testFolder.remove(); michael@0: is(gLastFolderAction, "remove", "Check event handler for removing child folder"); michael@0: rootKidCount--; michael@0: is(root.children.length, rootKidCount, "Check root folder child count after removing a child folder"); michael@0: michael@0: // test moving between folders michael@0: var testFolderA = root.addFolder("folder-a"); michael@0: var testFolderB = root.addFolder("folder-b"); michael@0: michael@0: var testMove = testFolderA.addBookmark("Mozilla", url("https://www.mozilla.org/")); michael@0: testMove.events.addListener("move", onBookmarkMove); michael@0: is(testMove.parent.title, "folder-a", "Checking for new parent before moving bookmark"); michael@0: michael@0: testMove.parent = testFolderB; michael@0: is(testMove.parent.title, "folder-b", "Checking for new parent after moving bookmark"); michael@0: is(gLastBookmarkAction, "move", "Checking for event handler after moving bookmark"); michael@0: michael@0: // test moving a folder michael@0: testFolderA.events.addListener("move", onFolderMove); michael@0: testFolderA.parent = testFolderB; michael@0: is(testFolderA.parent.title, "folder-b", "Checking for new parent after moving folder"); michael@0: is(gLastFolderAction, "move", "Checking for event handler after moving folder"); michael@0: michael@0: // test events on the root michael@0: root.events.addListener("add", onRootAdd); michael@0: root.events.addListener("remove", onRootRemove); michael@0: root.events.addListener("change", onRootChange); michael@0: var testFolderC = root.addFolder("folder-c"); michael@0: is(gLastRootAction, "add"); michael@0: michael@0: root.events.removeListener("add", onRootAdd); michael@0: gLastRootAction = ""; michael@0: var testFolderD = root.addFolder("folder-d"); michael@0: is(gLastRootAction, ""); michael@0: michael@0: testFolderC.remove(); michael@0: is(gLastRootAction, "remove"); michael@0: michael@0: testFolderD.description = "Foo"; michael@0: is(gLastRootAction, "bookmarkProperties/description"); michael@0: } michael@0: michael@0: function onFolderChange(evt) { michael@0: gLastFolderAction = evt.data; michael@0: } michael@0: michael@0: function onFolderRemove(evt) { michael@0: gLastFolderAction = evt.type; michael@0: } michael@0: michael@0: function onFolderAddChild(evt) { michael@0: gLastFolderAction = evt.type; michael@0: } michael@0: michael@0: function onFolderRemoveChild(evt) { michael@0: gLastFolderAction = evt.type; michael@0: } michael@0: michael@0: function onFolderMove(evt) { michael@0: gLastFolderAction = evt.type; michael@0: } michael@0: michael@0: function onBookmarkChange(evt) { michael@0: gLastBookmarkAction = evt.data; michael@0: } michael@0: michael@0: function onBookmarkRemove(evt) { michael@0: gLastBookmarkAction = evt.type; michael@0: } michael@0: michael@0: function onBookmarkMove(evt) { michael@0: gLastBookmarkAction = evt.type; michael@0: } michael@0: michael@0: function onRootAdd(evt) { michael@0: gLastRootAction = evt.type; michael@0: } michael@0: michael@0: function onRootRemove(evt) { michael@0: gLastRootAction = evt.type; michael@0: } michael@0: michael@0: function onRootChange(evt) { michael@0: gLastRootAction = evt.data; michael@0: }