OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 /** |
| 6 * @fileoverview Unit tests for the hterm.Screen class. |
| 7 */ |
| 8 hterm.Screen.Tests = new TestManager.Suite('hterm.Screen.Tests'); |
| 9 |
| 10 /** |
| 11 * Clear out the current document and create a new hterm.Screen object for |
| 12 * testing. |
| 13 * |
| 14 * Called before each test case in this suite. |
| 15 */ |
| 16 hterm.Screen.Tests.prototype.preamble = function(result, cx) { |
| 17 cx.window.document.body.innerHTML = ''; |
| 18 cx.window.screen = this.screen = new hterm.Screen(); |
| 19 cx.window.screen.setColumnCount(80); |
| 20 }; |
| 21 |
| 22 /** |
| 23 * Test the push and pop functionality of the hterm.Screen. |
| 24 */ |
| 25 hterm.Screen.Tests.addTest('push-pop', function(result, cx) { |
| 26 // Push one at a time. |
| 27 var ary = []; |
| 28 for (var i = 0; i < 10; i++) { |
| 29 ary[i] = document.createElement('div'); |
| 30 ary[i].textContent = i; |
| 31 this.screen.pushRow(ary[i]); |
| 32 } |
| 33 |
| 34 result.assertEQ(ary.length, this.screen.getHeight()); |
| 35 |
| 36 // Pop one at a time. |
| 37 for (var i = ary.length - 1; i >= 0; i--) { |
| 38 result.assertEQ(ary[i], this.screen.popRow(), 'i:' + i); |
| 39 } |
| 40 |
| 41 // Bulk push. |
| 42 this.screen.pushRows(ary); |
| 43 result.assertEQ(ary.length, this.screen.rowsArray.length); |
| 44 |
| 45 // Bulk pop. |
| 46 var popary = this.screen.popRows(ary.length); |
| 47 |
| 48 result.assertEQ(ary.length, popary.length); |
| 49 |
| 50 for (var i = ary.length - 1; i >= 0; i--) { |
| 51 result.assertEQ(ary[i], popary[i], 'i:' + i); |
| 52 } |
| 53 |
| 54 // Reset, then partial bulk pop. |
| 55 this.screen.pushRows(ary); |
| 56 result.assertEQ(ary.length, this.screen.rowsArray.length); |
| 57 |
| 58 var popary = this.screen.popRows(5); |
| 59 for (var i = 0; i < 5; i++) { |
| 60 result.assertEQ(ary[i + 5], popary[i], 'i:' + i); |
| 61 } |
| 62 |
| 63 result.pass(); |
| 64 }); |
| 65 |
| 66 /** |
| 67 * Test the unshift and shift functionality of the hterm.Screen. |
| 68 */ |
| 69 hterm.Screen.Tests.addTest('unshift-shift', function(result, cx) { |
| 70 // Unshift one at a time. |
| 71 var ary = []; |
| 72 for (var i = 0; i < 10; i++) { |
| 73 ary[i] = document.createElement('div'); |
| 74 ary[i].textContent = i; |
| 75 this.screen.unshiftRow(ary[i]); |
| 76 } |
| 77 |
| 78 result.assertEQ(ary.length, this.screen.rowsArray.length); |
| 79 |
| 80 // Shift one at a time. |
| 81 for (var i = ary.length - 1; i >= 0; i--) { |
| 82 result.assertEQ(ary[i], this.screen.shiftRow(), 'i:' + i); |
| 83 } |
| 84 |
| 85 // Bulk unshift. |
| 86 this.screen.unshiftRows(ary); |
| 87 result.assertEQ(ary.length, this.screen.rowsArray.length); |
| 88 |
| 89 // Bulk shift. |
| 90 var shiftary = this.screen.shiftRows(ary.length); |
| 91 |
| 92 result.assertEQ(ary.length, shiftary.length); |
| 93 |
| 94 for (var i = ary.length - 1; i >= 0; i--) { |
| 95 result.assertEQ(ary[i], shiftary[i], 'i:' + i); |
| 96 } |
| 97 |
| 98 // Reset, then partial bulk shift. |
| 99 this.screen.unshiftRows(ary); |
| 100 result.assertEQ(ary.length, this.screen.rowsArray.length); |
| 101 |
| 102 var shiftary = this.screen.shiftRows(5); |
| 103 for (var i = 0; i < 5; i++) { |
| 104 result.assertEQ(ary[i], shiftary[i], 'i:' + i); |
| 105 } |
| 106 |
| 107 result.pass(); |
| 108 }); |
| 109 |
| 110 /** |
| 111 * Test cursor positioning functionality. |
| 112 */ |
| 113 hterm.Screen.Tests.addTest('cursor-movement', function(result, cx) { |
| 114 var ary = []; |
| 115 |
| 116 for (var i = 0; i < 3; i++) { |
| 117 ary[i] = document.createElement('div'); |
| 118 ary[i].textContent = i; |
| 119 this.screen.pushRow(ary[i]); |
| 120 } |
| 121 |
| 122 this.screen.setCursorPosition(0, 0); |
| 123 result.assertEQ(this.screen.cursorRowNode_, ary[0]); |
| 124 result.assertEQ(this.screen.cursorNode_, ary[0].firstChild); |
| 125 result.assertEQ(this.screen.cursorOffset_, 0); |
| 126 |
| 127 this.screen.setCursorPosition(1, 0); |
| 128 result.assertEQ(this.screen.cursorRowNode_, ary[1]); |
| 129 result.assertEQ(this.screen.cursorNode_, ary[1].firstChild); |
| 130 result.assertEQ(this.screen.cursorOffset_, 0); |
| 131 |
| 132 this.screen.setCursorPosition(1, 10); |
| 133 result.assertEQ(this.screen.cursorRowNode_, ary[1]); |
| 134 result.assertEQ(this.screen.cursorNode_, ary[1].firstChild); |
| 135 result.assertEQ(this.screen.cursorOffset_, 10); |
| 136 |
| 137 this.screen.setCursorPosition(1, 5); |
| 138 result.assertEQ(this.screen.cursorRowNode_, ary[1]); |
| 139 result.assertEQ(this.screen.cursorNode_, ary[1].firstChild); |
| 140 result.assertEQ(this.screen.cursorOffset_, 5); |
| 141 |
| 142 this.screen.setCursorPosition(1, 10); |
| 143 result.assertEQ(this.screen.cursorRowNode_, ary[1]); |
| 144 result.assertEQ(this.screen.cursorNode_, ary[1].firstChild); |
| 145 result.assertEQ(this.screen.cursorOffset_, 10); |
| 146 |
| 147 ary[2].innerHTML = '01<div>23</div>45<div>67</div>89'; |
| 148 |
| 149 this.screen.setCursorPosition(2, 0); |
| 150 result.assertEQ(this.screen.cursorRowNode_, ary[2]); |
| 151 result.assertEQ(this.screen.cursorNode_, ary[2].firstChild); |
| 152 result.assertEQ(this.screen.cursorOffset_, 0); |
| 153 |
| 154 this.screen.setCursorPosition(2, 1); |
| 155 result.assertEQ(this.screen.cursorRowNode_, ary[2]); |
| 156 result.assertEQ(this.screen.cursorNode_, ary[2].firstChild); |
| 157 result.assertEQ(this.screen.cursorOffset_, 1); |
| 158 |
| 159 this.screen.setCursorPosition(2, 2); |
| 160 result.assertEQ(this.screen.cursorRowNode_, ary[2]); |
| 161 result.assertEQ(this.screen.cursorNode_, ary[2].childNodes[1]); |
| 162 result.assertEQ(this.screen.cursorOffset_, 0); |
| 163 |
| 164 this.screen.setCursorPosition(2, 3); |
| 165 result.assertEQ(this.screen.cursorRowNode_, ary[2]); |
| 166 result.assertEQ(this.screen.cursorNode_, ary[2].childNodes[1]); |
| 167 result.assertEQ(this.screen.cursorOffset_, 1); |
| 168 |
| 169 this.screen.setCursorPosition(2, 4); |
| 170 result.assertEQ(this.screen.cursorRowNode_, ary[2]); |
| 171 result.assertEQ(this.screen.cursorNode_, ary[2].childNodes[2]); |
| 172 result.assertEQ(this.screen.cursorOffset_, 0); |
| 173 |
| 174 this.screen.setCursorPosition(2, 5); |
| 175 result.assertEQ(this.screen.cursorRowNode_, ary[2]); |
| 176 result.assertEQ(this.screen.cursorNode_, ary[2].childNodes[2]); |
| 177 result.assertEQ(this.screen.cursorOffset_, 1); |
| 178 |
| 179 this.screen.setCursorPosition(2, 6); |
| 180 result.assertEQ(this.screen.cursorRowNode_, ary[2]); |
| 181 result.assertEQ(this.screen.cursorNode_, ary[2].childNodes[3]); |
| 182 result.assertEQ(this.screen.cursorOffset_, 0); |
| 183 |
| 184 this.screen.setCursorPosition(2, 7); |
| 185 result.assertEQ(this.screen.cursorRowNode_, ary[2]); |
| 186 result.assertEQ(this.screen.cursorNode_, ary[2].childNodes[3]); |
| 187 result.assertEQ(this.screen.cursorOffset_, 1); |
| 188 |
| 189 this.screen.setCursorPosition(2, 8); |
| 190 result.assertEQ(this.screen.cursorRowNode_, ary[2]); |
| 191 result.assertEQ(this.screen.cursorNode_, ary[2].childNodes[4]); |
| 192 result.assertEQ(this.screen.cursorOffset_, 0); |
| 193 |
| 194 this.screen.setCursorPosition(2, 9); |
| 195 result.assertEQ(this.screen.cursorRowNode_, ary[2]); |
| 196 result.assertEQ(this.screen.cursorNode_, ary[2].childNodes[4]); |
| 197 result.assertEQ(this.screen.cursorOffset_, 1); |
| 198 |
| 199 this.screen.setCursorPosition(2, 18); |
| 200 result.assertEQ(this.screen.cursorRowNode_, ary[2]); |
| 201 result.assertEQ(this.screen.cursorNode_, ary[2].childNodes[4]); |
| 202 result.assertEQ(this.screen.cursorOffset_, 10); |
| 203 |
| 204 result.pass(); |
| 205 }); |
| 206 |
| 207 /** |
| 208 * Test character removal. |
| 209 */ |
| 210 hterm.Screen.Tests.addTest('delete-chars', function(result, cx) { |
| 211 var row = document.createElement('div'); |
| 212 row.innerHTML = 'hello<div id="1"> </div><div id="2">world</div>'; |
| 213 this.screen.pushRow(row); |
| 214 |
| 215 this.screen.setCursorPosition(0, 3); |
| 216 this.screen.deleteChars(5); |
| 217 |
| 218 result.assertEQ(row.innerHTML, 'hel<div id="2">rld</div>'); |
| 219 result.pass(); |
| 220 }); |
| 221 |
| 222 /** |
| 223 * Test the ability to insert text in a line. |
| 224 */ |
| 225 hterm.Screen.Tests.addTest('insert', function(result, cx) { |
| 226 // Sample rows. Row 0 is a simple, empty row. Row 1 simulates rows with |
| 227 // mixed text attributes. |
| 228 var ary = [document.createElement('div'), document.createElement('div')]; |
| 229 ary[1].innerHTML = 'hello<div id="1"> </div><div id="2">world</div>'; |
| 230 this.screen.pushRows(ary); |
| 231 |
| 232 // Basic insert. |
| 233 this.screen.setCursorPosition(0, 0); |
| 234 this.screen.insertString('XXXXX'); |
| 235 result.assertEQ(ary[0].innerHTML, 'XXXXX'); |
| 236 |
| 237 // Test that positioning the cursor beyond the end of the current text does |
| 238 // not cause spaces to be printed. |
| 239 this.screen.clearCursorRow(); |
| 240 this.screen.setCursorPosition(0, 3); |
| 241 result.assertEQ(ary[0].innerHTML, ''); |
| 242 |
| 243 // Print some text at this cursor position and make sure the spaces show up. |
| 244 this.screen.insertString('XXXXX'); |
| 245 result.assertEQ(ary[0].innerHTML, ' XXXXX'); |
| 246 |
| 247 // Fetch enough whitespace to ensure that the row is full. |
| 248 var ws = hterm.getWhitespace(this.screen.getWidth()); |
| 249 |
| 250 // Check simple overflow. |
| 251 this.screen.clearCursorRow(); |
| 252 this.screen.insertString('XXXX'); |
| 253 this.screen.setCursorPosition(0, 0); |
| 254 var overflow = this.screen.insertString(ws); |
| 255 result.assertEQ(overflow, 'XXXX'); |
| 256 |
| 257 // Insert into a more complicated row. |
| 258 this.screen.setCursorPosition(1, 3); |
| 259 this.screen.insertString('XXXXX'); |
| 260 result.assertEQ(ary[1].innerHTML, 'helXXXXXlo<div id="1"> </div>' + |
| 261 '<div id="2">world</div>'); |
| 262 |
| 263 // Check that multi-attribute is not implemented. We'll want a better test |
| 264 // once its implemented. |
| 265 this.screen.setCursorPosition(1, 0); |
| 266 try { |
| 267 this.screen.insertString(ws); |
| 268 result.assert(false); |
| 269 } catch (ex) { |
| 270 result.assertEQ(ex, 'NOT IMPLEMENTED'); |
| 271 } |
| 272 |
| 273 result.pass(); |
| 274 }); |
| 275 |
| 276 /** |
| 277 * Test the ability to overwrite test. |
| 278 */ |
| 279 hterm.Screen.Tests.addTest('overwrite', function(result, cx) { |
| 280 var ary = []; |
| 281 ary[0] = document.createElement('div'); |
| 282 ary[0].innerHTML = 'hello<div id="1"> </div><div id="2">world</div>'; |
| 283 ary[1] = document.createElement('div'); |
| 284 this.screen.pushRows(ary); |
| 285 |
| 286 this.screen.setCursorPosition(0, 3); |
| 287 this.screen.overwriteString('XXXXX'); |
| 288 |
| 289 result.assertEQ(ary[0].innerHTML, 'helXXXXX<div id="2">rld</div>'); |
| 290 |
| 291 this.screen.setCursorPosition(1, 0); |
| 292 this.screen.overwriteString('XXXXX'); |
| 293 |
| 294 result.assertEQ(ary[1].innerHTML, 'XXXXX'); |
| 295 |
| 296 result.pass(); |
| 297 }); |
OLD | NEW |