OLD | NEW |
| (Empty) |
1 // Copyright 2014 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 // Include test fixture. | |
6 GEN_INCLUDE(['../testing/chromevox_unittest_base.js']); | |
7 | |
8 /** | |
9 * Test fixture. | |
10 * @constructor | |
11 * @extends {ChromeVoxUnitTestBase} | |
12 */ | |
13 function CvoxPageSelectionUnitTest() {} | |
14 | |
15 CvoxPageSelectionUnitTest.prototype = { | |
16 __proto__: ChromeVoxUnitTestBase.prototype, | |
17 | |
18 /** @override */ | |
19 closureModuleDeps: [ | |
20 'cvox.CursorSelection', | |
21 'cvox.PageSelection', | |
22 ], | |
23 | |
24 /** @override */ | |
25 setUp: function() { | |
26 this.loadDoc(function() {/*! | |
27 <p id='p1'>The quick</p> | |
28 <a id='a1' href='#'>brown fox</a> | |
29 <h1 id='h1'>jumped over</h1> | |
30 */}); | |
31 this.pSel = cvox.CursorSelection.fromNode($('p1')); | |
32 this.pSel.start.index = 0; | |
33 this.pSel.end.index = 1; | |
34 this.aSel = cvox.CursorSelection.fromNode($('a1')); | |
35 this.aSel.start.index = 0; | |
36 this.aSel.end.index = 1; | |
37 this.hSel = cvox.CursorSelection.fromNode($('h1')); | |
38 this.hSel.start.index = 0; | |
39 this.hSel.end.index = 1; | |
40 }, | |
41 | |
42 /** | |
43 * Asserts a selection. | |
44 * @param {string} str The expected contents of selection. | |
45 * @private | |
46 */ | |
47 assertSelection_: function(str) { | |
48 assertEquals(str, window.getSelection().toString()); | |
49 } | |
50 }; | |
51 | |
52 TEST_F('CvoxPageSelectionUnitTest', 'BasicExtend', function() { | |
53 var pageSel = new cvox.PageSelection(this.pSel); | |
54 pageSel.extend(this.hSel); | |
55 this.assertSelection_('The quick\n\nbrown fox\njumped over'); | |
56 this.hSel.end.node = this.hSel.end.node.firstChild; | |
57 this.hSel.end.index = 6; | |
58 pageSel.extend(this.hSel); | |
59 this.assertSelection_('The quick\n\nbrown fox\njumped'); | |
60 }); | |
61 | |
62 | |
63 /** Tests a reverse extension. */ | |
64 TEST_F('CvoxPageSelectionUnitTest', 'ReverseExtend', function() { | |
65 var pageSel = new cvox.PageSelection(this.pSel); | |
66 this.assertSelection_('The quick'); | |
67 pageSel.extend(this.hSel); | |
68 this.assertSelection_('The quick\n\nbrown fox\njumped over'); | |
69 pageSel.extend(this.aSel.setReversed(true)); | |
70 this.assertSelection_('The quick\n\nbrown fox'); | |
71 this.pSel.setReversed(true); | |
72 pageSel.extend(this.pSel); | |
73 this.assertSelection_('The quick'); | |
74 this.pSel.start.node = this.pSel.start.node.firstChild; | |
75 this.pSel.start.index = 3; | |
76 pageSel.extend(this.pSel); | |
77 this.assertSelection_('The'); | |
78 }); | |
79 | |
80 | |
81 /** Tests all possible configurations of PageSelection's and extending | |
82 * CursorSelection's. | |
83 */ | |
84 TEST_F('CvoxPageSelectionUnitTest', 'ExtendDirections', function() { | |
85 // A normal page selection, with a normal extension. | |
86 var pageSel = new cvox.PageSelection(this.aSel); | |
87 assertTrue(pageSel.extend(this.hSel)); | |
88 | |
89 // A normal page selection, with a reversed extension. | |
90 assertTrue(pageSel.extend(this.hSel.setReversed(true))); | |
91 | |
92 // A reversed page selection, with a normal cursor selection. | |
93 var rPageSel = new cvox.PageSelection(this.aSel.setReversed(true)); | |
94 assertTrue(rPageSel.extend(this.pSel)); | |
95 | |
96 // A reversed page selection, with a reversed extension. | |
97 assertTrue(rPageSel.extend(this.pSel.setReversed(true))); | |
98 }); | |
99 | |
100 | |
101 /** Tests degenerate extensions. */ | |
102 TEST_F('CvoxPageSelectionUnitTest', 'DegenerateExtensions', function() { | |
103 var pageSel = new cvox.PageSelection(this.aSel); | |
104 | |
105 // A normal page selection, with a normal extension not in document order. | |
106 assertFalse(pageSel.extend(this.pSel)); | |
107 | |
108 // And, this causes a reset of page selection. | |
109 assertTrue(pageSel.sel_.equals(this.pSel)); | |
110 | |
111 // Reinitialize. | |
112 pageSel = new cvox.PageSelection(this.aSel.setReversed(false)); | |
113 | |
114 // A normal page selection, with a reversed extension not in document order. | |
115 assertFalse(pageSel.extend(this.pSel.setReversed(true))); | |
116 | |
117 // And, again, it causes reset of page selection. | |
118 assertTrue(pageSel.sel_.equals(this.pSel)); | |
119 | |
120 // Reverse page selections. | |
121 var rPageSel = new cvox.PageSelection(this.aSel.setReversed(true)); | |
122 | |
123 // A reversed page selection, with a normal extension not in document order. | |
124 assertFalse(rPageSel.extend(this.hSel)); | |
125 assertTrue(rPageSel.sel_.equals(this.hSel)); | |
126 | |
127 // A reversed page selection, with a reversed extension not in document order. | |
128 rPageSel = new cvox.PageSelection(this.aSel.setReversed(true)); | |
129 assertFalse(rPageSel.extend(this.hSel.setReversed(true))); | |
130 assertTrue(rPageSel.sel_.equals(this.hSel)); | |
131 | |
132 // And, finally, try extending to oneself in either direction. | |
133 pageSel = new cvox.PageSelection(this.aSel.setReversed(false)); | |
134 | |
135 // A normal page selection, with an extension to itself. | |
136 assertFalse(pageSel.extend(this.aSel.setReversed(false))); | |
137 assertFalse(pageSel.extend(this.aSel.setReversed(true))); | |
138 | |
139 // A reversed page selection, with an extension to itself. | |
140 var rPageSel = new cvox.PageSelection(this.aSel.setReversed(true)); | |
141 assertFalse(rPageSel.extend(this.aSel.setReversed(true))); | |
142 assertFalse(rPageSel.extend(this.aSel.setReversed(false))); | |
143 }); | |
OLD | NEW |