summaryrefslogtreecommitdiffstats
path: root/browser/components/places/tests/unit/test_421483.js
blob: a0d1383728e2909e4da15222c9f8fa3d9b4db38d (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
/* -*- 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/. */


const SMART_BOOKMARKS_PREF = "browser.places.smartBookmarksVersion";

var gluesvc = Cc["@mozilla.org/browser/browserglue;1"].
                getService(Ci.nsIObserver);
// Avoid default bookmarks import.
gluesvc.observe(null, "initial-migration-will-import-default-bookmarks", "");

function run_test() {
  run_next_test();
}

add_task(function* smart_bookmarks_disabled() {
  Services.prefs.setIntPref("browser.places.smartBookmarksVersion", -1);
  yield rebuildSmartBookmarks();

  let smartBookmarkItemIds =
    PlacesUtils.annotations.getItemsWithAnnotation(SMART_BOOKMARKS_ANNO);
  Assert.equal(smartBookmarkItemIds.length, 0);

  do_print("check that pref has not been bumped up");
  Assert.equal(Services.prefs.getIntPref("browser.places.smartBookmarksVersion"), -1);
});

add_task(function* create_smart_bookmarks() {
  Services.prefs.setIntPref("browser.places.smartBookmarksVersion", 0);
  yield rebuildSmartBookmarks();

  let smartBookmarkItemIds =
    PlacesUtils.annotations.getItemsWithAnnotation(SMART_BOOKMARKS_ANNO);
  Assert.notEqual(smartBookmarkItemIds.length, 0);

  do_print("check that pref has been bumped up");
  Assert.ok(Services.prefs.getIntPref("browser.places.smartBookmarksVersion") > 0);
});

add_task(function* remove_smart_bookmark_and_restore() {
  let smartBookmarkItemIds =
    PlacesUtils.annotations.getItemsWithAnnotation(SMART_BOOKMARKS_ANNO);
  let smartBookmarksCount = smartBookmarkItemIds.length;
  do_print("remove one smart bookmark and restore");

  let guid = yield PlacesUtils.promiseItemGuid(smartBookmarkItemIds[0]);
  yield PlacesUtils.bookmarks.remove(guid);
  Services.prefs.setIntPref("browser.places.smartBookmarksVersion", 0);

  yield rebuildSmartBookmarks();
  smartBookmarkItemIds =
    PlacesUtils.annotations.getItemsWithAnnotation(SMART_BOOKMARKS_ANNO);
  Assert.equal(smartBookmarkItemIds.length, smartBookmarksCount);

  do_print("check that pref has been bumped up");
  Assert.ok(Services.prefs.getIntPref("browser.places.smartBookmarksVersion") > 0);
});

add_task(function* move_smart_bookmark_rename_and_restore() {
  let smartBookmarkItemIds =
    PlacesUtils.annotations.getItemsWithAnnotation(SMART_BOOKMARKS_ANNO);
  let smartBookmarksCount = smartBookmarkItemIds.length;
  do_print("smart bookmark should be restored in place");

  let guid = yield PlacesUtils.promiseItemGuid(smartBookmarkItemIds[0]);
  let bm = yield PlacesUtils.bookmarks.fetch(guid);
  let oldTitle = bm.title;

  // create a subfolder and move inside it
  let subfolder = yield PlacesUtils.bookmarks.insert({
    parentGuid: bm.parentGuid,
    title: "test",
    index: PlacesUtils.bookmarks.DEFAULT_INDEX,
    type: PlacesUtils.bookmarks.TYPE_FOLDER
  });

  // change title and move into new subfolder
  yield PlacesUtils.bookmarks.update({
    guid: guid,
    parentGuid: subfolder.guid,
    index: PlacesUtils.bookmarks.DEFAULT_INDEX,
    title: "new title"
  });

  // restore
  Services.prefs.setIntPref("browser.places.smartBookmarksVersion", 0);
  yield rebuildSmartBookmarks();

  smartBookmarkItemIds =
    PlacesUtils.annotations.getItemsWithAnnotation(SMART_BOOKMARKS_ANNO);
  Assert.equal(smartBookmarkItemIds.length, smartBookmarksCount);

  guid = yield PlacesUtils.promiseItemGuid(smartBookmarkItemIds[0]);
  bm = yield PlacesUtils.bookmarks.fetch(guid);
  Assert.equal(bm.parentGuid, subfolder.guid);
  Assert.equal(bm.title, oldTitle);

  do_print("check that pref has been bumped up");
  Assert.ok(Services.prefs.getIntPref("browser.places.smartBookmarksVersion") > 0);
});