Bug 1433015 - Part 2: Change the xpcom test to use the observer service, r=froydnj
authorNika Layzell <nika@thelayzells.com>
Mon, 29 Jan 2018 17:24:08 -0500
changeset 401628 ba613dacbfa6cebfc48aca3fca7c3926cd13c8e0
parent 401627 316ac856dc899dcd53f38da4f82d4fd23f8b1e0a
child 401629 64084638c5562111f4c57baa69f9b63e2206c14c
push id33351
push userbtara@mozilla.com
push dateWed, 31 Jan 2018 10:00:45 +0000
treeherdermozilla-central@7b46ef2ae141 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersfroydnj
bugs1433015
milestone60.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1433015 - Part 2: Change the xpcom test to use the observer service, r=froydnj MozReview-Commit-ID: JSnMIvu97aT
xpcom/rust/gtest/xpcom/Test.cpp
xpcom/rust/gtest/xpcom/test.rs
--- a/xpcom/rust/gtest/xpcom/Test.cpp
+++ b/xpcom/rust/gtest/xpcom/Test.cpp
@@ -1,24 +1,24 @@
 /* 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"
+#include "nsIObserverService.h"
 
-extern "C" nsIIOService* Rust_CallIURIFromRust();
+extern "C" nsIObserverService* Rust_ObserveFromRust();
 
-TEST(RustXpcom, CallIURIFromRust)
+TEST(RustXpcom, ObserverFromRust)
 {
-  nsCOMPtr<nsIIOService> rust = Rust_CallIURIFromRust();
-  nsCOMPtr<nsIIOService> cpp = mozilla::services::GetIOService();
+  nsCOMPtr<nsIObserverService> rust = Rust_ObserveFromRust();
+  nsCOMPtr<nsIObserverService> cpp = mozilla::services::GetObserverService();
   EXPECT_EQ(rust, cpp);
 }
 
 extern "C" void Rust_ImplementRunnableInRust(bool* aItWorked,
                                              nsIRunnable** aRunnable);
 
 TEST(RustXpcom, ImplementRunnableInRust)
 {
--- a/xpcom/rust/gtest/xpcom/test.rs
+++ b/xpcom/rust/gtest/xpcom/test.rs
@@ -4,38 +4,63 @@
 
 #![allow(non_snake_case)]
 
 #[macro_use]
 extern crate xpcom;
 
 extern crate nserror;
 
-extern crate nsstring;
-
 use std::ptr;
-use xpcom::{XpCom, getter_addrefs, interfaces};
+use std::ffi::{CStr, CString};
+use xpcom::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();
+pub unsafe extern fn Rust_ObserveFromRust() -> *const interfaces::nsIObserverService {
+    let obssvc = xpcom::services::get_ObserverService().unwrap();
 
-    let uri = getter_addrefs(|p| iosvc.NewURI(&nsCStr::from("https://google.com"), ptr::null(), ptr::null(), p)).unwrap();
+    // Define an observer
+    #[derive(xpcom)]
+    #[xpimplements(nsIObserver)]
+    #[refcnt = "nonatomic"]
+    struct InitObserver {
+        run: *mut bool
+    }
+    impl Observer {
+        unsafe fn Observe(
+            &self,
+            _subject: *const interfaces::nsISupports,
+            topic: *const i8,
+            _data: *const i16
+        ) -> nsresult {
+            *self.run = true;
+            assert!(CStr::from_ptr(topic).to_str() == Ok("test-rust-observe"));
+            NS_OK
+        }
+    }
 
-    let mut host = nsCString::new();
-    let rv = uri.GetHost(&mut host);
+    let topic = CString::new("test-rust-observe").unwrap();
+
+    let mut run = false;
+    let observer = Observer::allocate(InitObserver{ run: &mut run });
+    let rv = obssvc.AddObserver(observer.coerce::<interfaces::nsIObserver>(), topic.as_ptr(), false);
     assert!(rv.succeeded());
 
-    assert_eq!(&*host, "google.com");
+    let rv = obssvc.NotifyObservers(ptr::null(), topic.as_ptr(), ptr::null());
+    assert!(rv.succeeded());
+    assert!(run, "The observer should have been run!");
 
-    assert!(iosvc.query_interface::<interfaces::nsISupports>().is_some());
-    assert!(iosvc.query_interface::<interfaces::nsIURI>().is_none());
-    &*iosvc
+    let rv = obssvc.RemoveObserver(observer.coerce::<interfaces::nsIObserver>(), topic.as_ptr());
+    assert!(rv.succeeded());
+
+    assert!(observer.coerce::<interfaces::nsISupports>() as *const _==
+            &*observer.query_interface::<interfaces::nsISupports>().unwrap() as *const _);
+
+    &*obssvc
 }
 
 #[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)]