File: spannable_test.unitjs

package info (click to toggle)
chromium-browser 41.0.2272.118-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 2,189,132 kB
  • sloc: cpp: 9,691,462; ansic: 3,341,451; python: 712,689; asm: 518,779; xml: 208,926; java: 169,820; sh: 119,353; perl: 68,907; makefile: 28,311; yacc: 13,305; objc: 11,385; tcl: 3,186; cs: 2,225; sql: 2,217; lex: 2,215; lisp: 1,349; pascal: 1,256; awk: 407; ruby: 155; sed: 53; php: 14; exp: 11
file content (481 lines) | stat: -rw-r--r-- 18,149 bytes parent folder | download | duplicates (2)
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
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// Include test fixture.
GEN_INCLUDE(['../testing/chromevox_unittest_base.js']);

UnserializableSpan = function() {};

StatelessSerializableSpan = function() {};

NonStatelessSerializableSpan = function(value) {
  this.value = value;
};

/**
 * @param {!Object} obj object containing the
 *     serializable representation.
 * @return {!Object} The Spannable.
 */
NonStatelessSerializableSpan.fromJson = function(obj) {
  return new NonStatelessSerializableSpan(obj.value / 2);
};

/**
 * @return {Object} the json serializable form.
 */
NonStatelessSerializableSpan.prototype.toJson = function() {
  return {value: this.value * 2};
};

/**
 * Test fixture.
 * @constructor
 * @extends {ChromeVoxUnitTestBase}
 */
function CvoxSpannableUnitTest() {}

CvoxSpannableUnitTest.prototype = {
  __proto__: ChromeVoxUnitTestBase.prototype,

  /** @override */
  closureModuleDeps: [
    'cvox.Spannable',
  ],

  /** @override */
  setUp: function() {
    cvox.Spannable.registerStatelessSerializableSpan(
        StatelessSerializableSpan, 'StatelessSerializableSpan');

    cvox.Spannable.registerSerializableSpan(
        NonStatelessSerializableSpan, 'NonStatelessSerializableSpan',
        NonStatelessSerializableSpan.fromJson,
        NonStatelessSerializableSpan.prototype.toJson);
  }
};

TEST_F('CvoxSpannableUnitTest', 'ToStringUnannotated', function() {
  assertEquals('', new cvox.Spannable().toString());
  assertEquals('hello world', new cvox.Spannable('hello world').toString());
});

/** Tests that toString works correctly on annotated strings. */
TEST_F('CvoxSpannableUnitTest', 'ToStringAnnotated', function() {
  var spannable = new cvox.Spannable('Hello Google');
  spannable.setSpan('http://www.google.com/', 6, 12);
  assertEquals('Hello Google', spannable.toString());
});

/** Tests the length calculation. */
TEST_F('CvoxSpannableUnitTest', 'GetLength', function() {
  var spannable = new cvox.Spannable('Hello');
  spannable.setSpan({}, 0, 3);
  assertEquals(5, spannable.getLength());
  spannable.append(' world');
  assertEquals(11, spannable.getLength());
  spannable.append(new cvox.Spannable(' from cvox.Spannable'));
  assertEquals(31, spannable.getLength());
});

/** Tests that a span can be added and retrieved at the beginning. */
TEST_F('CvoxSpannableUnitTest', 'SpanBeginning', function() {
  var annotation = {};
  var spannable = new cvox.Spannable('Hello world');
  spannable.setSpan(annotation, 0, 5);
  assertSame(annotation, spannable.getSpan(0));
  assertSame(annotation, spannable.getSpan(3));
  assertUndefined(spannable.getSpan(5));
  assertUndefined(spannable.getSpan(8));
});

/** Tests that a span can be added and retrieved at the beginning. */
TEST_F('CvoxSpannableUnitTest', 'SpanEnd', function() {
  var annotation = {};
  var spannable = new cvox.Spannable('Hello world');
  spannable.setSpan(annotation, 6, 11);
  assertUndefined(spannable.getSpan(3));
  assertUndefined(spannable.getSpan(5));
  assertSame(annotation, spannable.getSpan(6));
  assertSame(annotation, spannable.getSpan(10));
});

/** Tests that a zero-length span is not retrieved. */
TEST_F('CvoxSpannableUnitTest', 'SpanZeroLength', function() {
  var annotation = {};
  var spannable = new cvox.Spannable('Hello world');
  spannable.setSpan(annotation, 3, 3);
  assertUndefined(spannable.getSpan(2));
  assertUndefined(spannable.getSpan(3));
  assertUndefined(spannable.getSpan(4));
});

