summaryrefslogtreecommitdiffstats
path: root/testing/mochitest/chrome/test_sanityEventUtils.xhtml
blob: ccfd9dbd7ccbd3f8d6f3ccb93698358d4911eeb7 (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
<?xml version="1.0"?>
<!-- This Source Code Form is subject to the terms of the Mozilla Public
   - License, v. 2.0. If a copy of the MPL was not distributed with this
   - file, You can obtain one at http://mozilla.org/MPL/2.0/. -->
<?xml-stylesheet href="chrome://mochikit/content/tests/SimpleTest/test.css"
                 type="text/css"?>
<window title="Test EventUtils functions"
        xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
  <script src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"/>
  <script type="text/javascript">
  var start = new Date();
  </script>
  <script src="chrome://mochikit/content/tests/SimpleTest/EventUtils.js"></script>
  <script type="text/javascript">
  var loadTime = new Date();
  </script>
  <script type="application/javascript">
  <![CDATA[
    info("\nProfile::EventUtilsLoadTime: " + (loadTime - start) + "\n");
    var testFile = Services.dirsvc.get("CurWorkD", Ci.nsIFile);
    var regularDtForDrag1 = null;
    var gSetDropEffect = true;
    var gData;
    var gEnter = false;
    var gOver  = false;
    var dragDrop = [[
      { type    : "text/plain",
        data    : "This is a test" }
    ]];
    // this is the expected data arrays
    // for testing drag of 2 items create 2 inner arrays
    var drag1 = [[
      { type  : "text/uri-list",
        data  : "http://www.mozilla.org/" }
    ]];
    var drag2items = [[
      { type  : "text/uri-list",
        data  : "http://www.mozilla.org/" }
      ],[
      { type  : "text/uri-list",
        data  : "http://www.mozilla.org/" }
    ]];
    var drag1WrongFlavor = [[
      { type  : "text/plain",
        data  : "this is text/plain" }
    ]];
    var drag2 = [[
      { type  : "text/plain",
        data  : "this is text/plain" },
      { type  : "text/uri-list",
        data  : "http://www.mozilla.org/" }
    ]];
    var drag2WrongOrder = [[
      { type  : "text/uri-list",
        data  : "http://www.mozilla.org/" },
      { type  : "text/plain",
        data  : "this is text/plain" }
    ]];
    var dragfile = [[
      { type    : "application/x-moz-file",
        data    : testFile,
        eqTest(actualData, expectedData) {return expectedData.equals(actualData);} },
      { type    : "Files",
        data    : null }
    ]];

    function doOnDrop(aEvent) {
      gData = aEvent.dataTransfer.getData(dragDrop[0][0].type);
      aEvent.preventDefault(); // cancels event and keeps dropEffect
                               // as was before event.
    }

    function doOnDragStart(aEvent) {
      var dt = aEvent.dataTransfer;
      switch (aEvent.currentTarget.id) {
        case "drag2" :
          dt.setData("text/plain", "this is text/plain");
          // fallthrough
        case "drag1" :
          regularDtForDrag1 = dt;
          dt.setData("text/uri-list", "http://www.mozilla.org/");
          break;
        case "dragfile" :
          dt.mozSetDataAt("application/x-moz-file", testFile, 0);
          break;
      }
        dt.effectAllowed = "all";
    }

    function doOnDragEnter(aEvent) {
      gEnter = true;
      aEvent.dataTransfer.effectAllowed = "all";
      aEvent.preventDefault(); // sets target this element
    }

    function doOnDragOver(aEvent) {
      gOver = true;
      if (gSetDropEffect)
        aEvent.dataTransfer.dropEffect = "copy";
      aEvent.preventDefault();
    }

    SimpleTest.waitForExplicitFinish();
    async function test() {
      var startTime = new Date();
      var result;

      /* test synthesizeDrop */
      result = synthesizeDrop($("dragDrop"), $("dragDrop"), dragDrop, null, window);
      ok(gEnter, "Fired dragenter");
      ok(gOver,  "Fired dragover");
      is(result, "copy", "copy is dropEffect");
      is(gData, dragDrop[0][0].data, "Received valid drop data");

      gSetDropEffect = false;
      result = synthesizeDrop($("dragDrop"), $("dragDrop"), dragDrop, "link", window);
      is(result, "link", "link is dropEffect");
      gSetDropEffect = true;

      $("textB").focus();
      var content = synthesizeQueryTextContent(0, 100);
      ok(content, "synthesizeQueryTextContent should not be null");
      ok(content.succeeded, "synthesizeQueryTextContent should succeed");
      is(content.text, "I haz a content", "synthesizeQueryTextContent should be 'I haz a content': " + content.text);

      content = synthesizeQueryCaretRect(0);
      ok(content, "synthesizeQueryCaretRect should not be null");
      ok(content.succeeded, "synthesizeQueryCaretRect should succeed");

      content = synthesizeQueryTextRect(0, 100);
      ok(content, "synthesizeQueryTextRect should not be null");
      ok(content.succeeded, "synthesizeQueryTextRect should succeed");

      content = synthesizeQueryEditorRect();
      ok(content, "synthesizeQueryEditorRect should not be null");
      ok(content.succeeded, "synthesizeQueryEditorRect should succeed");

      content = synthesizeCharAtPoint(0, 0);
      ok(content, "synthesizeCharAtPoint should not be null");
      ok(content.succeeded, "synthesizeCharAtPoint should succeed");

      content = await synthesizeSelectionSet(0, 100);
      ok(content, "synthesizeSelectionSet should not be null");
      is(content, true, "synthesizeSelectionSet should succeed");

      var endTime = new Date();
      info("\nProfile::EventUtilsRunTime: " + (endTime-startTime) + "\n");
      SimpleTest.finish();
    };
  ]]>
  </script>

  <body xmlns="http://www.w3.org/1999/xhtml" onload="setTimeout(test, 0)">
    <input id="textB" value="I haz a content"/>
    <p id="display"></p>
    <div id="content" style="display:none;"></div>
    <pre id="test"></pre>
    <div id="drag1" ondragstart="doOnDragStart(event);">Need some space here</div>
    <p id="display"></p>
    <div id="content" style="display:none;"></div>
    <pre id="test"></pre>
    <div id="dragDrop" ondragover  ="doOnDragOver(event);"
                    ondragenter ="doOnDragEnter(event);"
                    ondragleave ="doOnDragLeave(event);"
                    ondrop      ="doOnDrop(event);">
    Need some depth and height to drag here
    </div>
    <div id="drag2" ondragstart="doOnDragStart(event);">Need more space</div>
    <div id="dragfile" ondragstart="doOnDragStart(event);">Sure why not here too</div>
  </body>
</window>