Bug 1090229 - Add tests for FilePicker r=bnicholson

This commit is contained in:
Mark Finkle 2014-10-28 17:16:20 -04:00
Родитель c44b0eed31
Коммит d6b227ce95
4 изменённых файлов: 135 добавлений и 3 удалений

Просмотреть файл

@ -104,6 +104,7 @@ skip-if = android_version == "10"
[testBrowserDiscovery]
[testDebuggerServer]
[testDeviceSearchEngine]
[testFilePicker]
[testJNI]
# [testMozPay] # see bug 945675
[testNetworkManager]

Просмотреть файл

@ -0,0 +1,52 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */
package org.mozilla.gecko.tests;
import static org.mozilla.gecko.tests.helpers.AssertionHelper.fFail;
import org.mozilla.gecko.EventDispatcher;
import org.mozilla.gecko.GeckoAppShell;
import org.mozilla.gecko.GeckoEvent;
import org.mozilla.gecko.util.GeckoEventListener;
import org.json.JSONException;
import org.json.JSONObject;
public class testFilePicker extends JavascriptTest implements GeckoEventListener {
private static final String TEST_FILENAME = "/mnt/sdcard/my-favorite-martian.png";
public testFilePicker() {
super("testFilePicker.js");
}
@Override
public void handleMessage(String event, final JSONObject message) {
// We handle the FilePicker message here so we can send back hard coded file information. We
// don't want to try to emulate "picking" a file using the Android intent chooser.
if (event.equals("FilePicker:Show")) {
try {
message.put("file", TEST_FILENAME);
} catch (JSONException ex) {
fFail("Can't add filename to message " + TEST_FILENAME);
}
GeckoAppShell.sendEventToGecko(GeckoEvent.createBroadcastEvent("FilePicker:Result", message.toString()));
}
}
@Override
public void setUp() throws Exception {
super.setUp();
EventDispatcher.getInstance().registerGeckoThreadListener(this, "FilePicker:Show");
}
@Override
public void tearDown() throws Exception {
super.tearDown();
EventDispatcher.getInstance().unregisterGeckoThreadListener(this, "FilePicker:Show");
}
}

Просмотреть файл

@ -0,0 +1,78 @@
// -*- indent-tabs-mode: nil; js-indent-level: 2 -*-
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */
"use strict";
const { classes: Cc, interfaces: Ci, utils: Cu } = Components;
Cu.import("resource://gre/modules/Services.jsm");
function ok(passed, text) {
do_report_result(passed, text, Components.stack.caller, false);
}
function is(lhs, rhs, text) {
do_report_result(lhs === rhs, text, Components.stack.caller, false);
}
add_test(function filepicker_open() {
let chromeWin = Services.wm.getMostRecentWindow("navigator:browser");
do_test_pending();
let fp = Cc["@mozilla.org/filepicker;1"].createInstance(Ci.nsIFilePicker);
fp.appendFilter("Martian files", "*.martian");
fp.appendFilters(Ci.nsIFilePicker.filterAll);
fp.filterIndex = 0;
let fpCallback = function(result) {
if (result == Ci.nsIFilePicker.returnOK || result == Ci.nsIFilePicker.returnReplace) {
do_print("File: " + fp.file.path);
is(fp.file.path, "/mnt/sdcard/my-favorite-martian.png", "Retrieve the right martian file!");
let files = fp.files;
while (files.hasMoreElements()) {
let file = files.getNext().QueryInterface(Ci.nsIFile);
do_print("File: " + file.path);
is(file.path, "/mnt/sdcard/my-favorite-martian.png", "Retrieve the right martian file from array!");
}
do_print("DOMFile: " + fp.domfile.mozFullPath);
is(fp.domfile.mozFullPath, "/mnt/sdcard/my-favorite-martian.png", "Retrieve the right martian domfile!");
let domfiles = fp.domfiles;
while (domfiles.hasMoreElements()) {
let domfile = domfiles.getNext();
do_print("DOMFile: " + domfile.mozFullPath);
is(domfile.mozFullPath, "/mnt/sdcard/my-favorite-martian.png", "Retrieve the right martian file from domfile array!");
}
do_test_finished();
run_next_test();
}
};
try {
fp.init(chromeWin, "Open", Ci.nsIFilePicker.modeOpen);
} catch(ex) {
ok(false, "Android should support FilePicker.modeOpen: " + ex);
}
fp.open(fpCallback);
});
add_test(function filepicker_save() {
let fp = Cc["@mozilla.org/filepicker;1"].createInstance(Ci.nsIFilePicker);
try {
fp.init(null, "Save", Ci.nsIFilePicker.modeSave);
} catch(ex) {
ok(true, "Android does not support FilePicker.modeSave");
}
run_next_test();
});
run_next_test();

Просмотреть файл

@ -146,8 +146,10 @@ FilePicker.prototype = {
return null;
}
if (this._domWin) {
return new this._domWin.File(f);
let win = this._domWin;
if (win) {
let utils = win.QueryInterface(Ci.nsIInterfaceRequestor).getInterface(Ci.nsIDOMWindowUtils);
return utils.wrapDOMFile(f);
}
return new File(f);
@ -213,7 +215,6 @@ FilePicker.prototype = {
let msg = {
type: "FilePicker:Show",
guid: this.guid,
guid: this.guid,
title: this._title,
};