f43443857b3d971e701eeebb1d2b6cdde88cd791
[platform/upstream/nodejs.git] / test / parallel / test-readline-set-raw-mode.js
1 // Copyright Joyent, Inc. and other Node contributors.
2 //
3 // Permission is hereby granted, free of charge, to any person obtaining a
4 // copy of this software and associated documentation files (the
5 // "Software"), to deal in the Software without restriction, including
6 // without limitation the rights to use, copy, modify, merge, publish,
7 // distribute, sublicense, and/or sell copies of the Software, and to permit
8 // persons to whom the Software is furnished to do so, subject to the
9 // following conditions:
10 //
11 // The above copyright notice and this permission notice shall be included
12 // in all copies or substantial portions of the Software.
13 //
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17 // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18 // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19 // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20 // USE OR OTHER DEALINGS IN THE SOFTWARE.
21
22
23
24 var assert = require('assert');
25 var readline = require('readline');
26 var Stream = require('stream');
27
28 var stream = new Stream();
29 var expectedRawMode = true;
30 var rawModeCalled = false;
31 var resumeCalled = false;
32 var pauseCalled = false;
33
34 stream.setRawMode = function(mode) {
35   rawModeCalled = true;
36   assert.equal(mode, expectedRawMode);
37 };
38 stream.resume = function() {
39   resumeCalled = true;
40 };
41 stream.pause = function() {
42   pauseCalled = true;
43 };
44
45 // when the "readline" starts in "terminal" mode,
46 // then setRawMode(true) should be called
47 var rli = readline.createInterface({
48   input: stream,
49   output: stream,
50   terminal: true
51 });
52 assert(rli.terminal);
53 assert(rawModeCalled);
54 assert(resumeCalled);
55 assert(!pauseCalled);
56
57
58 // pause() should call *not* call setRawMode()
59 rawModeCalled = false;
60 resumeCalled = false;
61 pauseCalled = false;
62 rli.pause();
63 assert(!rawModeCalled);
64 assert(!resumeCalled);
65 assert(pauseCalled);
66
67
68 // resume() should *not* call setRawMode()
69 rawModeCalled = false;
70 resumeCalled = false;
71 pauseCalled = false;
72 rli.resume();
73 assert(!rawModeCalled);
74 assert(resumeCalled);
75 assert(!pauseCalled);
76
77
78 // close() should call setRawMode(false)
79 expectedRawMode = false;
80 rawModeCalled = false;
81 resumeCalled = false;
82 pauseCalled = false;
83 rli.close();
84 assert(rawModeCalled);
85 assert(!resumeCalled);
86 assert(pauseCalled);
87
88 assert.deepEqual(stream.listeners('keypress'), []);
89 // one data listener for the keypress events.
90 assert.equal(stream.listeners('data').length, 1);