test_bug293834.html 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <!DOCTYPE HTML>
  2. <html>
  3. <!--
  4. https://bugzilla.mozilla.org/show_bug.cgi?id=293834
  5. -->
  6. <head>
  7. <title>Test for Bug 293834</title>
  8. <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
  9. <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
  10. </head>
  11. <body>
  12. <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=293834">Mozilla Bug 293834</a>
  13. <p id="display">
  14. </p>
  15. <pre id="results"></pre>
  16. <div id="content" style="display: none">
  17. <iframe src="bug293834_form.html" id="source"></iframe>
  18. <br>
  19. <iframe id="dest"></iframe>
  20. </div>
  21. <pre id="test">
  22. <script class="testbody" type="text/javascript">
  23. /** Test for Bug 293834 **/
  24. var textareas = ["a-textbox", "a-prefilled-textbox"];
  25. var textboxes = ["a-text", "a-prefilled-text"];
  26. function fillform(doc) {
  27. for (var i in textareas) {
  28. doc.getElementById(textareas[i]).textContent += "form state";
  29. }
  30. for (var i in textboxes) {
  31. doc.getElementById(textboxes[i]).value += "form state";
  32. }
  33. doc.getElementById('a-checkbox').checked = true;
  34. doc.getElementById("radioa").checked = true;
  35. doc.getElementById("aselect").selectedIndex = 0;
  36. }
  37. function checkform(doc) {
  38. for (var i in textareas) {
  39. var textContent = doc.getElementById(textareas[i]).textContent;
  40. ok(/form\s+state/m.test(textContent),
  41. "Modified textarea "+textareas[i]+" form state not preserved!");
  42. }
  43. for (var i in textboxes) {
  44. var value = doc.getElementById(textboxes[i]).value;
  45. ok(/form\s+state/m.test(value),
  46. "Modified textbox "+textboxes[i]+" form state not preserved!");
  47. }
  48. ok(doc.getElementById('a-checkbox').checked,
  49. "Modified checkbox checked state not preserved!");
  50. ok(doc.getElementById("radioa").checked,
  51. "Modified radio checked state not preserved!");
  52. ok(doc.getElementById("aselect").selectedIndex == 0,
  53. "Modified select selected index not preserved");
  54. }
  55. const Cc = SpecialPowers.Cc;
  56. const Ci = SpecialPowers.Ci;
  57. function getTempDir() {
  58. return Cc["@mozilla.org/file/directory_service;1"]
  59. .getService(Ci.nsIProperties)
  60. .get("TmpD", Ci.nsILocalFile);
  61. }
  62. function getFileContents(aFile) {
  63. const PR_RDONLY = 0x01;
  64. var fileStream = Cc["@mozilla.org/network/file-input-stream;1"]
  65. .createInstance(Ci.nsIFileInputStream);
  66. fileStream.init(aFile, PR_RDONLY, 0400,
  67. Ci.nsIFileInputStream.DELETE_ON_CLOSE
  68. | Ci.nsIFileInputStream.CLOSE_ON_EOF);
  69. var inputStream = Cc["@mozilla.org/scriptableinputstream;1"]
  70. .createInstance(Ci.nsIScriptableInputStream);
  71. inputStream.init(fileStream);
  72. var data = "";
  73. do {
  74. var str = inputStream.read(inputStream.available());
  75. data += str;
  76. } while(str.length > 0);
  77. return data;
  78. }
  79. function persistDocument(aDoc) {
  80. const nsIWBP = Ci.nsIWebBrowserPersist;
  81. const persistFlags =
  82. nsIWBP.PERSIST_FLAGS_REPLACE_EXISTING_FILES
  83. | nsIWBP.PERSIST_FLAGS_FROM_CACHE
  84. | nsIWBP.PERSIST_FLAGS_AUTODETECT_APPLY_CONVERSION
  85. const encodingFlags =
  86. nsIWBP.ENCODE_FLAGS_ENCODE_BASIC_ENTITIES;
  87. var ioService = Cc["@mozilla.org/network/io-service;1"]
  88. .getService(Ci.nsIIOService);
  89. var file = getTempDir();
  90. file.append("bug293834-serialized.html");
  91. var persist = Cc["@mozilla.org/embedding/browser/nsWebBrowserPersist;1"]
  92. .createInstance(Ci.nsIWebBrowserPersist);
  93. persist.progressListener = null;
  94. persist.persistFlags = persistFlags;
  95. const kWrapColumn = 80;
  96. var folder = getTempDir();
  97. folder.append("bug293834-serialized");
  98. persist.saveDocument(aDoc, ioService.newFileURI(file),
  99. folder,
  100. aDoc.contentType,
  101. encodingFlags, kWrapColumn);
  102. return getFileContents(file);
  103. }
  104. SimpleTest.waitForExplicitFinish();
  105. addLoadEvent(function() {
  106. var srcDoc = document.getElementById('source').contentDocument;
  107. fillform(srcDoc);
  108. checkform(srcDoc);
  109. var serializedString = persistDocument(srcDoc);
  110. // We can't access file:/// URLs directly for security reasons,
  111. // so we have to parse the serialized content string indirectly
  112. var targetDoc = document.getElementById('dest').contentDocument;
  113. targetDoc.write(serializedString);
  114. checkform(targetDoc);
  115. SimpleTest.finish();
  116. });
  117. </script>
  118. </pre>
  119. </body>
  120. </html>