browser_410196_paste_into_tags.js
Go to the documentation of this file.
1 /* vim:set ts=2 sw=2 sts=2 et: */
2 /* ***** BEGIN LICENSE BLOCK *****
3  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4  *
5  * The contents of this file are subject to the Mozilla Public License Version
6  * 1.1 (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  * http://www.mozilla.org/MPL/
9  *
10  * Software distributed under the License is distributed on an "AS IS" basis,
11  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12  * for the specific language governing rights and limitations under the
13  * License.
14  *
15  * The Original Code is Places test code.
16  *
17  * The Initial Developer of the Original Code is Mozilla Corp.
18  * Portions created by the Initial Developer are Copyright (C) 2009
19  * the Initial Developer. All Rights Reserved.
20  *
21  * Contributor(s):
22  * David Dahl <ddahl@mozilla.com>
23  *
24  * Alternatively, the contents of this file may be used under the terms of
25  * either the GNU General Public License Version 2 or later (the "GPL"), or
26  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27  * in which case the provisions of the GPL or the LGPL are applicable instead
28  * of those above. If you wish to allow use of your version of this file only
29  * under the terms of either the GPL or the LGPL, and not to allow others to
30  * use your version of this file under the terms of the MPL, indicate your
31  * decision by deleting the provisions above and replace them with the notice
32  * and other provisions required by the GPL or the LGPL. If you do not delete
33  * the provisions above, a recipient may use your version of this file under
34  * the terms of any one of the MPL, the GPL or the LGPL.
35  *
36  * ***** END LICENSE BLOCK ***** */
37 
38 // Get history services
39 var hs = Cc["@mozilla.org/browser/nav-history-service;1"].
40  getService(Ci.nsINavHistoryService);
41 var gh = hs.QueryInterface(Ci.nsIGlobalHistory2);
42 var bh = hs.QueryInterface(Ci.nsIBrowserHistory);
43 var ts = Cc["@mozilla.org/browser/tagging-service;1"].
44  getService(Components.interfaces.nsITaggingService);
45 var bs = Cc["@mozilla.org/browser/nav-bookmarks-service;1"].
46  getService(Ci.nsINavBookmarksService);
47 
48 function add_visit(aURI, aReferrer) {
49  var visitId = hs.addVisit(aURI,
50  Date.now() * 1000,
51  aReferrer,
52  hs.TRANSITION_TYPED, // user typed in URL bar
53  false, // not redirect
54  0);
55  return visitId;
56 }
57 
58 function add_bookmark(aURI) {
59  var bId = bs.insertBookmark(bs.unfiledBookmarksFolder, aURI,
60  bs.DEFAULT_INDEX, "bookmark/" + aURI.spec);
61  return bId;
62 }
63 
64 const TEST_URL = "http://example.com/";
65 const MOZURISPEC = "http://mozilla.com/";
66 
67 function test() {
69  var win = window.openDialog("chrome://browser/content/places/places.xul",
70  "",
71  "chrome,toolbar=yes,dialog=no,resizable");
72 
73  win.addEventListener("load", function onload() {
74  win.removeEventListener("load", onload, false);
75  executeSoon(function () {
76  var PU = win.PlacesUtils;
77  var PO = win.PlacesOrganizer;
78  var PUIU = win.PlacesUIUtils;
79 
80  // individual tests for each step of tagging a history item
81  var tests = {
82 
83  sanity: function(){
84  // sanity check
85  ok(PU, "PlacesUtils in scope");
86  ok(PUIU, "PlacesUIUtils in scope");
87  ok(PO, "Places organizer in scope");
88  },
89 
90  makeHistVisit: function() {
91  // need to add a history object
92  var testURI1 = PU._uri(MOZURISPEC);
93  isnot(testURI1, null, "testURI is not null");
94  var visitId = add_visit(testURI1);
95  ok(visitId > 0, "A visit was added to the history");
96  ok(gh.isVisited(testURI1), MOZURISPEC + " is a visited url.");
97  },
98 
99  makeTag: function() {
100  // create an initial tag to work with
101  var bmId = add_bookmark(PlacesUtils._uri(TEST_URL));
102  ok(bmId > 0, "A bookmark was added");
103  ts.tagURI(PlacesUtils._uri(TEST_URL), ["foo"]);
104  var tags = ts.getTagsForURI(PU._uri(TEST_URL), {});
105  is(tags[0], 'foo', "tag is foo");
106  },
107 
108  focusTag: function (paste){
109  // focus the new tag
110  PO.selectLeftPaneQuery("Tags");
111  var tags = PO._places.selectedNode;
112  tags.containerOpen = true;
113  var fooTag = tags.getChild(0);
114  this.tagNode = fooTag;
115  PO._places.selectNode(fooTag);
116  is(this.tagNode.title, 'foo', "tagNode title is foo");
117  var ip = PO._places.insertionPoint;
118  ok(ip.isTag, "IP is a tag");
119  if (paste) {
120  ok(true, "About to paste");
121  PO._places.controller.paste();
122  }
123  },
124 
125  histNode: null,
126 
127  copyHistNode: function (){
128  // focus the history object
129  PO.selectLeftPaneQuery("History");
130  var histContainer = PO._places.selectedNode.QueryInterface(Ci.nsINavHistoryContainerResultNode);
131  histContainer.containerOpen = true;
132  PO._places.selectNode(histContainer.getChild(0));
133  this.histNode = PO._content.view.nodeForTreeIndex(0);
134  PO._content.selectNode(this.histNode);
135  is(this.histNode.uri, MOZURISPEC,
136  "historyNode exists: " + this.histNode.uri);
137  // copy the history node
138  PO._content.controller.copy();
139  },
140 
141  waitForPaste: function (){
142  try {
143  var xferable = Cc["@mozilla.org/widget/transferable;1"].
144  createInstance(Ci.nsITransferable);
145  xferable.addDataFlavor(PU.TYPE_X_MOZ_PLACE);
146  PUIU.clipboard.getData(xferable, Ci.nsIClipboard.kGlobalClipboard);
147  var data = { }, type = { };
148  xferable.getAnyTransferData(type, data, { });
149  // Data is in the clipboard
150  continue_test();
151  } catch (ex) {
152  // check again after 100ms.
153  setTimeout(tests.waitForPaste, 100);
154  }
155  },
156 
157  pasteToTag: function (){
158  // paste history node into tag
159  this.focusTag(true);
160  },
161 
162  historyNode: function (){
163  // re-focus the history again
164  PO.selectLeftPaneQuery("History");
165  var histContainer = PO._places.selectedNode.QueryInterface(Ci.nsINavHistoryContainerResultNode);
166  histContainer.containerOpen = true;
167  PO._places.selectNode(histContainer.getChild(0));
168  var histNode = PO._content.view.nodeForTreeIndex(0);
169  ok(histNode, "histNode exists: " + histNode.title);
170  // check to see if the history node is tagged!
171  var tags = PU.tagging.getTagsForURI(PU._uri(MOZURISPEC), {});
172  ok(tags.length == 1, "history node is tagged: " + tags.length);
173  // check if a bookmark was created
174  var isBookmarked = PU.bookmarks.isBookmarked(PU._uri(MOZURISPEC));
175  is(isBookmarked, true, MOZURISPEC + " is bookmarked");
176  var bookmarkIds = PU.bookmarks.getBookmarkIdsForURI(
177  PU._uri(histNode.uri), {});
178  ok(bookmarkIds.length > 0, "bookmark exists for the tagged history item: " + bookmarkIds);
179  },
180 
181  checkForBookmarkInUI: function(){
182  // is the bookmark visible in the UI?
183  // get the Unsorted Bookmarks node
184  PO.selectLeftPaneQuery("UnfiledBookmarks");
185  // now we can see what is in the _content tree
186  var unsortedNode = PO._content.view.nodeForTreeIndex(1);
187  ok(unsortedNode, "unsortedNode is not null: " + unsortedNode.uri);
188  is(unsortedNode.uri, MOZURISPEC, "node uri's are the same");
189  },
190 
191  tagNode: null,
192 
193  cleanUp: function(){
194  ts.untagURI(PU._uri(MOZURISPEC), ["foo"]);
195  ts.untagURI(PU._uri(TEST_URL), ["foo"]);
196  hs.removeAllPages();
197  var tags = ts.getTagsForURI(PU._uri(TEST_URL), {});
198  is(tags.length, 0, "tags are gone");
199  bs.removeFolderChildren(bs.unfiledBookmarksFolder);
200  }
201  };
202 
203  tests.sanity();
204  tests.makeHistVisit();
205  tests.makeTag();
206  tests.focusTag();
207  tests.copyHistNode();
208  tests.waitForPaste();
209 
210  function continue_test() {
211  tests.pasteToTag();
212  tests.historyNode();
213  tests.checkForBookmarkInUI();
214 
215  // remove new places data we created
216  tests.cleanUp();
217 
218  win.close();
219  finish();
220  }
221 
222  });
223  },false);
224 }
const Cc
window onload
function add_visit(aURI, aReferrer)
sidebarFactory createInstance
Definition: nsSidebar.js:351
function continue_test()
getService(Ci.sbIFaceplateManager)
let window
waitForExplicitFinish()
aWindow setTimeout(function(){_this.restoreHistory(aWindow, aTabs, aTabData, aIdMap);}, 0)
return null
Definition: FeedWriter.js:1143
function test()
function add_bookmark(aURI)
function cleanUp()
const Ci
observe data
Definition: FeedWriter.js:1329