test_stringBindings.html (4695B)
1 <!DOCTYPE HTML> 2 <html> 3 <!-- 4 https://bugzilla.mozilla.org/show_bug.cgi?id=1334537 5 --> 6 <head> 7 <meta charset="utf-8"> 8 <title>Test for Bug 1334537</title> 9 <script src="/tests/SimpleTest/SimpleTest.js"></script> 10 <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/> 11 <script type="application/javascript"> 12 13 /** Test for Bug 1334537 */ 14 SimpleTest.waitForExplicitFinish(); 15 16 function go() { 17 // Need a new global that will pick up our pref. 18 var ifr = document.createElement("iframe"); 19 document.body.appendChild(ifr); 20 21 var t = new ifr.contentWindow.TestFunctions(); 22 var testString = "abcdefghijklmnopqrstuvwxyz"; 23 const substringLength = 10; 24 var shortTestString = testString.substring(0, substringLength); 25 26 t.setStringData(testString); 27 // Note: we want to do all our gets before we start running code we don't 28 // control inside the test harness, if we really want to exercise the string 29 // cache in controlled ways. 30 31 var asShortDOMString = t.getStringDataAsDOMString(substringLength); 32 var asFullDOMString = t.getStringDataAsDOMString(); 33 var asShortAString = t.getStringDataAsAString(substringLength); 34 var asAString = t.getStringDataAsAString(); 35 36 is(asShortDOMString, shortTestString, "Short DOMString should be short"); 37 is(asFullDOMString, testString, "Full DOMString should be test string"); 38 is(asShortAString, shortTestString, "Short AString should be short"); 39 is(asAString, testString, "Full AString should be test string"); 40 41 // These strings should match the strings used in TestFunctions.cpp, but we 42 // want to launder them through something on the JS side that will convert 43 // them into strings stored in the JS heap. 44 function launder(str) { 45 // This should be sufficient, but if the JIT gets too smart we may need 46 // to get more clever. 47 return str.split("").join(""); 48 } 49 var shortString = launder(t.getShortLiteralString()); 50 var mediumString = launder(t.getMediumLiteralString()); 51 var longString = launder(t.getLongLiteralString()); 52 53 // A short or medium non-external string will become an inline FakeString. 54 is(t.getStringType(shortString), "inline", 55 "Short string should become inline"); 56 is(t.getStringType(mediumString), "inline", 57 "Medium string should become inline"); 58 // A long string will become a stringbuffer FakeString on the way in. 59 is(t.getStringType(longString), "stringbuffer", 60 "Long string should become a stringbuffer"); 61 62 // A short literal string will become an inline JS string on the way out 63 // and then become an inline FakeString on the way in. 64 is(t.getStringType(t.getShortLiteralString()), "inline", 65 "Short literal string should become inline"); 66 // A medium or long literal string will become an external JS string on the 67 // way out and then become a literal FakeString on the way in. 68 is(t.getStringType(t.getMediumLiteralString()), "literal", 69 "Medium literal string should become literal"); 70 is(t.getStringType(t.getLongLiteralString()), "literal", 71 "Long literal string should become literal"); 72 73 // A short stringbuffer string will become an inline JS string on the way 74 // out and then become an inline FakeString on the way in. 75 is(t.getStringType(t.getStringbufferString(shortString)), "inline", 76 "Short stringbuffer string should become inline"); 77 // A medium or long stringbuffer string will become an external JS string 78 // on the way out and then become a stringbuffer FakeString on the way in. 79 is(t.getStringType(t.getStringbufferString(mediumString)), "stringbuffer", 80 "Medium stringbuffer string should become stringbuffer"); 81 is(t.getStringType(t.getStringbufferString(longString)), "stringbuffer", 82 "Long stringbuffer string should become stringbuffer"); 83 84 // Now test that roundtripping works fine. We need to make sure the string 85 // we are storing is not equal to any of the ones we have used above, to 86 // avoid the external string cache interfering. 87 t.setStringData(longString + "unique"); // Should store with stringbuffer. 88 ok(t.stringbufferMatchesStored(t.getStringDataAsAString()), 89 "Stringbuffer should have roundtripped"); 90 91 SimpleTest.finish(); 92 } 93 94 addLoadEvent(function() { 95 SpecialPowers.pushPrefEnv({set: [["dom.expose_test_interfaces", true]]}, 96 go); 97 }); 98 </script> 99 </head> 100 <body> 101 <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1334537">Mozilla Bug 1334537</a> 102 <p id="display"></p> 103 <div id="content" style="display: none"> 104 105 </div> 106 <pre id="test"> 107 </pre> 108 </body> 109 </html>