|
1 <?xml version="1.0"?> |
|
2 <?xml-stylesheet href="chrome://global/skin" type="text/css"?> |
|
3 <?xml-stylesheet |
|
4 href="chrome://mochikit/content/tests/SimpleTest/test.css" type="text/css"?> |
|
5 <window title="Reload Livemarks" |
|
6 xmlns:html="http://www.w3.org/1999/xhtml" |
|
7 xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul" |
|
8 onload="runTest()" onunload="cleanup()"> |
|
9 <script type="application/javascript" |
|
10 src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script> |
|
11 |
|
12 <body xmlns="http://www.w3.org/1999/xhtml" /> |
|
13 |
|
14 <script type="application/javascript"> |
|
15 <![CDATA[ |
|
16 // Test that for concurrent reload of livemarks. |
|
17 |
|
18 SimpleTest.waitForExplicitFinish(); |
|
19 |
|
20 const Cc = Components.classes; |
|
21 const Ci = Components.interfaces; |
|
22 const Cr = Components.results; |
|
23 |
|
24 Components.utils.import("resource://gre/modules/XPCOMUtils.jsm"); |
|
25 Components.utils.import("resource://gre/modules/NetUtil.jsm"); |
|
26 Components.utils.import("resource://gre/modules/PlacesUtils.jsm"); |
|
27 |
|
28 let gLivemarks = [ |
|
29 { id: -1, |
|
30 title: "foo", |
|
31 parentId: PlacesUtils.toolbarFolderId, |
|
32 index: PlacesUtils.bookmarks.DEFAULT_INDEX, |
|
33 feedURI: NetUtil.newURI("http://mochi.test:8888/tests/toolkit/components/places/tests/chrome/link-less-items.rss") |
|
34 }, |
|
35 { id: -1, |
|
36 title: "bar", |
|
37 parentId: PlacesUtils.toolbarFolderId, |
|
38 index: PlacesUtils.bookmarks.DEFAULT_INDEX, |
|
39 feedURI: NetUtil.newURI("http://mochi.test:8888/tests/toolkit/components/places/tests/chrome/link-less-items-no-site-uri.rss") |
|
40 }, |
|
41 ]; |
|
42 |
|
43 function runTest() |
|
44 { |
|
45 addLivemarks(function () { |
|
46 reloadLivemarks(false, function () { |
|
47 reloadLivemarks(true, function () { |
|
48 removeLivemarks(SimpleTest.finish); |
|
49 }); |
|
50 }); |
|
51 // Ensure this normal reload doesn't overwrite the forced one. |
|
52 PlacesUtils.livemarks.reloadLivemarks(); |
|
53 }); |
|
54 } |
|
55 |
|
56 function addLivemarks(aCallback) { |
|
57 info("Adding livemarks"); |
|
58 let count = gLivemarks.length; |
|
59 gLivemarks.forEach(function(aLivemarkData) { |
|
60 PlacesUtils.livemarks.addLivemark(aLivemarkData) |
|
61 .then(function (aLivemark) { |
|
62 ok(aLivemark.feedURI.equals(aLivemarkData.feedURI), "Livemark added"); |
|
63 aLivemarkData.id = aLivemark.id; |
|
64 if (--count == 0) { |
|
65 aCallback(); |
|
66 } |
|
67 }, |
|
68 function () { |
|
69 is(true, false, "Should not fail adding a livemark."); |
|
70 aCallback(); |
|
71 }); |
|
72 }); |
|
73 } |
|
74 |
|
75 function reloadLivemarks(aForceUpdate, aCallback) { |
|
76 info("Reloading livemarks with forceUpdate: " + aForceUpdate); |
|
77 let count = gLivemarks.length; |
|
78 gLivemarks.forEach(function(aLivemarkData) { |
|
79 PlacesUtils.livemarks.getLivemark(aLivemarkData) |
|
80 .then(aLivemark => { |
|
81 ok(aLivemark.feedURI.equals(aLivemarkData.feedURI), "Livemark found"); |
|
82 aLivemarkData._observer = new resultObserver(aLivemark, function() { |
|
83 if (++count == gLivemarks.length) { |
|
84 aCallback(); |
|
85 } |
|
86 }); |
|
87 if (--count == 0) { |
|
88 PlacesUtils.livemarks.reloadLivemarks(aForceUpdate); |
|
89 } |
|
90 }, |
|
91 function() { |
|
92 is(true, false, "Should not fail getting a livemark."); |
|
93 aCallback(); |
|
94 } |
|
95 ); |
|
96 }); |
|
97 } |
|
98 |
|
99 function removeLivemarks(aCallback) { |
|
100 info("Removing livemarks"); |
|
101 let count = gLivemarks.length; |
|
102 gLivemarks.forEach(function(aLivemarkData) { |
|
103 PlacesUtils.livemarks.removeLivemark(aLivemarkData).then( |
|
104 function (aLivemark) { |
|
105 if (--count == 0) { |
|
106 aCallback(); |
|
107 } |
|
108 }, |
|
109 function() { |
|
110 is(true, false, "Should not fail adding a livemark."); |
|
111 aCallback(); |
|
112 } |
|
113 ); |
|
114 }); |
|
115 } |
|
116 |
|
117 function resultObserver(aLivemark, aCallback) { |
|
118 this._node = {}; |
|
119 this._livemark = aLivemark; |
|
120 this._callback = aCallback; |
|
121 this._livemark.registerForUpdates(this._node, this); |
|
122 } |
|
123 resultObserver.prototype = { |
|
124 nodeInserted: function() {}, |
|
125 nodeRemoved: function() {}, |
|
126 nodeAnnotationChanged: function() {}, |
|
127 nodeTitleChanged: function() {}, |
|
128 nodeHistoryDetailsChanged: function() {}, |
|
129 nodeMoved: function() {}, |
|
130 ontainerStateChanged: function () {}, |
|
131 sortingChanged: function() {}, |
|
132 batching: function() {}, |
|
133 invalidateContainer: function(aContainer) { |
|
134 // Wait for load finish. |
|
135 if (this._livemark.status == Ci.mozILivemark.STATUS_LOADING) |
|
136 return; |
|
137 |
|
138 this._terminate(); |
|
139 this._callback(); |
|
140 }, |
|
141 _terminate: function () { |
|
142 if (!this._terminated) { |
|
143 this._livemark.unregisterForUpdates(this._node); |
|
144 this._terminated = true; |
|
145 } |
|
146 } |
|
147 }; |
|
148 |
|
149 function cleanup() { |
|
150 gLivemarks.forEach(function(aLivemarkData) { |
|
151 if (aLivemarkData._observer) |
|
152 aLivemarkData._observer._terminate(); |
|
153 }); |
|
154 } |
|
155 ]]> |
|
156 </script> |
|
157 </window> |