1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/browser/fuel/test/browser_Bookmarks.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,253 @@ 1.4 +var gLastFolderAction = ""; 1.5 +var gLastBookmarkAction = ""; 1.6 +var gLastRootAction = ""; 1.7 + 1.8 +function url(spec) { 1.9 + return Services.io.newURI(spec, null, null); 1.10 +} 1.11 + 1.12 +function test() { 1.13 + // Some very basic tests on the tags root 1.14 + var tags = Application.bookmarks.tags; 1.15 + ok(tags, "Check access to bookmark tags root"); 1.16 + ok(!tags.parent, "Check tags parent (should be null)"); 1.17 + 1.18 + //---------------------------------------------- 1.19 + 1.20 + // Some very basic tests on the unfiled root 1.21 + var unfiled = Application.bookmarks.unfiled; 1.22 + ok(unfiled, "Check access to bookmark unfiled root"); 1.23 + ok(!unfiled.parent, "Check unfiled parent (should be null)"); 1.24 + 1.25 + //---------------------------------------------- 1.26 + 1.27 + // Some basic tests on the toolbar root 1.28 + var toolbar = Application.bookmarks.toolbar; 1.29 + ok(toolbar, "Check access to bookmark toolbar root"); 1.30 + ok(!toolbar.parent, "Check toolbar parent (should be null)"); 1.31 + 1.32 + var toolbarKidCount = toolbar.children.length; 1.33 + 1.34 + // test adding folders 1.35 + var testFolderToolbar = toolbar.addFolder("FUEL in Toolbar"); 1.36 + ok(testFolderToolbar, "Check folder creation"); 1.37 + is(testFolderToolbar.type, "folder", "Check 'folder.type' after creation"); 1.38 + ok(testFolderToolbar.parent, "Check parent after folder creation"); 1.39 + 1.40 + toolbarKidCount++; 1.41 + is(toolbar.children.length, toolbarKidCount, "Check toolbar folder child count after adding a child folder"); 1.42 + 1.43 + //---------------------------------------------- 1.44 + 1.45 + // Main testing is done on the bookmarks menu root 1.46 + var root = Application.bookmarks.menu; 1.47 + ok(root, "Check access to bookmark root"); 1.48 + ok(!root.parent, "Check root parent (should be null)"); 1.49 + 1.50 + var rootKidCount = root.children.length; 1.51 + 1.52 + // test adding folders 1.53 + var testFolder = root.addFolder("FUEL"); 1.54 + ok(testFolder, "Check folder creation"); 1.55 + is(testFolder.type, "folder", "Check 'folder.type' after creation"); 1.56 + ok(testFolder.parent, "Check parent after folder creation"); 1.57 + 1.58 + rootKidCount++; 1.59 + is(root.children.length, rootKidCount, "Check root folder child count after adding a child folder"); 1.60 + 1.61 + // test modifying a folder 1.62 + testFolder.events.addListener("change", onFolderChange); 1.63 + testFolder.description = "FUEL folder"; 1.64 + is(testFolder.description, "FUEL folder", "Check setting 'folder.description'"); 1.65 + is(gLastFolderAction, "bookmarkProperties/description", "Check event handler for setting 'folder.description'"); 1.66 + 1.67 + testFolder.title = "fuel-is-cool"; 1.68 + is(testFolder.title, "fuel-is-cool", "Check setting 'folder.title'"); 1.69 + is(gLastFolderAction, "title", "Check event handler for setting 'folder.title'"); 1.70 + 1.71 + testFolder.annotations.set("testing/folder", "annotate-this", 0); 1.72 + ok(testFolder.annotations.has("testing/folder"), "Checking existence of added annotation"); 1.73 + is(gLastFolderAction, "testing/folder", "Check event handler for setting annotation"); 1.74 + gLastFolderAction = ""; 1.75 + is(testFolder.annotations.get("testing/folder"), "annotate-this", "Checking existence of added annotation"); 1.76 + testFolder.annotations.remove("testing/folder"); 1.77 + ok(!testFolder.annotations.has("testing/folder"), "Checking existence of removed annotation"); 1.78 + is(gLastFolderAction, "testing/folder", "Check event handler for removing annotation"); 1.79 + 1.80 + testFolder.events.addListener("addchild", onFolderAddChild); 1.81 + testFolder.events.addListener("removechild", onFolderRemoveChild); 1.82 + 1.83 + // test adding a bookmark 1.84 + var testBookmark = testFolder.addBookmark("Mozilla", url("https://www.mozilla.org/")); 1.85 + ok(testBookmark, "Check bookmark creation"); 1.86 + ok(testBookmark.parent, "Check parent after bookmark creation"); 1.87 + is(gLastFolderAction, "addchild", "Check event handler for adding a child to a folder"); 1.88 + is(testBookmark.type, "bookmark", "Check 'bookmark.type' after creation"); 1.89 + is(testBookmark.title, "Mozilla", "Check 'bookmark.title' after creation"); 1.90 + is(testBookmark.uri.spec, "https://www.mozilla.org/", "Check 'bookmark.uri' after creation"); 1.91 + 1.92 + is(testFolder.children.length, 1, "Check test folder child count after adding a child bookmark"); 1.93 + 1.94 + // test modifying a bookmark 1.95 + testBookmark.events.addListener("change", onBookmarkChange); 1.96 + testBookmark.description = "mozcorp"; 1.97 + is(testBookmark.description, "mozcorp", "Check setting 'bookmark.description'"); 1.98 + is(gLastBookmarkAction, "bookmarkProperties/description", "Check event handler for setting 'bookmark.description'"); 1.99 + 1.100 + testBookmark.keyword = "moz" 1.101 + is(testBookmark.keyword, "moz", "Check setting 'bookmark.keyword'"); 1.102 + is(gLastBookmarkAction, "keyword", "Check event handler for setting 'bookmark.keyword'"); 1.103 + 1.104 + testBookmark.title = "MozCorp" 1.105 + is(testBookmark.title, "MozCorp", "Check setting 'bookmark.title'"); 1.106 + is(gLastBookmarkAction, "title", "Check event handler for setting 'bookmark.title'"); 1.107 + 1.108 + testBookmark.uri = url("http://www.mozilla.org/"); 1.109 + is(testBookmark.uri.spec, "http://www.mozilla.org/", "Check setting 'bookmark.uri'"); 1.110 + is(gLastBookmarkAction, "uri", "Check event handler for setting 'bookmark.uri'"); 1.111 + 1.112 + // test adding and removing a bookmark annotation 1.113 + testBookmark.annotations.set("testing/bookmark", "annotate-this", 0); 1.114 + ok(testBookmark.annotations.has("testing/bookmark"), "Checking existence of added annotation"); 1.115 + is(gLastBookmarkAction, "testing/bookmark", "Check event handler for setting annotation"); 1.116 + gLastBookmarkAction = ""; 1.117 + is(testBookmark.annotations.get("testing/bookmark"), "annotate-this", "Checking existence of added annotation"); 1.118 + testBookmark.annotations.remove("testing/bookmark"); 1.119 + ok(!testBookmark.annotations.has("testing/bookmark"), "Checking existence of removed annotation"); 1.120 + is(gLastBookmarkAction, "testing/bookmark", "Check event handler for removing annotation"); 1.121 + is(testBookmark.annotations.get("testing/bookmark"), null, "Check existence of a missing annotation"); 1.122 + 1.123 + // quick annotation type tests 1.124 + testBookmark.annotations.set("testing/bookmark/string", "annotate-this", 0); 1.125 + ok(testBookmark.annotations.has("testing/bookmark/string"), "Checking existence of added string annotation"); 1.126 + is(testBookmark.annotations.get("testing/bookmark/string"), "annotate-this", "Checking value of added string annotation"); 1.127 + is(gLastBookmarkAction, "testing/bookmark/string", "Check event handler for setting annotation"); 1.128 + gLastBookmarkAction = ""; 1.129 + testBookmark.annotations.set("testing/bookmark/int", 100, 0); 1.130 + ok(testBookmark.annotations.has("testing/bookmark/int"), "Checking existence of added integer annotation"); 1.131 + is(testBookmark.annotations.get("testing/bookmark/int"), 100, "Checking value of added integer annotation"); 1.132 + is(gLastBookmarkAction, "testing/bookmark/int", "Check event handler for setting annotation"); 1.133 + gLastBookmarkAction = ""; 1.134 + testBookmark.annotations.set("testing/bookmark/double", 3.333, 0); 1.135 + ok(testBookmark.annotations.has("testing/bookmark/double"), "Checking existence of added double annotation"); 1.136 + is(testBookmark.annotations.get("testing/bookmark/double"), 3.333, "Checking value of added double annotation"); 1.137 + is(gLastBookmarkAction, "testing/bookmark/double", "Check event handler for setting annotation"); 1.138 + gLastBookmarkAction = ""; 1.139 + 1.140 + // test names array - NOTE: "bookmarkProperties/description" is an annotation too 1.141 + var names = testBookmark.annotations.names; 1.142 + ok(names.some(function (f) f == "bookmarkProperties/description"), "Checking for description annotation"); 1.143 + ok(names.some(function (f) f == "testing/bookmark/string"), "Checking for string test annotation"); 1.144 + ok(names.some(function (f) f == "testing/bookmark/int"), "Checking for int test annotation"); 1.145 + ok(names.some(function (f) f == "testing/bookmark/double"), "Checking for double test annotation"); 1.146 + 1.147 + // test adding a separator 1.148 + var testSeparator = testFolder.addSeparator(); 1.149 + ok(testSeparator, "Check bookmark creation"); 1.150 + ok(testSeparator.parent, "Check parent after separator creation"); 1.151 + is(gLastFolderAction, "addchild", "Check event handler for adding a child separator to a folder"); 1.152 + is(testSeparator.type, "separator", "Check 'bookmark.type' after separator creation"); 1.153 + 1.154 + is(testFolder.children.length, 2, "Check test folder child count after adding a child separator"); 1.155 + 1.156 + // test removing separator 1.157 + testSeparator.events.addListener("remove", onBookmarkRemove); 1.158 + testSeparator.remove(); 1.159 + is(gLastBookmarkAction, "remove", "Check event handler for removing separator"); 1.160 + is(gLastFolderAction, "removechild", "Check event handler for removing a child separator from a folder"); 1.161 + is(testFolder.children.length, 1, "Check test folder child count after removing a child separator"); 1.162 + 1.163 + // test removing bookmark 1.164 + testBookmark.events.addListener("remove", onBookmarkRemove); 1.165 + testBookmark.remove(); 1.166 + is(gLastBookmarkAction, "remove", "Check event handler for removing bookmark"); 1.167 + is(gLastFolderAction, "removechild", "Check event handler for removing a child from a folder"); 1.168 + is(testFolder.children.length, 0, "Check test folder child count after removing a child bookmark"); 1.169 + 1.170 + // test removing a folder 1.171 + testFolder.events.addListener("remove", onFolderRemove); 1.172 + testFolder.remove(); 1.173 + is(gLastFolderAction, "remove", "Check event handler for removing child folder"); 1.174 + rootKidCount--; 1.175 + is(root.children.length, rootKidCount, "Check root folder child count after removing a child folder"); 1.176 + 1.177 + // test moving between folders 1.178 + var testFolderA = root.addFolder("folder-a"); 1.179 + var testFolderB = root.addFolder("folder-b"); 1.180 + 1.181 + var testMove = testFolderA.addBookmark("Mozilla", url("https://www.mozilla.org/")); 1.182 + testMove.events.addListener("move", onBookmarkMove); 1.183 + is(testMove.parent.title, "folder-a", "Checking for new parent before moving bookmark"); 1.184 + 1.185 + testMove.parent = testFolderB; 1.186 + is(testMove.parent.title, "folder-b", "Checking for new parent after moving bookmark"); 1.187 + is(gLastBookmarkAction, "move", "Checking for event handler after moving bookmark"); 1.188 + 1.189 + // test moving a folder 1.190 + testFolderA.events.addListener("move", onFolderMove); 1.191 + testFolderA.parent = testFolderB; 1.192 + is(testFolderA.parent.title, "folder-b", "Checking for new parent after moving folder"); 1.193 + is(gLastFolderAction, "move", "Checking for event handler after moving folder"); 1.194 + 1.195 + // test events on the root 1.196 + root.events.addListener("add", onRootAdd); 1.197 + root.events.addListener("remove", onRootRemove); 1.198 + root.events.addListener("change", onRootChange); 1.199 + var testFolderC = root.addFolder("folder-c"); 1.200 + is(gLastRootAction, "add"); 1.201 + 1.202 + root.events.removeListener("add", onRootAdd); 1.203 + gLastRootAction = ""; 1.204 + var testFolderD = root.addFolder("folder-d"); 1.205 + is(gLastRootAction, ""); 1.206 + 1.207 + testFolderC.remove(); 1.208 + is(gLastRootAction, "remove"); 1.209 + 1.210 + testFolderD.description = "Foo"; 1.211 + is(gLastRootAction, "bookmarkProperties/description"); 1.212 +} 1.213 + 1.214 +function onFolderChange(evt) { 1.215 + gLastFolderAction = evt.data; 1.216 +} 1.217 + 1.218 +function onFolderRemove(evt) { 1.219 + gLastFolderAction = evt.type; 1.220 +} 1.221 + 1.222 +function onFolderAddChild(evt) { 1.223 + gLastFolderAction = evt.type; 1.224 +} 1.225 + 1.226 +function onFolderRemoveChild(evt) { 1.227 + gLastFolderAction = evt.type; 1.228 +} 1.229 + 1.230 +function onFolderMove(evt) { 1.231 + gLastFolderAction = evt.type; 1.232 +} 1.233 + 1.234 +function onBookmarkChange(evt) { 1.235 + gLastBookmarkAction = evt.data; 1.236 +} 1.237 + 1.238 +function onBookmarkRemove(evt) { 1.239 + gLastBookmarkAction = evt.type; 1.240 +} 1.241 + 1.242 +function onBookmarkMove(evt) { 1.243 + gLastBookmarkAction = evt.type; 1.244 +} 1.245 + 1.246 +function onRootAdd(evt) { 1.247 + gLastRootAction = evt.type; 1.248 +} 1.249 + 1.250 +function onRootRemove(evt) { 1.251 + gLastRootAction = evt.type; 1.252 +} 1.253 + 1.254 +function onRootChange(evt) { 1.255 + gLastRootAction = evt.data; 1.256 +}