summaryrefslogtreecommitdiffstats
path: root/browser/components/places/tests/browser/browser_library_bulk_tag_bookmarks.js
blob: 23d3b305644a0197e1216ce95e3d1656da0bdfa0 (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
140
141
142
143
144
145
146
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

/**
 * Test that a tag can be added to multiple bookmarks at once from the library.
 */
"use strict";

const TEST_URLS = ["about:buildconfig", "about:robots"];

add_task(async function test_bulk_tag_from_library() {
  // Create multiple bookmarks.
  for (const url of TEST_URLS) {
    await PlacesUtils.bookmarks.insert({
      parentGuid: PlacesUtils.bookmarks.unfiledGuid,
      url,
    });
  }

  // Open library panel.
  const library = await promiseLibrary("UnfiledBookmarks");
  const cleanupFn = async () => {
    await promiseLibraryClosed(library);
    await PlacesUtils.bookmarks.eraseEverything();
  };
  registerCleanupFunction(cleanupFn);

  // Add a tag to multiple bookmarks.
  library.ContentTree.view.selectAll();
  const promiseAllTagsChanged = TEST_URLS.map(url =>
    PlacesTestUtils.waitForNotification("bookmark-tags-changed", events =>
      events.some(evt => evt.url === url)
    )
  );
  const tag = "some, tag";
  const tagWithDuplicates = `${tag}, tag`;
  fillBookmarkTextField("editBMPanel_tagsField", tagWithDuplicates, library);
  await Promise.all(promiseAllTagsChanged);
  await TestUtils.waitForCondition(
    () =>
      library.document.getElementById("editBMPanel_tagsField").value === tag,
    "Input field matches the new tags and duplicates are removed."
  );

  // Verify that the bookmarks were tagged successfully.
  for (const url of TEST_URLS) {
    Assert.deepEqual(
      PlacesUtils.tagging.getTagsForURI(Services.io.newURI(url)),
      ["some", "tag"],
      url + " should have the correct tags."
    );
  }
  await cleanupFn();
});

add_task(async function test_bulk_tag_tags_selector() {
  // Create multiple bookmarks with a common tag.
  for (const [i, url] of TEST_URLS.entries()) {
    await PlacesUtils.bookmarks.insert({
      parentGuid: PlacesUtils.bookmarks.unfiledGuid,
      url,
    });
    PlacesUtils.tagging.tagURI(Services.io.newURI(url), [
      "common",
      `unique_${i}`,
    ]);
  }

  // Open library panel.
  const library = await promiseLibrary("UnfiledBookmarks");
  const cleanupFn = async () => {
    await promiseLibraryClosed(library);
    await PlacesUtils.bookmarks.eraseEverything();
  };
  registerCleanupFunction(cleanupFn);

  // Open tags selector.
  library.document.getElementById("editBMPanel_tagsSelectorRow").hidden = false;

  // Select all bookmarks.
  const tagsSelector = library.document.getElementById(
    "editBMPanel_tagsSelector"
  );
  library.ContentTree.view.selectAll();

  // Verify that the input field only shows the common tag.
  await TestUtils.waitForCondition(
    () =>
      library.document.getElementById("editBMPanel_tagsField").value ===
      "common",
    "Input field only shows the common tag."
  );

  // Verify that the tags selector shows all tags, and only the common one is
  // checked.
  async function checkTagsSelector(aAvailableTags, aCheckedTags) {
    let tags = await PlacesUtils.bookmarks.fetchTags();
    is(tags.length, aAvailableTags.length, "Check tags list");
    let children = tagsSelector.children;
    is(
      children.length,
      aAvailableTags.length,
      "Found expected number of tags in the tags selector"
    );

    Array.prototype.forEach.call(children, function (aChild) {
      let tag = aChild.querySelector("label").getAttribute("value");
      ok(true, "Found tag '" + tag + "' in the selector");
      ok(aAvailableTags.includes(tag), "Found expected tag");
      let checked = aChild.getAttribute("checked") == "true";
      is(checked, aCheckedTags.includes(tag), "Tag is correctly marked");
    });
  }

  async function promiseTagSelectorUpdated(task) {
    let promise = BrowserTestUtils.waitForEvent(
      tagsSelector,
      "BookmarkTagsSelectorUpdated"
    );

    await task();
    return promise;
  }

  info("Check the initial common tag.");
  await checkTagsSelector(["common", "unique_0", "unique_1"], ["common"]);

  // Verify that the common tag can be edited.
  await promiseTagSelectorUpdated(() => {
    info("Edit the common tag.");
    fillBookmarkTextField("editBMPanel_tagsField", "common_updated", library);
  });
  await checkTagsSelector(
    ["common_updated", "unique_0", "unique_1"],
    ["common_updated"]
  );

  // Verify that the common tag can be removed.
  await promiseTagSelectorUpdated(() => {
    info("Remove the commmon tag.");
    fillBookmarkTextField("editBMPanel_tagsField", "", library);
  });
  await checkTagsSelector(["unique_0", "unique_1"], []);

  await cleanupFn();
});