summaryrefslogtreecommitdiffstats
path: root/browser/components/places/tests/browser/browser_paste_into_tags.js
blob: d159fa564655cce4cbd5fef6896d47beb95167bd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/
 */

"use strict";

const TEST_URL = Services.io.newURI("http://example.com/");
const MOZURISPEC = Services.io.newURI("http://mozilla.com/");

add_task(async function () {
  let organizer = await promiseLibrary();

  ok(PlacesUtils, "PlacesUtils in scope");
  ok(PlacesUIUtils, "PlacesUIUtils in scope");

  let PlacesOrganizer = organizer.PlacesOrganizer;
  ok(PlacesOrganizer, "Places organizer in scope");

  let ContentTree = organizer.ContentTree;
  ok(ContentTree, "ContentTree is in scope");

  let visits = {
    uri: MOZURISPEC,
    transition: PlacesUtils.history.TRANSITION_TYPED,
  };
  await PlacesTestUtils.addVisits(visits);

  // create an initial tag to work with
  let newBookmark = await PlacesUtils.bookmarks.insert({
    parentGuid: PlacesUtils.bookmarks.unfiledGuid,
    index: PlacesUtils.bookmarks.DEFAULT_INDEX,
    type: PlacesUtils.bookmarks.TYPE_BOOKMARK,
    title: "bookmark/" + TEST_URL.spec,
    url: TEST_URL,
  });

  ok(newBookmark, "A bookmark was added");
  PlacesUtils.tagging.tagURI(TEST_URL, ["foo"]);
  let tags = PlacesUtils.tagging.getTagsForURI(TEST_URL);
  is(tags[0], "foo", "tag is foo");

  // focus the new tag
  focusTag(PlacesOrganizer);

  let populate = () => copyHistNode(PlacesOrganizer, ContentTree);
  await promiseClipboard(populate, PlacesUtils.TYPE_X_MOZ_PLACE);

  focusTag(PlacesOrganizer);
  await PlacesOrganizer._places.controller.paste();

  // re-focus the history again
  PlacesOrganizer.selectLeftPaneBuiltIn("History");
  let histContainer = PlacesOrganizer._places.selectedNode;
  PlacesUtils.asContainer(histContainer);
  histContainer.containerOpen = true;
  PlacesOrganizer._places.selectNode(histContainer.getChild(0));
  let histNode = ContentTree.view.view.nodeForTreeIndex(0);
  ok(histNode, "histNode exists: " + histNode.title);

  // check to see if the history node is tagged!
  tags = PlacesUtils.tagging.getTagsForURI(MOZURISPEC);
  ok(tags.length == 1, "history node is tagged: " + tags.length);

  // check if a bookmark was created
  let bookmarks = [];
  await PlacesUtils.bookmarks.fetch({ url: MOZURISPEC }, bm => {
    bookmarks.push(bm);
  });
  ok(!!bookmarks.length, "bookmark exists for the tagged history item");

  // is the bookmark visible in the UI?
  // get the Unsorted Bookmarks node
  PlacesOrganizer.selectLeftPaneBuiltIn("UnfiledBookmarks");

  // now we can see what is in the ContentTree tree
  let unsortedNode = ContentTree.view.view.nodeForTreeIndex(1);
  ok(unsortedNode, "unsortedNode is not null: " + unsortedNode.uri);
  is(unsortedNode.uri, MOZURISPEC.spec, "node uri's are the same");

  await promiseLibraryClosed(organizer);

  // Remove new Places data we created.
  PlacesUtils.tagging.untagURI(MOZURISPEC, ["foo"]);
  PlacesUtils.tagging.untagURI(TEST_URL, ["foo"]);
  tags = PlacesUtils.tagging.getTagsForURI(TEST_URL);
  is(tags.length, 0, "tags are gone");

  await PlacesUtils.bookmarks.eraseEverything();
  await PlacesUtils.history.clear();
});

function focusTag(PlacesOrganizer) {
  PlacesOrganizer.selectLeftPaneBuiltIn("Tags");
  let tags = PlacesOrganizer._places.selectedNode;
  tags.containerOpen = true;
  let fooTag = tags.getChild(0);
  let tagNode = fooTag;
  PlacesOrganizer._places.selectNode(fooTag);
  is(tagNode.title, "foo", "tagNode title is foo");
  let ip = PlacesOrganizer._places.insertionPoint;
  ok(ip.isTag, "IP is a tag");
}

function copyHistNode(PlacesOrganizer, ContentTree) {
  // focus the history object
  PlacesOrganizer.selectLeftPaneBuiltIn("History");
  let histContainer = PlacesOrganizer._places.selectedNode;
  PlacesUtils.asContainer(histContainer);
  histContainer.containerOpen = true;
  PlacesOrganizer._places.selectNode(histContainer.getChild(0));
  let histNode = ContentTree.view.view.nodeForTreeIndex(0);
  ContentTree.view.selectNode(histNode);
  is(histNode.uri, MOZURISPEC.spec, "historyNode exists: " + histNode.uri);
  // copy the history node
  ContentTree.view.controller.copy();
}