browser_Bookmarks.js
Go to the documentation of this file.
1 const Ci = Components.interfaces;
2 const Cc = Components.classes;
3 
6 
7 function url(spec) {
8  var ios = Cc["@mozilla.org/network/io-service;1"].getService(Ci.nsIIOService);
9  return ios.newURI(spec, null, null);
10 }
11 
12 function test() {
13  // Some very basic tests on the tags root
14  var tags = Application.bookmarks.tags;
15  ok(tags, "Check access to bookmark tags root");
16  ok(!tags.parent, "Check tags parent (should be null)");
17 
18  //----------------------------------------------
19 
20  // Some very basic tests on the unfiled root
21  var unfiled = Application.bookmarks.unfiled;
22  ok(unfiled, "Check access to bookmark unfiled root");
23  ok(!unfiled.parent, "Check unfiled parent (should be null)");
24 
25  //----------------------------------------------
26 
27  // Some basic tests on the toolbar root
28  var toolbar = Application.bookmarks.toolbar;
29  ok(toolbar, "Check access to bookmark toolbar root");
30  ok(!toolbar.parent, "Check toolbar parent (should be null)");
31 
32  var toolbarKidCount = toolbar.children.length;
33 
34  // test adding folders
35  var testFolderToolbar = toolbar.addFolder("FUEL in Toolbar");
36  ok(testFolderToolbar, "Check folder creation");
37  is(testFolderToolbar.type, "folder", "Check 'folder.type' after creation");
38  ok(testFolderToolbar.parent, "Check parent after folder creation");
39 
40  toolbarKidCount++;
41  is(toolbar.children.length, toolbarKidCount, "Check toolbar folder child count after adding a child folder");
42 
43  //----------------------------------------------
44 
45  // Main testing is done on the bookmarks menu root
46  var root = Application.bookmarks.menu;
47  ok(root, "Check access to bookmark root");
48  ok(!root.parent, "Check root parent (should be null)");
49 
50  var rootKidCount = root.children.length;
51 
52  // test adding folders
53  var testFolder = root.addFolder("FUEL");
54  ok(testFolder, "Check folder creation");
55  is(testFolder.type, "folder", "Check 'folder.type' after creation");
56  ok(testFolder.parent, "Check parent after folder creation");
57 
58  rootKidCount++;
59  is(root.children.length, rootKidCount, "Check root folder child count after adding a child folder");
60 
61  // test modifying a folder
62  testFolder.events.addListener("change", onFolderChange);
63  testFolder.description = "FUEL folder";
64  is(testFolder.description, "FUEL folder", "Check setting 'folder.description'");
65  is(gLastFolderAction, "bookmarkProperties/description", "Check event handler for setting 'folder.description'");
66 
67  testFolder.title = "fuel-is-cool";
68  is(testFolder.title, "fuel-is-cool", "Check setting 'folder.title'");
69  is(gLastFolderAction, "title", "Check event handler for setting 'folder.title'");
70 
71  testFolder.annotations.set("testing/folder", "annotate-this", 0);
72  ok(testFolder.annotations.has("testing/folder"), "Checking existence of added annotation");
73  is(gLastFolderAction, "testing/folder", "Check event handler for setting annotation");
74  gLastFolderAction = "";
75  is(testFolder.annotations.get("testing/folder"), "annotate-this", "Checking existence of added annotation");
76  testFolder.annotations.remove("testing/folder");
77  ok(!testFolder.annotations.has("testing/folder"), "Checking existence of removed annotation");
78  is(gLastFolderAction, "testing/folder", "Check event handler for removing annotation");
79 
80  testFolder.events.addListener("addchild", onFolderAddChild);
81  testFolder.events.addListener("removechild", onFolderRemoveChild);
82 
83  // test adding a bookmark
84  var testBookmark = testFolder.addBookmark("Mozilla", url("http://www.mozilla.com/"));
85  ok(testBookmark, "Check bookmark creation");
86  ok(testBookmark.parent, "Check parent after bookmark creation");
87  is(gLastFolderAction, "addchild", "Check event handler for adding a child to a folder");
88  is(testBookmark.type, "bookmark", "Check 'bookmark.type' after creation");
89  is(testBookmark.title, "Mozilla", "Check 'bookmark.title' after creation");
90  is(testBookmark.uri.spec, "http://www.mozilla.com/", "Check 'bookmark.uri' after creation");
91 
92  is(testFolder.children.length, 1, "Check test folder child count after adding a child bookmark");
93 
94  // test modifying a bookmark
95  testBookmark.events.addListener("change", onBookmarkChange);
96  testBookmark.description = "mozcorp";
97  is(testBookmark.description, "mozcorp", "Check setting 'bookmark.description'");
98  is(gLastBookmarkAction, "bookmarkProperties/description", "Check event handler for setting 'bookmark.description'");
99 
100  testBookmark.keyword = "moz"
101  is(testBookmark.keyword, "moz", "Check setting 'bookmark.keyword'");
102  is(gLastBookmarkAction, "keyword", "Check event handler for setting 'bookmark.keyword'");
103 
104  testBookmark.title = "MozCorp"
105  is(testBookmark.title, "MozCorp", "Check setting 'bookmark.title'");
106  is(gLastBookmarkAction, "title", "Check event handler for setting 'bookmark.title'");
107 
108  testBookmark.uri = url("http://www.mozilla.org/");
109  is(testBookmark.uri.spec, "http://www.mozilla.org/", "Check setting 'bookmark.uri'");
110  is(gLastBookmarkAction, "uri", "Check event handler for setting 'bookmark.uri'");
111 
112  // test adding and removing a bookmark annotation
113  testBookmark.annotations.set("testing/bookmark", "annotate-this", 0);
114  ok(testBookmark.annotations.has("testing/bookmark"), "Checking existence of added annotation");
115  is(gLastBookmarkAction, "testing/bookmark", "Check event handler for setting annotation");
116  gLastBookmarkAction = "";
117  is(testBookmark.annotations.get("testing/bookmark"), "annotate-this", "Checking existence of added annotation");
118  testBookmark.annotations.remove("testing/bookmark");
119  ok(!testBookmark.annotations.has("testing/bookmark"), "Checking existence of removed annotation");
120  is(gLastBookmarkAction, "testing/bookmark", "Check event handler for removing annotation");
121  is(testBookmark.annotations.get("testing/bookmark"), null, "Check existence of a missing annotation");
122 
123  // quick annotation type tests
124  testBookmark.annotations.set("testing/bookmark/string", "annotate-this", 0);
125  ok(testBookmark.annotations.has("testing/bookmark/string"), "Checking existence of added string annotation");
126  is(testBookmark.annotations.get("testing/bookmark/string"), "annotate-this", "Checking value of added string annotation");
127  is(gLastBookmarkAction, "testing/bookmark/string", "Check event handler for setting annotation");
128  gLastBookmarkAction = "";
129  testBookmark.annotations.set("testing/bookmark/int", 100, 0);
130  ok(testBookmark.annotations.has("testing/bookmark/int"), "Checking existence of added integer annotation");
131  is(testBookmark.annotations.get("testing/bookmark/int"), 100, "Checking value of added integer annotation");
132  is(gLastBookmarkAction, "testing/bookmark/int", "Check event handler for setting annotation");
133  gLastBookmarkAction = "";
134  testBookmark.annotations.set("testing/bookmark/double", 3.333, 0);
135  ok(testBookmark.annotations.has("testing/bookmark/double"), "Checking existence of added double annotation");
136  is(testBookmark.annotations.get("testing/bookmark/double"), 3.333, "Checking value of added double annotation");
137  is(gLastBookmarkAction, "testing/bookmark/double", "Check event handler for setting annotation");
138  gLastBookmarkAction = "";
139 
140  // test names array - NOTE: "bookmarkProperties/description" is an annotation too
141  var names = testBookmark.annotations.names;
142  is(names[1], "testing/bookmark/string", "Checking contents of annotation names array");
143  is(names.length, 4, "Checking the annotation names array after adding 3 annotations");
144 
145  // test adding a separator
146  var testSeparator = testFolder.addSeparator();
147  ok(testSeparator, "Check bookmark creation");
148  ok(testSeparator.parent, "Check parent after separator creation");
149  is(gLastFolderAction, "addchild", "Check event handler for adding a child separator to a folder");
150  is(testSeparator.type, "separator", "Check 'bookmark.type' after separator creation");
151 
152  is(testFolder.children.length, 2, "Check test folder child count after adding a child separator");
153 
154  // test removing separator
155  testSeparator.events.addListener("remove", onBookmarkRemove);
156  testSeparator.remove();
157  is(gLastBookmarkAction, "remove", "Check event handler for removing separator");
158  is(gLastFolderAction, "removechild", "Check event handler for removing a child separator from a folder");
159  is(testFolder.children.length, 1, "Check test folder child count after removing a child separator");
160 
161  // test removing bookmark
162  testBookmark.events.addListener("remove", onBookmarkRemove);
163  testBookmark.remove();
164  is(gLastBookmarkAction, "remove", "Check event handler for removing bookmark");
165  is(gLastFolderAction, "removechild", "Check event handler for removing a child from a folder");
166  is(testFolder.children.length, 0, "Check test folder child count after removing a child bookmark");
167 
168  // test removing a folder
169  testFolder.events.addListener("remove", onFolderRemove);
170  testFolder.remove();
171  is(gLastFolderAction, "remove", "Check event handler for removing child folder");
172  rootKidCount--;
173  is(root.children.length, rootKidCount, "Check root folder child count after removing a child folder");
174 
175  // test moving between folders
176  var testFolderA = root.addFolder("folder-a");
177  var testFolderB = root.addFolder("folder-b");
178 
179  var testMove = testFolderA.addBookmark("Mozilla", url("http://www.mozilla.com/"));
180  testMove.events.addListener("move", onBookmarkMove);
181  is(testMove.parent.title, "folder-a", "Checking for new parent before moving bookmark");
182 
183  testMove.parent = testFolderB;
184  is(testMove.parent.title, "folder-b", "Checking for new parent after moving bookmark");
185  is(gLastBookmarkAction, "move", "Checking for event handler after moving bookmark");
186 
187  // test moving a folder
188  testFolderA.events.addListener("move", onFolderMove);
189  testFolderA.parent = testFolderB;
190  is(testFolderA.parent.title, "folder-b", "Checking for new parent after moving folder");
191  is(gLastFolderAction, "move", "Checking for event handler after moving folder");
192 }
193 
194 function onFolderChange(evt) {
195  gLastFolderAction = evt.data;
196 }
197 
198 function onFolderRemove(evt) {
199  gLastFolderAction = evt.type;
200 }
201 
202 function onFolderAddChild(evt) {
203  gLastFolderAction = evt.type;
204 }
205 
206 function onFolderRemoveChild(evt) {
207  gLastFolderAction = evt.type;
208 }
209 
210 function onFolderMove(evt) {
211  gLastFolderAction = evt.type;
212 }
213 
214 function onBookmarkChange(evt) {
215  gLastBookmarkAction = evt.data;
216 }
217 
218 function onBookmarkRemove(evt) {
219  gLastBookmarkAction = evt.type;
220 }
221 
222 function onBookmarkMove(evt) {
223  gLastBookmarkAction = evt.type;
224 }
function onFolderChange(evt)
var testFolder
var Application
Definition: sbAboutDRM.js:37
function onFolderMove(evt)
var gLastBookmarkAction
function onBookmarkRemove(evt)
const Ci
function test()
return null
Definition: FeedWriter.js:1143
function url(spec)
function onBookmarkMove(evt)
var gLastFolderAction
function onFolderRemove(evt)
function onFolderRemoveChild(evt)
var ios
Definition: head_feeds.js:5
const Cc
function onBookmarkChange(evt)
function onFolderAddChild(evt)