summaryrefslogtreecommitdiffstats
path: root/toolkit/components/places/tests/chrome/test_reloadLivemarks.xul
blob: 43772d09f40413225722b0cd1641d08a35323042 (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
<?xml version="1.0"?>
<?xml-stylesheet href="chrome://global/skin" type="text/css"?>
<?xml-stylesheet
  href="chrome://mochikit/content/tests/SimpleTest/test.css" type="text/css"?>
<window title="Reload Livemarks"
  xmlns:html="http://www.w3.org/1999/xhtml"
  xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"
  onload="runTest()" onunload="cleanup()">
  <script type="application/javascript"
   src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>

  <body xmlns="http://www.w3.org/1999/xhtml" />

<script type="application/javascript">
<![CDATA[
// Test that for concurrent reload of livemarks.

SimpleTest.waitForExplicitFinish();

var Cc = Components.classes;
var Ci = Components.interfaces;
var Cr = Components.results;

Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
Components.utils.import("resource://gre/modules/NetUtil.jsm");
Components.utils.import("resource://gre/modules/PlacesUtils.jsm");

let gLivemarks = [
  { id: -1,
    title: "foo",
    parentGuid: PlacesUtils.bookmarks.toolbarGuid,
    feedURI: NetUtil.newURI("http://mochi.test:8888/tests/toolkit/components/places/tests/chrome/link-less-items.rss")
  },
  { id: -1,
    title: "bar",
    parentGuid: PlacesUtils.bookmarks.toolbarGuid,
    feedURI: NetUtil.newURI("http://mochi.test:8888/tests/toolkit/components/places/tests/chrome/link-less-items-no-site-uri.rss")
  },
];

function runTest()
{
  addLivemarks(function () {
    reloadLivemarks(false, function () {
      reloadLivemarks(true, function () {
        removeLivemarks(SimpleTest.finish);
      });
    });
    // Ensure this normal reload doesn't overwrite the forced one.
    PlacesUtils.livemarks.reloadLivemarks();
  });
}

function addLivemarks(aCallback) {
  info("Adding livemarks");
  let count = gLivemarks.length;
  gLivemarks.forEach(function(aLivemarkData) {
    PlacesUtils.livemarks.addLivemark(aLivemarkData)
      .then(function (aLivemark) {
        ok(aLivemark.feedURI.equals(aLivemarkData.feedURI), "Livemark added");
        aLivemarkData.id = aLivemark.id;
        if (--count == 0) {
          aCallback();
        }
      },
      function () {
        is(true, false, "Should not fail adding a livemark.");
        aCallback();
      });
  });
}

function reloadLivemarks(aForceUpdate, aCallback) {
  info("Reloading livemarks with forceUpdate: " + aForceUpdate);
  let count = gLivemarks.length;
  gLivemarks.forEach(function(aLivemarkData) {
    PlacesUtils.livemarks.getLivemark(aLivemarkData)
      .then(aLivemark => {
        ok(aLivemark.feedURI.equals(aLivemarkData.feedURI), "Livemark found");
        aLivemarkData._observer = new resultObserver(aLivemark, function() {
          if (++count == gLivemarks.length) {
            aCallback();
          }
        });
        if (--count == 0) {
          PlacesUtils.livemarks.reloadLivemarks(aForceUpdate);
        }
      },
      function() {
        is(true, false, "Should not fail getting a livemark.");
        aCallback();
      }
    );
  });
}

function removeLivemarks(aCallback) {
  info("Removing livemarks");
  let count = gLivemarks.length;
  gLivemarks.forEach(function(aLivemarkData) {
    PlacesUtils.livemarks.removeLivemark(aLivemarkData).then(
      function (aLivemark) {
        if (--count == 0) {
          aCallback();
        }
      },
      function() {
        is(true, false, "Should not fail adding a livemark.");
        aCallback();
      }
    );
  });
}

function resultObserver(aLivemark, aCallback) {
  this._node = {};
  this._livemark = aLivemark;
  this._callback = aCallback;
  this._livemark.registerForUpdates(this._node, this);
}
resultObserver.prototype = {
  nodeInserted: function() {},
  nodeRemoved: function() {},
  nodeAnnotationChanged: function() {},
  nodeTitleChanged: function() {},
  nodeHistoryDetailsChanged: function() {},
  nodeMoved: function() {},
  ontainerStateChanged: function () {},
  sortingChanged: function() {},
  batching: function() {},
  invalidateContainer: function(aContainer) {
    // Wait for load finish.
    if (this._livemark.status == Ci.mozILivemark.STATUS_LOADING)
      return;

    this._terminate();
    this._callback();
  },
  _terminate: function () {
    if (!this._terminated) {
      this._livemark.unregisterForUpdates(this._node);
      this._terminated = true;
    }
  }
};

function cleanup() {
  gLivemarks.forEach(function(aLivemarkData) {
    if (aLivemarkData._observer)
      aLivemarkData._observer._terminate();
  });
}
]]>
</script>
</window>