1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/content/xul/templates/tests/chrome/test_tmpl_errors.xul Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,280 @@ 1.4 +<?xml version="1.0"?> 1.5 +<?xml-stylesheet href="chrome://global/skin" type="text/css"?> 1.6 +<?xml-stylesheet href="chrome://mochikit/content/tests/SimpleTest/test.css" type="text/css"?> 1.7 + 1.8 +<!-- 1.9 + tests for templates with invalid syntax 1.10 +--> 1.11 + 1.12 +<window title="XUL Invalid Template Tests" width="500" height="600" 1.13 + onload="runTest();" 1.14 + xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"> 1.15 + <script type="application/javascript" 1.16 + src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script> 1.17 + 1.18 + <body xmlns="http://www.w3.org/1999/xhtml" style="height: 300px; overflow: auto;"/> 1.19 + 1.20 +<script> 1.21 +<![CDATA[ 1.22 +SimpleTest.waitForExplicitFinish(); 1.23 + 1.24 +var consoleService = Components.classes["@mozilla.org/consoleservice;1"]. 1.25 + getService(Components.interfaces.nsIConsoleService); 1.26 + 1.27 +function checkConsole(expectedError) 1.28 +{ 1.29 + var message = consoleService.getMessageArray()[0].message; 1.30 + is(message, expectedError, "logged message " + expectedError); 1.31 +} 1.32 + 1.33 +// each test consists of a pre function executed before the template build, an 1.34 +// expected error message, and a post function executed after the template build 1.35 +var tests = [ 1.36 + 1.37 +// <queryset> used in invalid location 1.38 +{ 1.39 + pre: function(template) template.insertBefore(document.createElement("queryset"), template.lastChild), 1.40 + error: "Error parsing template: unexpected <queryset> element", 1.41 + post: function(queryset) queryset.parentNode.removeChild(queryset) 1.42 +}, 1.43 + 1.44 +// no member variable found 1.45 +{ 1.46 + pre: function(template) $("action").firstChild.removeAttribute("uri"), 1.47 + error: "Error parsing template: no member variable found. Action body should have an element with uri attribute", 1.48 + post: function() $("action").firstChild.setAttribute("uri", "?child") 1.49 +}, 1.50 + 1.51 +// bad binding subject 1.52 +{ 1.53 + pre: function(template) $("binding").removeAttribute("subject"), 1.54 + error: "Error parsing template: <binding> requires a variable for its subject attribute", 1.55 + post: function() $("binding").setAttribute("subject", "?child"), 1.56 +}, 1.57 + 1.58 +// bad binding predicate 1.59 +{ 1.60 + pre: function(template) $("binding").removeAttribute("predicate"), 1.61 + error: "Error parsing template: <binding> element is missing a predicate attribute", 1.62 + post: function() $("binding").setAttribute("predicate", "http://www.some-fictitious-zoo.com/rdf#name"), 1.63 +}, 1.64 + 1.65 +// bad binding object 1.66 +{ 1.67 + pre: function(template) $("binding").setAttribute("object", "blah"), 1.68 + error: "Error parsing template: <binding> requires a variable for its object attribute", 1.69 + post: function() $("binding").setAttribute("object", "?name"), 1.70 +}, 1.71 + 1.72 +// where condition missing a subject 1.73 +{ 1.74 + pre: function(template) { var rule = $("rule"); 1.75 + var where = document.createElement("where"); 1.76 + where.setAttribute("subject", ""); 1.77 + where.setAttribute("rel", "equals"); 1.78 + where.setAttribute("value", "Raven"); 1.79 + rule.appendChild(where); 1.80 + return where; }, 1.81 + error: "Error parsing template: <where> element is missing a subject attribute", 1.82 + post: function(where) { where.parentNode.removeChild(where); } 1.83 +}, 1.84 + 1.85 +// where condition missing a rel 1.86 +{ 1.87 + pre: function(template) { var rule = $("rule"); 1.88 + var where = document.createElement("where"); 1.89 + where.setAttribute("subject", "?name"); 1.90 + where.setAttribute("rel", ""); 1.91 + where.setAttribute("value", "Raven"); 1.92 + rule.appendChild(where); 1.93 + return where; }, 1.94 + error: "Error parsing template: <where> element is missing a rel attribute", 1.95 + post: function(where) { where.parentNode.removeChild(where); } 1.96 +}, 1.97 + 1.98 +// where condition missing a value 1.99 +{ 1.100 + pre: function(template) { var rule = $("rule"); 1.101 + var where = document.createElement("where"); 1.102 + where.setAttribute("subject", "?name"); 1.103 + where.setAttribute("rel", "equals"); 1.104 + where.setAttribute("value", ""); 1.105 + rule.appendChild(where); 1.106 + return where; }, 1.107 + error: "Error parsing template: <where> element is missing a value attribute", 1.108 + post: function(where) { where.parentNode.removeChild(where); } 1.109 +}, 1.110 + 1.111 +// where condition missing a variable 1.112 +{ 1.113 + pre: function(template) { var rule = $("rule"); 1.114 + var where = document.createElement("where"); 1.115 + where.setAttribute("subject", "name"); 1.116 + where.setAttribute("rel", "equals"); 1.117 + where.setAttribute("value", "Raven"); 1.118 + rule.appendChild(where); 1.119 + return where; }, 1.120 + error: "Error parsing template: <where> element must have at least one variable as a subject or value", 1.121 + post: function(where) { where.parentNode.removeChild(where); } 1.122 +}, 1.123 + 1.124 +// bad member container 1.125 +{ 1.126 + pre: function(template) $("member").setAttribute("container", "blah"), 1.127 + error: "Error parsing template: <member> requires a variable for its container attribute", 1.128 + post: function() $("member").setAttribute("container", "?uri"), 1.129 +}, 1.130 + 1.131 +// bad member child 1.132 +{ 1.133 + pre: function(template) $("member").setAttribute("child", "blah"), 1.134 + error: "Error parsing template: <member> requires a variable for its child attribute", 1.135 + post: function() $("member").setAttribute("child", "?child"), 1.136 +}, 1.137 + 1.138 +// bad triple subject 1.139 +{ 1.140 + pre: function(template) $("triple").removeAttribute("subject"), 1.141 + error: "Error parsing template: <triple> requires a variable for its subject attribute", 1.142 + post: function() $("triple").setAttribute("subject", "?child"), 1.143 +}, 1.144 + 1.145 +// missing triple predicate 1.146 +{ 1.147 + pre: function(template) $("triple").removeAttribute("predicate"), 1.148 + error: "Error parsing template: <triple> should have a non-variable value as a predicate", 1.149 + post: function() $("triple").setAttribute("predicate", "http://www.some-fictitious-zoo.com/rdf#name"), 1.150 +}, 1.151 + 1.152 +// bad triple predicate 1.153 +{ 1.154 + pre: function(template) $("triple").setAttribute("predicate", "?predicate"), 1.155 + error: "Error parsing template: <triple> should have a non-variable value as a predicate", 1.156 + post: function() $("triple").setAttribute("predicate", "http://www.some-fictitious-zoo.com/rdf#name"), 1.157 +}, 1.158 + 1.159 +// bad triple object 1.160 +{ 1.161 + pre: function(template) $("triple").removeAttribute("object"), 1.162 + error: "Error parsing template: <triple> requires a variable for its object attribute", 1.163 + post: function() $("triple").setAttribute("object", "?name"), 1.164 +}, 1.165 + 1.166 +// content not first element in query 1.167 +{ 1.168 + pre: function(template) { var content = $("content"); content.parentNode.appendChild(content); return content; }, 1.169 + error: "Error parsing template: expected <content> to be first", 1.170 + post: function(content) content.parentNode.insertBefore(content, content.parentNode.firstChild), 1.171 +}, 1.172 + 1.173 +// member container variable not bound 1.174 +{ 1.175 + pre: function(template) $("member").removeAttribute("container"), 1.176 + error: "Error parsing template: neither container or child variables of <member> has a value", 1.177 + post: function() $("member").setAttribute("container", "?uri"), 1.178 +}, 1.179 + 1.180 +// neither triple subject or object variable are bound 1.181 +{ 1.182 + pre: function(template) $("triple").setAttribute("subject", "?blah"), 1.183 + error: "Error parsing template: neither subject or object variables of <triple> has a value", 1.184 + post: function() $("triple").setAttribute("subject", "?child"), 1.185 +}, 1.186 + 1.187 +// neither triple subject or object variable are bound 1.188 +{ 1.189 + pre: function(template) { var triple = $("triple"); triple.setAttribute("subject", "blah"); 1.190 + triple.setAttribute("object", "blah"); }, 1.191 + error: "Error parsing template: <triple> should have at least one variable as a subject or object", 1.192 + post: function() { var triple = $("triple"); triple.setAttribute("subject", "?uri"); 1.193 + triple.setAttribute("object", "?uri") } 1.194 +}, 1.195 + 1.196 +// could not parse xml query expression 1.197 +{ 1.198 + firstXMLTest: true, 1.199 + pre: function(template) { $("query").setAttribute("expr", "something()"); }, 1.200 + error: "Error parsing template: XPath expression in query could not be parsed", 1.201 + post: function() { } 1.202 +}, 1.203 + 1.204 +// could not parse xml assign expression 1.205 +{ 1.206 + pre: function(template) { var query = $("query"); 1.207 + query.setAttribute("expr", "*"); 1.208 + var assign = document.createElement("assign"); 1.209 + assign.setAttribute("var", "?name"); 1.210 + assign.setAttribute("expr", "something()"); 1.211 + query.appendChild(assign); 1.212 + return assign; }, 1.213 + error: "Error parsing template: XPath expression in <assign> could not be parsed", 1.214 + post: function(assign) { assign.parentNode.removeChild(assign); } 1.215 +}, 1.216 + 1.217 +// could not parse xml binding expression 1.218 +{ 1.219 + pre: function(template) { $("binding").setAttribute("predicate", "something()"); }, 1.220 + error: "Error parsing template: XPath expression in <binding> could not be parsed", 1.221 + post: function() { $("binding").setAttribute("predicate", "[name]"); }, 1.222 +}, 1.223 + 1.224 +]; 1.225 + 1.226 +function runTest() 1.227 +{ 1.228 + var root = $("root"); 1.229 + var template = $("template"); 1.230 + while (test = tests.shift()) { 1.231 + consoleService.reset(); 1.232 + var context = test.pre(template); 1.233 + root.builder.rebuild(); 1.234 + checkConsole(test.error); 1.235 + test.post(context); 1.236 + 1.237 + // preload and set up for the xml datasource query error tests 1.238 + if (tests.length && tests[0].firstXMLTest) { 1.239 + var src = window.location.href.replace(/test_tmpl.*xul/, "animals.xml"); 1.240 + xmlDoc = new XMLHttpRequest(); 1.241 + xmlDoc.open("get", src, false); 1.242 + xmlDoc.send(null); 1.243 + 1.244 + var root = $("root"); 1.245 + root.setAttribute("querytype", "xml"); 1.246 + root.setAttribute("datasources", "animals.xml"); 1.247 + $("binding").setAttribute("predicate", "[name]"); 1.248 + 1.249 + function waitForDatasource() { 1.250 + // wait for the datasource to be available before continuing the test 1.251 + if (root.builder.datasource instanceof XMLDocument) 1.252 + runTest(); 1.253 + else 1.254 + setTimeout(waitForDatasource, 100); 1.255 + } 1.256 + 1.257 + setTimeout(waitForDatasource, 0); 1.258 + return; 1.259 + } 1.260 + } 1.261 + SimpleTest.finish(); 1.262 +} 1.263 + 1.264 +]]> 1.265 +</script> 1.266 + 1.267 +<vbox id="root" datasources="animals.rdf" ref="http://www.some-fictitious-zoo.com/birds"> 1.268 +<template id="template"> 1.269 + <query id="query"> 1.270 + <content id="content" uri="?uri"/> 1.271 + <member id="member" container="?uri" child="?child"/> 1.272 + <triple id="triple" subject="?child" predicate="http://www.some-fictitious-zoo.com/rdf#name" object="?name"/> 1.273 + </query> 1.274 + <rule id="rule"> 1.275 + <binding id="binding" subject="?child" predicate="http://www.some-fictitious-zoo.com/rdf#name" object="?name"/> 1.276 + <action id="action"> 1.277 + <label uri="?child" value="?name"/> 1.278 + </action> 1.279 + </rule> 1.280 +</template> 1.281 +</vbox> 1.282 + 1.283 +</window>