/** Tests that a removed span is not returned. */
TEST_F('CvoxSpannableUnitTest', 'RemoveSpan', function() {
  var annotation = {};
  var spannable = new cvox.Spannable('Hello world');
  spannable.setSpan(annotation, 0, 3);
  assertSame(annotation, spannable.getSpan(1));
  spannable.removeSpan(annotation);
  assertUndefined(spannable.getSpan(1));
});

/** Tests that adding a span in one place removes it from another. */
TEST_F('CvoxSpannableUnitTest', 'SetSpanMoves', function() {
  var annotation = {};
  var spannable = new cvox.Spannable('Hello world');
  spannable.setSpan(annotation, 0, 3);
  assertSame(annotation, spannable.getSpan(1));
  assertUndefined(spannable.getSpan(4));
  spannable.setSpan(annotation, 3, 6);
  assertUndefined(spannable.getSpan(1));
  assertSame(annotation, spannable.getSpan(4));
});

/** Tests that setSpan objects to out-of-range arguments. */
TEST_F('CvoxSpannableUnitTest', 'SetSpanRangeError', function() {
  var spannable = new cvox.Spannable('Hello world');

  // Start index out of range.
  assertException('expected range error', function() {
    spannable.setSpan({}, -1, 0);
  }, 'RangeError');

  // End index out of range.
  assertException('expected range error', function() {
    spannable.setSpan({}, 0, 12);
  }, 'RangeError');

  // End before start.
  assertException('expected range error', function() {
    spannable.setSpan({}, 1, 0);
  }, 'RangeError');
});

/**
 * Tests that multiple spans can be retrieved at one point.
 * The first one added which applies should be returned by getSpan.
 */
TEST_F('CvoxSpannableUnitTest', 'MultipleSpans', function() {
  var annotation1 = { number: 1 };
  var annotation2 = { number: 2 };
  assertNotSame(annotation1, annotation2);
  var spannable = new cvox.Spannable('Hello world');
  spannable.setSpan(annotation1, 1, 4);
  spannable.setSpan(annotation2, 2, 7);
  assertSame(annotation1, spannable.getSpan(1));
  assertThat([annotation1], eqJSON(spannable.getSpans(1)));
  assertSame(annotation1, spannable.getSpan(3));
  assertThat([annotation1, annotation2], eqJSON(spannable.getSpans(3)));
  assertSame(annotation2, spannable.getSpan(6));
  assertThat([annotation2], eqJSON(spannable.getSpans(6)));
});

/** Tests that appending appends the strings. */
TEST_F('CvoxSpannableUnitTest', 'AppendToString', function() {
  var spannable = new cvox.Spannable('Google');
  assertEquals('Google', spannable.toString());
  spannable.append(' Chrome');
  assertEquals('Google Chrome', spannable.toString());
  spannable.append(new cvox.Spannable('Vox'));
  assertEquals('Google ChromeVox', spannable.toString());
});

/**
 * Tests that appending Spannables combines annotations.
 */
TEST_F('CvoxSpannableUnitTest', 'AppendAnnotations', function() {
  var annotation1 = { number: 1 };
  var annotation2 = { number: 2 };
  assertNotSame(annotation1, annotation2);
  var left = new cvox.Spannable('hello');
  left.setSpan(annotation1, 0, 3);
  var right = new cvox.Spannable(' world');
  right.setSpan(annotation2, 0, 3);
  left.append(right);
  assertSame(annotation1, left.getSpan(1));
  assertSame(annotation2, left.getSpan(6));
});

/**
 * Tests that a span's bounds can be retrieved.
 */
TEST_F('CvoxSpannableUnitTest', 'GetSpanStartAndEnd', function() {
  var annotation = {};
  var spannable = new cvox.Spannable('potato wedges');
  spannable.setSpan(annotation, 8, 12);
  assertEquals(8, spannable.getSpanStart(annotation));
  assertEquals(12, spannable.getSpanEnd(annotation));
});

/**
 * Tests that an absent span's bounds are reported correctly.
 */
TEST_F('CvoxSpannableUnitTest', 'GetSpanStartAndEndAbsent', function() {
  var annotation = {};
  var spannable = new cvox.Spannable('potato wedges');
  assertUndefined(spannable.getSpanStart(annotation));
  assertUndefined(spannable.getSpanEnd(annotation));
});

/**
 * Test that a zero length span can still be found.
 */
