onlinehandler.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. // Copyright 2012 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 NetworkStatusMonitor test double.
  16. * @author dbk@google.com (David Barrett-Kahn)
  17. */
  18. goog.setTestOnly('goog.testing.events.OnlineHandler');
  19. goog.provide('goog.testing.events.OnlineHandler');
  20. goog.require('goog.events.EventTarget');
  21. goog.require('goog.net.NetworkStatusMonitor');
  22. /**
  23. * NetworkStatusMonitor test double.
  24. * @param {boolean} initialState The initial online state of the mock.
  25. * @constructor
  26. * @extends {goog.events.EventTarget}
  27. * @implements {goog.net.NetworkStatusMonitor}
  28. * @final
  29. */
  30. goog.testing.events.OnlineHandler = function(initialState) {
  31. goog.testing.events.OnlineHandler.base(this, 'constructor');
  32. /**
  33. * Whether the mock is online.
  34. * @private {boolean}
  35. */
  36. this.online_ = initialState;
  37. };
  38. goog.inherits(goog.testing.events.OnlineHandler, goog.events.EventTarget);
  39. /** @override */
  40. goog.testing.events.OnlineHandler.prototype.isOnline = function() {
  41. return this.online_;
  42. };
  43. /**
  44. * Sets the online state.
  45. * @param {boolean} newOnlineState The new online state.
  46. */
  47. goog.testing.events.OnlineHandler.prototype.setOnline = function(
  48. newOnlineState) {
  49. if (newOnlineState != this.online_) {
  50. this.online_ = newOnlineState;
  51. this.dispatchEvent(
  52. newOnlineState ? goog.net.NetworkStatusMonitor.EventType.ONLINE :
  53. goog.net.NetworkStatusMonitor.EventType.OFFLINE);
  54. }
  55. };