1 #include "TestAsyncReturns.h"
2 
3 #include "IPDLUnitTests.h"  // fail etc.
4 
5 #include "mozilla/AbstractThread.h"
6 #include "mozilla/Unused.h"
7 
8 namespace mozilla {
9 namespace _ipdltest {
10 
11 static uint32_t sMagic1 = 0x105b59fb;
12 static uint32_t sMagic2 = 0x09b6f5e3;
13 
14 //-----------------------------------------------------------------------------
15 // parent
16 
TestAsyncReturnsParent()17 TestAsyncReturnsParent::TestAsyncReturnsParent() {
18   MOZ_COUNT_CTOR(TestAsyncReturnsParent);
19 }
20 
~TestAsyncReturnsParent()21 TestAsyncReturnsParent::~TestAsyncReturnsParent() {
22   MOZ_COUNT_DTOR(TestAsyncReturnsParent);
23 }
24 
Main()25 void TestAsyncReturnsParent::Main() {
26   SendNoReturn()->Then(
27       MessageLoop::current()->SerialEventTarget(), __func__,
28       [](bool unused) { fail("resolve handler should not be called"); },
29       [](ResponseRejectReason&& aReason) {
30         // MozPromise asserts in debug build if the
31         // handler is not called
32         if (aReason != ResponseRejectReason::ChannelClosed) {
33           fail("reject with wrong reason");
34         }
35         passed("reject handler called on channel close");
36       });
37   SendPing()->Then(
38       MessageLoop::current()->SerialEventTarget(), __func__,
39       [this](bool one) {
40         if (one) {
41           passed("take one argument");
42         } else {
43           fail("get one argument but has wrong value");
44         }
45 
46         // Also try with the callback-based API.
47         SendPing(
48             [this](bool one) {
49               if (one) {
50                 passed("take one argument");
51               } else {
52                 fail("get one argument but has wrong value");
53               }
54               Close();
55             },
56             [](ResponseRejectReason&& aReason) { fail("sending Ping"); });
57       },
58       [](ResponseRejectReason&& aReason) { fail("sending Ping"); });
59 }
60 
RecvPong(PongResolver && aResolve)61 mozilla::ipc::IPCResult TestAsyncReturnsParent::RecvPong(
62     PongResolver&& aResolve) {
63   aResolve(Tuple<const uint32_t&, const uint32_t&>(sMagic1, sMagic2));
64   return IPC_OK();
65 }
66 
67 //-----------------------------------------------------------------------------
68 // child
69 
TestAsyncReturnsChild()70 TestAsyncReturnsChild::TestAsyncReturnsChild() {
71   MOZ_COUNT_CTOR(TestAsyncReturnsChild);
72 }
73 
~TestAsyncReturnsChild()74 TestAsyncReturnsChild::~TestAsyncReturnsChild() {
75   MOZ_COUNT_DTOR(TestAsyncReturnsChild);
76 }
77 
RecvNoReturn(NoReturnResolver && aResolve)78 mozilla::ipc::IPCResult TestAsyncReturnsChild::RecvNoReturn(
79     NoReturnResolver&& aResolve) {
80   // Not resolving the promise intentionally
81   return IPC_OK();
82 }
83 
RecvPing(PingResolver && aResolve)84 mozilla::ipc::IPCResult TestAsyncReturnsChild::RecvPing(
85     PingResolver&& aResolve) {
86   SendPong()->Then(
87       MessageLoop::current()->SerialEventTarget(), __func__,
88       [aResolve](const Tuple<uint32_t, uint32_t>& aParam) {
89         if (Get<0>(aParam) == sMagic1 && Get<1>(aParam) == sMagic2) {
90           passed("take two arguments");
91         } else {
92           fail("get two argument but has wrong value");
93         }
94         aResolve(true);
95       },
96       [](ResponseRejectReason&& aReason) { fail("sending Pong"); });
97   return IPC_OK();
98 }
99 
100 }  // namespace _ipdltest
101 }  // namespace mozilla
102