TEST_F('CvoxSpannableUnitTest', 'GetSpanStartAndEndZeroLength', function() {
  var annotation = {};
  var spannable = new cvox.Spannable('potato wedges');
  spannable.setSpan(annotation, 8, 8);
  assertEquals(8, spannable.getSpanStart(annotation));
  assertEquals(8, spannable.getSpanEnd(annotation));
});

/**
 * Tests that == (but not ===) objects are treated distinctly when getting
 * span bounds.
 */
TEST_F('CvoxSpannableUnitTest', 'GetSpanStartAndEndEquality', function() {
  // Note that 0 == '' and '' == 0 in JavaScript.
  var spannable = new cvox.Spannable('wat');
  spannable.setSpan(0, 0, 0);
  spannable.setSpan('', 1, 3);
  assertEquals(0, spannable.getSpanStart(0));
  assertEquals(0, spannable.getSpanEnd(0));
  assertEquals(1, spannable.getSpanStart(''));
  assertEquals(3, spannable.getSpanEnd(''));
});

/**
 * Tests that substrings have the correct character sequence.
 */
TEST_F('CvoxSpannableUnitTest', 'Substring', function() {
  var assertSubstringResult = function(expected, initial, start, opt_end) {
    var spannable = new cvox.Spannable(initial);
    var substring = spannable.substring(start, opt_end);
    assertEquals(expected, substring.toString());
  };
  assertSubstringResult('Page', 'Google PageRank', 7, 11);
  assertSubstringResult('Goog', 'Google PageRank', 0, 4);
  assertSubstringResult('Rank', 'Google PageRank', 11, 15);
  assertSubstringResult('Rank', 'Google PageRank', 11);
});

/**
 * Tests that substring arguments are validated properly.
 */
TEST_F('CvoxSpannableUnitTest', 'SubstringRangeError', function() {
  var assertRangeError = function(initial, start, opt_end) {
    var spannable = new cvox.Spannable(initial);
    assertException('expected range error', function() {
      spannable.substring(start, opt_end);
    }, 'RangeError');
  };
  assertRangeError('Google PageRank', -1, 5);
  assertRangeError('Google PageRank', 0, 99);
  assertRangeError('Google PageRank', 5, 2);
});

/**
 * Tests that spans in the substring range are preserved.
 */
TEST_F('CvoxSpannableUnitTest', 'SubstringSpansIncluded', function() {
  var assertSpanIncluded = function(expectedSpanStart, expectedSpanEnd,
      initial, initialSpanStart, initialSpanEnd, start, opt_end) {
    var annotation = {};
    var spannable = new cvox.Spannable(initial);
    spannable.setSpan(annotation, initialSpanStart, initialSpanEnd);
    var substring = spannable.substring(start, opt_end);
    assertEquals(expectedSpanStart, substring.getSpanStart(annotation));
    assertEquals(expectedSpanEnd, substring.getSpanEnd(annotation));
  };
  assertSpanIncluded(1, 5, 'potato wedges', 8, 12, 7);
  assertSpanIncluded(1, 5, 'potato wedges', 8, 12, 7, 13);
  assertSpanIncluded(1, 5, 'potato wedges', 8, 12, 7, 12);
  assertSpanIncluded(0, 4, 'potato wedges', 8, 12, 8, 12);
  assertSpanIncluded(0, 3, 'potato wedges', 0, 3, 0);
  assertSpanIncluded(0, 3, 'potato wedges', 0, 3, 0, 3);
  assertSpanIncluded(0, 3, 'potato wedges', 0, 3, 0, 6);
  assertSpanIncluded(0, 5, 'potato wedges', 8, 13, 8);
  assertSpanIncluded(0, 5, 'potato wedges', 8, 13, 8, 13);
  assertSpanIncluded(1, 6, 'potato wedges', 8, 13, 7, 13);

  // Note: we should keep zero-length spans, even at the edges of the range.
  assertSpanIncluded(0, 0, 'potato wedges', 0, 0, 0, 0);
  assertSpanIncluded(0, 0, 'potato wedges', 0, 0, 0, 6);
  assertSpanIncluded(1, 1, 'potato wedges', 8, 8, 7, 13);
  assertSpanIncluded(6, 6, 'potato wedges', 6, 6, 0, 6);
});

/**
 * Tests that spans outside the range are omitted.
 * It's fine to keep zero-length spans at the ends, though.
 */
