fieldmock.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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 Mock of goog.editor.field.
  16. *
  17. * @author robbyw@google.com (Robby Walker)
  18. */
  19. goog.setTestOnly('goog.testing.editor.FieldMock');
  20. goog.provide('goog.testing.editor.FieldMock');
  21. goog.require('goog.dom');
  22. goog.require('goog.dom.Range');
  23. goog.require('goog.editor.Field');
  24. goog.require('goog.testing.LooseMock');
  25. goog.require('goog.testing.mockmatchers');
  26. /**
  27. * Mock of goog.editor.Field.
  28. * @param {Window=} opt_window Window the field would edit. Defaults to
  29. * {@code window}.
  30. * @param {Window=} opt_appWindow "AppWindow" of the field, which can be
  31. * different from {@code opt_window} when mocking a field that uses an
  32. * iframe. Defaults to {@code opt_window}.
  33. * @param {goog.dom.AbstractRange=} opt_range An object (mock or real) to be
  34. * returned by getRange(). If omitted, a new goog.dom.Range is created
  35. * from the window every time getRange() is called.
  36. * @constructor
  37. * @extends {goog.testing.LooseMock}
  38. * @suppress {missingProperties} Mocks do not fit in the type system well.
  39. * @final
  40. */
  41. goog.testing.editor.FieldMock = function(opt_window, opt_appWindow, opt_range) {
  42. goog.testing.LooseMock.call(this, goog.editor.Field);
  43. opt_window = opt_window || window;
  44. opt_appWindow = opt_appWindow || opt_window;
  45. this.getAppWindow();
  46. this.$anyTimes();
  47. this.$returns(opt_appWindow);
  48. this.getRange();
  49. this.$anyTimes();
  50. this.$does(function() {
  51. return opt_range || goog.dom.Range.createFromWindow(opt_window);
  52. });
  53. this.getEditableDomHelper();
  54. this.$anyTimes();
  55. this.$returns(goog.dom.getDomHelper(opt_window.document));
  56. this.usesIframe();
  57. this.$anyTimes();
  58. this.getBaseZindex();
  59. this.$anyTimes();
  60. this.$returns(0);
  61. this.restoreSavedRange(goog.testing.mockmatchers.ignoreArgument);
  62. this.$anyTimes();
  63. this.$does(function(range) {
  64. if (range) {
  65. range.restore();
  66. }
  67. this.focus();
  68. });
  69. // These methods cannot be set on the prototype, because the prototype
  70. // gets stepped on by the mock framework.
  71. var inModalMode = false;
  72. /**
  73. * @return {boolean} Whether we're in modal interaction mode.
  74. */
  75. this.inModalMode = function() { return inModalMode; };
  76. /**
  77. * @param {boolean} mode Sets whether we're in modal interaction mode.
  78. */
  79. this.setModalMode = function(mode) { inModalMode = mode; };
  80. var uneditable = false;
  81. /**
  82. * @return {boolean} Whether the field is uneditable.
  83. */
  84. this.isUneditable = function() { return uneditable; };
  85. /**
  86. * @param {boolean} isUneditable Whether the field is uneditable.
  87. */
  88. this.setUneditable = function(isUneditable) { uneditable = isUneditable; };
  89. };
  90. goog.inherits(goog.testing.editor.FieldMock, goog.testing.LooseMock);