summaryrefslogtreecommitdiffstats
path: root/editor/libeditor/tests/test_set_document_title_transaction.html
blob: d745d4f135c0e205f1c7936eb36a16a8256c7c94 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
<!DOCTYPE html>
<html>
<head>
  <title>Test for SetDocumentTitleTransaction</title>
  <script type="text/javascript"
          src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" type="text/css"
          href="chrome://mochikit/content/tests/SimpleTest/test.css" />
</head>
<body onload="runTests()">
<div id="display">
  <iframe src="data:text/html,<!DOCTYPE html><html><head><title>first title</title></head><body></body></html>"></iframe>
</div>
<div id="content" style="display: none">
</div>
<pre id="test">
</pre>

<script class="testbody" type="application/javascript">
function runTests() {
  var iframe = document.getElementsByTagName("iframe")[0];
  function isDocumentTitleEquals(aDescription, aExpectedTitle) {
    is(iframe.contentDocument.title, aExpectedTitle, aDescription + ": document.title should be " + aExpectedTitle);
    is(iframe.contentDocument.getElementsByTagName("title")[0].textContent, aExpectedTitle, aDescription + ": The text in the title element should be " + aExpectedTitle);
  }

  isDocumentTitleEquals("Checking isDocumentTitleEquals()", "first title");

  const kTests = [
    { description: "designMode=\"on\"",
      init: function () {
        iframe.contentDocument.designMode = "on";
      },
      cleanUp: function () {
        iframe.contentDocument.designMode = "off";
      }
    },
    { description: "html element has contenteditable attribute",
      init: function () {
        iframe.contentDocument.documentElement.setAttribute("contenteditable", "true");
      },
      cleanUp: function () {
        iframe.contentDocument.documentElement.removeAttribute("contenteditable");
      }
    },
  ];

  for (var i = 0; i < kTests.length; i++) {
    const kTest = kTests[i];
    kTest.init();

    var editor = SpecialPowers.wrap(iframe.contentWindow).
                   QueryInterface(SpecialPowers.Ci.nsIInterfaceRequestor).
                   getInterface(SpecialPowers.Ci.nsIWebNavigation).
                   QueryInterface(SpecialPowers.Ci.nsIDocShell).editor;
    ok(editor, kTest.description + ": The docshell should have editor");
    var htmlEditor = editor.QueryInterface(SpecialPowers.Ci.nsIHTMLEditor);
    ok(htmlEditor, kTest.description + ": The editor should have nsIHTMLEditor interface");

    // Replace existing title.
    htmlEditor.setDocumentTitle("Modified title");
    isDocumentTitleEquals(kTest.description, "Modified title");

    // When the document doesn't have <title> element, title element should be created automatically.
    iframe.contentDocument.head.removeChild(iframe.contentDocument.getElementsByTagName("title")[0]);
    is(iframe.contentDocument.getElementsByTagName("title").length, 0, kTest.description + ": There should be no title element");
    htmlEditor.setDocumentTitle("new title");
    is(iframe.contentDocument.getElementsByTagName("title").length, 1, kTest.description + ": There should be a title element");
    isDocumentTitleEquals(kTest.description, "new title");

    kTest.cleanUp();
  }

  SimpleTest.finish();
}
</script>
</body>

</html>