summaryrefslogtreecommitdiffstats
path: root/editor/libeditor/tests/test_cut_copy_delete_command_enabled.xhtml
blob: 46c91e87d0d365bba32798706f880f7fdf3a4526 (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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
<?xml version="1.0"?>
<?xml-stylesheet type="text/css" href="chrome://global/skin"?>
<?xml-stylesheet type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css"?>

<window xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"
        title="Test for enabled state of cut/copy/delete commands">
  <script src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>


  <script type="application/javascript">
  <![CDATA[
    SimpleTest.waitForExplicitFinish();
    SimpleTest.waitForFocus(() => {
      let text = document.getElementById("textbox");
      let password = document.getElementById("password");

      let editor1 = text.editor;
      let editor2 = password.editor;

      text.focus();
      text.select();

      ok(editor1.canCopy(),
         "nsIEditor.canCopy() should return true in <input>");
      ok(editor1.canCut(),
         "nsIEditor.canCut() should return true in <input>");
      ok(SpecialPowers.isCommandEnabled(window, "cmd_copy"),
         "cmd_copy command should be enabled in <input>");
      ok(SpecialPowers.isCommandEnabled(window, "cmd_cut"),
         "cmd_cut command should be enabled in <input>");
      ok(SpecialPowers.isCommandEnabled(window, "cmd_delete"),
         "cmd_delete command should be enabled in <input>");

      password.focus();
      password.select();

      // Copy and cut commands should be disabled on password fields.
      ok(!editor2.canCopy(),
         "nsIEditor.canCopy() should return false in <input type=password>");
      ok(!editor2.canCut(),
         "nsIEditor.canCut() should return false in <input type=password>");
      ok(!SpecialPowers.isCommandEnabled(window, "cmd_copy"),
         "cmd_copy command should be disabled in <input type=password>");
      ok(!SpecialPowers.isCommandEnabled(window, "cmd_cut"),
         "cmd_cut command should be disabled in <input type=password>");
      ok(SpecialPowers.isCommandEnabled(window, "cmd_delete"),
         "cmd_delete command should be enabled in <input type=password>");

      // If selection is in unmasked range, allow to copy the selected
      // password into the clipboard.
      editor2.unmask(0);
      ok(editor2.canCopy(),
         "nsIEditor.canCopy() should return true in <input type=password> if the password is unmasked");
      ok(editor2.canCut(),
         "nsIEditor.canCut() should return true in <input type=password> if the password is unmasked");
      ok(SpecialPowers.isCommandEnabled(window, "cmd_copy"),
         "cmd_copy command should be enabled in <input type=password> if the password is unmasked");
      ok(SpecialPowers.isCommandEnabled(window, "cmd_cut"),
         "cmd_cut command should be enabled in <input type=password> if the password is unmasked");
      ok(SpecialPowers.isCommandEnabled(window, "cmd_delete"),
         "cmd_delete command should be enabled in <input type=password> if the password is unmasked");

      // If unmasked range will be masked automatically, we shouldn't allow to
      // copy the selected password since the state may be changed during
      // showing edit menu or something.
      editor2.unmask(0, 13, 1000);
      ok(!editor2.canCopy(),
         "nsIEditor.canCopy() should return false in <input type=password> if the password is unmasked but will be masked automatically");
      ok(!editor2.canCut(),
         "nsIEditor.canCut() should return false in <input type=password> if the password is unmasked but will be masked automatically");
      ok(!SpecialPowers.isCommandEnabled(window, "cmd_copy"),
         "cmd_copy command should be disabled in <input type=password> if the password is unmasked but will be masked automatically");
      ok(!SpecialPowers.isCommandEnabled(window, "cmd_cut"),
         "cmd_cut command should be disabled in <input type=password> if the password is unmasked but will be masked automatically");
      ok(SpecialPowers.isCommandEnabled(window, "cmd_delete"),
         "cmd_delete command should be enabled in <input type=password> if the password is unmasked but will be masked automatically");

      // <input type="password"> does not support setSelectionRange() oddly.
      function setSelectionRange(aEditor, aStart, aEnd) {
        let container = aEditor.rootElement.firstChild;
        aEditor.selection.setBaseAndExtent(container, aStart, container, aEnd);
      }

      // Check the range boundaries.
      editor2.unmask(3, 9);
      setSelectionRange(editor2, 0, 2);
      ok(!editor2.canCopy(),
         "nsIEditor.canCopy() should return false in <input type=password> (unmasked range 3-9, selected range 0-2)");
      ok(!editor2.canCut(),
         "nsIEditor.canCut() should return false in <input type=password> (unmasked range 3-9, selected range 0-2)");
      ok(!SpecialPowers.isCommandEnabled(window, "cmd_copy"),
         "cmd_copy command should be disabled in <input type=password> (unmasked range 3-9, selected range 0-2)");
      ok(!SpecialPowers.isCommandEnabled(window, "cmd_cut"),
         "cmd_cut command should be disabled in <input type=password> (unmasked range 3-9, selected range 0-2)");
      ok(SpecialPowers.isCommandEnabled(window, "cmd_delete"),
         "cmd_delete command should be enabled in <input type=password> (unmasked range 3-9, selected range 0-2)");

      setSelectionRange(editor2, 2, 3);
      ok(!editor2.canCopy(),
         "nsIEditor.canCopy() should return false in <input type=password> (unmasked range 3-9, selected range 2-3)");
      ok(!editor2.canCut(),
         "nsIEditor.canCut() should return false in <input type=password> (unmasked range 3-9, selected range 2-3)");
      ok(!SpecialPowers.isCommandEnabled(window, "cmd_copy"),
         "cmd_copy command should be disabled in <input type=password> (unmasked range 3-9, selected range 2-3)");
      ok(!SpecialPowers.isCommandEnabled(window, "cmd_cut"),
         "cmd_cut command should be disabled in <input type=password> (unmasked range 3-9, selected range 2-3)");
      ok(SpecialPowers.isCommandEnabled(window, "cmd_delete"),
         "cmd_delete command should be enabled in <input type=password> (unmasked range 3-9, selected range 2-3)");

      setSelectionRange(editor2, 2, 5);
      ok(!editor2.canCopy(),
         "nsIEditor.canCopy() should return false in <input type=password> (unmasked range 3-9, selected range 2-5)");
      ok(!editor2.canCut(),
         "nsIEditor.canCut() should return false in <input type=password> (unmasked range 3-9, selected range 2-5)");
      ok(!SpecialPowers.isCommandEnabled(window, "cmd_copy"),
         "cmd_copy command should be disabled in <input type=password> (unmasked range 3-9, selected range 2-5)");
      ok(!SpecialPowers.isCommandEnabled(window, "cmd_cut"),
         "cmd_cut command should be disabled in <input type=password> (unmasked range 3-9, selected range 2-5)");
      ok(SpecialPowers.isCommandEnabled(window, "cmd_delete"),
         "cmd_delete command should be enabled in <input type=password> (unmasked range 3-9, selected range 2-5)");

      setSelectionRange(editor2, 2, 10);
      ok(!editor2.canCopy(),
         "nsIEditor.canCopy() should return false in <input type=password> (unmasked range 3-9, selected range 2-10)");
      ok(!editor2.canCut(),
         "nsIEditor.canCut() should return false in <input type=password> (unmasked range 3-9, selected range 2-10)");
      ok(!SpecialPowers.isCommandEnabled(window, "cmd_copy"),
         "cmd_copy command should be disabled in <input type=password> (unmasked range 3-9, selected range 2-10)");
      ok(!SpecialPowers.isCommandEnabled(window, "cmd_cut"),
         "cmd_cut command should be disabled in <input type=password> (unmasked range 3-9, selected range 2-10)");
      ok(SpecialPowers.isCommandEnabled(window, "cmd_delete"),
         "cmd_delete command should be enabled in <input type=password> (unmasked range 3-9, selected range 2-10)");

      setSelectionRange(editor2, 2, 10);
      ok(!editor2.canCopy(),
         "nsIEditor.canCopy() should return false in <input type=password> (unmasked range 3-9, selected range 3-10)");
      ok(!editor2.canCut(),
         "nsIEditor.canCut() should return false in <input type=password> (unmasked range 3-9, selected range 3-10)");
      ok(!SpecialPowers.isCommandEnabled(window, "cmd_copy"),
         "cmd_copy command should be disabled in <input type=password> (unmasked range 3-9, selected range 3-10)");
      ok(!SpecialPowers.isCommandEnabled(window, "cmd_cut"),
         "cmd_cut command should be disabled in <input type=password> (unmasked range 3-9, selected range 3-10)");
      ok(SpecialPowers.isCommandEnabled(window, "cmd_delete"),
         "cmd_delete command should be enabled in <input type=password> (unmasked range 3-9, selected range 3-10)");

      setSelectionRange(editor2, 8, 12);
      ok(!editor2.canCopy(),
         "nsIEditor.canCopy() should return false in <input type=password> (unmasked range 3-9, selected range 8-12)");
      ok(!editor2.canCut(),
         "nsIEditor.canCut() should return false in <input type=password> (unmasked range 3-9, selected range 8-12)");
      ok(!SpecialPowers.isCommandEnabled(window, "cmd_copy"),
         "cmd_copy command should be disabled in <input type=password> (unmasked range 3-9, selected range 8-12)");
      ok(!SpecialPowers.isCommandEnabled(window, "cmd_cut"),
         "cmd_cut command should be disabled in <input type=password> (unmasked range 3-9, selected range 8-12)");
      ok(SpecialPowers.isCommandEnabled(window, "cmd_delete"),
         "cmd_delete command should be enabled in <input type=password> (unmasked range 3-9, selected range 8-12)");

      setSelectionRange(editor2, 9, 12);
      ok(!editor2.canCopy(),
         "nsIEditor.canCopy() should return false in <input type=password> (unmasked range 3-9, selected range 9-12)");
      ok(!editor2.canCut(),
         "nsIEditor.canCut() should return false in <input type=password> (unmasked range 3-9, selected range 9-12)");
      ok(!SpecialPowers.isCommandEnabled(window, "cmd_copy"),
         "cmd_copy command should be disabled in <input type=password> (unmasked range 3-9, selected range 9-12)");
      ok(!SpecialPowers.isCommandEnabled(window, "cmd_cut"),
         "cmd_cut command should be disabled in <input type=password> (unmasked range 3-9, selected range 9-12)");
      ok(SpecialPowers.isCommandEnabled(window, "cmd_delete"),
         "cmd_delete command should be enabled in <input type=password> (unmasked range 3-9, selected range 9-12)");

      setSelectionRange(editor2, 10, 12);
      ok(!editor2.canCopy(),
         "nsIEditor.canCopy() should return false in <input type=password> (unmasked range 3-9, selected range 10-12)");
      ok(!editor2.canCut(),
         "nsIEditor.canCut() should return false in <input type=password> (unmasked range 3-9, selected range 10-12)");
      ok(!SpecialPowers.isCommandEnabled(window, "cmd_copy"),
         "cmd_copy command should be disabled in <input type=password> (unmasked range 3-9, selected range 10-12)");
      ok(!SpecialPowers.isCommandEnabled(window, "cmd_cut"),
         "cmd_cut command should be disabled in <input type=password> (unmasked range 3-9, selected range 10-12)");
      ok(SpecialPowers.isCommandEnabled(window, "cmd_delete"),
         "cmd_delete command should be enabled in <input type=password> (unmasked range 3-9, selected range 10-12)");

      setSelectionRange(editor2, 3, 9);
      ok(editor2.canCopy(),
         "nsIEditor.canCopy() should return true in <input type=password> (unmasked range 3-9, selected range 3-9)");
      ok(editor2.canCut(),
         "nsIEditor.canCut() should return true in <input type=password> (unmasked range 3-9, selected range 3-9)");
      ok(SpecialPowers.isCommandEnabled(window, "cmd_copy"),
         "cmd_copy command should be enabled in <input type=password> (unmasked range 3-9, selected range 3-9)");
      ok(SpecialPowers.isCommandEnabled(window, "cmd_cut"),
         "cmd_cut command should be enabled in <input type=password> (unmasked range 3-9, selected range 3-9)");
      ok(SpecialPowers.isCommandEnabled(window, "cmd_delete"),
         "cmd_delete command should be enabled in <input type=password> (unmasked range 3-9, selected range 3-9)");

      setSelectionRange(editor2, 4, 8);
      ok(editor2.canCopy(),
         "nsIEditor.canCopy() should return true in <input type=password> (unmasked range 3-9, selected range 4-8)");
      ok(editor2.canCut(),
         "nsIEditor.canCut() should return true in <input type=password> (unmasked range 3-9, selected range 4-8)");
      ok(SpecialPowers.isCommandEnabled(window, "cmd_copy"),
         "cmd_copy command should be enabled in <input type=password> (unmasked range 3-9, selected range 4-8)");
      ok(SpecialPowers.isCommandEnabled(window, "cmd_cut"),
         "cmd_cut command should be enabled in <input type=password> (unmasked range 3-9, selected range 4-8)");
      ok(SpecialPowers.isCommandEnabled(window, "cmd_delete"),
         "cmd_delete command should be enabled in <input type=password> (unmasked range 3-9, selected range 4-8)");

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

  <vbox flex="1">
    <input xmlns="http://www.w3.org/1999/xhtml" id="textbox" value="normal text"/>
    <input xmlns="http://www.w3.org/1999/xhtml" id="password" type="password" value="password text"/>
  </vbox>

</window>