Home | History | Annotate | Download | only in background
      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_e2e_test_base.js']);
      7 
      8 /**
      9  * Test fixture for cvox2.Background.
     10  * @constructor
     11  * @extends {ChromeVoxE2ETest}
     12  */
     13 function BackgroundTest() {}
     14 
     15 BackgroundTest.prototype = {
     16   __proto__: ChromeVoxE2ETest.prototype,
     17 
     18   // This method is called without a |this| instance bound.
     19   /** @override */
     20   testGenCppIncludes: function() {
     21     ChromeVoxE2ETest.prototype.testGenCppIncludes.call(this);
     22     GEN('#include "base/command_line.h"');
     23     GEN('#include "chromeos/chromeos_switches.h"');
     24   },
     25 
     26   // This method is called without a |this| instance bound.
     27   /** @override */
     28   testGenPreamble: function() {
     29     GEN_BLOCK(function() {/*!
     30       CommandLine* command_line = CommandLine::ForCurrentProcess();
     31       command_line->AppendSwitch(chromeos::switches::kEnableChromeVoxNext);
     32     */});
     33     ChromeVoxE2ETest.prototype.testGenPreamble.call(this);
     34   },
     35 
     36   /** @override */
     37   setUp: function() {
     38     this.mockTts = new MockTts();
     39     cvox.ChromeVox.tts = this.mockTts;
     40   }
     41 };
     42 
     43 /**
     44  * Mock tts class.
     45  * @constructor
     46  * @extends {cvox.TtsInterface}
     47  */
     48 var MockTts = function() {
     49 };
     50 
     51 MockTts.prototype = {
     52   /** Tracks all spoken text. @type {!Array.<string>} */
     53   utterances: [],
     54 
     55   /** @override */
     56   speak: function(textString, queueMode, properties) {
     57     this.utterances.push(textString);
     58   },
     59 
     60   /**
     61    * Checks to see if a string was spoken.
     62    * @param {string} textString The string to check.
     63    * @return {boolean} True if the string was spoken (possibly as part of a
     64    * larger utterance).
     65    */
     66   checkIfSubstringWasSpoken: function(textString) {
     67     return this.utterances.some(function(t) {
     68       return t.indexOf(textString) != -1;
     69     });
     70   }
     71 };
     72 
     73 /** Tests that ChromeVox classic is in this context. */
     74 SYNC_TEST_F('BackgroundTest', 'ClassicNamespaces', function() {
     75   assertEquals('object', typeof(cvox));
     76   assertEquals('function', typeof(cvox.ChromeVoxBackground));
     77 });
     78 
     79 /** Tests that ChromeVox next is in this context. */
     80 SYNC_TEST_F('BackgroundTest', 'NextNamespaces', function() {
     81   assertEquals('object', typeof(cvox2));
     82   assertEquals('function', typeof(cvox2.Background));
     83 });
     84 
     85 /** Tests that ChromeVox reads the desktop tree. */
     86 TEST_F('BackgroundTest', 'DesktopFocus', function() {
     87   function findStatusTray(root) {
     88     if (root.role == chrome.automation.RoleType.button &&
     89         root.attributes.name &&
     90         root.attributes.name.indexOf('Status tray') != -1) {
     91       return root;
     92     }
     93     for (var i = 0; i < root.children().length; i++) {
     94       var found = findStatusTray(root.children()[i]);
     95       if (found)
     96         return found;
     97     }
     98     return null;
     99   }
    100 
    101   chrome.automation.getDesktop(function(root) {
    102     var testButton = findStatusTray(root);
    103     testButton.addEventListener(chrome.automation.EventType.focus,
    104         function(e) {
    105           var result =
    106               cvox.ChromeVox.tts.checkIfSubstringWasSpoken('Status tray');
    107           testDone([result, '']);
    108         },
    109         true);
    110     testButton.focus();
    111   });
    112 });
    113