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.
5 // Include test fixture.
6 GEN_INCLUDE(['../testing/chromevox_unittest_base.js']);
11 * @extends {ChromeVoxUnitTestBase}
13 function CvoxPageSelectionUnitTest() {}
15 CvoxPageSelectionUnitTest.prototype = {
16 __proto__: ChromeVoxUnitTestBase.prototype,
20 'cvox.CursorSelection',
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>
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;
43 * Asserts a selection.
44 * @param {string} str The expected contents of selection.
47 assertSelection_: function(str) {
48 assertEquals(str, window.getSelection().toString());
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');
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');
81 /** Tests all possible configurations of PageSelection's and extending
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));
89 // A normal page selection, with a reversed extension.
90 assertTrue(pageSel.extend(this.hSel.setReversed(true)));
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));
96 // A reversed page selection, with a reversed extension.
97 assertTrue(rPageSel.extend(this.pSel.setReversed(true)));
101 /** Tests degenerate extensions. */
102 TEST_F('CvoxPageSelectionUnitTest', 'DegenerateExtensions', function() {
103 var pageSel = new cvox.PageSelection(this.aSel);
105 // A normal page selection, with a normal extension not in document order.
106 assertFalse(pageSel.extend(this.pSel));
108 // And, this causes a reset of page selection.
109 assertTrue(pageSel.sel_.equals(this.pSel));
112 pageSel = new cvox.PageSelection(this.aSel.setReversed(false));
114 // A normal page selection, with a reversed extension not in document order.
115 assertFalse(pageSel.extend(this.pSel.setReversed(true)));
117 // And, again, it causes reset of page selection.
118 assertTrue(pageSel.sel_.equals(this.pSel));
120 // Reverse page selections.
121 var rPageSel = new cvox.PageSelection(this.aSel.setReversed(true));
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));
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));
132 // And, finally, try extending to oneself in either direction.
133 pageSel = new cvox.PageSelection(this.aSel.setReversed(false));
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)));
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)));