Add unit testing to file manager; Replace uuid by stringified version in FileManagerThreadMsg

This commit is contained in:
Zhen Zhang 2016-06-02 15:08:44 +08:00
parent 3977128d7e
commit f8fa9aaf42
7 changed files with 100 additions and 73 deletions

View file

@ -5,18 +5,60 @@
use ipc_channel::ipc::{self, IpcSender};
use net::filemanager_thread::FileManagerThreadFactory;
use net_traits::filemanager_thread::{FileManagerThreadMsg, FileManagerThreadError};
use std::fs::File;
use std::io::Read;
use std::path::PathBuf;
#[test]
fn test_filemanager() {
let chan: IpcSender<FileManagerThreadMsg> = FileManagerThreadFactory::new();
{
let (tx, rx) = ipc::channel().unwrap();
let _ = chan.send(FileManagerThreadMsg::SelectFile(tx));
// 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");
let mut test_file_content = vec![];
match rx.recv().unwrap() {
Err(FileManagerThreadError::InvalidSelection) => {},
_ => assert!(false, "Should be an invalid selection before dialog is implemented"),
handler.read_to_end(&mut test_file_content)
.expect("Read tests/unit/net/test.txt error");
{
// Try to select a dummy file "tests/unit/net/test.txt"
let (tx, rx) = ipc::channel().unwrap();
chan.send(FileManagerThreadMsg::SelectFile(tx)).unwrap();
let selected = rx.recv().expect("File manager channel is broken")
.expect("The file manager failed to find test.txt");
// Expecting attributes conforming the spec
assert!(selected.filename == PathBuf::from("test.txt"));
assert!(selected.type_string == "text/plain".to_string());
// Test by reading, expecting same content
{
let (tx2, rx2) = ipc::channel().unwrap();
chan.send(FileManagerThreadMsg::ReadFile(tx2, selected.id.clone())).unwrap();
let msg = rx2.recv().expect("File manager channel is broken");
let vec = msg.expect("File manager reading failure is unexpected");
assert!(test_file_content == vec, "Read content differs");
}
// Delete the id
chan.send(FileManagerThreadMsg::DeleteFileID(selected.id.clone())).unwrap();
// Test by reading again, expecting read error because we invalidated the id
{
let (tx2, rx2) = ipc::channel().unwrap();
chan.send(FileManagerThreadMsg::ReadFile(tx2, selected.id.clone())).unwrap();
let msg = rx2.recv().expect("File manager channel is broken");
match msg {
Err(FileManagerThreadError::ReadFileError) => {},
other => {
assert!(false, "Get unexpected response after deleting the id: {:?}", other);
}
}
}
}
@ -26,9 +68,6 @@ fn test_filemanager() {
let (tx, rx) = ipc::channel().unwrap();
let _ = chan.send(FileManagerThreadMsg::SelectFile(tx));
match rx.try_recv() {
Ok(_) => assert!(false, "The thread should not response fine after exited"),
Err(_) => {},
}
assert!(rx.try_recv().is_err(), "The thread should not respond normally after exited");
}
}