braille_integration_test.unitjs revision 1320f92c476a1ad9d19dba2a48c72b75566198e9
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.
6GEN_INCLUDE(['../../testing/chromevox_unittest_base.js',
7            '../../testing/assert_additions.js']);
8
9// Tests the communication between content script and background page for
10// braille.
11
12/**
13 * Test fixture.
14 * @constructor
15 * @extends {ChromeVoxUnitTestBase}
16 */
17function CvoxBrailleIntegrationUnitTest() {}
18
19CvoxBrailleIntegrationUnitTest.prototype = {
20  __proto__: ChromeVoxUnitTestBase.prototype,
21
22  /** @override */
23  closureModuleDeps: [
24    'cvox.BrailleBackground',
25    'cvox.BrailleInputHandler',
26    'cvox.BrailleKeyCommand',
27    'cvox.BrailleUtil',
28    'cvox.ChromeBraille',
29    'cvox.ExpandingBrailleTranslator',
30  ],
31
32  /** @override */
33  setUp: function() {
34    cvox.BrailleTable.getAll = function() {}
35    this.displayManager = new FakeDisplayManager();
36    this.inputHandler = new FakeInputHandler();
37
38    this.brailleBackground = new cvox.BrailleBackground(
39        this.displayManager, this.inputHandler);
40
41    cvox.ExtensionBridge = new FakeExtensionBridge(this.brailleBackground);
42
43    this.braille = new cvox.ChromeBraille();
44    this.braille.setCommandListener(function(command, content) {
45      this.lastCommand = command;
46      this.lastCommandContent = content;
47    }.bind(this));
48    this.lastCommand = null;
49    this.lastCommandContent = null;
50
51    // Create convenience objects used in all tests.
52    this.command1 = {command: cvox.BrailleKeyCommand.PAN_LEFT};
53    this.content1 = cvox.NavBraille.fromText('text 1');
54    this.command2 = {command: cvox.BrailleKeyCommand.ROUTING};
55    this.content2 = cvox.NavBraille.fromText('text 2');
56  },
57
58  /** @Override */
59  tearDown: function() {
60    cvox.ExtensionBridge = null;
61  },
62
63  sendCommand: function(command, content) {
64    this.displayManager.commandListener(command, content);
65  }
66};
67
68function FakeExtensionBridge(brailleBackground) {
69  /** @private {Function} */
70  this.messageListener_ = null;
71  /** @private {cvox.BrailleBackground} */
72  this.brailleBackground_ = brailleBackground;
73}
74
75FakeExtensionBridge.prototype = {
76  uniqueId: function() { return 1; },
77
78  /** @param {Function} listener The listener. */
79  addMessageListener: function(listener) {
80    assertEquals(null, this.messageListener_);
81    this.messageListener_ = listener;
82  },
83
84  send: function(msg) {
85    if (msg['message'] == 'BRAILLE') {
86      assertNotEquals(null, this.messageListener_);
87      this.messageListener_(msg);
88    } else {
89      assertEquals('BRAILLE', msg['target']);
90      this.brailleBackground_.onBrailleMessage(msg);
91    }
92  }
93};
94
95/** @extends {cvox.BrailleDisplaymanager} */
96function FakeDisplayManager() {
97  /** @type {Function} */
98  this.commandListener = null;
99  /** @type {cvox.NavBraille} */
100  this.content = null;
101}
102
103FakeDisplayManager.prototype = {
104  /** @Override */
105  setCommandListener: function(listener) {
106    this.commandListener = listener;
107  },
108
109  /** @Override */
110  setContent: function(content, expansionType) {
111    assertEquals(cvox.ExpandingBrailleTranslator.ExpansionType.SELECTION,
112                 expansionType);
113    this.content = content;
114  }
115};
116
117/** @extends {cvox.BrailleInputHandler} */
118function FakeInputHandler() {
119}
120
121FakeInputHandler.prototype = {
122  /** @Override */
123  init: function() {},
124
125  /** @Override */
126  onBrailleKeyEvent: function() {
127    return false;
128  },
129
130  /** @Override */
131  onDisplayContentChanged: function() {},
132
133  /** @Override */
134  getExpansionType: function() {
135    return cvox.ExpandingBrailleTranslator.ExpansionType.SELECTION;
136  }
137};
138
139TEST_F('CvoxBrailleIntegrationUnitTest', 'Write', function() {
140  this.braille.write(this.content1);
141  assertEqualsJSON(this.content1, this.displayManager.content);
142});
143
144TEST_F('CvoxBrailleIntegrationUnitTest', 'WriteWithSpans', function() {
145  var selectionSpan = new cvox.BrailleUtil.ValueSelectionSpan();
146  var valueSpan = new cvox.BrailleUtil.ValueSpan(20);
147  var toSend = cvox.NavBraille.fromText(
148      new cvox.Spannable('Hello', valueSpan));
149  toSend.text.setSpan(selectionSpan, 0, 0);
150  toSend.text.setSpan(document.body, 0, toSend.text.getLength());
151  var expected = cvox.NavBraille.fromText(
152      new cvox.Spannable(toSend.text.toString(), valueSpan));
153  expected.text.setSpan(selectionSpan, 0, 0);
154
155  this.braille.write(toSend);
156  assertEqualsJSON(expected, this.displayManager.content);
157});
158
159TEST_F('CvoxBrailleIntegrationUnitTest', 'CommandNoContent', function() {
160  // Commands are only delivered to the content script if the window has focus.
161  window.focus();
162  this.sendCommand(this.command1, null);
163  assertEqualsJSON(this.command1, this.lastCommand);
164  assertEquals(null, this.lastCommandContent);
165});
166
167TEST_F('CvoxBrailleIntegrationUnitTest', 'InterleavedWritesAndCommands',
168       function() {
169  window.focus();
170  this.braille.write(this.content1);
171  this.sendCommand(this.command1, this.displayManager.content);
172  assertEqualsJSON(this.command1, this.lastCommand);
173  assertEqualsJSON(this.content1, this.lastCommandContent);
174
175  var savedContent1 = this.displayManager.content;
176  this.braille.write(this.content2);
177  // Old content still on display.
178  this.sendCommand(this.command1, savedContent1);
179  assertEqualsJSON(this.command1, this.lastCommand);
180  assertEquals(null, this.lastCommandContent);
181  this.sendCommand(this.command2, this.displayManager.content);
182  assertEqualsJSON(this.command2, this.lastCommand);
183  assertEqualsJSON(this.content2, this.lastCommandContent);
184
185});
186
187TEST_F('CvoxBrailleIntegrationUnitTest', 'CommandAfterBackgroundWrite',
188       function() {
189  window.focus();
190  this.braille.write(this.content1);
191  this.sendCommand(this.command1, this.displayManager.content);
192  assertEqualsJSON(this.command1, this.lastCommand);
193  assertEqualsJSON(this.content1, this.lastCommandContent);
194
195  this.brailleBackground.write(this.content2);
196  assertEqualsJSON(this.content2, this.displayManager.content);
197  this.sendCommand(this.command2, this.displayManager.content);
198  assertEqualsJSON(this.command2, this.lastCommand);
199  assertEquals(null, this.lastCommandContent);
200});
201
202TEST_F('CvoxBrailleIntegrationUnitTest', 'CommandAfterOtherTabWrite',
203       function() {
204  window.focus();
205  // Ignore the listener of the braille from the second 'tab'.
206  cvox.ExtensionBridge.addMessageListener = function() {}
207  // Create another content script braille object, presumably from another
208  // tab.
209  var anotherBraille = new cvox.ChromeBraille();
210  this.braille.write(this.content1);
211  this.sendCommand(this.command1, this.displayManager.content);
212  // Now, this other braille gets a different unique id.
213  cvox.ExtensionBridge.uniqueId = function() { return 2; }
214  anotherBraille.write(this.content2);
215  this.sendCommand(this.command2, this.displayManager.content);
216  // The first 'tab' still gets the command, but since the second 'tab's\
217  // braille was on the display, it gets null content.
218  assertEqualsJSON(this.command2, this.lastCommand);
219  assertEquals(null, this.lastCommandContent);
220});
221