mockuseragent.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. // Copyright 2008 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 MockUserAgent overrides goog.userAgent.getUserAgentString()
  16. * depending on a specified configuration.
  17. *
  18. */
  19. goog.setTestOnly('goog.testing.MockUserAgent');
  20. goog.provide('goog.testing.MockUserAgent');
  21. goog.require('goog.Disposable');
  22. goog.require('goog.labs.userAgent.util');
  23. goog.require('goog.testing.PropertyReplacer');
  24. goog.require('goog.userAgent');
  25. /**
  26. * Class for unit testing code that uses goog.userAgent.
  27. *
  28. * @extends {goog.Disposable}
  29. * @constructor
  30. * @final
  31. */
  32. goog.testing.MockUserAgent = function() {
  33. goog.Disposable.call(this);
  34. /**
  35. * Property replacer used to mock out User-Agent functions.
  36. * @type {!goog.testing.PropertyReplacer}
  37. * @private
  38. */
  39. this.propertyReplacer_ = new goog.testing.PropertyReplacer();
  40. /**
  41. * The userAgent string used by goog.userAgent.
  42. * @type {?string}
  43. * @private
  44. */
  45. this.userAgent_ = goog.userAgent.getUserAgentString();
  46. /**
  47. * The navigator object used by goog.userAgent
  48. * @type {Object}
  49. * @private
  50. */
  51. this.navigator_ = goog.userAgent.getNavigator();
  52. /**
  53. * The documentMode number used by goog.userAgent
  54. * @type {number|undefined}
  55. * @private
  56. */
  57. this.documentMode_ = goog.userAgent.DOCUMENT_MODE;
  58. };
  59. goog.inherits(goog.testing.MockUserAgent, goog.Disposable);
  60. /**
  61. * Whether this MockUserAgent has been installed.
  62. * @type {boolean}
  63. * @private
  64. */
  65. goog.testing.MockUserAgent.prototype.installed_;
  66. /**
  67. * Installs this MockUserAgent.
  68. */
  69. goog.testing.MockUserAgent.prototype.install = function() {
  70. if (!this.installed_) {
  71. // Stub out user agent functions.
  72. this.propertyReplacer_.replace(
  73. goog.userAgent, 'getUserAgentString',
  74. goog.bind(this.getUserAgentString, this));
  75. this.propertyReplacer_.replace(
  76. goog.labs.userAgent.util, 'getUserAgent',
  77. goog.bind(this.getUserAgentString, this));
  78. // Stub out navigator functions.
  79. this.propertyReplacer_.replace(
  80. goog.userAgent, 'getNavigator', goog.bind(this.getNavigator, this));
  81. // Stub out documentMode functions.
  82. this.propertyReplacer_.replace(
  83. goog.userAgent, 'getDocumentMode_',
  84. goog.bind(this.getDocumentMode, this));
  85. this.propertyReplacer_.replace(
  86. goog.userAgent, 'DOCUMENT_MODE', this.getDocumentMode());
  87. this.installed_ = true;
  88. }
  89. };
  90. /**
  91. * @return {?string} The userAgent set in this class.
  92. */
  93. goog.testing.MockUserAgent.prototype.getUserAgentString = function() {
  94. return this.userAgent_;
  95. };
  96. /**
  97. * @param {string} userAgent The desired userAgent string to use.
  98. */
  99. goog.testing.MockUserAgent.prototype.setUserAgentString = function(userAgent) {
  100. this.userAgent_ = userAgent;
  101. };
  102. /**
  103. * @return {Object} The Navigator set in this class.
  104. */
  105. goog.testing.MockUserAgent.prototype.getNavigator = function() {
  106. return this.navigator_;
  107. };
  108. /**
  109. * @param {Object} navigator The desired Navigator object to use.
  110. */
  111. goog.testing.MockUserAgent.prototype.setNavigator = function(navigator) {
  112. this.navigator_ = navigator;
  113. };
  114. /**
  115. * @return {number|undefined} The documentMode set in this class.
  116. */
  117. goog.testing.MockUserAgent.prototype.getDocumentMode = function() {
  118. return this.documentMode_;
  119. };
  120. /**
  121. * @param {number} documentMode The desired documentMode to use.
  122. */
  123. goog.testing.MockUserAgent.prototype.setDocumentMode = function(documentMode) {
  124. this.documentMode_ = documentMode;
  125. this.propertyReplacer_.set(goog.userAgent, 'DOCUMENT_MODE', documentMode);
  126. };
  127. /**
  128. * Uninstalls the MockUserAgent.
  129. */
  130. goog.testing.MockUserAgent.prototype.uninstall = function() {
  131. if (this.installed_) {
  132. this.propertyReplacer_.reset();
  133. this.installed_ = false;
  134. }
  135. };
  136. /** @override */
  137. goog.testing.MockUserAgent.prototype.disposeInternal = function() {
  138. this.uninstall();
  139. delete this.propertyReplacer_;
  140. delete this.navigator_;
  141. delete this.documentMode_;
  142. goog.testing.MockUserAgent.base(this, 'disposeInternal');
  143. };