summaryrefslogtreecommitdiffstats
path: root/xpcom/rust/gtest/xpcom/TestXpcom.cpp
blob: 69425274f60ef02d4e2a9434c447c928137971db (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/* 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 "nsIObserver.h"
#include "mozilla/Services.h"
#include "nsIObserverService.h"

extern "C" nsIObserverService* Rust_ObserveFromRust();

TEST(RustXpcom, ObserverFromRust)
{
  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)
{
  bool itWorked = false;
  nsCOMPtr<nsIRunnable> runnable;
  Rust_ImplementRunnableInRust(&itWorked, getter_AddRefs(runnable));

  EXPECT_TRUE(runnable);
  EXPECT_FALSE(itWorked);
  runnable->Run();
  EXPECT_TRUE(itWorked);
}

extern "C" void Rust_GetMultipleInterfaces(nsIRunnable** aRunnable,
                                           nsIObserver** aObserver);

TEST(RustXpcom, DynamicCastVoid)
{
  nsCOMPtr<nsIRunnable> runnable;
  nsCOMPtr<nsIObserver> observer;
  Rust_GetMultipleInterfaces(getter_AddRefs(runnable),
                             getter_AddRefs(observer));

  // They should have different addresses when `static_cast` to void*
  EXPECT_NE(static_cast<void*>(runnable.get()),
            static_cast<void*>(observer.get()));

  // These should be the same object
  nsCOMPtr<nsISupports> runnableSupports = do_QueryInterface(runnable);
  nsCOMPtr<nsISupports> observerSupports = do_QueryInterface(observer);
  EXPECT_EQ(runnableSupports.get(), observerSupports.get());

#ifndef XP_WIN
  // They should have the same address when dynamic_cast to void*
  // dynamic_cast<void*> is not supported without rtti on windows.
  EXPECT_EQ(dynamic_cast<void*>(runnable.get()),
            dynamic_cast<void*>(observer.get()));

  // The nsISupports pointer from `do_QueryInterface` should match
  // `dynamic_cast<void*>`
  EXPECT_EQ(dynamic_cast<void*>(observer.get()),
            static_cast<void*>(observerSupports.get()));
#endif
}