From d6b227ce9555f4bae6c2205c084361f3c255dd1f Mon Sep 17 00:00:00 2001 From: Mark Finkle Date: Tue, 28 Oct 2014 17:16:20 -0400 Subject: [PATCH] Bug 1090229 - Add tests for FilePicker r=bnicholson --- mobile/android/base/tests/robocop.ini | 1 + mobile/android/base/tests/testFilePicker.java | 52 +++++++++++++ mobile/android/base/tests/testFilePicker.js | 78 +++++++++++++++++++ mobile/android/components/FilePicker.js | 7 +- 4 files changed, 135 insertions(+), 3 deletions(-) create mode 100644 mobile/android/base/tests/testFilePicker.java create mode 100644 mobile/android/base/tests/testFilePicker.js diff --git a/mobile/android/base/tests/robocop.ini b/mobile/android/base/tests/robocop.ini index 5345e9712179..f1b6e9739631 100644 --- a/mobile/android/base/tests/robocop.ini +++ b/mobile/android/base/tests/robocop.ini @@ -104,6 +104,7 @@ skip-if = android_version == "10" [testBrowserDiscovery] [testDebuggerServer] [testDeviceSearchEngine] +[testFilePicker] [testJNI] # [testMozPay] # see bug 945675 [testNetworkManager] diff --git a/mobile/android/base/tests/testFilePicker.java b/mobile/android/base/tests/testFilePicker.java new file mode 100644 index 000000000000..e83a4dc6e5b4 --- /dev/null +++ b/mobile/android/base/tests/testFilePicker.java @@ -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"); + } +} diff --git a/mobile/android/base/tests/testFilePicker.js b/mobile/android/base/tests/testFilePicker.js new file mode 100644 index 000000000000..ae3d01cdf8f8 --- /dev/null +++ b/mobile/android/base/tests/testFilePicker.js @@ -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(); + diff --git a/mobile/android/components/FilePicker.js b/mobile/android/components/FilePicker.js index 2addcd32cf6d..ca35d3421538 100644 --- a/mobile/android/components/FilePicker.js +++ b/mobile/android/components/FilePicker.js @@ -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, };