1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/toolkit/components/places/tests/chrome/test_reloadLivemarks.xul Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,157 @@ 1.4 +<?xml version="1.0"?> 1.5 +<?xml-stylesheet href="chrome://global/skin" type="text/css"?> 1.6 +<?xml-stylesheet 1.7 + href="chrome://mochikit/content/tests/SimpleTest/test.css" type="text/css"?> 1.8 +<window title="Reload Livemarks" 1.9 + xmlns:html="http://www.w3.org/1999/xhtml" 1.10 + xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul" 1.11 + onload="runTest()" onunload="cleanup()"> 1.12 + <script type="application/javascript" 1.13 + src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script> 1.14 + 1.15 + <body xmlns="http://www.w3.org/1999/xhtml" /> 1.16 + 1.17 +<script type="application/javascript"> 1.18 +<![CDATA[ 1.19 +// Test that for concurrent reload of livemarks. 1.20 + 1.21 +SimpleTest.waitForExplicitFinish(); 1.22 + 1.23 +const Cc = Components.classes; 1.24 +const Ci = Components.interfaces; 1.25 +const Cr = Components.results; 1.26 + 1.27 +Components.utils.import("resource://gre/modules/XPCOMUtils.jsm"); 1.28 +Components.utils.import("resource://gre/modules/NetUtil.jsm"); 1.29 +Components.utils.import("resource://gre/modules/PlacesUtils.jsm"); 1.30 + 1.31 +let gLivemarks = [ 1.32 + { id: -1, 1.33 + title: "foo", 1.34 + parentId: PlacesUtils.toolbarFolderId, 1.35 + index: PlacesUtils.bookmarks.DEFAULT_INDEX, 1.36 + feedURI: NetUtil.newURI("http://mochi.test:8888/tests/toolkit/components/places/tests/chrome/link-less-items.rss") 1.37 + }, 1.38 + { id: -1, 1.39 + title: "bar", 1.40 + parentId: PlacesUtils.toolbarFolderId, 1.41 + index: PlacesUtils.bookmarks.DEFAULT_INDEX, 1.42 + feedURI: NetUtil.newURI("http://mochi.test:8888/tests/toolkit/components/places/tests/chrome/link-less-items-no-site-uri.rss") 1.43 + }, 1.44 +]; 1.45 + 1.46 +function runTest() 1.47 +{ 1.48 + addLivemarks(function () { 1.49 + reloadLivemarks(false, function () { 1.50 + reloadLivemarks(true, function () { 1.51 + removeLivemarks(SimpleTest.finish); 1.52 + }); 1.53 + }); 1.54 + // Ensure this normal reload doesn't overwrite the forced one. 1.55 + PlacesUtils.livemarks.reloadLivemarks(); 1.56 + }); 1.57 +} 1.58 + 1.59 +function addLivemarks(aCallback) { 1.60 + info("Adding livemarks"); 1.61 + let count = gLivemarks.length; 1.62 + gLivemarks.forEach(function(aLivemarkData) { 1.63 + PlacesUtils.livemarks.addLivemark(aLivemarkData) 1.64 + .then(function (aLivemark) { 1.65 + ok(aLivemark.feedURI.equals(aLivemarkData.feedURI), "Livemark added"); 1.66 + aLivemarkData.id = aLivemark.id; 1.67 + if (--count == 0) { 1.68 + aCallback(); 1.69 + } 1.70 + }, 1.71 + function () { 1.72 + is(true, false, "Should not fail adding a livemark."); 1.73 + aCallback(); 1.74 + }); 1.75 + }); 1.76 +} 1.77 + 1.78 +function reloadLivemarks(aForceUpdate, aCallback) { 1.79 + info("Reloading livemarks with forceUpdate: " + aForceUpdate); 1.80 + let count = gLivemarks.length; 1.81 + gLivemarks.forEach(function(aLivemarkData) { 1.82 + PlacesUtils.livemarks.getLivemark(aLivemarkData) 1.83 + .then(aLivemark => { 1.84 + ok(aLivemark.feedURI.equals(aLivemarkData.feedURI), "Livemark found"); 1.85 + aLivemarkData._observer = new resultObserver(aLivemark, function() { 1.86 + if (++count == gLivemarks.length) { 1.87 + aCallback(); 1.88 + } 1.89 + }); 1.90 + if (--count == 0) { 1.91 + PlacesUtils.livemarks.reloadLivemarks(aForceUpdate); 1.92 + } 1.93 + }, 1.94 + function() { 1.95 + is(true, false, "Should not fail getting a livemark."); 1.96 + aCallback(); 1.97 + } 1.98 + ); 1.99 + }); 1.100 +} 1.101 + 1.102 +function removeLivemarks(aCallback) { 1.103 + info("Removing livemarks"); 1.104 + let count = gLivemarks.length; 1.105 + gLivemarks.forEach(function(aLivemarkData) { 1.106 + PlacesUtils.livemarks.removeLivemark(aLivemarkData).then( 1.107 + function (aLivemark) { 1.108 + if (--count == 0) { 1.109 + aCallback(); 1.110 + } 1.111 + }, 1.112 + function() { 1.113 + is(true, false, "Should not fail adding a livemark."); 1.114 + aCallback(); 1.115 + } 1.116 + ); 1.117 + }); 1.118 +} 1.119 + 1.120 +function resultObserver(aLivemark, aCallback) { 1.121 + this._node = {}; 1.122 + this._livemark = aLivemark; 1.123 + this._callback = aCallback; 1.124 + this._livemark.registerForUpdates(this._node, this); 1.125 +} 1.126 +resultObserver.prototype = { 1.127 + nodeInserted: function() {}, 1.128 + nodeRemoved: function() {}, 1.129 + nodeAnnotationChanged: function() {}, 1.130 + nodeTitleChanged: function() {}, 1.131 + nodeHistoryDetailsChanged: function() {}, 1.132 + nodeMoved: function() {}, 1.133 + ontainerStateChanged: function () {}, 1.134 + sortingChanged: function() {}, 1.135 + batching: function() {}, 1.136 + invalidateContainer: function(aContainer) { 1.137 + // Wait for load finish. 1.138 + if (this._livemark.status == Ci.mozILivemark.STATUS_LOADING) 1.139 + return; 1.140 + 1.141 + this._terminate(); 1.142 + this._callback(); 1.143 + }, 1.144 + _terminate: function () { 1.145 + if (!this._terminated) { 1.146 + this._livemark.unregisterForUpdates(this._node); 1.147 + this._terminated = true; 1.148 + } 1.149 + } 1.150 +}; 1.151 + 1.152 +function cleanup() { 1.153 + gLivemarks.forEach(function(aLivemarkData) { 1.154 + if (aLivemarkData._observer) 1.155 + aLivemarkData._observer._terminate(); 1.156 + }); 1.157 +} 1.158 +]]> 1.159 +</script> 1.160 +</window>