summaryrefslogtreecommitdiffstats
path: root/browser/components/places/tests/browser/browser_bookmark_folder_moveability.js
blob: d981aa4713d02f4ac5f9e28a4690776f27253950 (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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/* vim:set ts=2 sw=2 sts=2 et: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

"use strict";

add_task(async function () {
  let root = await PlacesUtils.bookmarks.insert({
    parentGuid: PlacesUtils.bookmarks.unfiledGuid,
    title: "",
    type: PlacesUtils.bookmarks.TYPE_FOLDER,
  });

  registerCleanupFunction(async () => {
    await PlacesUtils.bookmarks.eraseEverything();
  });

  await withSidebarTree("bookmarks", async function (tree) {
    info("Test a regular folder");
    let folder = await PlacesUtils.bookmarks.insert({
      parentGuid: root.guid,
      title: "",
      type: PlacesUtils.bookmarks.TYPE_FOLDER,
    });
    tree.selectItems([folder.guid]);
    Assert.equal(
      tree.selectedNode.bookmarkGuid,
      folder.guid,
      "Selected the expected node"
    );
    Assert.equal(tree.selectedNode.type, 6, "node is a folder");
    Assert.ok(
      tree.controller.canMoveNode(tree.selectedNode),
      "can move regular folder node"
    );

    info("Test a folder shortcut");
    let shortcut = await PlacesUtils.bookmarks.insert({
      parentGuid: root.guid,
      title: "bar",
      url: `place:parent=${folder.guid}`,
    });
    tree.selectItems([shortcut.guid]);
    Assert.equal(
      tree.selectedNode.bookmarkGuid,
      shortcut.guid,
      "Selected the expected node"
    );
    Assert.equal(tree.selectedNode.type, 9, "node is a folder shortcut");
    Assert.equal(
      PlacesUtils.getConcreteItemGuid(tree.selectedNode),
      folder.guid,
      "shortcut node guid and concrete guid match"
    );
    Assert.ok(
      tree.controller.canMoveNode(tree.selectedNode),
      "can move folder shortcut node"
    );

    info("Test a query");
    let bookmark = await PlacesUtils.bookmarks.insert({
      parentGuid: root.guid,
      title: "",
      url: "http://foo.com",
    });
    tree.selectItems([bookmark.guid]);
    Assert.equal(
      tree.selectedNode.bookmarkGuid,
      bookmark.guid,
      "Selected the expected node"
    );
    let query = await PlacesUtils.bookmarks.insert({
      parentGuid: root.guid,
      title: "bar",
      url: `place:terms=foo`,
    });
    tree.selectItems([query.guid]);
    Assert.equal(
      tree.selectedNode.bookmarkGuid,
      query.guid,
      "Selected the expected node"
    );
    Assert.ok(
      tree.controller.canMoveNode(tree.selectedNode),
      "can move query node"
    );

    info("Test a tag container");
    PlacesUtils.tagging.tagURI(bookmark.url.URI, ["bar"]);
    // Add the tags root query.
    let tagsQuery = await PlacesUtils.bookmarks.insert({
      parentGuid: root.guid,
      title: "",
      url: "place:type=" + Ci.nsINavHistoryQueryOptions.RESULTS_AS_TAGS_ROOT,
    });
    tree.selectItems([tagsQuery.guid]);
    PlacesUtils.asQuery(tree.selectedNode).containerOpen = true;
    Assert.equal(tree.selectedNode.childCount, 1, "has tags");
    let tagNode = tree.selectedNode.getChild(0);
    Assert.ok(
      !tree.controller.canMoveNode(tagNode),
      "should not be able to move tag container node"
    );
    tree.selectedNode.containerOpen = false;

    info(
      "Test that special folders and cannot be moved but other shortcuts can."
    );
    let roots = [
      PlacesUtils.bookmarks.menuGuid,
      PlacesUtils.bookmarks.unfiledGuid,
      PlacesUtils.bookmarks.toolbarGuid,
    ];

    for (let guid of roots) {
      tree.selectItems([guid]);
      Assert.ok(
        !tree.controller.canMoveNode(tree.selectedNode),
        "shouldn't be able to move default shortcuts to roots"
      );
      let s = await PlacesUtils.bookmarks.insert({
        parentGuid: root.guid,
        title: "bar",
        url: `place:parent=${guid}`,
      });
      tree.selectItems([s.guid]);
      Assert.equal(
        tree.selectedNode.bookmarkGuid,
        s.guid,
        "Selected the expected node"
      );
      Assert.ok(
        tree.controller.canMoveNode(tree.selectedNode),
        "should be able to move user-created shortcuts to roots"
      );
    }
  });
});