summaryrefslogtreecommitdiffstats
path: root/mobile/android/tests/browser/robocop/robocop_input.html
blob: 50ddd6e9acbb00e81cff8c06011db9dbf96bd786 (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
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Robocop Input</title>
  </head>
  <body>
    <p>Input: <input id="input" type="text"></p>
    <p>Text area: <textarea id="text-area"></textarea></p>
    <p>Content editable: <div id="content-editable" contentEditable="true"></div></p>
    <p>Design mode: <iframe id="design-mode" src="data:text/html;charset=utf-8,<html><body></body></html>"></iframe></p>
    <p>Resetting input: <input id="resetting-input" type="text"></p>
    <p>Hiding input: <input id="hiding-input" type="text"></p>
    <script type="application/javascript;version=1.8" src="robocop_head.js"></script>
    <script type="application/javascript;version=1.8">
      let input = document.getElementById("input");
      let textArea = document.getElementById("text-area");
      let contentEditable = document.getElementById("content-editable");

      let designMode = document.getElementById("design-mode");
      try {
        designMode.contentDocument.designMode = "on";
      } catch (e) {
        // Setting designMode above sometimes fails, so try again later.
        setTimeout(function() { designMode.contentDocument.designMode = "on" }, 0);
      }

      // Spatial navigation interferes with design-mode key event tests.
      SpecialPowers.setBoolPref("snav.enabled", false);

      // An input that resets the editor on every input by resetting the value property.
      let resetting_input = document.getElementById("resetting-input");
      resetting_input.addEventListener('input', function() {
        this.value = this.value;
      });

      // An input that hides on input.
      let hiding_input = document.getElementById("hiding-input");
      hiding_input.addEventListener('keydown', function(e) {
        if (e.key === "!") { // '!' key event as sent by testInputConnection.java.
          this.value = "";
          this.style.display = "none";
        }
      });

      let getEditor, setValue, setSelection;

      let test = {
        focus_input: function(val) {
          getEditor = function() {
            return SpecialPowers.wrap(input).QueryInterface(
                SpecialPowers.Ci.nsIDOMNSEditableElement).editor;
          };
          setValue = function(val) {
            input.value = val;
          };
          setSelection = function(pos) {
            input.setSelectionRange(pos, pos);
          };
          setValue(val);
          input.focus();
        },

        focus_text_area: function(val) {
          getEditor = function() {
            return SpecialPowers.wrap(textArea).QueryInterface(
                SpecialPowers.Ci.nsIDOMNSEditableElement).editor;
          };
          setValue = function(val) {
            textArea.value = val;
          };
          setSelection = function(pos) {
            textArea.setSelectionRange(pos, pos);
          };
          setValue(val);
          textArea.focus();
        },

        focus_content_editable: function(val) {
          getEditor = function() {
            return SpecialPowers.wrap(window).QueryInterface(
                SpecialPowers.Ci.nsIInterfaceRequestor).getInterface(
                SpecialPowers.Ci.nsIWebNavigation).QueryInterface(
                SpecialPowers.Ci.nsIDocShell).editor;
          };
          setValue = function(val) {
            contentEditable.innerHTML = val;
          };
          setSelection = function(pos) {
            window.getSelection().collapse(contentEditable.firstChild, pos);
          };
          setValue(val);
          contentEditable.focus();
        },

        focus_design_mode: function(val) {
          getEditor = function() {
            return SpecialPowers.wrap(designMode.contentWindow).QueryInterface(
                SpecialPowers.Ci.nsIInterfaceRequestor).getInterface(
                SpecialPowers.Ci.nsIWebNavigation).QueryInterface(
                SpecialPowers.Ci.nsIDocShell).editor;
          };
          setValue = function(val) {
            designMode.contentDocument.body.innerHTML = val;
          };
          setSelection = function(pos) {
            designMode.contentWindow.getSelection().collapse(
                designMode.contentDocument.body.firstChild, pos);
          };
          setValue(val);
          designMode.contentWindow.focus();
          designMode.contentDocument.body.focus();
        },

        test_reflush_changes: function() {
          let inputIme = getEditor().QueryInterface(SpecialPowers.Ci.nsIEditorIMESupport);
          do_check_true(inputIme.composing);

          // Ending the composition then setting the input value triggers the bug.
          inputIme.forceCompositionEnd();
          setValue("good"); // Value that testInputConnection.java expects.
          setSelection(4);
        },

        test_set_selection: function() {
          let inputIme = getEditor().QueryInterface(SpecialPowers.Ci.nsIEditorIMESupport);
          do_check_true(inputIme.composing);

          // Ending the composition then setting the selection triggers the bug.
          inputIme.forceCompositionEnd();
          setSelection(3); // Offsets that testInputConnection.java expects.
        },

        test_bug1123514: function() {
          document.activeElement.addEventListener('input', function test_bug1123514_listener() {
            this.removeEventListener('input', test_bug1123514_listener);

            // Only works on input and textarea.
            if (this.value === 'b') {
              this.value = 'abc';
            }
          });
        },

        focus_resetting_input: function(val) {
          resetting_input.value = val;
          resetting_input.focus();
        },

        focus_hiding_input: function(val) {
          hiding_input.value = val;
          hiding_input.style.display = "";
          hiding_input.focus();
        },

        finish_test: function() {
          java.disconnect();
        },
      };

      var java = new JavaBridge(test);
    </script>
  </body>
</html>