summaryrefslogtreecommitdiff
path: root/docshell/test/navigation/test_bug386782.html
blob: 2434963f1578738854e22ce725ef955614ff788d (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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=386782
-->
<head>
  <title>Test for Bug 386782</title>
  <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
  <script type="text/javascript" src="/tests/SimpleTest/EventUtils.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
  
  <script>

    // This tests if we can load a document whose root is in designMode,
    // edit it, navigate to a new page, navigate back, still edit, and still
    // undo/redo. Note that this is different from the case where the
    // designMode document is in a frame inside the window, as this means
    // the editable region is not in the root docshell (a less complicated case).  

    var pageShowChecker = '<scr' + 'ipt>' +
      'window.addEventListener("pageshow", function(event) {' +
        'window.opener.postMessage({persisted:event.persisted}, "*");' +
      '});</scr' + 'ipt>';
  
    var gTests = [
      {
        // <html><body><p>designModeDocument</p></body></html>
        url: "data:text/html;charset=utf-8,<html><head>" + pageShowChecker + "</head><body><p>designModeDocument</p></body></html>",
        name: 'designModeNavigate',
        onload(doc) { doc.designMode = "on"; },
        expectedBodyBeforeEdit: '<p>designModeDocument</p>',
        expectedBodyAfterEdit:  '<p>EDITED designModeDocument</p>',
        expectedBodyAfterSecondEdit: '<p>EDITED TWICE designModeDocument</p>',
      },
      {
        // <html><body contentEditable="true"><p>contentEditable</p></body></html>
        url: "data:text/html;charset=utf-8,<html><head>" + pageShowChecker + "</head><body contentEditable=\"true\"><p>contentEditable</p></body></html>",
        name: 'contentEditableNavigate',
        expectedBodyBeforeEdit: '<p>contentEditable</p>',
        expectedBodyAfterEdit:  'EDITED <br><p>contentEditable</p>',
        expectedBodyAfterSecondEdit: 'EDITED TWICE <br><p>contentEditable</p>',
      }
    ];
    
    var gTestNum = -1;
    var gTest = null;
    
    window.onload = goNext();
 
    function goNext() {
      gTestNum++;
      if (gTestNum >= gTests.length) {
        SimpleTest.finish();
        return;
      }
      gTest = gTests[gTestNum];
      gTest.window = window.open(gTest.url, gTest.name, "width=500,height=500");
      window.onmessage = function(e) {
        is(e.data.persisted, false, "Initial load cannot be persisted");
        window.onmessage = null;
        if ("onload" in gTest) {
          gTest.onload(gTest.window.document);
        }
        SimpleTest.waitForFocus(beginTest, gTest.window);
      };
    }

    function beginTest() {
      gTest.window.document.body.focus();

      // WARNING: If the following test fails, give the setTimeout() in the onload()
      // a bit longer; the doc hasn't had enough time to setup its editor.
      is(gTest.window.document.body.innerHTML, gTest.expectedBodyBeforeEdit, "Is doc setup yet");
      sendString('EDITED ', gTest.window);
      is(gTest.window.document.body.innerHTML, gTest.expectedBodyAfterEdit, "Editing failed.");

      gTest.window.location = 'data:text/html;charset=utf-8,SomeOtherDocument';
      SimpleTest.waitForFocus(goBack, gTest.window);
    }
    
    function goBack() {
      window.onmessage = function(e) {
        window.onmessage = null;
        // Skip the test if the page is not loaded from the bf-cache when going back.
        if (e.data.persisted) {
          checkStillEditable();
        } else {
          gTest.window.close();
          goNext();
        }
      };
      gTest.window.history.back();
    }

    function checkStillEditable() {

      // Check that the contents are correct.
      is(gTest.window.document.body.innerHTML, gTest.expectedBodyAfterEdit, "Edited contents still correct?");
      
      // Check that we can undo/redo and the contents are correct.
      gTest.window.document.execCommand("undo", false, null);
      is(gTest.window.document.body.innerHTML, gTest.expectedBodyBeforeEdit, "Can we undo?");

      gTest.window.document.execCommand("redo", false, null);
      is(gTest.window.document.body.innerHTML, gTest.expectedBodyAfterEdit, "Can we redo?");
            
      // Check that we can still edit the page.
      gTest.window.document.body.focus();
      sendString('TWICE ', gTest.window);
      is(gTest.window.document.body.innerHTML, gTest.expectedBodyAfterSecondEdit, "Can we still edit?");
      
      gTest.window.close();
      goNext();
      
    }
    
  </script>
  
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=386782">Mozilla Bug 386782</a>
<p id="display"></p>
<div id="content" style="display: none">
  
</div>
<pre id="test">
<script class="testbody" type="text/javascript">

/** Test for Bug 386782 **/

SimpleTest.waitForExplicitFinish();

</script>
</pre>
</body>
</html>