Home | History | Annotate | Download | only in unittests
      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 // This file contains various mock objects for the chrome platform to make
      6 // unit testing easier.
      7 
      8 (function(scope){
      9 
     10 var chromeMocks = {};
     11 
     12 chromeMocks.Event = function() {
     13   this.listeners_ = [];
     14 };
     15 
     16 chromeMocks.Event.prototype.addListener = function(callback) {
     17   this.listeners_.push(callback);
     18 };
     19 
     20 chromeMocks.Event.prototype.removeListener = function(callback) {
     21   for (var i = 0; i < this.listeners_.length; i++) {
     22     if (this.listeners_[i] === callback) {
     23       this.listeners_.splice(i, 1);
     24       break;
     25     }
     26   }
     27 };
     28 
     29 chromeMocks.Event.prototype.mock$fire = function(data) {
     30   this.listeners_.forEach(function(listener){
     31     listener(data);
     32   });
     33 };
     34 
     35 chromeMocks.runtime = {};
     36 
     37 chromeMocks.runtime.Port = function() {
     38   this.onMessage = new chromeMocks.Event();
     39   this.onDisconnect = new chromeMocks.Event();
     40 
     41   this.name = '';
     42   this.sender = null;
     43 };
     44 
     45 chromeMocks.runtime.Port.prototype.disconnect = function() {};
     46 chromeMocks.runtime.Port.prototype.postMessage = function() {};
     47 
     48 scope.chromeMocks = chromeMocks;
     49 
     50 })(window);
     51