summaryrefslogtreecommitdiffstats
path: root/browser/components/places/tests/unit/test_leftpane_corruption_handling.js
blob: 0af6f4e95d29017d599b4f026122229b052da652 (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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/* -*- 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/. */

/**
 * Tests that we build a working leftpane in various corruption situations.
 */

// Used to store the original leftPaneFolderId getter.
var gLeftPaneFolderIdGetter;
var gAllBookmarksFolderIdGetter;
// Used to store the original left Pane status as a JSON string.
var gReferenceHierarchy;
var gLeftPaneFolderId;

add_task(function* () {
  // We want empty roots.
  yield PlacesUtils.bookmarks.eraseEverything();

  // Sanity check.
  Assert.ok(!!PlacesUIUtils);

  // Check getters.
  gLeftPaneFolderIdGetter = Object.getOwnPropertyDescriptor(PlacesUIUtils, "leftPaneFolderId");
  Assert.equal(typeof(gLeftPaneFolderIdGetter.get), "function");
  gAllBookmarksFolderIdGetter = Object.getOwnPropertyDescriptor(PlacesUIUtils, "allBookmarksFolderId");
  Assert.equal(typeof(gAllBookmarksFolderIdGetter.get), "function");

  do_register_cleanup(() => PlacesUtils.bookmarks.eraseEverything());
});

add_task(function* () {
  // Add a third party bogus annotated item.  Should not be removed.
  let folder = yield PlacesUtils.bookmarks.insert({
    parentGuid: PlacesUtils.bookmarks.unfiledGuid,
    title: "test",
    index: PlacesUtils.bookmarks.DEFAULT_INDEX,
    type: PlacesUtils.bookmarks.TYPE_FOLDER
  });

  let folderId = yield PlacesUtils.promiseItemId(folder.guid);
  PlacesUtils.annotations.setItemAnnotation(folderId, ORGANIZER_QUERY_ANNO,
                                            "test", 0,
                                            PlacesUtils.annotations.EXPIRE_NEVER);

  // Create the left pane, and store its current status, it will be used
  // as reference value.
  gLeftPaneFolderId = PlacesUIUtils.leftPaneFolderId;
  gReferenceHierarchy = folderIdToHierarchy(gLeftPaneFolderId);

  while (gTests.length) {
    // Run current test.
    yield Task.spawn(gTests.shift());

    // Regenerate getters.
    Object.defineProperty(PlacesUIUtils, "leftPaneFolderId", gLeftPaneFolderIdGetter);
    gLeftPaneFolderId = PlacesUIUtils.leftPaneFolderId;
    Object.defineProperty(PlacesUIUtils, "allBookmarksFolderId", gAllBookmarksFolderIdGetter);

    // Check the new left pane folder.
    let leftPaneHierarchy = folderIdToHierarchy(gLeftPaneFolderId)
    Assert.equal(gReferenceHierarchy, leftPaneHierarchy);

    folder = yield PlacesUtils.bookmarks.fetch({guid: folder.guid});
    Assert.equal(folder.title, "test");
  }
});

// Corruption cases.
var gTests = [

  function* test1() {
    print("1. Do nothing, checks test calibration.");
  },

  function* test2() {
    print("2. Delete the left pane folder.");
    let guid = yield PlacesUtils.promiseItemGuid(gLeftPaneFolderId);
    yield PlacesUtils.bookmarks.remove(guid);
  },

  function* test3() {
    print("3. Delete a child of the left pane folder.");
    let guid = yield PlacesUtils.promiseItemGuid(gLeftPaneFolderId);
    let bm = yield PlacesUtils.bookmarks.fetch({parentGuid: guid, index: 0});
    yield PlacesUtils.bookmarks.remove(bm.guid);
  },

  function* test4() {
    print("4. Delete AllBookmarks.");
    let guid = yield PlacesUtils.promiseItemGuid(PlacesUIUtils.allBookmarksFolderId);
    yield PlacesUtils.bookmarks.remove(guid);
  },

  function* test5() {
    print("5. Create a duplicated left pane folder.");
    let folder = yield PlacesUtils.bookmarks.insert({
      parentGuid: PlacesUtils.bookmarks.unfiledGuid,
      title: "PlacesRoot",
      index: PlacesUtils.bookmarks.DEFAULT_INDEX,
      type: PlacesUtils.bookmarks.TYPE_FOLDER
    });

    let folderId = yield PlacesUtils.promiseItemId(folder.guid);
    PlacesUtils.annotations.setItemAnnotation(folderId, ORGANIZER_FOLDER_ANNO,
                                              "PlacesRoot", 0,
                                              PlacesUtils.annotations.EXPIRE_NEVER);
  },

  function* test6() {
    print("6. Create a duplicated left pane query.");
    let folder = yield PlacesUtils.bookmarks.insert({
      parentGuid: PlacesUtils.bookmarks.unfiledGuid,
      title: "AllBookmarks",
      index: PlacesUtils.bookmarks.DEFAULT_INDEX,
      type: PlacesUtils.bookmarks.TYPE_FOLDER
    });

    let folderId = yield PlacesUtils.promiseItemId(folder.guid);
    PlacesUtils.annotations.setItemAnnotation(folderId, ORGANIZER_QUERY_ANNO,
                                              "AllBookmarks", 0,
                                              PlacesUtils.annotations.EXPIRE_NEVER);
  },

  function* test7() {
    print("7. Remove the left pane folder annotation.");
    PlacesUtils.annotations.removeItemAnnotation(gLeftPaneFolderId,
                                                 ORGANIZER_FOLDER_ANNO);
  },

  function* test8() {
    print("8. Remove a left pane query annotation.");
    PlacesUtils.annotations.removeItemAnnotation(PlacesUIUtils.allBookmarksFolderId,
                                                 ORGANIZER_QUERY_ANNO);
  },

  function* test9() {
    print("9. Remove a child of AllBookmarks.");
    let guid = yield PlacesUtils.promiseItemGuid(PlacesUIUtils.allBookmarksFolderId);
    let bm = yield PlacesUtils.bookmarks.fetch({parentGuid: guid, index: 0});
    yield PlacesUtils.bookmarks.remove(bm.guid);
  }

];

/**
 * Convert a folder item id to a JSON representation of it and its contents.
 */
function folderIdToHierarchy(aFolderId) {
  let root = PlacesUtils.getFolderContents(aFolderId).root;
  let hier = JSON.stringify(hierarchyToObj(root));
  root.containerOpen = false;
  return hier;
}

function hierarchyToObj(aNode) {
  let o = {}
  o.title = aNode.title;
  o.annos = PlacesUtils.getAnnotationsForItem(aNode.itemId)
  if (PlacesUtils.nodeIsURI(aNode)) {
    o.uri = aNode.uri;
  }
  else if (PlacesUtils.nodeIsFolder(aNode)) {
    o.children = [];
    PlacesUtils.asContainer(aNode).containerOpen = true;
    for (let i = 0; i < aNode.childCount; ++i) {
      o.children.push(hierarchyToObj(aNode.getChild(i)));
    }
    aNode.containerOpen = false;
  }
  return o;
}