summaryrefslogtreecommitdiffstats
path: root/dom/tests/mochitest/ajax/scriptaculous/test/unit/ajax_inplaceeditor_test.html
blob: 4104d9959ddcaa676fbcb79bbf6e6f76e29c30b1 (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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <title>script.aculo.us Unit test file</title>
  <meta http-equiv="content-type" content="text/html; charset=utf-8" />
  <script src="../../lib/prototype.js" type="text/javascript"></script>
  <script src="../../src/scriptaculous.js" type="text/javascript"></script>
  <script src="../../src/unittest.js" type="text/javascript"></script>
  <link rel="stylesheet" href="../test.css" type="text/css" />
</head>
<body>
<h1>script.aculo.us Unit test file</h1>
<p>
  Tests for Ajax.InPlaceEditor in controls.js
</p>

<!-- Log output -->
<div id="testlog"> </div>

<h1 id="tobeedited">To be edited</h1>
<a id="tobeeditedEditControl" href="#">edit</a>

<div id="tobeeditedMultiLine">First line<br/>Second line<br/>Third line</div>

<!-- Tests follow -->
<script type="text/javascript" language="javascript" charset="utf-8">
// <![CDATA[

  new Test.Unit.Runner({

    setup: function() { with(this) {
      inPlaceEditor = new Ajax.InPlaceEditor($('tobeedited'), '_ajax_inplaceeditor_result.html', {
        externalControl: $('tobeeditedEditControl'),
        ajaxOptions: {method: 'get'} //override so we can use a static for the result
      });
      inPlaceEditorMultiLine = new Ajax.InPlaceEditor($('tobeeditedMultiLine'), '_ajax_inplaceeditor_result.html', {
        ajaxOptions: {method: 'get'} //override so we can use a static for the result
      });
    }},

    teardown: function() { with(this) {
      inPlaceEditor.dispose();
    }},

    testDisposesProperly: function() { with(this) {
      assertEqual("rgba(0, 0, 0, 0)", Element.getStyle('tobeedited','background-color'));
      inPlaceEditor.dispose();
      assertEqual("rgba(0, 0, 0, 0)", Element.getStyle('tobeedited','background-color'));
      assertVisible($('tobeedited'));
      Event.simulateMouse('tobeedited','click');
      assertVisible($('tobeedited'));
    }},

    testUsesTextAreaWhenMoreThanOneRows: function() { with(this) {
      inPlaceEditor.options.rows = 5;
      inPlaceEditor.enterEditMode();
      assertEqual("TEXTAREA", document.forms[0].firstChild.tagName);
      assertEqual("BR", document.forms[0].childNodes[1].tagName);
    }},

    testCanSpecifyAllTextsThroughOptions: function() { with(this) {
       // swedish translation ;-)
      inPlaceEditor.options.okText = "spara";
      inPlaceEditor.options.cancelText = "avbryt";
      inPlaceEditor.options.savingText = "Sparar...";
      inPlaceEditor.enterEditMode();
      assertEqual("spara", document.forms[0].lastChild.previousSibling.value);
      assertEqual("avbryt", document.forms[0].lastChild.innerHTML);
      inPlaceEditor.showSaving();
      assertEqual("Sparar...", $('tobeedited').innerHTML);
    }},

    testCanSpecifyFormIdThroughOptions: function() { with(this) {
      inPlaceEditor.enterEditMode();
      // default form id
      assertEqual("tobeedited-inplaceeditor", document.forms[0].id);
      inPlaceEditor.leaveEditMode();
      inPlaceEditor.options.formId = "myFormId";
      inPlaceEditor.enterEditMode();
      assertEqual("myFormId", document.forms[0].id);
    }},

    testCantEditWhileSaving: function() { with(this) {
      inPlaceEditor.onLoading();
      Event.simulateMouse('tobeedited','mouseover');
      assertEqual("rgba(0, 0, 0, 0)", Element.getStyle('tobeedited','background-color'));
      Event.simulateMouse('tobeedited','click');
      assertVisible($('tobeedited'));
    }},

    testCallbackFunctionGetsCalled: function() { with(this) {
      called = false;
      inPlaceEditor.options.callback = function(form) {
        called = true;
      }
      Event.simulateMouse('tobeedited','click');
      Event.simulateMouse(document.forms[0].childNodes[1],'click');
      assert(called, "callback was not called");
    }},

    testCanUseExternalElementToGoIntoEditMode: function() { with(this) {
      Event.simulateMouse('tobeeditedEditControl','click');
      assertNotNull(document.forms[0], "external control didn't work");
      // doesn't work if you click it again while in edit mode
      Event.simulateMouse('tobeeditedEditControl','click');
      assertNull(document.forms[1], "external control created two forms");
      assertNotVisible($('tobeeditedEditControl'));
      Event.simulateMouse(document.forms[0].childNodes[2],'click');
      assertVisible($('tobeeditedEditControl'));
    }},

    testHasLineBreaksDetectsHTMLLineBreaks: function() { with(this) {
      assert(inPlaceEditorMultiLine.hasHTMLLineBreaks("Line 1<br/>Line 2"));
      assert(inPlaceEditorMultiLine.hasHTMLLineBreaks("Line 1<br>Line 2"));
      assert(inPlaceEditorMultiLine.hasHTMLLineBreaks("<p>Line 1</p>Line 2"));
      assert(inPlaceEditorMultiLine.hasHTMLLineBreaks("Line 1<BR/>Line 2"));
      assert(inPlaceEditorMultiLine.hasHTMLLineBreaks("Line 1<BR>Line 2"));
      assert(inPlaceEditorMultiLine.hasHTMLLineBreaks("<P>Line 1</P>Line 2"));
      assert(inPlaceEditorMultiLine.hasHTMLLineBreaks("<P>Line 1<P>Line 2"));
      assert(!inPlaceEditorMultiLine.hasHTMLLineBreaks("One line"));
    }},

    testConvertsHTMLLineBreaksIntoNewLines: function() { with(this) {
      assertEqual("Line 1\nLine 2", inPlaceEditorMultiLine.convertHTMLLineBreaks("Line 1<br/>Line 2"));
      assertEqual("Line 1\nLine 2", inPlaceEditorMultiLine.convertHTMLLineBreaks("Line 1<br>Line 2"));
      // <p> not supported (too hard)
      //assertEqual("Line 1\nLine 2", inPlaceEditorMultiLine.convertHTMLLineBreaks("<p>Line 1</p>Line 2"));
      assertEqual("Line 1\nLine 2", inPlaceEditorMultiLine.convertHTMLLineBreaks("Line 1<BR/>Line 2"));
      assertEqual("Line 1\nLine 2", inPlaceEditorMultiLine.convertHTMLLineBreaks("Line 1<BR>Line 2"));
      // <p> not supported (too hard)
      //assertEqual("Line 1\nLine 2", inPlaceEditorMultiLine.convertHTMLLineBreaks("<P>Line 1</P>Line 2"));
      // unclosed <P>s not supported yet
      //assertEqual("Line 1\nLine 2", inPlaceEditorMultiLine.convertHTMLLineBreaks("<P>Line 1<P>Line 2"));
      // <p> not supported (too hard)
      //assertEqual("Line 1\nLine 2\nLine 3\nLine 4", inPlaceEditorMultiLine.convertHTMLLineBreaks("<P>Line 1</P>Line 2<br>Line 3<p>Line 4</P>"));
      assertEqual("One line", inPlaceEditorMultiLine.convertHTMLLineBreaks("One line"));
    }},

    testConvertsParagraphsAndBRsIntoLineBreaks: function() { with(this) {
      inPlaceEditorMultiLine.enterEditMode();
      assertEqual("TEXTAREA", document.forms[0].firstChild.tagName);
      assertEqual("First line\nSecond line\nThird line", document.forms[0].firstChild.value)
      // doesn't automatically determine size yet
      //assertEqual(3, document.forms[0].firstChild.rows);
    }}

  });
// ]]>
</script>
</body>
</html>