/toolkit/content/tests/browser/common/mockFilePicker.js

http://github.com/zpao/v8monkey · JavaScript · 119 lines · 45 code · 7 blank · 67 comment · 3 complexity · 13a9715d9e1403f7ecb07751ce961fcd MD5 · raw file

  1. /* ***** BEGIN LICENSE BLOCK *****
  2. * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  3. *
  4. * The contents of this file are subject to the Mozilla Public License Version
  5. * 1.1 (the "License"); you may not use this file except in compliance with
  6. * the License. You may obtain a copy of the License at
  7. * http://www.mozilla.org/MPL/
  8. *
  9. * Software distributed under the License is distributed on an "AS IS" basis,
  10. * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  11. * for the specific language governing rights and limitations under the
  12. * License.
  13. *
  14. * The Original Code is Mozilla XUL Toolkit Testing Code.
  15. *
  16. * The Initial Developer of the Original Code is
  17. * Paolo Amadini <http://www.amadzone.org/>.
  18. * Portions created by the Initial Developer are Copyright (C) 2009
  19. * the Initial Developer. All Rights Reserved.
  20. *
  21. * Contributor(s):
  22. *
  23. * Alternatively, the contents of this file may be used under the terms of
  24. * either the GNU General Public License Version 2 or later (the "GPL"), or
  25. * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  26. * in which case the provisions of the GPL or the LGPL are applicable instead
  27. * of those above. If you wish to allow use of your version of this file only
  28. * under the terms of either the GPL or the LGPL, and not to allow others to
  29. * use your version of this file under the terms of the MPL, indicate your
  30. * decision by deleting the provisions above and replace them with the notice
  31. * and other provisions required by the GPL or the LGPL. If you do not delete
  32. * the provisions above, a recipient may use your version of this file under
  33. * the terms of any one of the MPL, the GPL or the LGPL.
  34. *
  35. * ***** END LICENSE BLOCK ***** */
  36. Cc["@mozilla.org/moz/jssubscript-loader;1"]
  37. .getService(Ci.mozIJSSubScriptLoader)
  38. .loadSubScript("chrome://mochitests/content/browser/toolkit/content/tests/browser/common/mockObjects.js",
  39. this);
  40. var mockFilePickerSettings = {
  41. /**
  42. * File object pointing to the directory where the files will be saved.
  43. * The files will be saved with the default name, and will be overwritten
  44. * if they exist.
  45. */
  46. destDir: null,
  47. /**
  48. * Index of the filter to be returned by the file picker, or -1 to return
  49. * the filter proposed by the caller.
  50. */
  51. filterIndex: -1
  52. };
  53. var mockFilePickerResults = {
  54. /**
  55. * File object corresponding to the last automatically selected file.
  56. */
  57. selectedFile: null,
  58. /**
  59. * Index of the filter that was set on the file picker by the caller.
  60. */
  61. proposedFilterIndex: -1
  62. };
  63. /**
  64. * This file picker implementation uses the global settings defined in
  65. * mockFilePickerSettings, and updates the mockFilePickerResults object
  66. * when its "show" method is called.
  67. */
  68. function MockFilePicker() { };
  69. MockFilePicker.prototype = {
  70. QueryInterface: XPCOMUtils.generateQI([Ci.nsIFilePicker]),
  71. init: function () {},
  72. appendFilters: function () {},
  73. appendFilter: function () {},
  74. defaultString: "",
  75. defaultExtension: "",
  76. filterIndex: 0,
  77. displayDirectory: null,
  78. file: null,
  79. get fileURL() {
  80. return Cc["@mozilla.org/network/io-service;1"].
  81. getService(Ci.nsIIOService).newFileURI(this.file);
  82. },
  83. get files() {
  84. throw Cr.NS_ERROR_NOT_IMPLEMENTED;
  85. },
  86. show: function MFP_show() {
  87. // Select the destination file with the specified default file name. If the
  88. // default file name was never specified or was set to an empty string by
  89. // the caller, ensure that a fallback file name is used.
  90. this.file = mockFilePickerSettings.destDir.clone();
  91. this.file.append(this.defaultString || "no_default_file_name");
  92. // Store the current file picker settings for testing them later.
  93. mockFilePickerResults.selectedFile = this.file.clone();
  94. mockFilePickerResults.proposedFilterIndex = this.filterIndex;
  95. // Select a different file filter if required.
  96. if (mockFilePickerSettings.filterIndex != -1)
  97. this.filterIndex = mockFilePickerSettings.filterIndex;
  98. // Assume we overwrite the file if it exists.
  99. return (this.file.exists() ?
  100. Ci.nsIFilePicker.returnReplace :
  101. Ci.nsIFilePicker.returnOK);
  102. }
  103. };
  104. // Create an instance of a MockObjectRegisterer whose methods can be used to
  105. // temporarily replace the default "@mozilla.org/filepicker;1" object factory
  106. // with one that provides the mock implementation above. To activate the mock
  107. // object factory, call the "register" method. Starting from that moment, all
  108. // the file picker objects that are requested will be mock objects, until the
  109. // "unregister" method is called.
  110. var mockFilePickerRegisterer =
  111. new MockObjectRegisterer("@mozilla.org/filepicker;1",
  112. MockFilePicker);