forked from libapps/libapps-mirror
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhterm_terminal_io_tests.js
More file actions
183 lines (154 loc) · 4.28 KB
/
hterm_terminal_io_tests.js
File metadata and controls
183 lines (154 loc) · 4.28 KB
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
// Copyright 2017 The ChromiumOS Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
/**
* @fileoverview hterm.Terminal.IO unit tests.
*/
import {hterm} from '../index.js';
/**
* Simple mock Terminal object for an IO object's needs.
*
* These tests focus on the IO object itself rather than integration with the
*
* @constructor
* @extends {hterm.Terminal}
*/
const MockTerminalForIO = function() {
this.overlayVisible = false;
this.showCount = 0;
this.overlayMessage = '';
this.profileName = '';
this.buffer = '';
// This comes last.
this.io = new hterm.Terminal.IO(this);
};
/**
* @param {string|!Node} message
* @param {?number=} timeout
* @override
*/
MockTerminalForIO.prototype.showOverlay = function(message, timeout) {
this.showCount++;
// For timeouts, we'll track the final state (i.e. after the timeout).
this.overlayVisible = (timeout === null);
this.overlayMessage = message;
};
/** @override */
MockTerminalForIO.prototype.hideOverlay = function() {
this.overlayVisible = false;
};
/**
* @param {string} profileName
* @override
*/
MockTerminalForIO.prototype.setProfile = function(profileName) {
this.profileName = profileName;
};
/**
* @param {string} str
* @override
*/
MockTerminalForIO.prototype.interpret = function(str) {
this.buffer += str;
};
/**
* Create a new IO object for every test.
*
* Called before each test case in this suite.
*/
beforeEach(function() {
this.mockTerm = new MockTerminalForIO();
this.io = this.mockTerm.io.push();
});
/**
* Check print functionality.
*/
it('print', function() {
this.io.print('');
assert.equal('', this.mockTerm.buffer);
this.io.print('a');
assert.equal('a', this.mockTerm.buffer);
this.io.print('');
assert.equal('a', this.mockTerm.buffer);
this.io.print('bc');
assert.equal('abc', this.mockTerm.buffer);
});
/**
* Check println functionality.
*/
it('println', function() {
this.io.println('a');
assert.equal('a\r\n', this.mockTerm.buffer);
this.io.print('bc');
assert.equal('a\r\nbc', this.mockTerm.buffer);
this.io.println('');
assert.equal('a\r\nbc\r\n', this.mockTerm.buffer);
});
/**
* Verify pushing/popping works.
*/
it('push-pop', function() {
const io1 = this.io.push();
io1.print('Hello');
assert.equal('Hello', this.mockTerm.buffer);
const io2 = io1.push();
io2.print('World');
assert.equal('HelloWorld', this.mockTerm.buffer);
io2.pop();
io1.print('ItsMe');
assert.equal('HelloWorldItsMe', this.mockTerm.buffer);
io1.pop();
this.io.print('Bye');
assert.equal('HelloWorldItsMeBye', this.mockTerm.buffer);
});
/**
* Verify profile selection.
*/
it('profile-selection', function() {
assert.equal('', this.mockTerm.profileName);
this.io.setTerminalProfile('foo');
assert.equal('foo', this.mockTerm.profileName);
});
/**
* Check overlay display.
*/
it('overlay', function() {
// Start with default timeout.
this.io.showOverlay('msg');
assert.equal(1, this.mockTerm.showCount);
assert.isFalse(this.mockTerm.overlayVisible);
assert.equal('msg', this.mockTerm.overlayMessage);
// A short message to "hide" it.
this.io.showOverlay('', 1);
assert.equal(2, this.mockTerm.showCount);
assert.isFalse(this.mockTerm.overlayVisible);
assert.equal('', this.mockTerm.overlayMessage);
// Keep the overlay up forever.
this.io.showOverlay('hi', null);
assert.equal(3, this.mockTerm.showCount);
assert.isTrue(this.mockTerm.overlayVisible);
assert.equal('hi', this.mockTerm.overlayMessage);
// Hide it immediately.
this.io.hideOverlay();
assert.equal(3, this.mockTerm.showCount);
assert.isFalse(this.mockTerm.overlayVisible);
assert.equal('hi', this.mockTerm.overlayMessage);
});
/**
* Check background IO objects.
*/
it('buffer-background', function() {
// Create a new foreground IO and show some stuff.
const io = this.io.push();
io.print('Fore');
assert.equal('Fore', this.mockTerm.buffer);
// Try to display something with the background IO.
this.io.print('Back');
assert.equal('Fore', this.mockTerm.buffer);
// Unload the foreground IO at which point the background should flush.
io.pop();
assert.equal('ForeBack', this.mockTerm.buffer);
// And we should resume OK.
this.io.print('Done');
assert.equal('ForeBackDone', this.mockTerm.buffer);
});