Upstream version 10.38.222.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / resources / chromeos / chromevox / common / cursor_selection_test.unitjs
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 CvoxCursorSelectionUnitTest() {}
14
15 CvoxCursorSelectionUnitTest.prototype = {
16   __proto__: ChromeVoxUnitTestBase.prototype,
17
18   /** @override */
19   closureModuleDeps: [
20     'cvox.CursorSelection'
21   ]
22 };
23
24 TEST_F('CvoxCursorSelectionUnitTest', 'Reverse', function() {
25   this.loadDoc(function() {/*!
26      <div>
27        <p id="a">a</p>
28        <p id="b">b</p>
29      </div>
30   */});
31   var a = new cvox.Cursor($('a'), 0, '');
32   var b = new cvox.Cursor($('b'), 0, '');
33
34   var aa = new cvox.CursorSelection(a, a);
35   assertEquals(false, aa.isReversed());
36   aa.setReversed(true);
37   assertEquals(true, aa.isReversed());
38
39   var ab = new cvox.CursorSelection(a, b);
40   assertEquals(false, ab.isReversed());
41   ab.setReversed(true);
42   assertEquals(true, ab.isReversed());
43   assertEquals(true, ab.start.equals(b));
44   assertEquals(true, ab.end.equals(a));
45   ab.setReversed(false);
46   assertEquals(false, ab.isReversed());
47   assertEquals(true, ab.start.equals(a));
48   assertEquals(true, ab.end.equals(b));
49
50   ab = new cvox.CursorSelection(b, a);
51   assertEquals(false, ab.isReversed());
52   assertEquals(true, ab.start.equals(a));
53   assertEquals(true, ab.end.equals(b));
54
55   var ba = new cvox.CursorSelection(b, a, true);
56   assertEquals(true, ba.isReversed());
57   assertEquals(true, ba.start.equals(b));
58   assertEquals(true, ba.end.equals(a));
59
60   ba = new cvox.CursorSelection(a, b, true);
61   assertEquals(true, ba.isReversed());
62   assertEquals(true, ba.start.equals(b));
63   assertEquals(true, ba.end.equals(a));
64 });
65
66
67 /** Tests correctness of collapsing selections.  */
68 TEST_F('CvoxCursorSelectionUnitTest', 'Collapse', function() {
69   this.loadDoc(function() {/*!
70     <p id='1'>This is a test.</p>
71   */});
72   var text = $('1').firstChild;
73   var a = new cvox.Cursor(text, 0, 'This is a test.');
74   var b = new cvox.Cursor(text, 13, 'This is a test.');
75   var c = new cvox.Cursor(text, 5, 'This is a test.');
76   var d = new cvox.Cursor(text, 8, 'This is a test.');
77
78   var aa = new cvox.CursorSelection(a, a).collapse();
79   assertEquals(0, aa.start.index);
80   assertEquals(0, aa.end.index);
81
82   var ab = new cvox.CursorSelection(a, b).collapse();
83   assertEquals(0, ab.start.index);
84   assertEquals(1, ab.end.index);
85
86   var ba = new cvox.CursorSelection(b, a, true).collapse();
87   assertEquals(12, ba.absStart().index);
88   assertEquals(13, ba.absEnd().index);
89
90   var cd = new cvox.CursorSelection(c, d).collapse();
91   assertEquals(5, cd.start.index);
92   assertEquals(6, cd.end.index);
93
94   var dc = new cvox.CursorSelection(d, c, true).collapse();
95   assertEquals(7, dc.absStart().index);
96   assertEquals(8, dc.absEnd().index);
97 });