summaryrefslogtreecommitdiffstats
path: root/devtools/client/commandline/test/helpers.js
blob: d365765a280e8442593f3b3e0c8e96cdf7ab8b71 (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
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
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
/*
 * Copyright 2012, Mozilla Foundation and contributors
 *
 * Licensed under the Apache License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

"use strict";

// A copy of this code exists in firefox mochitests. They should be kept
// in sync. Hence the exports synonym for non AMD contexts.
var { helpers, assert } = (function () {

  var helpers = {};

  var { require } = Cu.import("resource://devtools/shared/Loader.jsm", {});
  var { TargetFactory } = require("devtools/client/framework/target");
  var Services = require("Services");

  var assert = { ok: ok, is: is, log: info };
  var util = require("gcli/util/util");
  var cli = require("gcli/cli");
  var KeyEvent = require("gcli/util/util").KeyEvent;

  const { GcliFront } = require("devtools/shared/fronts/gcli");

/**
 * See notes in helpers.checkOptions()
 */
  var createDeveloperToolbarAutomator = function (toolbar) {
    var automator = {
      setInput: function (typed) {
        return toolbar.inputter.setInput(typed);
      },

      setCursor: function (cursor) {
        return toolbar.inputter.setCursor(cursor);
      },

      focus: function () {
        return toolbar.inputter.focus();
      },

      fakeKey: function (keyCode) {
        var fakeEvent = {
          keyCode: keyCode,
          preventDefault: function () { },
          timeStamp: new Date().getTime()
        };

        toolbar.inputter.onKeyDown(fakeEvent);

        if (keyCode === KeyEvent.DOM_VK_BACK_SPACE) {
          var input = toolbar.inputter.element;
          input.value = input.value.slice(0, -1);
        }

        return toolbar.inputter.handleKeyUp(fakeEvent);
      },

      getInputState: function () {
        return toolbar.inputter.getInputState();
      },

      getCompleterTemplateData: function () {
        return toolbar.completer._getCompleterTemplateData();
      },

      getErrorMessage: function () {
        return toolbar.tooltip.errorEle.textContent;
      }
    };

    Object.defineProperty(automator, "focusManager", {
      get: function () { return toolbar.focusManager; },
      enumerable: true
    });

    Object.defineProperty(automator, "field", {
      get: function () { return toolbar.tooltip.field; },
      enumerable: true
    });

    return automator;
  };

/**
 * Warning: For use with Firefox Mochitests only.
 *
 * Open a new tab at a URL and call a callback on load, and then tidy up when
 * the callback finishes.
 * The function will be passed a set of test options, and will usually return a
 * promise to indicate that the tab can be cleared up. (To be formal, we call
 * Promise.resolve() on the return value of the callback function)
 *
 * The options used by addTab include:
 * - chromeWindow: XUL window parent of created tab. a.k.a 'window' in mochitest
 * - tab: The new XUL tab element, as returned by gBrowser.addTab()
 * - target: The debug target as defined by the devtools framework
 * - browser: The XUL browser element for the given tab
 * - isFirefox: Always true. Allows test sharing with GCLI
 *
 * Normally addTab will create an options object containing the values as
 * described above. However these options can be customized by the third
 * 'options' parameter. This has the ability to customize the value of
 * chromeWindow or isFirefox, and to add new properties.
 *
 * @param url The URL for the new tab
 * @param callback The function to call on page load
 * @param options An optional set of options to customize the way the tests run
 */
  helpers.addTab = function (url, callback, options) {
    waitForExplicitFinish();

    options = options || {};
    options.chromeWindow = options.chromeWindow || window;
    options.isFirefox = true;

    var tabbrowser = options.chromeWindow.gBrowser;
    options.tab = tabbrowser.addTab();
    tabbrowser.selectedTab = options.tab;
    options.browser = tabbrowser.getBrowserForTab(options.tab);
    options.target = TargetFactory.forTab(options.tab);

    var loaded = helpers.listenOnce(options.browser, "load", true).then(function (ev) {
      var reply = callback.call(null, options);

      return Promise.resolve(reply).then(null, function (error) {
        ok(false, error);
      }).then(function () {
        tabbrowser.removeTab(options.tab);

        delete options.target;
        delete options.browser;
        delete options.tab;

        delete options.chromeWindow;
        delete options.isFirefox;
      });
    });

    options.browser.contentWindow.location = url;
    return loaded;
  };

/**
 * Open a new tab
 * @param url Address of the page to open
 * @param options Object to which we add properties describing the new tab. The
 * following properties are added:
 * - chromeWindow
 * - tab
 * - browser
 * - target
 * @return A promise which resolves to the options object when the 'load' event
 * happens on the new tab
 */
  helpers.openTab = function (url, options) {
    waitForExplicitFinish();

    options = options || {};
    options.chromeWindow = options.chromeWindow || window;
    options.isFirefox = true;

    var tabbrowser = options.chromeWindow.gBrowser;
    options.tab = tabbrowser.addTab();
    tabbrowser.selectedTab = options.tab;
    options.browser = tabbrowser.getBrowserForTab(options.tab);
    options.target = TargetFactory.forTab(options.tab);

    return helpers.navigate(url, options);
  };

/**
 * Undo the effects of |helpers.openTab|
 * @param options The options object passed to |helpers.openTab|
 * @return A promise resolved (with undefined) when the tab is closed
 */
  helpers.closeTab = function (options) {
    options.chromeWindow.gBrowser.removeTab(options.tab);

    delete options.target;
    delete options.browser;
    delete options.tab;

    delete options.chromeWindow;
    delete options.isFirefox;

    return Promise.resolve(undefined);
  };

/**
 * Open the developer toolbar in a tab
 * @param options Object to which we add properties describing the developer
 * toolbar. The following properties are added:
 * - automator
 * - requisition
 * @return A promise which resolves to the options object when the 'load' event
 * happens on the new tab
 */
  helpers.openToolbar = function (options) {
    options = options || {};
    options.chromeWindow = options.chromeWindow || window;

    return options.chromeWindow.DeveloperToolbar.show(true).then(function () {
      var toolbar = options.chromeWindow.DeveloperToolbar;
      options.automator = createDeveloperToolbarAutomator(toolbar);
      options.requisition = toolbar.requisition;
      return options;
    });
  };

/**
 * Navigate the current tab to a URL
 */
  helpers.navigate = Task.async(function* (url, options) {
    options = options || {};
    options.chromeWindow = options.chromeWindow || window;
    options.tab = options.tab || options.chromeWindow.gBrowser.selectedTab;

    var tabbrowser = options.chromeWindow.gBrowser;
    options.browser = tabbrowser.getBrowserForTab(options.tab);

    let onLoaded = BrowserTestUtils.browserLoaded(options.browser);
    options.browser.loadURI(url);
    yield onLoaded;

    return options;
  });

/**
 * Undo the effects of |helpers.openToolbar|
 * @param options The options object passed to |helpers.openToolbar|
 * @return A promise resolved (with undefined) when the toolbar is closed
 */
  helpers.closeToolbar = function (options) {
    return options.chromeWindow.DeveloperToolbar.hide().then(function () {
      delete options.automator;
      delete options.requisition;
    });
  };

/**
 * A helper to work with Task.spawn so you can do:
 *   return Task.spawn(realTestFunc).then(finish, helpers.handleError);
 */
  helpers.handleError = function (ex) {
    console.error(ex);
    ok(false, ex);
    finish();
  };

/**
 * A helper for calling addEventListener and then removeEventListener as soon
 * as the event is called, passing the results on as a promise
 * @param element The DOM element to listen on
 * @param event The name of the event to listen for
 * @param useCapture Should we use the capturing phase?
 * @return A promise resolved with the event object when the event first happens
 */
  helpers.listenOnce = function (element, event, useCapture) {
    return new Promise(function (resolve, reject) {
      var onEvent = function (ev) {
        element.removeEventListener(event, onEvent, useCapture);
        resolve(ev);
      };
      element.addEventListener(event, onEvent, useCapture);
    }.bind(this));
  };

/**
 * A wrapper for calling Services.obs.[add|remove]Observer using promises.
 * @param topic The topic parameter to Services.obs.addObserver
 * @param ownsWeak The ownsWeak parameter to Services.obs.addObserver with a
 * default value of false
 * @return a promise that resolves when the ObserverService first notifies us
 * of the topic. The value of the promise is the first parameter to the observer
 * function other parameters are dropped.
 */
  helpers.observeOnce = function (topic, ownsWeak = false) {
    return new Promise(function (resolve, reject) {
      let resolver = function (subject) {
        Services.obs.removeObserver(resolver, topic);
        resolve(subject);
      };
      Services.obs.addObserver(resolver, topic, ownsWeak);
    }.bind(this));
  };

/**
 * Takes a function that uses a callback as its last parameter, and returns a
 * new function that returns a promise instead
 */
  helpers.promiseify = function (functionWithLastParamCallback, scope) {
    return function () {
      let args = [].slice.call(arguments);
      return new Promise(resolve => {
        args.push((...results) => {
          resolve(results.length > 1 ? results : results[0]);
        });
        functionWithLastParamCallback.apply(scope, args);
      });
    };
  };

/**
 * Warning: For use with Firefox Mochitests only.
 *
 * As addTab, but that also opens the developer toolbar. In addition a new
 * 'automator' property is added to the options object which uses the
 * developer toolbar
 */
  helpers.addTabWithToolbar = function (url, callback, options) {
    return helpers.addTab(url, function (innerOptions) {
      var win = innerOptions.chromeWindow;

      return win.DeveloperToolbar.show(true).then(function () {
        var toolbar = win.DeveloperToolbar;
        innerOptions.automator = createDeveloperToolbarAutomator(toolbar);
        innerOptions.requisition = toolbar.requisition;

        var reply = callback.call(null, innerOptions);

        return Promise.resolve(reply).then(null, function (error) {
          ok(false, error);
          console.error(error);
        }).then(function () {
          win.DeveloperToolbar.hide().then(function () {
            delete innerOptions.automator;
          });
        });
      });
    }, options);
  };

/**
 * Warning: For use with Firefox Mochitests only.
 *
 * Run a set of test functions stored in the values of the 'exports' object
 * functions stored under setup/shutdown will be run at the start/end of the
 * sequence of tests.
 * A test will be considered finished when its return value is resolved.
 * @param options An object to be passed to the test functions
 * @param tests An object containing named test functions
 * @return a promise which will be resolved when all tests have been run and
 * their return values resolved
 */
  helpers.runTests = function (options, tests) {
    var testNames = Object.keys(tests).filter(function (test) {
      return test != "setup" && test != "shutdown";
    });

    var recover = function (error) {
      ok(false, error);
      console.error(error, error.stack);
    };

    info("SETUP");
    var setupDone = (tests.setup != null) ?
      Promise.resolve(tests.setup(options)) :
      Promise.resolve();

    var testDone = setupDone.then(function () {
      return util.promiseEach(testNames, function (testName) {
        info(testName);
        var action = tests[testName];

        if (typeof action === "function") {
          var reply = action.call(tests, options);
          return Promise.resolve(reply);
        }
        else if (Array.isArray(action)) {
          return helpers.audit(options, action);
        }

        return Promise.reject("test action '" + testName +
                            "' is not a function or helpers.audit() object");
      });
    }, recover);

    return testDone.then(function () {
      info("SHUTDOWN");
      return (tests.shutdown != null) ?
        Promise.resolve(tests.shutdown(options)) :
        Promise.resolve();
    }, recover);
  };

  const MOCK_COMMANDS_URI = "chrome://mochitests/content/browser/devtools/client/commandline/test/mockCommands.js";

  const defer = function () {
    const deferred = { };
    deferred.promise = new Promise(function (resolve, reject) {
      deferred.resolve = resolve;
      deferred.reject = reject;
    });
    return deferred;
  };

/**
 * This does several actions associated with running a GCLI test in mochitest
 * 1. Create a new tab containing basic markup for GCLI tests
 * 2. Open the developer toolbar
 * 3. Register the mock commands with the server process
 * 4. Wait for the proxy commands to be auto-regitstered with the client
 * 5. Register the mock converters with the client process
 * 6. Run all the tests
 * 7. Tear down all the setup
 */
  helpers.runTestModule = function (exports, name) {
    return Task.spawn(function* () {
      const uri = "data:text/html;charset=utf-8," +
                "<style>div{color:red;}</style>" +
                "<div id='gcli-root'>" + name + "</div>";

      const options = yield helpers.openTab(uri);
      options.isRemote = true;

      yield helpers.openToolbar(options);

      const system = options.requisition.system;

    // Register a one time listener with the local set of commands
      const addedDeferred = defer();
      const removedDeferred = defer();
      let state = "preAdd"; // Then 'postAdd' then 'postRemove'

      system.commands.onCommandsChange.add(function (ev) {
        if (system.commands.get("tsslow") != null) {
          if (state === "preAdd") {
            addedDeferred.resolve();
            state = "postAdd";
          }
        }
        else {
          if (state === "postAdd") {
            removedDeferred.resolve();
            state = "postRemove";
          }
        }
      });

    // Send a message to add the commands to the content process
      const front = yield GcliFront.create(options.target);
      yield front._testOnlyAddItemsByModule(MOCK_COMMANDS_URI);

    // This will cause the local set of commands to be updated with the
    // command proxies, wait for that to complete.
      yield addedDeferred.promise;

    // Now we need to add the converters to the local GCLI
      const converters = mockCommands.items.filter(item => item.item === "converter");
      system.addItems(converters);

    // Next run the tests
      yield helpers.runTests(options, exports);

    // Finally undo the mock commands and converters
      system.removeItems(converters);
      const removePromise = system.commands.onCommandsChange.once();
      yield front._testOnlyRemoveItemsByModule(MOCK_COMMANDS_URI);
      yield removedDeferred.promise;

    // And close everything down
      yield helpers.closeToolbar(options);
      yield helpers.closeTab(options);
    }).then(finish, helpers.handleError);
  };

/**
 * Ensure that the options object is setup correctly
 * options should contain an automator object that looks like this:
 * {
 *   getInputState: function() { ... },
 *   setCursor: function(cursor) { ... },
 *   getCompleterTemplateData: function() { ... },
 *   focus: function() { ... },
 *   getErrorMessage: function() { ... },
 *   fakeKey: function(keyCode) { ... },
 *   setInput: function(typed) { ... },
 *   focusManager: ...,
 *   field: ...,
 * }
 */
  function checkOptions(options) {
    if (options == null) {
      console.trace();
      throw new Error("Missing options object");
    }
    if (options.requisition == null) {
      console.trace();
      throw new Error("options.requisition == null");
    }
  }

/**
 * Various functions to return the actual state of the command line
 */
  helpers._actual = {
    input: function (options) {
      return options.automator.getInputState().typed;
    },

    hints: function (options) {
      return options.automator.getCompleterTemplateData().then(function (data) {
        var emptyParams = data.emptyParameters.join("");
        return (data.directTabText + emptyParams + data.arrowTabText)
                .replace(/\u00a0/g, " ")
                .replace(/\u21E5/, "->")
                .replace(/ $/, "");
      });
    },

    markup: function (options) {
      var cursor = helpers._actual.cursor(options);
      var statusMarkup = options.requisition.getInputStatusMarkup(cursor);
      return statusMarkup.map(function (s) {
        return new Array(s.string.length + 1).join(s.status.toString()[0]);
      }).join("");
    },

    cursor: function (options) {
      return options.automator.getInputState().cursor.start;
    },

    current: function (options) {
      var cursor = helpers._actual.cursor(options);
      return options.requisition.getAssignmentAt(cursor).param.name;
    },

    status: function (options) {
      return options.requisition.status.toString();
    },

    predictions: function (options) {
      var cursor = helpers._actual.cursor(options);
      var assignment = options.requisition.getAssignmentAt(cursor);
      var context = options.requisition.executionContext;
      return assignment.getPredictions(context).then(function (predictions) {
        return predictions.map(function (prediction) {
          return prediction.name;
        });
      });
    },

    unassigned: function (options) {
      return options.requisition._unassigned.map(function (assignment) {
        return assignment.arg.toString();
      }.bind(this));
    },

    outputState: function (options) {
      var outputData = options.automator.focusManager._shouldShowOutput();
      return outputData.visible + ":" + outputData.reason;
    },

    tooltipState: function (options) {
      var tooltipData = options.automator.focusManager._shouldShowTooltip();
      return tooltipData.visible + ":" + tooltipData.reason;
    },

    options: function (options) {
      if (options.automator.field.menu == null) {
        return [];
      }
      return options.automator.field.menu.items.map(function (item) {
        return item.name.textContent ? item.name.textContent : item.name;
      });
    },

    message: function (options) {
      return options.automator.getErrorMessage();
    }
  };

  function shouldOutputUnquoted(value) {
    var type = typeof value;
    return value == null || type === "boolean" || type === "number";
  }

  function outputArray(array) {
    return (array.length === 0) ?
      "[ ]" :
      "[ '" + array.join("', '") + "' ]";
  }

  helpers._createDebugCheck = function (options) {
    checkOptions(options);
    var requisition = options.requisition;
    var command = requisition.commandAssignment.value;
    var cursor = helpers._actual.cursor(options);
    var input = helpers._actual.input(options);
    var padding = new Array(input.length + 1).join(" ");

    var hintsPromise = helpers._actual.hints(options);
    var predictionsPromise = helpers._actual.predictions(options);

    return Promise.all([ hintsPromise, predictionsPromise ]).then(function (values) {
      var hints = values[0];
      var predictions = values[1];
      var output = "";

      output += "return helpers.audit(options, [\n";
      output += "  {\n";

      if (cursor === input.length) {
        output += "    setup:    '" + input + "',\n";
      }
      else {
        output += "    name: '" + input + " (cursor=" + cursor + ")',\n";
        output += "    setup: function() {\n";
        output += "      return helpers.setInput(options, '" + input + "', " + cursor + ");\n";
        output += "    },\n";
      }

      output += "    check: {\n";

      output += "      input:  '" + input + "',\n";
      output += "      hints:  " + padding + "'" + hints + "',\n";
      output += "      markup: '" + helpers._actual.markup(options) + "',\n";
      output += "      cursor: " + cursor + ",\n";
      output += "      current: '" + helpers._actual.current(options) + "',\n";
      output += "      status: '" + helpers._actual.status(options) + "',\n";
      output += "      options: " + outputArray(helpers._actual.options(options)) + ",\n";
      output += "      message: '" + helpers._actual.message(options) + "',\n";
      output += "      predictions: " + outputArray(predictions) + ",\n";
      output += "      unassigned: " + outputArray(requisition._unassigned) + ",\n";
      output += "      outputState: '" + helpers._actual.outputState(options) + "',\n";
      output += "      tooltipState: '" + helpers._actual.tooltipState(options) + "'" +
              (command ? "," : "") + "\n";

      if (command) {
        output += "      args: {\n";
        output += "        command: { name: '" + command.name + "' },\n";

        requisition.getAssignments().forEach(function (assignment) {
          output += "        " + assignment.param.name + ": { ";

          if (typeof assignment.value === "string") {
            output += "value: '" + assignment.value + "', ";
          }
          else if (shouldOutputUnquoted(assignment.value)) {
            output += "value: " + assignment.value + ", ";
          }
        else {
            output += "/*value:" + assignment.value + ",*/ ";
          }

          output += "arg: '" + assignment.arg + "', ";
          output += "status: '" + assignment.getStatus().toString() + "', ";
          output += "message: '" + assignment.message + "'";
          output += " },\n";
        });

        output += "      }\n";
      }

      output += "    },\n";
      output += "    exec: {\n";
      output += "      output: '',\n";
      output += "      type: 'string',\n";
      output += "      error: false\n";
      output += "    }\n";
      output += "  }\n";
      output += "]);";

      return output;
    }.bind(this), util.errorHandler);
  };

/**
 * Simulate focusing the input field
 */
  helpers.focusInput = function (options) {
    checkOptions(options);
    options.automator.focus();
  };

/**
 * Simulate pressing TAB in the input field
 */
  helpers.pressTab = function (options) {
    checkOptions(options);
    return helpers.pressKey(options, KeyEvent.DOM_VK_TAB);
  };

/**
 * Simulate pressing RETURN in the input field
 */
  helpers.pressReturn = function (options) {
    checkOptions(options);
    return helpers.pressKey(options, KeyEvent.DOM_VK_RETURN);
  };

/**
 * Simulate pressing a key by keyCode in the input field
 */
  helpers.pressKey = function (options, keyCode) {
    checkOptions(options);
    return options.automator.fakeKey(keyCode);
  };

/**
 * A list of special key presses and how to to them, for the benefit of
 * helpers.setInput
 */
  var ACTIONS = {
    "<TAB>": function (options) {
      return helpers.pressTab(options);
    },
    "<RETURN>": function (options) {
      return helpers.pressReturn(options);
    },
    "<UP>": function (options) {
      return helpers.pressKey(options, KeyEvent.DOM_VK_UP);
    },
    "<DOWN>": function (options) {
      return helpers.pressKey(options, KeyEvent.DOM_VK_DOWN);
    },
    "<BACKSPACE>": function (options) {
      return helpers.pressKey(options, KeyEvent.DOM_VK_BACK_SPACE);
    }
  };

/**
 * Used in helpers.setInput to cut an input string like 'blah<TAB>foo<UP>' into
 * an array like [ 'blah', '<TAB>', 'foo', '<UP>' ].
 * When using this RegExp, you also need to filter out the blank strings.
 */
  var CHUNKER = /([^<]*)(<[A-Z]+>)/;

/**
 * Alter the input to <code>typed</code> optionally leaving the cursor at
 * <code>cursor</code>.
 * @return A promise of the number of key-presses to respond
 */
  helpers.setInput = function (options, typed, cursor) {
    checkOptions(options);
    var inputPromise;
    var automator = options.automator;
  // We try to measure average keypress time, but setInput can simulate
  // several, so we try to keep track of how many
    var chunkLen = 1;

  // The easy case is a simple string without things like <TAB>
    if (typed.indexOf("<") === -1) {
      inputPromise = automator.setInput(typed);
    }
    else {
    // Cut the input up into input strings separated by '<KEY>' tokens. The
    // CHUNKS RegExp leaves blanks so we filter them out.
      var chunks = typed.split(CHUNKER).filter(function (s) {
        return s !== "";
      });
      chunkLen = chunks.length + 1;

    // We're working on this in chunks so first clear the input
      inputPromise = automator.setInput("").then(function () {
        return util.promiseEach(chunks, function (chunk) {
          if (chunk.charAt(0) === "<") {
            var action = ACTIONS[chunk];
            if (typeof action !== "function") {
              console.error("Known actions: " + Object.keys(ACTIONS).join());
              throw new Error('Key action not found "' + chunk + '"');
            }
            return action(options);
          }
          else {
            return automator.setInput(automator.getInputState().typed + chunk);
          }
        });
      });
    }

    return inputPromise.then(function () {
      if (cursor != null) {
        automator.setCursor({ start: cursor, end: cursor });
      }

      if (automator.focusManager) {
        automator.focusManager.onInputChange();
      }

    // Firefox testing is noisy and distant, so logging helps
      if (options.isFirefox) {
        var cursorStr = (cursor == null ? "" : ", " + cursor);
        log('setInput("' + typed + '"' + cursorStr + ")");
      }

      return chunkLen;
    });
  };

/**
 * Helper for helpers.audit() to ensure that all the 'check' properties match.
 * See helpers.audit for more information.
 * @param name The name to use in error messages
 * @param checks See helpers.audit for a list of available checks
 * @return A promise which resolves to undefined when the checks are complete
 */
  helpers._check = function (options, name, checks) {
  // A test method to check that all args are assigned in some way
    var requisition = options.requisition;
    requisition._args.forEach(function (arg) {
      if (arg.assignment == null) {
        assert.ok(false, "No assignment for " + arg);
      }
    });

    if (checks == null) {
      return Promise.resolve();
    }

    var outstanding = [];
    var suffix = name ? " (for '" + name + "')" : "";

    if (!options.isNode && "input" in checks) {
      assert.is(helpers._actual.input(options), checks.input, "input" + suffix);
    }

    if (!options.isNode && "cursor" in checks) {
      assert.is(helpers._actual.cursor(options), checks.cursor, "cursor" + suffix);
    }

    if (!options.isNode && "current" in checks) {
      assert.is(helpers._actual.current(options), checks.current, "current" + suffix);
    }

    if ("status" in checks) {
      assert.is(helpers._actual.status(options), checks.status, "status" + suffix);
    }

    if (!options.isNode && "markup" in checks) {
      assert.is(helpers._actual.markup(options), checks.markup, "markup" + suffix);
    }

    if (!options.isNode && "hints" in checks) {
      var hintCheck = function (actualHints) {
        assert.is(actualHints, checks.hints, "hints" + suffix);
      };
      outstanding.push(helpers._actual.hints(options).then(hintCheck));
    }

    if (!options.isNode && "predictions" in checks) {
      var predictionsCheck = function (actualPredictions) {
        helpers.arrayIs(actualPredictions,
                       checks.predictions,
                       "predictions" + suffix);
      };
      outstanding.push(helpers._actual.predictions(options).then(predictionsCheck));
    }

    if (!options.isNode && "predictionsContains" in checks) {
      var containsCheck = function (actualPredictions) {
        checks.predictionsContains.forEach(function (prediction) {
          var index = actualPredictions.indexOf(prediction);
          assert.ok(index !== -1,
                  "predictionsContains:" + prediction + suffix);
          if (index === -1) {
            log("Actual predictions (" + actualPredictions.length + "): " +
              actualPredictions.join(", "));
          }
        });
      };
      outstanding.push(helpers._actual.predictions(options).then(containsCheck));
    }

    if ("unassigned" in checks) {
      helpers.arrayIs(helpers._actual.unassigned(options),
                     checks.unassigned,
                     "unassigned" + suffix);
    }

  /* TODO: Fix this
  if (!options.isNode && 'tooltipState' in checks) {
    assert.is(helpers._actual.tooltipState(options),
              checks.tooltipState,
              'tooltipState' + suffix);
  }
  */

    if (!options.isNode && "outputState" in checks) {
      assert.is(helpers._actual.outputState(options),
              checks.outputState,
              "outputState" + suffix);
    }

    if (!options.isNode && "options" in checks) {
      helpers.arrayIs(helpers._actual.options(options),
                     checks.options,
                     "options" + suffix);
    }

    if (!options.isNode && "error" in checks) {
      assert.is(helpers._actual.message(options), checks.error, "error" + suffix);
    }

    if (checks.args != null) {
      Object.keys(checks.args).forEach(function (paramName) {
        var check = checks.args[paramName];

      // We allow an 'argument' called 'command' to be the command itself, but
      // what if the command has a parameter called 'command' (for example, an
      // 'exec' command)? We default to using the parameter because checking
      // the command value is less useful
        var assignment = requisition.getAssignment(paramName);
        if (assignment == null && paramName === "command") {
          assignment = requisition.commandAssignment;
        }

        if (assignment == null) {
          assert.ok(false, "Unknown arg: " + paramName + suffix);
          return;
        }

        if ("value" in check) {
          if (typeof check.value === "function") {
            try {
              check.value(assignment.value);
            }
          catch (ex) {
            assert.ok(false, "" + ex);
          }
          }
          else {
            assert.is(assignment.value,
                    check.value,
                    "arg." + paramName + ".value" + suffix);
          }
        }

        if ("name" in check) {
          assert.is(assignment.value.name,
                  check.name,
                  "arg." + paramName + ".name" + suffix);
        }

        if ("type" in check) {
          assert.is(assignment.arg.type,
                  check.type,
                  "arg." + paramName + ".type" + suffix);
        }

        if ("arg" in check) {
          assert.is(assignment.arg.toString(),
                  check.arg,
                  "arg." + paramName + ".arg" + suffix);
        }

        if ("status" in check) {
          assert.is(assignment.getStatus().toString(),
                  check.status,
                  "arg." + paramName + ".status" + suffix);
        }

        if (!options.isNode && "message" in check) {
          if (typeof check.message.test === "function") {
            assert.ok(check.message.test(assignment.message),
                    "arg." + paramName + ".message" + suffix);
          }
          else {
            assert.is(assignment.message,
                    check.message,
                    "arg." + paramName + ".message" + suffix);
          }
        }
      });
    }

    return Promise.all(outstanding).then(function () {
    // Ensure the promise resolves to nothing
      return undefined;
    });
  };

/**
 * Helper for helpers.audit() to ensure that all the 'exec' properties work.
 * See helpers.audit for more information.
 * @param name The name to use in error messages
 * @param expected See helpers.audit for a list of available exec checks
 * @return A promise which resolves to undefined when the checks are complete
 */
  helpers._exec = function (options, name, expected) {
    var requisition = options.requisition;
    if (expected == null) {
      return Promise.resolve({});
    }

    var origLogErrors = cli.logErrors;
    if (expected.error) {
      cli.logErrors = false;
    }

    try {
      return requisition.exec({ hidden: true }).then(function (output) {
        if ("type" in expected) {
          assert.is(output.type,
                  expected.type,
                  "output.type for: " + name);
        }

        if ("error" in expected) {
          assert.is(output.error,
                  expected.error,
                  "output.error for: " + name);
        }

        if (!("output" in expected)) {
          return { output: output };
        }

        var context = requisition.conversionContext;
        var convertPromise;
        if (options.isNode) {
          convertPromise = output.convert("string", context);
        }
        else {
          convertPromise = output.convert("dom", context).then(function (node) {
            return (node == null) ? "" : node.textContent.trim();
          });
        }

        return convertPromise.then(function (textOutput) {
          var doTest = function (match, against) {
          // Only log the real textContent if the test fails
            if (against.match(match) != null) {
              assert.ok(true, "html output for '" + name + "' " +
                            "should match /" + (match.source || match) + "/");
            } else {
              assert.ok(false, "html output for '" + name + "' " +
                             "should match /" + (match.source || match) + "/. " +
                             'Actual textContent: "' + against + '"');
            }
          };

          if (typeof expected.output === "string") {
            assert.is(textOutput,
                    expected.output,
                    "html output for " + name);
          }
          else if (Array.isArray(expected.output)) {
            expected.output.forEach(function (match) {
              doTest(match, textOutput);
            });
          }
        else {
            doTest(expected.output, textOutput);
          }

          if (expected.error) {
            cli.logErrors = origLogErrors;
          }
          return { output: output, text: textOutput };
        });
      }.bind(this)).then(function (data) {
        if (expected.error) {
          cli.logErrors = origLogErrors;
        }

        return data;
      });
    }
  catch (ex) {
    assert.ok(false, "Failure executing '" + name + "': " + ex);
    util.errorHandler(ex);

    if (expected.error) {
      cli.logErrors = origLogErrors;
    }
    return Promise.resolve({});
  }
  };

/**
 * Helper to setup the test
 */
  helpers._setup = function (options, name, audit) {
    if (typeof audit.setup === "string") {
      return helpers.setInput(options, audit.setup);
    }

    if (typeof audit.setup === "function") {
      return Promise.resolve(audit.setup.call(audit));
    }

    return Promise.reject("'setup' property must be a string or a function. Is " + audit.setup);
  };

/**
 * Helper to shutdown the test
 */
  helpers._post = function (name, audit, data) {
    if (typeof audit.post === "function") {
      return Promise.resolve(audit.post.call(audit, data.output, data.text));
    }
    return Promise.resolve(audit.post);
  };

/*
 * We do some basic response time stats so we can see if we're getting slow
 */
  var totalResponseTime = 0;
  var averageOver = 0;
  var maxResponseTime = 0;
  var maxResponseCulprit;
  var start;

/**
 * Restart the stats collection process
 */
  helpers.resetResponseTimes = function () {
    start = new Date().getTime();
    totalResponseTime = 0;
    averageOver = 0;
    maxResponseTime = 0;
    maxResponseCulprit = undefined;
  };

/**
 * Expose an average response time in milliseconds
 */
  Object.defineProperty(helpers, "averageResponseTime", {
    get: function () {
      return averageOver === 0 ?
        undefined :
        Math.round(100 * totalResponseTime / averageOver) / 100;
    },
    enumerable: true
  });

/**
 * Expose a maximum response time in milliseconds
 */
  Object.defineProperty(helpers, "maxResponseTime", {
    get: function () { return Math.round(maxResponseTime * 100) / 100; },
    enumerable: true
  });

/**
 * Expose the name of the test that provided the maximum response time
 */
  Object.defineProperty(helpers, "maxResponseCulprit", {
    get: function () { return maxResponseCulprit; },
    enumerable: true
  });

/**
 * Quick summary of the times
 */
  Object.defineProperty(helpers, "timingSummary", {
    get: function () {
      var elapsed = (new Date().getTime() - start) / 1000;
      return "Total " + elapsed + "s, " +
           "ave response " + helpers.averageResponseTime + "ms, " +
           "max response " + helpers.maxResponseTime + "ms " +
           "from '" + helpers.maxResponseCulprit + "'";
    },
    enumerable: true
  });

/**
 * A way of turning a set of tests into something more declarative, this helps
 * to allow tests to be asynchronous.
 * @param audits An array of objects each of which contains:
 * - setup: string/function to be called to set the test up.
 *     If audit is a string then it is passed to helpers.setInput().
 *     If audit is a function then it is executed. The tests will wait while
 *     tests that return promises complete.
 * - name: For debugging purposes. If name is undefined, and 'setup'
 *     is a string then the setup value will be used automatically
 * - skipIf: A function to define if the test should be skipped. Useful for
 *     excluding tests from certain environments (e.g. nodom, firefox, etc).
 *     The name of the test will be used in log messages noting the skip
 *     See helpers.reason for pre-defined skip functions. The skip function must
 *     be synchronous, and will be passed the test options object.
 * - skipRemainingIf: A function to skip all the remaining audits in this set.
 *     See skipIf for details of how skip functions work.
 * - check: Check data. Available checks:
 *   - input: The text displayed in the input field
 *   - cursor: The position of the start of the cursor
 *   - status: One of 'VALID', 'ERROR', 'INCOMPLETE'
 *   - hints: The hint text, i.e. a concatenation of the directTabText, the
 *       emptyParameters and the arrowTabText. The text as inserted into the UI
 *       will include NBSP and Unicode RARR characters, these should be
 *       represented using normal space and '->' for the arrow
 *   - markup: What state should the error markup be in. e.g. 'VVVIIIEEE'
 *   - args: Maps of checks to make against the arguments:
 *     - value: i.e. assignment.value (which ignores defaultValue)
 *     - type: Argument/BlankArgument/MergedArgument/etc i.e. what's assigned
 *             Care should be taken with this since it's something of an
 *             implementation detail
 *     - arg: The toString value of the argument
 *     - status: i.e. assignment.getStatus
 *     - message: i.e. assignment.message
 *     - name: For commands - checks assignment.value.name
 * - exec: Object to indicate we should execute the command and check the
 *     results. Available checks:
 *   - output: A string, RegExp or array of RegExps to compare with the output
 *       If typeof output is a string then the output should be exactly equal
 *       to the given string. If the type of output is a RegExp or array of
 *       RegExps then the output should match all RegExps
 *   - error: If true, then it is expected that this command will fail (that
 *       is, return a rejected promise or throw an exception)
 *   - type: A string documenting the expected type of the return value
 * - post: Function to be called after the checks have been run, which will be
 *     passed 2 parameters: the first being output data (with type, data, and
 *     error properties), and the second being the converted text version of
 *     the output data
 */
  helpers.audit = function (options, audits) {
    checkOptions(options);
    var skipReason = null;
    return util.promiseEach(audits, function (audit) {
      var name = audit.name;
      if (name == null && typeof audit.setup === "string") {
        name = audit.setup;
      }

      if (assert.testLogging) {
        log("- START '" + name + "' in " + assert.currentTest);
      }

      if (audit.skipRemainingIf) {
        var skipRemainingIf = (typeof audit.skipRemainingIf === "function") ?
          audit.skipRemainingIf(options) :
          !!audit.skipRemainingIf;
        if (skipRemainingIf) {
          skipReason = audit.skipRemainingIf.name ?
            "due to " + audit.skipRemainingIf.name :
            "";
          assert.log("Skipped " + name + " " + skipReason);

        // Tests need at least one pass, fail or todo. Create a dummy pass
          assert.ok(true, "Each test requires at least one pass, fail or todo");

          return Promise.resolve(undefined);
        }
      }

      if (audit.skipIf) {
        var skip = (typeof audit.skipIf === "function") ?
          audit.skipIf(options) :
          !!audit.skipIf;
        if (skip) {
          var reason = audit.skipIf.name ? "due to " + audit.skipIf.name : "";
          assert.log("Skipped " + name + " " + reason);
          return Promise.resolve(undefined);
        }
      }

      if (skipReason != null) {
        assert.log("Skipped " + name + " " + skipReason);
        return Promise.resolve(undefined);
      }

      var start = new Date().getTime();

      var setupDone = helpers._setup(options, name, audit);
      return setupDone.then(function (chunkLen) {
        if (typeof chunkLen !== "number") {
          chunkLen = 1;
        }

      // Nasty hack to allow us to auto-skip tests where we're actually testing
      // a key-sequence (i.e. targeting terminal.js) when there is no terminal
        if (chunkLen === -1) {
          assert.log("Skipped " + name + " " + skipReason);
          return Promise.resolve(undefined);
        }

        if (assert.currentTest) {
          var responseTime = (new Date().getTime() - start) / chunkLen;
          totalResponseTime += responseTime;
          if (responseTime > maxResponseTime) {
            maxResponseTime = responseTime;
            maxResponseCulprit = assert.currentTest + "/" + name;
          }
          averageOver++;
        }

        var checkDone = helpers._check(options, name, audit.check);
        return checkDone.then(function () {
          var execDone = helpers._exec(options, name, audit.exec);
          return execDone.then(function (data) {
            return helpers._post(name, audit, data).then(function () {
              if (assert.testLogging) {
                log("- END '" + name + "' in " + assert.currentTest);
              }
            });
          });
        });
      });
    }).then(function () {
      return options.automator.setInput("");
    }, function (ex) {
      options.automator.setInput("");
      throw ex;
    });
  };

/**
 * Compare 2 arrays.
 */
  helpers.arrayIs = function (actual, expected, message) {
    assert.ok(Array.isArray(actual), "actual is not an array: " + message);
    assert.ok(Array.isArray(expected), "expected is not an array: " + message);

    if (!Array.isArray(actual) || !Array.isArray(expected)) {
      return;
    }

    assert.is(actual.length, expected.length, "array length: " + message);

    for (var i = 0; i < actual.length && i < expected.length; i++) {
      assert.is(actual[i], expected[i], "member[" + i + "]: " + message);
    }
  };

/**
 * A quick helper to log to the correct place
 */
  function log(message) {
    if (typeof info === "function") {
      info(message);
    }
    else {
      console.log(message);
    }
  }

  return { helpers: helpers, assert: assert };
})();