summaryrefslogtreecommitdiffstats
path: root/ipc/ipdl/test/cxx/TestAsyncReturns.cpp
blob: 76597143c0b26b499c03f9dc940a2369f0630624 (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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include "TestAsyncReturns.h"

#include "IPDLUnitTests.h"  // fail etc.

#include "mozilla/AbstractThread.h"
#include "mozilla/Unused.h"

namespace mozilla {
namespace _ipdltest {

static uint32_t sMagic1 = 0x105b59fb;
static uint32_t sMagic2 = 0x09b6f5e3;

//-----------------------------------------------------------------------------
// parent

TestAsyncReturnsParent::TestAsyncReturnsParent() {
  MOZ_COUNT_CTOR(TestAsyncReturnsParent);
}

TestAsyncReturnsParent::~TestAsyncReturnsParent() {
  MOZ_COUNT_DTOR(TestAsyncReturnsParent);
}

void TestAsyncReturnsParent::Main() {
  SendNoReturn()->Then(
      MessageLoop::current()->SerialEventTarget(), __func__,
      [](bool unused) { fail("resolve handler should not be called"); },
      [](ResponseRejectReason&& aReason) {
        // MozPromise asserts in debug build if the
        // handler is not called
        if (aReason != ResponseRejectReason::ChannelClosed) {
          fail("reject with wrong reason");
        }
        passed("reject handler called on channel close");
      });
  SendPing()->Then(
      MessageLoop::current()->SerialEventTarget(), __func__,
      [this](bool one) {
        if (one) {
          passed("take one argument");
        } else {
          fail("get one argument but has wrong value");
        }

        // Also try with the callback-based API.
        SendPing(
            [this](bool one) {
              if (one) {
                passed("take one argument");
              } else {
                fail("get one argument but has wrong value");
              }
              Close();
            },
            [](ResponseRejectReason&& aReason) { fail("sending Ping"); });
      },
      [](ResponseRejectReason&& aReason) { fail("sending Ping"); });
}

mozilla::ipc::IPCResult TestAsyncReturnsParent::RecvPong(
    PongResolver&& aResolve) {
  aResolve(std::tuple<const uint32_t&, const uint32_t&>(sMagic1, sMagic2));
  return IPC_OK();
}

//-----------------------------------------------------------------------------
// child

TestAsyncReturnsChild::TestAsyncReturnsChild() {
  MOZ_COUNT_CTOR(TestAsyncReturnsChild);
}

TestAsyncReturnsChild::~TestAsyncReturnsChild() {
  MOZ_COUNT_DTOR(TestAsyncReturnsChild);
}

mozilla::ipc::IPCResult TestAsyncReturnsChild::RecvNoReturn(
    NoReturnResolver&& aResolve) {
  // Not resolving the promise intentionally
  return IPC_OK();
}

mozilla::ipc::IPCResult TestAsyncReturnsChild::RecvPing(
    PingResolver&& aResolve) {
  SendPong()->Then(
      MessageLoop::current()->SerialEventTarget(), __func__,
      [aResolve](const std::tuple<uint32_t, uint32_t>& aParam) {
        if (std::get<0>(aParam) == sMagic1 && std::get<1>(aParam) == sMagic2) {
          passed("take two arguments");
        } else {
          fail("get two argument but has wrong value");
        }
        aResolve(true);
      },
      [](ResponseRejectReason&& aReason) { fail("sending Pong"); });
  return IPC_OK();
}

}  // namespace _ipdltest
}  // namespace mozilla