TEST_F('CvoxSpannableUnitTest', 'SubstringSpansExcluded', function() {
  var assertSpanExcluded = function(initial, spanStart, spanEnd,
      start, opt_end) {
    var annotation = {};
    var spannable = new cvox.Spannable(initial);
    spannable.setSpan(annotation, spanStart, spanEnd);
    var substring = spannable.substring(start, opt_end);
    assertUndefined(substring.getSpanStart(annotation));
    assertUndefined(substring.getSpanEnd(annotation));
  };
  assertSpanExcluded('potato wedges', 8, 12, 0, 6);
  assertSpanExcluded('potato wedges', 7, 12, 0, 6);
  assertSpanExcluded('potato wedges', 0, 6, 8);
  assertSpanExcluded('potato wedges', 6, 6, 8);
});

/**
 * Tests that spans which cross the boundary are clipped.
 */
TEST_F('CvoxSpannableUnitTest', 'SubstringSpansClipped', function() {
  var assertSpanIncluded = function(expectedSpanStart, expectedSpanEnd,
      initial, initialSpanStart, initialSpanEnd, start, opt_end) {
    var annotation = {};
    var spannable = new cvox.Spannable(initial);
    spannable.setSpan(annotation, initialSpanStart, initialSpanEnd);
    var substring = spannable.substring(start, opt_end);
    assertEquals(expectedSpanStart, substring.getSpanStart(annotation));
    assertEquals(expectedSpanEnd, substring.getSpanEnd(annotation));
  };
  assertSpanIncluded(0, 4, 'potato wedges', 7, 13, 8, 12);
  assertSpanIncluded(0, 0, 'potato wedges', 0, 6, 0, 0);
  assertSpanIncluded(0, 0, 'potato wedges', 0, 6, 6, 6);

  // The first of the above should produce "edge".
  assertEquals('edge',
      new cvox.Spannable('potato wedges').substring(8, 12).toString());
});

/**
 * Tests that whitespace is trimmed.
 */
TEST_F('CvoxSpannableUnitTest', 'Trim', function() {
  var assertTrimResult = function(expected, initial) {
    assertEquals(expected, new cvox.Spannable(initial).trim().toString());
  };
  assertTrimResult('John F. Kennedy', 'John F. Kennedy');
  assertTrimResult('John F. Kennedy', '  John F. Kennedy');
  assertTrimResult('John F. Kennedy', 'John F. Kennedy     ');
  assertTrimResult('John F. Kennedy', '   \r\t   \nJohn F. Kennedy\n\n \n');
  assertTrimResult('', '');
  assertTrimResult('', '     \t\t    \n\r');
});

/**
 * Tests that trim keeps, drops and clips spans.
 */
TEST_F('CvoxSpannableUnitTest', 'TrimSpans', function() {
  var spannable = new cvox.Spannable(' \t Kennedy\n');
  spannable.setSpan('tab', 1, 2);
  spannable.setSpan('jfk', 3, 10);
  spannable.setSpan('jfk-newline', 3, 11);
  var trimmed = spannable.trim();
  assertUndefined(trimmed.getSpanStart('tab'));
  assertUndefined(trimmed.getSpanEnd('tab'));
  assertEquals(0, trimmed.getSpanStart('jfk'));
  assertEquals(7, trimmed.getSpanEnd('jfk'));
  assertEquals(0, trimmed.getSpanStart('jfk-newline'));
  assertEquals(7, trimmed.getSpanEnd('jfk-newline'));
});

/**
 * Tests that when a string is all whitespace, we trim off the *end*.
 */
TEST_F('CvoxSpannableUnitTest', 'TrimAllWhitespace', function() {
  var spannable = new cvox.Spannable('    ');
  spannable.setSpan('cursor 1', 0, 0);
  spannable.setSpan('cursor 2', 2, 2);
  var trimmed = spannable.trim();
  assertEquals(0, trimmed.getSpanStart('cursor 1'));
  assertEquals(0, trimmed.getSpanEnd('cursor 1'));
  assertUndefined(trimmed.getSpanStart('cursor 2'));
  assertUndefined(trimmed.getSpanEnd('cursor 2'));
});

/**
 * Tests finding a span which is an instance of a given class.
 */
