Flutter Engine
The Flutter Engine
Functions
MessageBusTest.cpp File Reference
#include "include/core/SkRefCnt.h"
#include "include/core/SkTypes.h"
#include "include/gpu/GrDirectContext.h"
#include "include/private/base/SkTArray.h"
#include "src/core/SkMessageBus.h"
#include "tests/Test.h"
#include <cstdint>
#include <utility>

Go to the source code of this file.

Functions

 DEF_TEST (MessageBus, r)
 
 DEF_TEST (MessageBusSp, r)
 
 DEF_TEST (MessageBus_SkShouldPostMessageToBus, r)
 

Function Documentation

◆ DEF_TEST() [1/3]

DEF_TEST ( MessageBus  ,
 
)

Definition at line 37 of file MessageBusTest.cpp.

37 {
38 using TestMessageBus = SkMessageBus<TestMessage, uint32_t>;
39 // Register two inboxes to receive all TestMessages.
40 TestMessageBus::Inbox inbox1(0), inbox2(0);
41
42 // Send two messages.
43 const TestMessage m1 = { 5, 4.2f };
44 const TestMessage m2 = { 6, 4.3f };
45 TestMessageBus::Post(std::move(m1));
46 TestMessageBus::Post(std::move(m2));
47
48 // Make sure we got two.
49 TArray<TestMessage> messages;
50 inbox1.poll(&messages);
51 REPORTER_ASSERT(r, 2 == messages.size());
52 REPORTER_ASSERT(r, 5 == messages[0].x);
53 REPORTER_ASSERT(r, 6 == messages[1].x);
54
55 // Send another; check we get just that one.
56 const TestMessage m3 = { 1, 0.3f };
57 TestMessageBus::Post(m3);
58 inbox1.poll(&messages);
59 REPORTER_ASSERT(r, 1 == messages.size());
60 REPORTER_ASSERT(r, 1 == messages[0].x);
61
62 // Nothing was sent since the last read.
63 inbox1.poll(&messages);
64 REPORTER_ASSERT(r, 0 == messages.size());
65
66 // Over all this time, inbox2 should have piled up 3 messages.
67 inbox2.poll(&messages);
68 REPORTER_ASSERT(r, 3 == messages.size());
69 REPORTER_ASSERT(r, 5 == messages[0].x);
70 REPORTER_ASSERT(r, 6 == messages[1].x);
71 REPORTER_ASSERT(r, 1 == messages[2].x);
72}
#define REPORTER_ASSERT(r, cond,...)
Definition: Test.h:286
int size() const
Definition: SkTArray.h:421
double x

◆ DEF_TEST() [2/3]

DEF_TEST ( MessageBus_SkShouldPostMessageToBus  ,
 
)

Definition at line 147 of file MessageBusTest.cpp.

147 {
149 using AddressedMessageBus = SkMessageBus<AddressedMessage, ID>;
150
151 ID idInvalid;
152 ID id1 = ID::Next(),
153 id2 = ID::Next(),
154 id3 = ID::Next();
155
156 AddressedMessageBus::Inbox inbox1(id1), inbox2(id2);
157
158 AddressedMessageBus::Post({idInvalid}); // Should go to both
159 AddressedMessageBus::Post({id1}); // Should go to inbox1
160 AddressedMessageBus::Post({id2}); // Should go to inbox2
161 AddressedMessageBus::Post({id3}); // Should go nowhere
162
164 inbox1.poll(&messages);
165 REPORTER_ASSERT(r, messages.size() == 2);
166 if (messages.size() == 2) {
167 REPORTER_ASSERT(r, !messages[0].fInboxID.isValid());
168 REPORTER_ASSERT(r, messages[1].fInboxID == id1);
169 }
170 inbox2.poll(&messages);
171 REPORTER_ASSERT(r, messages.size() == 2);
172 if (messages.size() == 2) {
173 REPORTER_ASSERT(r, !messages[0].fInboxID.isValid());
174 REPORTER_ASSERT(r, messages[1].fInboxID == id2);
175 }
176}

◆ DEF_TEST() [3/3]

DEF_TEST ( MessageBusSp  ,
 
)

Definition at line 91 of file MessageBusTest.cpp.

91 {
92 // Register two inboxes to receive all TestMessages.
93 using TestMessageBus = SkMessageBus<sk_sp<TestMessageRefCnt>, uint32_t, false>;
94 TestMessageBus::Inbox inbox1(0);
95
96 // Send two messages.
97 auto m1 = sk_make_sp<TestMessageRefCnt>(5, 4.2f);
98 auto m2 = sk_make_sp<TestMessageRefCnt>(6, 4.3f);
99 TestMessageBus::Post(std::move(m1));
100 TestMessageBus::Post(std::move(m2));
101
102 // Make sure we got two.
104 inbox1.poll(&messages);
105 REPORTER_ASSERT(r, 2 == messages.size());
106 REPORTER_ASSERT(r, messages[0]->unique());
107 REPORTER_ASSERT(r, messages[1]->unique());
108 REPORTER_ASSERT(r, 5 == messages[0]->x);
109 REPORTER_ASSERT(r, 6 == messages[1]->x);
110
111 // Send another; check we get just that one.
112 auto m3 = sk_make_sp<TestMessageRefCnt>(1, 0.3f);
113 TestMessageBus::Post(std::move(m3));
114 inbox1.poll(&messages);
115 REPORTER_ASSERT(r, 1 == messages.size());
116 REPORTER_ASSERT(r, messages[0]->unique());
117 REPORTER_ASSERT(r, 1 == messages[0]->x);
118
119 // Send another without std::move(), it should trigger SkASSERT().
120 // auto m4 = sk_make_sp<TestMessageRefCnt>(1, 0.3f);
121 // TestMessageBus::Post(m4);
122
123 // Nothing was sent since the last read.
124 inbox1.poll(&messages);
125 REPORTER_ASSERT(r, 0 == messages.size());
126}