forked from mozilla/mozmill
/
controller.js
905 lines (789 loc) · 28.5 KB
/
controller.js
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
// ***** BEGIN LICENSE BLOCK *****
// Version: MPL 1.1/GPL 2.0/LGPL 2.1
//
// The contents of this file are subject to the Mozilla Public License Version
// 1.1 (the "License"); you may not use this file except in compliance with
// the License. You may obtain a copy of the License at
// http://www.mozilla.org/MPL/
//
// Software distributed under the License is distributed on an "AS IS" basis,
// WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
// for the specific language governing rights and limitations under the
// License.
//
// The Original Code is Mozilla Corporation Code.
//
// The Initial Developer of the Original Code is
// Adam Christian.
// Portions created by the Initial Developer are Copyright (C) 2008
// the Initial Developer. All Rights Reserved.
//
// Contributor(s):
// Adam Christian <adam.christian@gmail.com>
// Mikeal Rogers <mikeal.rogers@gmail.com>
// Henrik Skupin <hskupin@mozilla.com>
//
// Alternatively, the contents of this file may be used under the terms of
// either the GNU General Public License Version 2 or later (the "GPL"), or
// the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
// in which case the provisions of the GPL or the LGPL are applicable instead
// of those above. If you wish to allow use of your version of this file only
// under the terms of either the GPL or the LGPL, and not to allow others to
// use your version of this file under the terms of the MPL, indicate your
// decision by deleting the provisions above and replace them with the notice
// and other provisions required by the GPL or the LGPL. If you do not delete
// the provisions above, a recipient may use your version of this file under
// the terms of any one of the MPL, the GPL or the LGPL.
//
// ***** END LICENSE BLOCK *****
var EXPORTED_SYMBOLS = ["MozMillController", "sleep", "waitForEval", "MozMillAsyncTest",
"globalEventRegistry", "waitFor"];
var events = {}; Components.utils.import('resource://mozmill/modules/events.js', events);
var EventUtils = {}; Components.utils.import('resource://mozmill/modules/EventUtils.js', EventUtils);
var utils = {}; Components.utils.import('resource://mozmill/modules/utils.js', utils);
var elementslib = {}; Components.utils.import('resource://mozmill/modules/elementslib.js', elementslib);
var frame = {}; Components.utils.import('resource://mozmill/modules/frame.js', frame);
var hwindow = Components.classes["@mozilla.org/appshell/appShellService;1"]
.getService(Components.interfaces.nsIAppShellService)
.hiddenDOMWindow;
var aConsoleService = Components.classes["@mozilla.org/consoleservice;1"].
getService(Components.interfaces.nsIConsoleService);
function sleep (milliseconds) {
var self = {};
// We basically just call this once after the specified number of milliseconds
function wait() {
self.timeup = true;
}
// Calls repeatedly every X milliseconds until clearInterval is called
var interval = hwindow.setInterval(wait, milliseconds);
var thread = Components.classes["@mozilla.org/thread-manager;1"]
.getService()
.currentThread;
// This blocks execution until our while loop condition is invalidated. Note
// that you must use a simple boolean expression for the loop, a function call
// will not work.
while(!self.timeup)
thread.processNextEvent(true);
hwindow.clearInterval(interval);
return true;
}
function waitForEval (expression, timeout, interval, subject) {
if (interval == undefined) {
interval = 100;
}
if (timeout == undefined) {
timeout = 30000;
}
var self = {};
self.counter = 0;
self.result = eval(expression);
function wait(){
self.result = eval(expression);
self.counter += interval;
}
var timeoutInterval = hwindow.setInterval(wait, interval);
var thread = Components.classes["@mozilla.org/thread-manager;1"]
.getService()
.currentThread;
while((self.result != true) && (self.counter < timeout)) {
thread.processNextEvent(true);
}
if (self.counter < timeout) { var r = true; }
else { var r = false; }
hwindow.clearInterval(timeoutInterval);
return r;
}
function waitForImage(elem, timeout, interval) {
if (interval == undefined) {
interval = 100;
}
if (timeout == undefined) {
timeout = 30000;
}
return waitForEval('subject.complete == true', timeout, interval, elem.getNode());
}
function waitForElement(elem, timeout, interval) {
if (interval == undefined) {
interval = 100;
}
if (timeout == undefined) {
timeout = 30000;
}
return waitForEval('subject.exists()', timeout, interval, elem);
}
var Menu = function (elements, doc, window) {
this.doc = doc;
this.window = window;
for each(node in elements) {
if (node.tagName){
if (node.tagName == "menu") {
var label = node.getAttribute("label");
var id = node.id;
this[label] = new Menu(node.getElementsByTagName("menupopup")[0].childNodes);
this[id] = this[label];
} else if (node.tagName == "menuitem") {
this[node.getAttribute("label")] = node;
this[node.id] = node;
}
}
}
};
Menu.prototype.reload = function () {
var utils = this.window.QueryInterface(Components.interfaces.nsIInterfaceRequestor).
getInterface(Components.interfaces.nsIDOMWindowUtils);
utils.forceUpdateNativeMenuAt("4");
utils.activateNativeMenuItemAt("4|10");
var elements = this.doc.getElementsByTagName('menubar')[0].childNodes;
for each(node in elements) {
if (node.tagName){
if (node.tagName == "menu") {
var label = node.getAttribute("label");
var id = node.id;
this[label] = new Menu(node.getElementsByTagName("menupopup")[0].childNodes);
this[id] = this[label];
} else if (node.tagName == "menuitem") {
this[node.getAttribute("label")] = node;
this[node.id] = node;
}
}
}
}
var MozMillController = function (window) {
// TODO: Check if window is loaded and block until it has if it hasn't.
this.window = window;
this.mozmillModule = {};
Components.utils.import('resource://mozmill/modules/mozmill.js', this.mozmillModule);
waitForEval("try { subject != null; } catch(err){}", 5000, undefined, window)
waitForEval("try { subject.documentLoaded != undefined; } catch(err){}", 5000, undefined, window)
if ( controllerAdditions[window.document.documentElement.getAttribute('windowtype')] != undefined ) {
this.prototype = new utils.Copy(this.prototype);
controllerAdditions[window.document.documentElement.getAttribute('windowtype')](this);
this.windowtype = window.document.documentElement.getAttribute('windowtype');
}
//this will break on windows for addons and downloads controller
try {
this.menus = new Menu(this.window.document.getElementsByTagName('menubar')[0].childNodes, this.window.document, this.window);
} catch(err){}
}
MozMillController.prototype.keypress = function(el, aKey, modifiers) {
var element = (el == null) ? this.window : el.getNode();
if (!element) {
throw new Error("could not find element " + el.getInfo());
return false;
}
events.triggerKeyEvent(element, 'keypress', aKey, modifiers);
frame.events.pass({'function':'Controller.keypress()'});
return true;
}
MozMillController.prototype.triggerKeyEvent = function(el, aKey, modifiers) {
var element = (el == null) ? this.window : el.getNode();
if (!element) {
throw new Error("could not find element " + el.getInfo());
return false;
}
events.triggerKeyEvent(element, 'keypress', aKey, modifiers);
frame.events.pass({'function':'Controller.keypress()'});
return true;
}
MozMillController.prototype.type = function (el, text) {
var element = (el == null) ? this.window : el.getNode();
if (!element) {
throw new Error("could not find element " + el.getInfo());
return false;
}
for (var indx = 0; indx < text.length; indx++) {
events.triggerKeyEvent(element, 'keypress', text.charAt(indx), {});
}
frame.events.pass({'function':'Controller.type()'});
return true;
}
MozMillController.prototype.open = function(url){
if (this.mozmillModule.Application == 'Firefox') {
this.window.openLocation();
} else if (this.mozmillModule.Application == 'SeaMonkey') {
this.window.ShowAndSelectContentsOfURLBar();
}
var el = new elementslib.ID(this.window.document, 'urlbar');
// Enter URL and press return
this.type(el, url);
events.triggerKeyEvent(el.getNode(), 'keypress', "VK_RETURN", {});
frame.events.pass({'function':'Controller.open()'});
}
MozMillController.prototype.rightClick = function(el, left, top){
var element = el.getNode();
if (!element){
throw new Error("could not find element " + el.getInfo());
return false;
}
if (isNaN(left)){ left = 1; }
if (isNaN(top)){ top = 1; }
EventUtils.synthesizeMouse(element, left, top, { type : "contextmenu", button: 2 }, element.ownerDocument.defaultView);
//var evt = element.ownerDocument.defaultView.document.createEvent('MouseEvents');
//evt.initMouseEvent("click", true, true, element.ownerDocument.defaultView, 0, 0, 0, 0, 0, false, false, false, false, 2, null);
frame.events.pass({'function':'Controller.rightclick()'});
return true;
};
//show a deprecation warning for rightclick to update to rightClick
MozMillController.prototype.rightclick = function(){
frame.log({function:'rightclick - Deprecation Warning', message:'Controller.rightclick should be renamed to Controller.rightClick'});
this.rightClick.apply(this, arguments);
}
MozMillController.prototype.click = function(el, left, top){
//this.window.focus();
var element = el.getNode();
if (!element){
throw new Error("could not find element " + el.getInfo());
return false;
}
if (element.tagName == "menuitem") {
element.click();
return true;
}
if (isNaN(left)){ left = 1; }
if (isNaN(top)){ top = 1; }
EventUtils.synthesizeMouse(element, left, top, {}, element.ownerDocument.defaultView);
frame.events.pass({'function':'Controller.click()'});
return true;
};
MozMillController.prototype.sleep = sleep;
MozMillController.prototype.waitForEval = function (expression, timeout, interval, subject) {
var r = waitForEval(expression, timeout, interval, subject);
if (!r) {
throw new Error("timeout exceeded for waitForEval('"+expression+"')");
}
}
MozMillController.prototype.waitForElement = function (elem, timeout, interval) {
var r = waitForElement(elem, timeout, interval);
if (!r) {
throw new Error("timeout exceeded for waitForElement "+elem.getInfo());
}
frame.events.pass({'function':'Controller.waitForElement()'});
}
MozMillController.prototype.waitForImage = function (elem, timeout, interval) {
var r = waitForImage(elem, timeout, interval);
if (!r) {
throw new Error("timeout exceeded for waitForImage "+elem.getInfo());
}
frame.events.pass({'function':'Controller.waitForImage()'});
}
MozMillController.prototype.waitThenClick = function (elem, timeout, interval) {
this.waitForElement(elem, timeout, interval);
this.click(elem);
}
/* Select the specified option and trigger the relevant events of the element.*/
MozMillController.prototype.select = function (el, indx, option, value) {
element = el.getNode();
if (!element){
throw new Error("Could not find element " + el.getInfo());
return false;
}
//if we have a select drop down
if (element.localName.toLowerCase() == "select"){
var item = null;
// The selected item should be set via its index
if (indx != undefined) {
// Resetting a menulist has to be handled separately
if (indx == -1) {
events.triggerEvent(element, 'focus', false);
element.selectedIndex = indx;
events.triggerEvent(element, 'change', true);
frame.events.pass({'function':'Controller.select()'});
return true;
} else {
item = element.options.item(indx);
}
} else {
for (var i = 0; i < element.options.length; i++) {
var entry = element.options.item(i);
if (option != undefined && entry.innerHTML == option ||
value != undefined && entry.value == value) {
item = entry;
break;
}
}
}
// Click the item
try {
// EventUtils.synthesizeMouse doesn't work.
events.triggerEvent(element, 'focus', false);
item.selected = true;
events.triggerEvent(element, 'change', true);
frame.events.pass({'function':'Controller.select()'});
return true;
} catch (ex) {
throw new Error("No item selected for element " + el.getInfo());
return false;
}
}
//if we have a xul menulist select accordingly
else if (element.localName.toLowerCase() == "menulist"){
var item = null;
if (indx != undefined) {
if (indx == -1) {
events.triggerEvent(element, 'focus', false);
element.selectedIndex = indx;
events.triggerEvent(element, 'change', true);
frame.events.pass({'function':'Controller.select()'});
return true;
} else {
item = element.getItemAtIndex(indx);
}
} else {
for (var i = 0; i < element.itemCount; i++) {
var entry = element.getItemAtIndex(i);
if (option != undefined && entry.label == option ||
value != undefined && entry.value == value) {
item = entry;
break;
}
}
}
// Click the item
try {
EventUtils.synthesizeMouse(element, 1, 1, {}, item.ownerDocument.defaultView);
this.sleep(0);
EventUtils.synthesizeMouse(item, 1, 1, {}, item.ownerDocument.defaultView);
frame.events.pass({'function':'Controller.select()'});
return true;
} catch (ex) {
throw new Error('No item selected for element ' + el.getInfo());
return false;
}
}
};
//Directly access mouse events
MozMillController.prototype.mousedown = function (el){
//this.window.focus();
var mdnElement = el.getNode();
events.triggerMouseEvent(mdnElement, 'mousedown', true);
frame.events.pass({'function':'Controller.mousedown()'});
return true;
};
MozMillController.prototype.mouseup = function (el){
//this.window.focus();
var mupElement = el.getNode();
events.triggerMouseEvent(mdnElement, 'mupElement', true);
frame.events.pass({'function':'Controller.mouseup()'});
return true;
};
MozMillController.prototype.mouseover = function (el){
//this.window.focus();
var mdnElement = el.getNode();
events.triggerMouseEvent(mdnElement, 'mouseover', true);
frame.events.pass({'function':'Controller.mouseover()'});
return true;
};
MozMillController.prototype.mouseout = function (el){
//this.window.focus();
var moutElement = el.getNode();
events.triggerMouseEvent(moutElement, 'mouseout', true);
frame.events.pass({'function':'Controller.mouseout()'});
return true;
};
//Browser navigation functions
MozMillController.prototype.goBack = function(){
//this.window.focus();
this.window.content.history.back();
frame.events.pass({'function':'Controller.goBack()'});
return true;
}
MozMillController.prototype.goForward = function(){
//this.window.focus();
this.window.content.history.forward();
frame.events.pass({'function':'Controller.goForward()'});
return true;
}
MozMillController.prototype.refresh = function(){
//this.window.focus();
this.window.content.location.reload(true);
frame.events.pass({'function':'Controller.refresh()'});
return true;
}
//there is a problem with checking via click in safari
MozMillController.prototype.check = function(el){
//this.window.focus();
var element = el.getNode();
return MozMillController.click(element);
}
//Radio buttons are even WIERDER in safari, not breaking in FF
MozMillController.prototype.radio = function(el){
//this.window.focus();
var element = el.getNode();
return MozMillController.click(element);
}
//Double click for Mozilla
MozMillController.prototype.doubleClick = function(el, left, top) {
//this.window.focus();
var element = el.getNode();
if (!element){
throw new Error("could not find element " + el.getInfo());
return false;
}
if (isNaN(left)) { left = 1; }
if (isNaN(top)) { top = 1; }
EventUtils.synthesizeMouse(element, left, top, {clickCount: 2}, element.ownerDocument.defaultView);
frame.events.pass({'function':'Controller.doubleClick()'});
return true;
};
MozMillController.prototype.assertText = function (el, text) {
//this.window.focus();
var n = el.getNode();
if (n.innerHTML == text){
frame.events.pass({'function':'Controller.assertText()'});
return true;
}
throw new Error("could not validate element " + el.getInfo()+" with text "+ text);
return false;
};
//Assert that a specified node exists
MozMillController.prototype.assertNode = function (el) {
//this.window.focus();
var element = el.getNode();
if (!element){
throw new Error("could not find element " + el.getInfo());
return false;
}
frame.events.pass({'function':'Controller.assertNode()'});
return true;
};
// Assert that a specified node doesn't exist
MozMillController.prototype.assertNodeNotExist = function (el) {
//this.window.focus();
try {
var element = el.getNode();
} catch(err){
frame.events.pass({'function':'Controller.assertNodeNotExist()'});
return true;
}
if (element) {
throw new Error("Unexpectedly found element " + el.getInfo());
return false;
} else {
frame.events.pass({'function':'Controller.assertNodeNotExist()'});
return true;
}
};
//Assert that a form element contains the expected value
MozMillController.prototype.assertValue = function (el, value) {
//this.window.focus();
var n = el.getNode();
if (n.value == value){
frame.events.pass({'function':'Controller.assertValue()'});
return true;
}
throw new Error("could not validate element " + el.getInfo()+" with value "+ value);
return false;
};
//Assert that a provided value is selected in a select element
MozMillController.prototype.assertJS = function (js) {
//this.window.focus();
var result = eval(js);
if (result){
frame.events.pass({'function':'Controller.assertJS()'});
return result;
}
else{
throw new Error("javascript assert was not succesful");
return result;}
};
//Assert that a provided value is selected in a select element
MozMillController.prototype.assertSelected = function (el, value) {
//this.window.focus();
var n = el.getNode();
var validator = value;
if (n.options[n.selectedIndex].value == validator){
frame.events.pass({'function':'Controller.assertSelected()'});
return true;
}
throw new Error("could not assert value for element " + el.getInfo()+" with value "+ value);
return false;
};
//Assert that a provided checkbox is checked
MozMillController.prototype.assertChecked = function (el) {
//this.window.focus();
var element = el.getNode();
if (element.checked == true){
frame.events.pass({'function':'Controller.assertChecked()'});
return true;
}
throw new Error("assert failed for checked element " + el.getInfo());
return false;
};
// Assert that a provided checkbox is not checked
MozMillController.prototype.assertNotChecked = function (el) {
var element = el.getNode();
if (!element) {
throw new Error("Could not find element" + el.getInfo());
}
if (!element.hasAttribute("checked") || element.checked != true){
frame.events.pass({'function':'Controller.assertNotChecked()'});
return true;
}
throw new Error("assert failed for not checked element " + el.getInfo());
return false;
};
// Assert that a an element's property is a particular value
MozMillController.prototype.assertProperty = function(el, attrib, val) {
var element = el.getNode();
if (!element){
throw new Error("could not find element " + el.getInfo());
return false;
}
var value = eval ('element.' + attrib+';');
var res = false;
try {
if (value.indexOf(val) != -1){
res = true;
}
}
catch(err){
}
if (String(value) == String(val)) {
res = true;
}
if (res) {
frame.events.pass({'function':'Controller.assertProperty()'});
} else {
throw new Error('Controller.assertProperty() failed');
}
return res;
};
// Assert that an element's property does not exist
MozMillController.prototype.assertPropertyNotExist = function(el, attrib) {
var element = el.getNode();
if (!element) {
throw new Error("could not find element " + el.getInfo());
return false;
}
if (!element.hasAttribute(attrib)) {
frame.events.pass({'function':'Controller.assertPropertyNotExist()'});
return true;
}
throw new Error("assert failed for checked element " + el.getInfo());
return false;
};
// Assert that a specified image has actually loaded
// The Safari workaround results in additional requests
// for broken images (in Safari only) but works reliably
MozMillController.prototype.assertImageLoaded = function (el) {
//this.window.focus();
var img = el.getNode();
if (!img || img.tagName != 'IMG') {
throw new Error('Controller.assertImageLoaded() failed.')
return false;
}
var comp = img.complete;
var ret = null; // Return value
// Workaround for Safari -- it only supports the
// complete attrib on script-created images
if (typeof comp == 'undefined') {
test = new Image();
// If the original image was successfully loaded,
// src for new one should be pulled from cache
test.src = img.src;
comp = test.complete;
}
// Check the complete attrib. Note the strict
// equality check -- we don't want undefined, null, etc.
// --------------------------
// False -- Img failed to load in IE/Safari, or is
// still trying to load in FF
if (comp === false) {
ret = false;
}
// True, but image has no size -- image failed to
// load in FF
else if (comp === true && img.naturalWidth == 0) {
ret = false;
}
// Otherwise all we can do is assume everything's
// hunky-dory
else {
ret = true;
}
if (ret) {
frame.events.pass({'function':'Controller.assertImageLoaded'});
} else {
throw new Error('Controller.assertImageLoaded() failed.')
}
return ret;
};
//Drag one eleent to the top x,y coords of another specified element
MozMillController.prototype.mouseMove = function (doc, start, dest) {
//if one of these elements couldn't be looked up
if (typeof start != 'object'){
throw new Error("received bad coordinates");
return false;
}
if (typeof dest != 'object'){
throw new Error("received bad coordinates");
return false;
}
//Do the initial move to the drag element position
events.triggerMouseEvent(doc.body, 'mousemove', true, start[0], start[1]);
events.triggerMouseEvent(doc.body, 'mousemove', true, dest[0], dest[1]);
frame.events.pass({'function':'Controller.mouseMove()'});
return true;
}
//Drag one eleent to the top x,y coords of another specified element
MozMillController.prototype.dragDropElemToElem = function (dstart, ddest) {
//Get the drag and dest
var drag = dstart.getNode();
var dest = ddest.getNode();
//if one of these elements couldn't be looked up
if (!drag){
throw new Error("could not find element " + drag.getInfo());
return false;
}
if (!dest){
throw new Error("could not find element " + dest.getInfo());
return false;
}
var dragCoords = null;
var destCoords = null;
dragCoords = drag.getBoundingClientRect();
destCoords = dest.getBoundingClientRect();
//Do the initial move to the drag element position
events.triggerMouseEvent(drag.ownerDocument.body, 'mousemove', true, dragCoords.left, dragCoords.top);
events.triggerMouseEvent(drag, 'mousedown', true, dragCoords.left, dragCoords.top); //do the mousedown
events.triggerMouseEvent(drag.ownerDocument.body, 'mousemove', true, destCoords.left, destCoords.top);
events.triggerMouseEvent(dest, 'mouseup', true, destCoords.left, destCoords.top);
events.triggerMouseEvent(dest, 'click', true, destCoords.left, destCoords.top);
frame.events.pass({'function':'Controller.dragDropElemToElem()'});
return true;
}
var waitFor = function (node, events) {
if (node.getNode != undefined) {
node = node.getNode();
}
this.events = events;
this.node = node;
node.firedEvents = {}
this.registry = {};
for each(e in events) {
var listener = function (event) {
this.firedEvents[event.type] = true;
}
this.registry[e] = listener;
this.registry[e].result = false;
this.node.addEventListener(e, this.registry[e], true);
}
}
waitFor.prototype.wait = function (timeout, interval) {
for (e in this.registry) {
var r = waitForEval("subject['"+e+"'] == true", timeout, interval, this.node.firedEvents)
if (!r) {
throw "Event didn't fire before timeout. event == "+e+", result is "+this.registry[e].result;
}
this.node.removeEventListener(e, this.registry[e], true);
}
}
function preferencesAdditions(controller) {
var mainTabs = controller.window.document.getAnonymousElementByAttribute(controller.window.document.documentElement, 'anonid', 'selector');
controller.tabs = {};
for (var i = 0; i < mainTabs.childNodes.length; i++) {
var node = mainTabs.childNodes[i];
var obj = {'button':node}
controller.tabs[i] = obj;
var label = node.attributes.item('label').value.replace('pane', '');
controller.tabs[label] = obj;
}
controller.prototype.__defineGetter__("activeTabButton",
function () {return mainTabs.getElementsByAttribute('selected', true)[0];
})
}
function Tabs (controller) {
this.controller = controller;
}
Tabs.prototype.getTab = function(index) {
return this.controller.window.gBrowser.browsers[index].contentDocument;
}
Tabs.prototype.__defineGetter__("activeTab", function() {
return this.controller.window.gBrowser.selectedBrowser.contentDocument;
})
Tabs.prototype.selectTab = function(index) {
// GO in to tab manager and grab the tab by index and call focus.
}
Tabs.prototype.findWindow = function (doc) {
for (i=0;i<=(this.controller.window.frames.length-1);i++) {
if (this.controller.window.frames[i].document == doc) {
return this.controller.window.frames[i];
}
}
throw "Cannot find window for document. Doc title == "+doc.title;
}
Tabs.prototype.getTabWindow = function(index) {
return this.findWindow(this.getTab(index));
}
Tabs.prototype.__defineGetter__("activeTabWindow", function () {
return this.findWindow(this.activeTab);
})
Tabs.prototype.__defineGetter__("length", function () {
return this.controller.window.gBrowser.browsers.length;
})
Tabs.prototype.__defineGetter__("activeTabIndex", function() {
return this.controller.window.gBrowser.tabContainer.selectedIndex;
})
Tabs.prototype.selectTabIndex = function(i) {
this.controller.window.gBrowser.selectTabAtIndex(i);
}
function browserAdditions( controller ) {
controller.tabs = new Tabs(controller);
controller.waitForPageLoad = function(_document, timeout, interval) {
//if a user tries to do waitForPageLoad(2000), this will assign the interval the first arg
//which is most likely what they were expecting
if (typeof(_document) == "number"){
var timeout = _document;
}
//incase they pass null
if (_document == null){
_document = 0;
}
//if _document isn't a document object
if (typeof(_document) != "object") {
var _document = controller.tabs.activeTab;
}
if (interval == undefined) {
var interval = 100;
}
if (timeout == undefined) {
var timeout = 30000;
}
var win = _document.defaultView;
waitForEval("subject.documentLoaded == true", timeout, interval, win);
//Once the object is available it's somewhere between 1 and 3 seconds before the DOM
//Actually becomes available to us
sleep(1);
frame.events.pass({'function':'Controller.waitForPageLoad()'});
}
}
controllerAdditions = {
'Browser:Preferences':preferencesAdditions,
'navigator:browser' :browserAdditions,
}
var withs = {}; Components.utils.import('resource://mozmill/stdlib/withs.js', withs);
MozMillAsyncTest = function (timeout) {
if (timeout == undefined) {
this.timeout = 6000;
} else {
this.timeout = timeout;
}
this._done = false;
this._mozmillasynctest = true;
}
MozMillAsyncTest.prototype.run = function () {
for (i in this) {
if (withs.startsWith(i, 'test') && typeof(this[i]) == 'function') {
this[i]();
}
}
var r = waitForEval("subject._done == true", this.timeout, undefined, this);
if (r == true) {
return true;
} else {
throw "MozMillAsyncTest did not finish properly: timed out. Done is "+this._done
}
}
MozMillAsyncTest.prototype.finish = function () {
this._done = true;
}