TEST_F('CvoxSpannableUnitTest', 'GetSpanInstanceOf', function() {
  function ExampleConstructorBase() {}
  function ExampleConstructor1() {}
  function ExampleConstructor2() {}
  function ExampleConstructor3() {}
  ExampleConstructor1.prototype = new ExampleConstructorBase();
  ExampleConstructor2.prototype = new ExampleConstructorBase();
  ExampleConstructor3.prototype = new ExampleConstructorBase();
  var ex1 = new ExampleConstructor1();
  var ex2 = new ExampleConstructor2();
  var spannable = new cvox.Spannable('Hello world');
  spannable.setSpan(ex1, 0, 0);
  spannable.setSpan(ex2, 1, 1);
  assertEquals(ex1, spannable.getSpanInstanceOf(ExampleConstructor1));
  assertEquals(ex2, spannable.getSpanInstanceOf(ExampleConstructor2));
  assertUndefined(spannable.getSpanInstanceOf(ExampleConstructor3));
  assertEquals(ex1, spannable.getSpanInstanceOf(ExampleConstructorBase));
});

/** Tests trimming only left or right. */
TEST_F('CvoxSpannableUnitTest', 'TrimLeftOrRight', function() {
  var spannable = new cvox.Spannable('    ');
  spannable.setSpan('cursor 1', 0, 0);
  spannable.setSpan('cursor 2', 2, 2);
  var trimmed = spannable.trimLeft();
  assertEquals(0, trimmed.getSpanStart('cursor 1'));
  assertEquals(0, trimmed.getSpanEnd('cursor 1'));
  assertUndefined(trimmed.getSpanStart('cursor 2'));
  assertUndefined(trimmed.getSpanEnd('cursor 2'));

  var spannable2 = new cvox.Spannable('0  ');
  spannable2.setSpan('cursor 1', 0, 0);
  spannable2.setSpan('cursor 2', 2, 2);
  var trimmed2 = spannable2.trimLeft();
  assertEquals(0, trimmed2.getSpanStart('cursor 1'));
  assertEquals(0, trimmed2.getSpanEnd('cursor 1'));
  assertEquals(2, trimmed2.getSpanStart('cursor 2'));
  assertEquals(2, trimmed2.getSpanEnd('cursor 2'));
  trimmed2 = trimmed2.trimRight();
  assertEquals(0, trimmed2.getSpanStart('cursor 1'));
  assertEquals(0, trimmed2.getSpanEnd('cursor 1'));
  assertUndefined(trimmed2.getSpanStart('cursor 2'));
  assertUndefined(trimmed2.getSpanEnd('cursor 2'));

  var spannable3 = new cvox.Spannable('  0');
  spannable3.setSpan('cursor 1', 0, 0);
  spannable3.setSpan('cursor 2', 2, 2);
  var trimmed3 = spannable3.trimRight();
  assertEquals(0, trimmed3.getSpanStart('cursor 1'));
  assertEquals(0, trimmed3.getSpanEnd('cursor 1'));
  assertEquals(2, trimmed3.getSpanStart('cursor 2'));
  assertEquals(2, trimmed3.getSpanEnd('cursor 2'));
  trimmed3 = trimmed3.trimLeft();
  assertUndefined(trimmed3.getSpanStart('cursor 1'));
  assertUndefined(trimmed3.getSpanEnd('cursor 1'));
  assertEquals(0, trimmed3.getSpanStart('cursor 2'));
  assertEquals(0, trimmed3.getSpanEnd('cursor 2'));
});

TEST_F('CvoxSpannableUnitTest', 'Serialize', function() {
  var fresh = new cvox.Spannable('text');
  var freshStatelessSerializable = new StatelessSerializableSpan();
  var freshNonStatelessSerializable = new NonStatelessSerializableSpan(14);
  fresh.setSpan(new UnserializableSpan(), 0, 1);
  fresh.setSpan(freshStatelessSerializable, 0, 2);
  fresh.setSpan(freshNonStatelessSerializable, 3, 4);
  var thawn = cvox.Spannable.fromJson(fresh.toJson());
  var thawnStatelessSerializable = thawn.getSpanInstanceOf(
      StatelessSerializableSpan);
  var thawnNonStatelessSerializable = thawn.getSpanInstanceOf(
      NonStatelessSerializableSpan);
  assertThat('text', eqJSON(thawn.toString()));
  assertUndefined(thawn.getSpanInstanceOf(UnserializableSpan));
  assertThat(
      fresh.getSpanStart(freshStatelessSerializable),
      eqJSON(thawn.getSpanStart(thawnStatelessSerializable)));
  assertThat(
      fresh.getSpanEnd(freshStatelessSerializable),
      eqJSON(thawn.getSpanEnd(thawnStatelessSerializable)));
  assertThat(freshNonStatelessSerializable,
             eqJSON(thawnNonStatelessSerializable));
});