Skip to content

Commit

Permalink
test(net): update test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
kwonoj committed Apr 27, 2018
1 parent f69b124 commit c4c0d26
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 27 deletions.
3 changes: 2 additions & 1 deletion components/net/tests/fetch.rs
Expand Up @@ -552,12 +552,13 @@ fn test_fetch_with_hsts() {
let mut ca_content = String::new();
File::open(cert_path).unwrap().read_to_string(&mut ca_content).unwrap();
let ssl_client = create_ssl_client(&ca_content);
let (sender, _) = channel();

let context = FetchContext {
state: Arc::new(HttpState::new(ssl_client)),
user_agent: DEFAULT_USER_AGENT.into(),
devtools_chan: None,
filemanager: FileManager::new(),
filemanager: FileManager::new(sender),
cancellation_listener: Arc::new(Mutex::new(CancellationListener::new(None))),
};

Expand Down
35 changes: 11 additions & 24 deletions components/net/tests/filemanager_thread.rs
Expand Up @@ -3,30 +3,20 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

use ipc_channel::ipc;
use net::filemanager_thread::{FileManager, UIProvider};
use net::filemanager_thread::FileManager;
use net_traits::blob_url_store::BlobURLStoreError;
use net_traits::filemanager_thread::{FilterPattern, FileManagerThreadMsg, FileManagerThreadError, ReadFileProgress};
use servo_config::prefs::{PrefValue, PREFS};
use std::fs::File;
use std::io::Read;
use std::path::PathBuf;

pub const TEST_PROVIDER: &'static TestProvider = &TestProvider;

pub struct TestProvider;

impl UIProvider for TestProvider {
fn open_file_dialog(&self, _path: &str, _patterns: Vec<FilterPattern>) -> Option<String> {
Some("tests/test.jpeg".to_string())
}

fn open_file_dialog_multi(&self, _path: &str, _patterns: Vec<FilterPattern>) -> Option<Vec<String>> {
Some(vec!["tests/test.jpeg".to_string()])
}
}
use std::sync::mpsc::channel;

#[test]
fn test_filemanager() {
let filemanager = FileManager::new();
let (sender, _) = channel();
let filemanager = FileManager::new(sender);
PREFS.set("dom.testing.htmlinputelement.select_files.enabled", PrefValue::Boolean(true));

// Try to open a dummy file "components/net/tests/test.jpeg" in tree
let mut handler = File::open("tests/test.jpeg").expect("test.jpeg is stolen");
Expand All @@ -41,8 +31,8 @@ fn test_filemanager() {
{
// Try to select a dummy file "components/net/tests/test.jpeg"
let (tx, rx) = ipc::channel().unwrap();
filemanager.handle(FileManagerThreadMsg::SelectFile(patterns.clone(), tx, origin.clone(), None),
TEST_PROVIDER);
filemanager.handle(FileManagerThreadMsg::SelectFile(patterns.clone(), tx, origin.clone(),
Some("tests/test.jpeg".to_string())));
let selected = rx.recv().expect("Broken channel")
.expect("The file manager failed to find test.jpeg");

Expand All @@ -53,8 +43,7 @@ fn test_filemanager() {
// Test by reading, expecting same content
{
let (tx2, rx2) = ipc::channel().unwrap();
filemanager.handle(FileManagerThreadMsg::ReadFile(tx2, selected.id.clone(), false, origin.clone()),
TEST_PROVIDER);
filemanager.handle(FileManagerThreadMsg::ReadFile(tx2, selected.id.clone(), false, origin.clone()));

let msg = rx2.recv().expect("Broken channel");

Expand Down Expand Up @@ -84,8 +73,7 @@ fn test_filemanager() {
// Delete the id
{
let (tx2, rx2) = ipc::channel().unwrap();
filemanager.handle(FileManagerThreadMsg::DecRef(selected.id.clone(), origin.clone(), tx2),
TEST_PROVIDER);
filemanager.handle(FileManagerThreadMsg::DecRef(selected.id.clone(), origin.clone(), tx2));

let ret = rx2.recv().expect("Broken channel");
assert!(ret.is_ok(), "DecRef is not okay");
Expand All @@ -94,8 +82,7 @@ fn test_filemanager() {
// Test by reading again, expecting read error because we invalidated the id
{
let (tx2, rx2) = ipc::channel().unwrap();
filemanager.handle(FileManagerThreadMsg::ReadFile(tx2, selected.id.clone(), false, origin.clone()),
TEST_PROVIDER);
filemanager.handle(FileManagerThreadMsg::ReadFile(tx2, selected.id.clone(), false, origin.clone()));

let msg = rx2.recv().expect("Broken channel");

Expand Down
5 changes: 4 additions & 1 deletion components/net/tests/main.rs
Expand Up @@ -16,6 +16,8 @@ extern crate msg;
extern crate net;
extern crate net_traits;
extern crate profile_traits;
extern crate script_traits;
extern crate servo_config;
extern crate servo_url;
extern crate time;
extern crate unicase;
Expand Down Expand Up @@ -56,11 +58,12 @@ struct FetchResponseCollector {

fn new_fetch_context(dc: Option<Sender<DevtoolsControlMsg>>) -> FetchContext {
let ssl_client = create_ssl_client(&resources::read_string(Resource::SSLCertificates));
let (sender, _) = channel();
FetchContext {
state: Arc::new(HttpState::new(ssl_client)),
user_agent: DEFAULT_USER_AGENT.into(),
devtools_chan: dc,
filemanager: FileManager::new(),
filemanager: FileManager::new(sender),
cancellation_listener: Arc::new(Mutex::new(CancellationListener::new(None))),
}
}
Expand Down
7 changes: 6 additions & 1 deletion components/net/tests/resource_thread.rs
Expand Up @@ -8,7 +8,9 @@ use net::test::parse_hostsfile;
use net_traits::CoreResourceMsg;
use profile_traits::mem::ProfilerChan as MemProfilerChan;
use profile_traits::time::ProfilerChan;
use script_traits::ConstellationMsg;
use std::net::IpAddr;
use std::sync::mpsc::channel;

fn ip(s: &str) -> IpAddr {
s.parse().unwrap()
Expand All @@ -18,9 +20,12 @@ fn ip(s: &str) -> IpAddr {
fn test_exit() {
let (tx, _rx) = ipc::channel().unwrap();
let (mtx, _mrx) = ipc::channel().unwrap();
let (constellation, _) = channel();
let (sender, receiver) = ipc::channel().unwrap();
let (resource_thread, _private_resource_thread) = new_core_resource_thread(
let (resource_thread, _private_resource_thread, constellation_sender) = new_core_resource_thread(
"".into(), None, ProfilerChan(tx), MemProfilerChan(mtx), None);
constellation_sender.send(constellation.clone()).unwrap();
let _ = constellation.send(ConstellationMsg::Exit);
resource_thread.send(CoreResourceMsg::Exit(sender)).unwrap();
receiver.recv().unwrap();
}
Expand Down

0 comments on commit c4c0d26

Please sign in to comment.