summaryrefslogtreecommitdiffstats
path: root/toolkit/components/places/tests/bookmarks/test_458683.js
blob: d31fca66e1e3516b9bc167705c2cf335a3136286 (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
/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */
/* 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/. */

/*
  This test is:
    - don't block while doing backup and restore if tag containers contain
      bogus items (separators, folders)
*/

const ITEM_TITLE = "invalid uri";
const ITEM_URL = "http://test.mozilla.org/";
const TAG_NAME = "testTag";

function validateResults() {
  let toolbar = PlacesUtils.getFolderContents(
    PlacesUtils.bookmarks.toolbarGuid
  ).root;
  // test for our bookmark
  Assert.equal(toolbar.childCount, 1);
  for (var i = 0; i < toolbar.childCount; i++) {
    var folderNode = toolbar.getChild(0);
    Assert.equal(folderNode.type, folderNode.RESULT_TYPE_URI);
    Assert.equal(folderNode.title, ITEM_TITLE);
  }
  toolbar.containerOpen = false;

  // test for our tag
  var tags = PlacesUtils.tagging.getTagsForURI(Services.io.newURI(ITEM_URL));
  Assert.equal(tags.length, 1);
  Assert.equal(tags[0], TAG_NAME);
}

add_task(async function () {
  let jsonFile = PathUtils.join(PathUtils.profileDir, "bookmarks.json");

  // add a valid bookmark
  let item = await PlacesUtils.bookmarks.insert({
    parentGuid: PlacesUtils.bookmarks.toolbarGuid,
    title: ITEM_TITLE,
    url: ITEM_URL,
  });

  // create a tag
  PlacesUtils.tagging.tagURI(Services.io.newURI(ITEM_URL), [TAG_NAME]);
  // get tag folder id
  let tagRoot = PlacesUtils.getFolderContents(
    PlacesUtils.bookmarks.tagsGuid
  ).root;
  Assert.equal(tagRoot.childCount, 1);
  let tagItemGuid = PlacesUtils.asContainer(tagRoot.getChild(0)).bookmarkGuid;
  tagRoot.containerOpen = false;

  function insert({ type, parentGuid }) {
    return PlacesUtils.withConnectionWrapper(
      "test_458683: insert",
      async db => {
        await db.executeCached(
          `INSERT INTO moz_bookmarks (type, parent, position, guid)
         VALUES (:type,
                 (SELECT id FROM moz_bookmarks WHERE guid = :parentGuid),
                 (SELECT MAX(position) + 1 FROM moz_bookmarks WHERE parent = (SELECT id FROM moz_bookmarks WHERE guid = :parentGuid)),
                 GENERATE_GUID())`,
          { type, parentGuid }
        );
      }
    );
  }

  // add a separator and a folder inside tag folder
  // We must insert these manually, because the new bookmarking API doesn't
  // support inserting invalid items into the tag folder.
  await insert({
    parentGuid: tagItemGuid,
    type: PlacesUtils.bookmarks.TYPE_SEPARATOR,
  });
  await insert({
    parentGuid: tagItemGuid,
    type: PlacesUtils.bookmarks.TYPE_FOLDER,
  });

  // add a separator and a folder inside tag root
  await PlacesUtils.bookmarks.insert({
    parentGuid: PlacesUtils.bookmarks.tagsGuid,
    type: PlacesUtils.bookmarks.TYPE_SEPARATOR,
  });
  await PlacesUtils.bookmarks.insert({
    parentGuid: PlacesUtils.bookmarks.tagsGuid,
    title: "test tags root folder",
    type: PlacesUtils.bookmarks.TYPE_FOLDER,
  });

  // sanity
  validateResults();

  await BookmarkJSONUtils.exportToFile(jsonFile);

  // clean
  PlacesUtils.tagging.untagURI(Services.io.newURI(ITEM_URL), [TAG_NAME]);
  await PlacesUtils.bookmarks.remove(item);

  // restore json file
  await BookmarkJSONUtils.importFromFile(jsonFile, { replace: true });

  validateResults();

  // clean up
  await IOUtils.remove(jsonFile);
});