Auto merge of #11717 - izgzhen:filepicker, r=Manishearth

Add filepicker

Add file picker based on tinyfiledialog to the file manager implementation.

Changes:
- [x] Add the picker invocation code
- [x] Rewrite unit test to accommodate the change
- [x] Patch up `htmlinputelement` to make things work

<!-- Please describe your changes on the following line: -->

---
<!-- Thank you for contributing to Servo! Please replace each `[ ]` by `[X]` when the step is complete, and replace `__` with appropriate data: -->
- [x] `./mach build -d` does not report any errors
- [x] `./mach test-tidy` does not report any errors
- [x] These changes are related to #11131.

<!-- Either: -->
- [x] There are tests for these changes OR

<!-- Pull requests that do not address these steps are welcome, but they will require additional verification as part of the review process. -->

<!-- Reviewable:start -->
---
This change is [<img src="https://reviewable.io/review_button.svg" height="35" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/servo/11717)
<!-- Reviewable:end -->
This commit is contained in:
bors-servo 2016-06-14 07:27:47 -05:00 committed by GitHub
commit 0cfae3a3e7
5 changed files with 135 additions and 46 deletions

View file

@ -3,15 +3,29 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
use ipc_channel::ipc::{self, IpcSender};
use net::filemanager_thread::FileManagerThreadFactory;
use net_traits::filemanager_thread::{FileManagerThreadMsg, FileManagerThreadError};
use net::filemanager_thread::{FileManagerThreadFactory, UIProvider};
use net_traits::filemanager_thread::{FilterPattern, FileManagerThreadMsg, FileManagerThreadError};
use std::fs::File;
use std::io::Read;
use std::path::PathBuf;
const TEST_PROVIDER: &'static TestProvider = &TestProvider;
struct TestProvider;
impl UIProvider for TestProvider {
fn open_file_dialog(&self, _: &str, _: Option<(&[&str], &str)>) -> Option<String> {
Some("test.txt".to_string())
}
fn open_file_dialog_multi(&self, _: &str, _: Option<(&[&str], &str)>) -> Option<Vec<String>> {
Some(vec!["test.txt".to_string()])
}
}
#[test]
fn test_filemanager() {
let chan: IpcSender<FileManagerThreadMsg> = FileManagerThreadFactory::new();
let chan: IpcSender<FileManagerThreadMsg> = FileManagerThreadFactory::new(TEST_PROVIDER);
// Try to open a dummy file "tests/unit/net/test.txt" in tree
let mut handler = File::open("test.txt").expect("test.txt is stolen");
@ -20,11 +34,13 @@ fn test_filemanager() {
handler.read_to_end(&mut test_file_content)
.expect("Read tests/unit/net/test.txt error");
let patterns = vec![FilterPattern(".txt".to_string())];
{
// Try to select a dummy file "tests/unit/net/test.txt"
let (tx, rx) = ipc::channel().unwrap();
chan.send(FileManagerThreadMsg::SelectFile(tx)).unwrap();
chan.send(FileManagerThreadMsg::SelectFile(patterns.clone(), tx)).unwrap();
let selected = rx.recv().expect("File manager channel is broken")
.expect("The file manager failed to find test.txt");
@ -66,7 +82,7 @@ fn test_filemanager() {
{
let (tx, rx) = ipc::channel().unwrap();
let _ = chan.send(FileManagerThreadMsg::SelectFile(tx));
let _ = chan.send(FileManagerThreadMsg::SelectFile(patterns.clone(), tx));
assert!(rx.try_recv().is_err(), "The thread should not respond normally after exited");
}