If JavaScript exists in your PDF documents it is probably important. Often JavaScript plays an active role within document workflows.
PDFUnit's ability to test JavaScript does not replace specialized JavaScript testing tools such as “Google JS Test” , but it is not easy to test the JavaScript in a PDF document using these tools.
You can test JavaScript using the following tag:
<!-- Tag to verify JavaScript: --> <hasJavaScript> <containing /> (optional) <equals /> (optional) <matchingComplete /> (optional) </hasJavaScript>
The following example checks whether a document contains JavaScript at all:
<testcase name="hasJavaScript"> <assertThat testDocument="javascript/javaScriptClock.pdf"> <hasJavaScript /> </assertThat> </testcase>
The expected JavaScript can be read from a file and compared with the JavaScript
in a PDF document. The utility ExtractJavaScript
extracts
JavaScript into a text file, which can then be used for tests:
<testcase name="hasJavaScript_ScriptFromFile"> <assertThat testDocument="javascript/javaScriptClock.pdf"> <hasJavaScript> <equals toFile="javascript/javascriptClock.js" /> </hasJavaScript> </assertThat> </testcase>
The expected JavaScript need not necessarily be read from a file. You can type it directly inside the tag:
<testcase name="hasJavaScript_ComparedToString"> <assertThat testDocument="javascript/javaScriptClock.pdf"> <hasJavaScript> <matchingComplete> <![CDATA[ // Constants used by the time calculations var oneSec = 1000; var oneMin = 60 * oneSec; var oneHour = 60 * oneMin; var strokeNormal = this.getField(\"SWStart\").strokeColor; var strokeLight = [\"RGB\",.35,.35,1]; var fillNormal = this.getField(\"SWStart\").fillColor; var fillLight = [\"RGB\",.35,.35,0.7]; function SetFldEnable(oFld, bEnable) { if(oFld) { oFld.strokeColor = bEnable?strokeNormal:strokeLight; oFld.fillColor = bEnable?fillNormal:fillLight; oFld.readonly = !bEnable; oFld.textColor = bEnable?color.white:[\"G\",.7]; } } ... (code shortened for presentation) ]]> </matchingComplete> </hasJavaScript> </assertThat> </testcase>
In the previous tests complete JavaScript code was used. But also small parts of a JavaScript code can be used for tests:
<testcase name="hasJavaScript_ContainingText"> <assertThat testDocument="javascript/javaScriptClock.pdf"> <hasJavaScript> <containing> function DoTimers() { var nCurTime = (new Date()).getTime(); ClockProc(nCurTime); StopWatchProc(nCurTime); CountDownProc(nCurTime); this.dirty = false; } </containing> </hasJavaScript> </assertThat> </testcase>
<testcase name="hasJavaScript_ContainingFunction_MultipleFunctionnames"> <assertThat testDocument="javascript/javaScriptClock.pdf"> <hasJavaScript> <containing>StopWatchProc</containing> <containing>SetFldEnable</containing> <containing>DoTimers</containing> <containing>ClockProc</containing> <containing>CountDownProc</containing> <containing>CDEnables</containing> <containing>SWSetEnables</containing> </hasJavaScript> </assertThat> </testcase>
Whitespaces are ignored when comparing JavaScript.
Since extracted JavaScript is plain text, no XML or XPath based tests are provided.