1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/dom/xslt/tests/buster/buster-test.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,356 @@ 1.4 +/* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +var parser = new DOMParser(); 1.10 +var methodExpr = (new XPathEvaluator).createExpression("xsl:output/@method", 1.11 + { 1.12 + lookupNamespaceURI: function(aPrefix) 1.13 + { 1.14 + if (aPrefix == "xsl") 1.15 + return "http://www.w3.org/1999/XSL/Transform"; 1.16 + return ""; 1.17 + } 1.18 + }); 1.19 + 1.20 +const nsIWebProgListener = Components.interfaces.nsIWebProgressListener; 1.21 + 1.22 +var runQueue = 1.23 +{ 1.24 + mArray : new Array(), 1.25 + push : function(aRunItem) 1.26 + { 1.27 + this.mArray.push(aRunItem); 1.28 + }, 1.29 + observe : function(aSubject, aTopic, aData) 1.30 + { 1.31 + var item = this.mArray.shift(); 1.32 + if (item) { 1.33 + item.run(this); 1.34 + } 1.35 + }, 1.36 + run : function() 1.37 + { 1.38 + this.observe(null,'',''); 1.39 + } 1.40 +} 1.41 + 1.42 +var itemCache = 1.43 +{ 1.44 + mArray : new Array(), 1.45 + getItem : function(aResource) 1.46 + { 1.47 + // Directory selected 1.48 + if (kContUtils.IsSeq(runItem.prototype.kDatabase, aResource)) { 1.49 + var aSeq = kContUtils.MakeSeq(runItem.prototype.kDatabase, aResource); 1.50 + dump("sequence: "+aSeq+" with "+aSeq.GetCount()+" elements\n"); 1.51 + var child, children = aSeq.GetElements(); 1.52 + var m = 0, first; 1.53 + while (children.hasMoreElements()) { 1.54 + m += 1; 1.55 + child = children.getNext(); 1.56 + child.QueryInterface(nsIRDFResource); 1.57 + if (!first) 1.58 + first = itemCache.getItem(child); 1.59 + else 1.60 + itemCache.getItem(child); 1.61 + } 1.62 + return first; 1.63 + } 1.64 + if (aResource.Value in this.mArray) { 1.65 + return this.mArray[aResource.Value]; 1.66 + } 1.67 + var retItem = new runItem(aResource); 1.68 + this.mArray[aResource.Value] = retItem; 1.69 + runQueue.push(retItem); 1.70 + return retItem; 1.71 + }, 1.72 + rerunItem : function(aResource, aObserver) 1.73 + { 1.74 + var anItem = new runItem(aResource); 1.75 + this.mArray[aResource.Value] = anItem; 1.76 + anItem.run(aObserver); 1.77 + }, 1.78 + observe : function(aSubject, aTopic, aData) 1.79 + { 1.80 + this.mRun += 1; 1.81 + if (aTopic == "success") { 1.82 + if (aData == "yes") { 1.83 + this.mGood += 1; 1.84 + } 1.85 + else { 1.86 + this.mFalse +=1; 1.87 + } 1.88 + } 1.89 + } 1.90 +} 1.91 + 1.92 +function runItem(aResource) 1.93 +{ 1.94 + this.mResource = aResource; 1.95 + // Directory selected 1.96 + if (kContUtils.IsSeq(this.kDatabase,this.mResource)) { 1.97 + var aSeq = kContUtils.MakeSeq(this.kDatabase,this.mResource); 1.98 + dump("THIS SHOULDN'T HAPPEN\n"); 1.99 + var child, children = aSeq.GetElements(); 1.100 + var m = 0; 1.101 + while (children.hasMoreElements()) { 1.102 + m += 1; 1.103 + child = children.getNext(); 1.104 + child.QueryInterface(nsIRDFResource); 1.105 + itemCache.getItem(child); 1.106 + } 1.107 + } 1.108 +} 1.109 + 1.110 +runItem.prototype = 1.111 +{ 1.112 + // RDF resource associated with this test 1.113 + mResource : null, 1.114 + // XML documents for the XSLT transformation 1.115 + mSourceDoc : null, 1.116 + mStyleDoc : null, 1.117 + mResDoc : null, 1.118 + // XML or plaintext document for the reference 1.119 + mRefDoc : null, 1.120 + // bitfield signaling the loaded documents 1.121 + mLoaded : 0, 1.122 + kSource : 1, 1.123 + kStyle : 2, 1.124 + kReference : 4, 1.125 + // a observer, potential argument to run() 1.126 + mObserver : null, 1.127 + mSuccess : null, 1.128 + mMethod : 'xml', 1.129 + // XSLTProcessor, shared by the instances 1.130 + kProcessor : new XSLTProcessor(), 1.131 + kXalan : kStandardURL.createInstance(nsIURL), 1.132 + kDatabase : null, 1.133 + kObservers : new Array(), 1.134 + 1.135 + run : function(aObserver) 1.136 + { 1.137 + if (aObserver && typeof(aObserver)=='function' || 1.138 + (typeof(aObserver)=='object' && 1.139 + typeof(aObserver.observe)=='function')) { 1.140 + this.mObserver=aObserver; 1.141 + } 1.142 + var name = this.kDatabase.GetTarget(this.mResource, krTypeName, true); 1.143 + if (name) { 1.144 + var cat = this.kDatabase.GetTarget(this.mResource, krTypeCat, true); 1.145 + var path = this.kDatabase.GetTarget(this.mResource, krTypePath, true); 1.146 + cat = cat.QueryInterface(nsIRDFLiteral); 1.147 + name = name.QueryInterface(nsIRDFLiteral); 1.148 + path = path.QueryInterface(nsIRDFLiteral); 1.149 + var xalan_fl = this.kXalan.resolve(cat.Value+"/"+path.Value); 1.150 + var xalan_ref = this.kXalan.resolve(cat.Value+"-gold/"+path.Value); 1.151 + this.mRefURL = 1.152 + this.kXalan.resolve(cat.Value + "-gold/" + path.Value + ".out"); 1.153 + dump(name.Value+" links to "+xalan_fl+"\n"); 1.154 + } 1.155 + // Directory selected 1.156 + if (kContUtils.IsSeq(this.kDatabase,this.mResource)) { 1.157 + return; 1.158 + var aSeq = kContUtils.MakeSeq(this.kDatabase,this.mResource); 1.159 + dump("sequence: "+aSeq+" with "+aSeq.GetCount()+" elements\n"); 1.160 + var child, children = aSeq.GetElements(); 1.161 + var m = 0; 1.162 + while (children.hasMoreElements()) { 1.163 + m += 1; 1.164 + child = children.getNext(); 1.165 + child.QueryInterface(nsIRDFResource); 1.166 + } 1.167 + } 1.168 + this.mSourceDoc = document.implementation.createDocument('', '', null); 1.169 + this.mSourceDoc.addEventListener("load",this.onload(1),false); 1.170 + this.mSourceDoc.load(xalan_fl+".xml"); 1.171 + this.mStyleDoc = document.implementation.createDocument('', '', null); 1.172 + this.mStyleDoc.addEventListener("load",this.styleLoaded(),false); 1.173 + this.mStyleDoc.load(xalan_fl+".xsl"); 1.174 + }, 1.175 + 1.176 + // nsIWebProgressListener 1.177 + QueryInterface: function(aIID) 1.178 + { 1.179 + return this; 1.180 + }, 1.181 + onStateChange: function(aProg, aRequest, aFlags, aStatus) 1.182 + { 1.183 + if ((aFlags & nsIWebProgListener.STATE_STOP) && 1.184 + (aFlags & nsIWebProgListener.STATE_IS_DOCUMENT)) { 1.185 + aProg.removeProgressListener(this); 1.186 + this.mRefDoc = document.getElementById('hiddenHtml').contentDocument; 1.187 + this.fileLoaded(4); 1.188 + } 1.189 + }, 1.190 + onProgressChange: function(aProg, b,c,d,e,f) 1.191 + { 1.192 + }, 1.193 + onLocationChange: function(aProg, aRequest, aURI, aFlags) 1.194 + { 1.195 + }, 1.196 + onStatusChange: function(aProg, aRequest, aStatus, aMessage) 1.197 + { 1.198 + }, 1.199 + onSecurityChange: function(aWebProgress, aRequest, aState) 1.200 + { 1.201 + }, 1.202 + 1.203 + // onload handler helper 1.204 + onload : function(file) 1.205 + { 1.206 + var self = this; 1.207 + return function(e) 1.208 + { 1.209 + return self.fileLoaded(file); 1.210 + }; 1.211 + }, 1.212 + 1.213 + styleLoaded : function() 1.214 + { 1.215 + var self = this; 1.216 + return function(e) 1.217 + { 1.218 + return self.styleLoadedHelper(); 1.219 + }; 1.220 + }, 1.221 + styleLoadedHelper : function() 1.222 + { 1.223 + var method = methodExpr.evaluate(this.mStyleDoc.documentElement, 2, 1.224 + null).stringValue; 1.225 + var refContent; 1.226 + if (!method) { 1.227 + // implicit method, guess from result 1.228 + refContent = this.loadTextFile(this.mRefURL); 1.229 + if (refContent.match(/^\s*<html/gi)) { 1.230 + method = 'html'; 1.231 + } 1.232 + else { 1.233 + method = 'xml'; 1.234 + } 1.235 + } 1.236 + this.mMethod = method; 1.237 + 1.238 + switch (method) { 1.239 + case 'xml': 1.240 + if (!refContent) { 1.241 + refContent = this.loadTextFile(this.mRefURL); 1.242 + } 1.243 + this.mRefDoc = parser.parseFromString(refContent, 'application/xml'); 1.244 + this.mLoaded += 4; 1.245 + break; 1.246 + case 'html': 1.247 + view.loadHtml(this.mRefURL, this); 1.248 + break; 1.249 + case 'text': 1.250 + if (!refContent) { 1.251 + refContent = this.loadTextFile(this.mRefURL); 1.252 + } 1.253 + const ns = 'http://www.mozilla.org/TransforMiix'; 1.254 + const qn = 'transformiix:result'; 1.255 + this.mRefDoc = 1.256 + document.implementation.createDocument(ns, qn, null); 1.257 + var txt = this.mRefDoc.createTextNode(refContent); 1.258 + this.mRefDoc.documentElement.appendChild(txt); 1.259 + this.mLoaded += 4; 1.260 + break; 1.261 + default: 1.262 + throw "unkown XSLT output method"; 1.263 + } 1.264 + this.fileLoaded(2) 1.265 + }, 1.266 + 1.267 + fileLoaded : function(mask) 1.268 + { 1.269 + this.mLoaded += mask; 1.270 + if (this.mLoaded < 7) { 1.271 + return; 1.272 + } 1.273 + this.doTransform(); 1.274 + }, 1.275 + 1.276 + doTransform : function() 1.277 + { 1.278 + this.kProcessor.reset(); 1.279 + try { 1.280 + this.kProcessor.importStylesheet(this.mStyleDoc); 1.281 + this.mResDoc = 1.282 + this.kProcessor.transformToDocument(this.mSourceDoc); 1.283 + this.mRefDoc.normalize(); 1.284 + isGood = DiffDOM(this.mResDoc.documentElement, 1.285 + this.mRefDoc.documentElement, 1.286 + this.mMethod == 'html'); 1.287 + } catch (e) { 1.288 + isGood = false; 1.289 + }; 1.290 + dump("This succeeded. "+isGood+"\n"); 1.291 + isGood = isGood.toString(); 1.292 + for (var i=0; i<this.kObservers.length; i++) { 1.293 + var aObs = this.kObservers[i]; 1.294 + if (typeof(aObs)=='object' && typeof(aObs.observe)=='function') { 1.295 + aObs.observe(this.mResource, 'success', isGood); 1.296 + } 1.297 + else if (typeof(aObs)=='function') { 1.298 + aObs(this.mResource, 'success', isGood); 1.299 + } 1.300 + } 1.301 + if (this.mObserver) { 1.302 + if (typeof(this.mObserver)=='object') { 1.303 + this.mObserver.observe(this.mResource, 'success', isGood); 1.304 + } 1.305 + else { 1.306 + this.mObserver(this.mResource, 'success', isGood); 1.307 + } 1.308 + } 1.309 + }, 1.310 + 1.311 + loadTextFile : function(url) 1.312 + { 1.313 + var serv = Components.classes[IOSERVICE_CTRID]. 1.314 + getService(nsIIOService); 1.315 + if (!serv) { 1.316 + throw Components.results.ERR_FAILURE; 1.317 + } 1.318 + var chan = serv.newChannel(url, null, null); 1.319 + var instream = doCreate(SIS_CTRID, nsISIS); 1.320 + instream.init(chan.open()); 1.321 + 1.322 + return instream.read(instream.available()); 1.323 + } 1.324 +} 1.325 + 1.326 +runItem.prototype.kXalan.QueryInterface(nsIStandardURL); 1.327 + 1.328 +var cmdTestController = 1.329 +{ 1.330 + supportsCommand: function(aCommand) 1.331 + { 1.332 + switch(aCommand) { 1.333 + case 'cmd_tst_run': 1.334 + case 'cmd_tst_runall': 1.335 + return true; 1.336 + default: 1.337 + } 1.338 + return false; 1.339 + }, 1.340 + isCommandEnabled: function(aCommand) 1.341 + { 1.342 + return this.supportsCommand(aCommand); 1.343 + }, 1.344 + doCommand: function(aCommand) 1.345 + { 1.346 + switch(aCommand) { 1.347 + case 'cmd_tst_run': 1.348 + dump("cmd_tst_run\n"); 1.349 + break; 1.350 + case 'cmd_tst_runall': 1.351 + dump("cmd_tst_runall\n"); 1.352 + var tst_run = document.getElementById('cmd_tst_run'); 1.353 + tst_run.doCommand(); 1.354 + default: 1.355 + } 1.356 + } 1.357 +}; 1.358 + 1.359 +registerController(cmdTestController);