Bug 1293362 - Part 8: Add some tests for using xpcom interfaces from rust code, r=froydnj

MozReview-Commit-ID: 1b6tfHtyDWf
This commit is contained in:
Nika Layzell 2018-01-04 17:38:05 -05:00
Родитель 98ea82060d
Коммит 281f61ed94
3 изменённых файлов: 81 добавлений и 0 удалений

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

@ -6,6 +6,7 @@
UNIFIED_SOURCES += [
'nsstring/Test.cpp',
'xpcom/Test.cpp',
]
FINAL_LIBRARY = 'xul-gtest'

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

@ -0,0 +1,33 @@
/* 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/. */
#include "gtest/gtest.h"
#include "nsCOMPtr.h"
#include "nsIRunnable.h"
#include "mozilla/Services.h"
#include "nsIIOService.h"
extern "C" nsIIOService* Rust_CallIURIFromRust();
TEST(RustXpcom, CallIURIFromRust)
{
nsCOMPtr<nsIIOService> rust = Rust_CallIURIFromRust();
nsCOMPtr<nsIIOService> cpp = mozilla::services::GetIOService();
EXPECT_EQ(rust, cpp);
}
extern "C" void Rust_ImplementRunnableInRust(bool* aItWorked,
nsIRunnable** aRunnable);
TEST(RustXpcom, ImplementRunnableInRust)
{
bool itWorked = false;
nsCOMPtr<nsIRunnable> runnable;
Rust_ImplementRunnableInRust(&itWorked, getter_AddRefs(runnable));
EXPECT_TRUE(runnable);
EXPECT_FALSE(itWorked);
runnable->Run();
EXPECT_TRUE(itWorked);
}

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

@ -10,3 +10,50 @@ extern crate xpcom;
extern crate nserror;
extern crate nsstring;
use std::ptr;
use xpcom::{XpCom, getter_addrefs, interfaces};
use nserror::{NsresultExt, nsresult, NS_OK};
use nsstring::{nsCStr, nsCString};
#[no_mangle]
pub unsafe extern fn Rust_CallIURIFromRust() -> *const interfaces::nsIIOService {
let iosvc = xpcom::services::get_IOService().unwrap();
let uri = getter_addrefs(|p| iosvc.NewURI(&nsCStr::from("https://google.com"), ptr::null(), ptr::null(), p)).unwrap();
let mut host = nsCString::new();
let rv = uri.GetHost(&mut host);
assert!(rv.succeeded());
assert_eq!(&*host, "google.com");
assert!(iosvc.query_interface::<interfaces::nsISupports>().is_some());
assert!(iosvc.query_interface::<interfaces::nsIURI>().is_none());
&*iosvc
}
#[no_mangle]
pub unsafe extern fn Rust_ImplementRunnableInRust(it_worked: *mut bool,
runnable: *mut *const interfaces::nsIRunnable) {
// Define a type which implements nsIRunnable in rust.
#[derive(xpcom)]
#[xpimplements(nsIRunnable)]
#[refcnt = "atomic"]
struct InitMyRunnable {
it_worked: *mut bool,
}
impl MyRunnable {
unsafe fn Run(&self) -> nsresult {
*self.it_worked = true;
NS_OK
}
}
// Create my runnable type, and forget it into the outparameter!
let my_runnable = MyRunnable::allocate(InitMyRunnable {
it_worked: it_worked
});
my_runnable.query_interface::<interfaces::nsIRunnable>().unwrap().forget(&mut *runnable);
}