PageRenderTime 21ms CodeModel.GetById 15ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 0ms

/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
 37Cc["@mozilla.org/moz/jssubscript-loader;1"]
 38  .getService(Ci.mozIJSSubScriptLoader)
 39  .loadSubScript("chrome://mochitests/content/browser/toolkit/content/tests/browser/common/mockObjects.js",
 40                 this);
 41
 42var mockFilePickerSettings = {
 43  /**
 44   * File object pointing to the directory where the files will be saved.
 45   * The files will be saved with the default name, and will be overwritten
 46   * if they exist.
 47   */
 48  destDir: null,
 49
 50  /**
 51   * Index of the filter to be returned by the file picker, or -1 to return
 52   * the filter proposed by the caller.
 53   */
 54  filterIndex: -1
 55};
 56
 57var mockFilePickerResults = {
 58  /**
 59   * File object corresponding to the last automatically selected file.
 60   */
 61  selectedFile: null,
 62
 63  /**
 64   * Index of the filter that was set on the file picker by the caller.
 65   */
 66  proposedFilterIndex: -1
 67};
 68
 69/**
 70 * This file picker implementation uses the global settings defined in
 71 * mockFilePickerSettings, and updates the mockFilePickerResults object
 72 * when its "show" method is called.
 73 */
 74function MockFilePicker() { };
 75MockFilePicker.prototype = {
 76  QueryInterface: XPCOMUtils.generateQI([Ci.nsIFilePicker]),
 77  init: function () {},
 78  appendFilters: function () {},
 79  appendFilter: function () {},
 80  defaultString: "",
 81  defaultExtension: "",
 82  filterIndex: 0,
 83  displayDirectory: null,
 84  file: null,
 85  get fileURL() {
 86    return Cc["@mozilla.org/network/io-service;1"].
 87           getService(Ci.nsIIOService).newFileURI(this.file);
 88  },
 89  get files() {
 90    throw Cr.NS_ERROR_NOT_IMPLEMENTED;
 91  },
 92  show: function MFP_show() {
 93    // Select the destination file with the specified default file name. If the
 94    // default file name was never specified or was set to an empty string by
 95    // the caller, ensure that a fallback file name is used.
 96    this.file = mockFilePickerSettings.destDir.clone();
 97    this.file.append(this.defaultString || "no_default_file_name");
 98    // Store the current file picker settings for testing them later.
 99    mockFilePickerResults.selectedFile = this.file.clone();
100    mockFilePickerResults.proposedFilterIndex = this.filterIndex;
101    // Select a different file filter if required.
102    if (mockFilePickerSettings.filterIndex != -1)
103      this.filterIndex = mockFilePickerSettings.filterIndex;
104    // Assume we overwrite the file if it exists.
105    return (this.file.exists() ?
106            Ci.nsIFilePicker.returnReplace :
107            Ci.nsIFilePicker.returnOK);
108  }
109};
110
111// Create an instance of a MockObjectRegisterer whose methods can be used to
112// temporarily replace the default "@mozilla.org/filepicker;1" object factory
113// with one that provides the mock implementation above. To activate the mock
114// object factory, call the "register" method. Starting from that moment, all
115// the file picker objects that are requested will be mock objects, until the
116// "unregister" method is called.
117var mockFilePickerRegisterer =
118  new MockObjectRegisterer("@mozilla.org/filepicker;1",
119                           MockFilePicker);