mockportnetwork.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. // Copyright 2011 The Closure Library Authors. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS-IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. /**
  15. * @fileoverview A fake PortNetwork implementation that simply produces
  16. * MockMessageChannels for all ports.
  17. *
  18. */
  19. goog.setTestOnly('goog.testing.messaging.MockPortNetwork');
  20. goog.provide('goog.testing.messaging.MockPortNetwork');
  21. goog.require('goog.messaging.PortNetwork'); // interface
  22. goog.require('goog.testing.messaging.MockMessageChannel');
  23. /**
  24. * The fake PortNetwork.
  25. *
  26. * @param {!goog.testing.MockControl} mockControl The mock control for creating
  27. * the mock message channels.
  28. * @constructor
  29. * @implements {goog.messaging.PortNetwork}
  30. * @final
  31. */
  32. goog.testing.messaging.MockPortNetwork = function(mockControl) {
  33. /**
  34. * The mock control for creating mock message channels.
  35. * @type {!goog.testing.MockControl}
  36. * @private
  37. */
  38. this.mockControl_ = mockControl;
  39. /**
  40. * The mock ports that have been created.
  41. * @type {!Object<!goog.testing.messaging.MockMessageChannel>}
  42. * @private
  43. */
  44. this.ports_ = {};
  45. };
  46. /**
  47. * Get the mock port with the given name.
  48. * @param {string} name The name of the port to get.
  49. * @return {!goog.testing.messaging.MockMessageChannel} The mock port.
  50. * @override
  51. */
  52. goog.testing.messaging.MockPortNetwork.prototype.dial = function(name) {
  53. if (!(name in this.ports_)) {
  54. this.ports_[name] =
  55. new goog.testing.messaging.MockMessageChannel(this.mockControl_);
  56. }
  57. return this.ports_[name];
  58. };